Skip to content

Commit

Permalink
move compose check to compose
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Abro <[email protected]>
  • Loading branch information
AustinAbro321 committed Aug 12, 2024
1 parent 265df7c commit e831b01
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
14 changes: 8 additions & 6 deletions src/pkg/packager/composer/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ func (ic *ImportChain) append(c v1alpha1.ZarfComponent, index int, originalPacka
}
}

const pkgValidateErrImportDefinition = "invalid imported definition for %s: %s"

// validateComponentCompose validates that a component doesn't break compose rules
func validateComponentCompose(c v1alpha1.ZarfComponent) error {
var err error
Expand All @@ -128,27 +126,27 @@ func validateComponentCompose(c v1alpha1.ZarfComponent) error {

// ensure path or url is provided
if path == "" && url == "" {
err = errors.Join(err, fmt.Errorf(pkgValidateErrImportDefinition, c.Name, "neither a path nor a URL was provided"))
err = errors.Join(err, errors.New("neither a path nor a URL was provided"))
}

// ensure path and url are not both provided
if path != "" && url != "" {
err = errors.Join(err, fmt.Errorf(pkgValidateErrImportDefinition, c.Name, "both a path and a URL were provided"))
err = errors.Join(err, errors.New("both a path and a URL were provided"))
}

// validation for path
if url == "" && path != "" {
// ensure path is not an absolute path
if filepath.IsAbs(path) {
err = errors.Join(err, fmt.Errorf(pkgValidateErrImportDefinition, c.Name, "path cannot be an absolute path"))
err = errors.Join(err, errors.New("path cannot be an absolute path"))
}
}

// validation for url
if url != "" && path == "" {
ok := helpers.IsOCIURL(url)
if !ok {
err = errors.Join(err, fmt.Errorf(pkgValidateErrImportDefinition, c.Name, "URL is not a valid OCI URL"))
err = errors.Join(err, errors.New("URL is not a valid OCI URL"))
}
}

Expand Down Expand Up @@ -178,6 +176,10 @@ func NewImportChain(ctx context.Context, head v1alpha1.ZarfComponent, index int,
return ic, nil
}

if err := validateComponentCompose(node.ZarfComponent); err != nil {
return nil, fmt.Errorf("invalid imported definition for %s: %w", node.Name, err)
}

// ensure that remote components are not importing other remote components
if node.prev != nil && node.prev.Import.URL != "" && isRemote {
return ic, fmt.Errorf("detected malformed import chain, cannot import remote components from remote components")
Expand Down
8 changes: 4 additions & 4 deletions src/pkg/packager/composer/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ func TestValidateZarfComponent(t *testing.T) {
Name: "neither",
},
expectedErrs: []string{
fmt.Sprintf(pkgValidateErrImportDefinition, "neither", "neither a path nor a URL was provided"),
"neither a path nor a URL was provided",
},
},
{
Expand All @@ -590,7 +590,7 @@ func TestValidateZarfComponent(t *testing.T) {
},
},
expectedErrs: []string{
fmt.Sprintf(pkgValidateErrImportDefinition, "both", "both a path and a URL were provided"),
"both a path and a URL were provided",
},
},
{
Expand All @@ -602,7 +602,7 @@ func TestValidateZarfComponent(t *testing.T) {
},
},
expectedErrs: []string{
fmt.Sprintf(pkgValidateErrImportDefinition, "abs-path", "path cannot be an absolute path"),
"path cannot be an absolute path",
},
},
{
Expand All @@ -614,7 +614,7 @@ func TestValidateZarfComponent(t *testing.T) {
},
},
expectedErrs: []string{
fmt.Sprintf(pkgValidateErrImportDefinition, "bad-url", "URL is not a valid OCI URL"),
"URL is not a valid OCI URL",
},
},
}
Expand Down

0 comments on commit e831b01

Please sign in to comment.