Skip to content

Commit da8cf79

Browse files
committed
mirror: overhaul validation errors
Signed-off-by: Maxim Vasilenko <[email protected]>
1 parent 0022039 commit da8cf79

File tree

3 files changed

+46
-4
lines changed

3 files changed

+46
-4
lines changed

internal/mirror/cmd/pull/validation.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,20 @@ package pull
1919
import (
2020
"errors"
2121
"fmt"
22+
"net/url"
2223
"os"
2324
"path/filepath"
2425

2526
"github.com/Masterminds/semver/v3"
27+
"github.com/google/go-containerregistry/pkg/name"
2628
"github.com/spf13/cobra"
2729
)
2830

2931
func parseAndValidateParameters(_ *cobra.Command, args []string) error {
3032
var err error
33+
if err = validateSourceRegistry(); err != nil {
34+
return err
35+
}
3136
if err = parseAndValidateVersionFlags(); err != nil {
3237
return err
3338
}
@@ -44,6 +49,31 @@ func parseAndValidateParameters(_ *cobra.Command, args []string) error {
4449
return nil
4550
}
4651

52+
func validateSourceRegistry() error {
53+
if SourceRegistryRepo == enterpriseEditionRepo {
54+
return nil // Default is fine
55+
}
56+
57+
// We first validate that passed repository reference is correct and can be parsed
58+
if _, err := name.NewRepository(SourceRegistryRepo); err != nil {
59+
return fmt.Errorf("Validate registry address: %w", err)
60+
}
61+
62+
// Then we parse it as URL to validate that it contains everything we need
63+
registryUrl, err := url.ParseRequestURI("docker://" + SourceRegistryRepo)
64+
if err != nil {
65+
return fmt.Errorf("Validate source registry parameter: %w", err)
66+
}
67+
if registryUrl.Host == "" {
68+
return errors.New("--source you provided contains no registry host. Please specify source registry host address correctly.")
69+
}
70+
if registryUrl.Path == "" {
71+
return errors.New("--source you provided contains no registry path. Please specify source registry repo path correctly.")
72+
}
73+
74+
return nil
75+
}
76+
4777
func validateImagesBundlePathArg(args []string) error {
4878
if len(args) != 1 {
4979
return errors.New("This command requires exactly 1 argument")
@@ -74,7 +104,7 @@ func validateImagesBundlePathArg(args []string) error {
74104
return fmt.Errorf("Read bundle directory: %w", err)
75105
}
76106

77-
if len(dirEntries) == 0 || (len(dirEntries) == 1 && dirEntries[0].Name() == ".tmp" && dirEntries[0].IsDir()) {
107+
if len(dirEntries) == 0 || (len(dirEntries) == 1 && dirEntries[0].Name() == ".tmp" && dirEntries[0].IsDir()) {
78108
return nil
79109
}
80110

internal/mirror/cmd/push/validation.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"path/filepath"
2525
"strings"
2626

27+
"github.com/google/go-containerregistry/pkg/name"
2728
"github.com/samber/lo"
2829
"github.com/spf13/cobra"
2930
)
@@ -89,17 +90,23 @@ func parseAndValidateRegistryURLArg(args []string) error {
8990
return errors.New("<registry> argument is empty")
9091
}
9192

93+
// We first validate that passed repository reference is correct and can be parsed
94+
if _, err := name.NewRepository(registry); err != nil {
95+
return fmt.Errorf("Validate registry address: %w", err)
96+
}
97+
98+
// Then we parse it as URL to validate that it contains everything we need
9299
registryUrl, err := url.ParseRequestURI("docker://" + registry)
93100
if err != nil {
94101
return fmt.Errorf("Validate registry address: %w", err)
95102
}
96103
RegistryHost = registryUrl.Host
97104
RegistryPath = registryUrl.Path
98105
if RegistryHost == "" {
99-
return errors.New("--registry you provided contains no registry host. Please specify registry address correctly.")
106+
return errors.New("<registry> you provided contains no registry host. Please specify registry address correctly.")
100107
}
101-
if RegistryPath == "" {
102-
return errors.New("--registry you provided contains no path to repo. Please specify registry repo path correctly.")
108+
if len(RegistryPath) < 2 || len(RegistryPath) > 255 {
109+
return errors.New("repository part must be between 2 and 255 characters in length. Please specify registry repo path correctly.")
103110
}
104111

105112
return nil

internal/mirror/operations/pull_modules.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ func PullModules(pullParams *params.PullParams, filter *modules.Filter) error {
4848
if err != nil {
4949
return fmt.Errorf("Find modules: %w", err)
5050
}
51+
52+
if len(modulesData) == 0 {
53+
return fmt.Errorf("Modules were not found, check your source repository address and modules path suffix")
54+
}
55+
5156
logger.InfoF("Repo contains %d modules: %s", len(modulesData), formatModulesList(modulesData))
5257

5358
logger.InfoLn("Creating OCI Layouts")

0 commit comments

Comments
 (0)