Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor DoHostnamesMatch #1953

Merged
merged 10 commits into from
Aug 8, 2023
Merged
10 changes: 5 additions & 5 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import (

"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"

"github.com/pterm/pterm"
"oras.land/oras-go/v2/registry"

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/internal/cluster"
"github.com/defenseunicorns/zarf/src/pkg/packager"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -185,10 +185,10 @@ var packagePublishCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
pkgConfig.PublishOpts.PackagePath = choosePackage(args)

if !utils.IsOCIURL(args[1]) {
if !helpers.IsOCIURL(args[1]) {
message.Fatal(nil, lang.CmdPackageRegistryPrefixErr)
}
parts := strings.Split(strings.TrimPrefix(args[1], utils.OCIURLPrefix), "/")
parts := strings.Split(strings.TrimPrefix(args[1], helpers.OCIURLPrefix), "/")
ref := registry.Reference{
Registry: parts[0],
Repository: strings.Join(parts[1:], "/"),
Expand Down Expand Up @@ -217,7 +217,7 @@ var packagePullCmd = &cobra.Command{
Example: lang.CmdPackagePullExample,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if !utils.IsOCIURL(args[0]) {
if !helpers.IsOCIURL(args[0]) {
message.Fatal(nil, lang.CmdPackageRegistryPrefixErr)
}

Expand Down
4 changes: 2 additions & 2 deletions src/internal/agent/hooks/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/defenseunicorns/zarf/src/internal/agent/state"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/transform"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
v1 "k8s.io/api/admission/v1"
)
Expand Down Expand Up @@ -71,7 +71,7 @@ func mutateGitRepo(r *v1.AdmissionRequest) (result *operations.Result, err error
// NOTE: We mutate on updates IF AND ONLY IF the hostname in the request is different than the hostname in the zarfState
// NOTE: We are checking if the hostname is different before because we do not want to potentially mutate a URL that has already been mutated.
if isUpdate {
isPatched, err = utils.DoHostnamesMatch(zarfState.GitServer.Address, src.Spec.URL)
isPatched, err = helpers.DoHostnamesMatch(zarfState.GitServer.Address, src.Spec.URL)
if err != nil {
return nil, fmt.Errorf(lang.AgentErrHostnameMatch, err)
}
Expand Down
3 changes: 2 additions & 1 deletion src/internal/packager/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
)

Expand Down Expand Up @@ -92,7 +93,7 @@ func ImportPackage(composedComponent *types.ZarfComponent) error {
if path != "" {
return fmt.Errorf(lang.PkgValidateErrImportOptions, composedComponent.Name)
}
ok := utils.IsOCIURL(url)
ok := helpers.IsOCIURL(url)
if !ok {
return fmt.Errorf(lang.PkgValidateErrImportURLInvalid, composedComponent.Import.URL)
}
Expand Down
3 changes: 2 additions & 1 deletion src/pkg/oci/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
zarfconfig "github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/configfile"
"oras.land/oras-go/v2"
Expand Down Expand Up @@ -41,7 +42,7 @@ type OrasRemote struct {
//
// Registry auth is handled by the Docker CLI's credential store and checked before returning the client
func NewOrasRemote(url string) (*OrasRemote, error) {
ref, err := registry.ParseReference(strings.TrimPrefix(url, utils.OCIURLPrefix))
ref, err := registry.ParseReference(strings.TrimPrefix(url, helpers.OCIURLPrefix))
if err != nil {
return nil, fmt.Errorf("failed to parse OCI reference: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion src/pkg/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
config "github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
goyaml "github.com/goccy/go-yaml"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand All @@ -27,7 +28,7 @@ import (
//
// appending the provided suffix to the version
func ReferenceFromMetadata(registryLocation string, metadata *types.ZarfMetadata, suffix string) (*registry.Reference, error) {
registryLocation = strings.TrimPrefix(registryLocation, utils.OCIURLPrefix)
registryLocation = strings.TrimPrefix(registryLocation, helpers.OCIURLPrefix)

ver := metadata.Version
if len(ver) == 0 {
Expand Down
5 changes: 3 additions & 2 deletions src/pkg/packager/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/oci"
"github.com/defenseunicorns/zarf/src/pkg/packager/deprecated"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
"github.com/mholt/archiver/v3"
)
Expand Down Expand Up @@ -97,7 +98,7 @@ func (p *Packager) getChildComponent(parent types.ZarfComponent, pathAncestry st
// Save all the OCI imported components into our build data
p.cfg.Pkg.Build.OCIImportedComponents[parent.Import.URL] = childComponentName

skelURL := strings.TrimPrefix(parent.Import.URL, utils.OCIURLPrefix)
skelURL := strings.TrimPrefix(parent.Import.URL, helpers.OCIURLPrefix)
cachePath = filepath.Join(config.GetAbsCachePath(), "oci", skelURL)
err = os.MkdirAll(cachePath, 0755)
if err != nil {
Expand Down Expand Up @@ -401,7 +402,7 @@ func (p *Packager) getComposedFilePath(prefix string, path string) string {
message.Debugf("packager.getComposedFilePath(%s, %s)", prefix, path)

// Return original if it is a remote file.
if utils.IsURL(path) {
if helpers.IsURL(path) {
return path
}

Expand Down
16 changes: 8 additions & 8 deletions src/pkg/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (p *Packager) Create(baseDir string) error {
return fmt.Errorf("unable to read the zarf.yaml file: %s", err.Error())
}

if utils.IsOCIURL(p.cfg.CreateOpts.Output) {
if helpers.IsOCIURL(p.cfg.CreateOpts.Output) {
ref, err := oci.ReferenceFromMetadata(p.cfg.CreateOpts.Output, &p.cfg.Pkg.Metadata, p.arch)
if err != nil {
return err
Expand Down Expand Up @@ -224,7 +224,7 @@ func (p *Packager) Create(baseDir string) error {
}
}

if utils.IsOCIURL(p.cfg.CreateOpts.Output) {
if helpers.IsOCIURL(p.cfg.CreateOpts.Output) {
err := p.remote.PublishPackage(&p.cfg.Pkg, p.tmp.Base, config.CommonOptions.OCIConcurrency)
if err != nil {
return fmt.Errorf("unable to publish package: %w", err)
Expand Down Expand Up @@ -352,7 +352,7 @@ func (p *Packager) addComponent(index int, component types.ZarfComponent, isSkel
rel := fmt.Sprintf("%s-%d", helm.StandardName(types.ValuesFolder, chart), valuesIdx)
dst := filepath.Join(componentPath.Base, rel)

if utils.IsURL(path) {
if helpers.IsURL(path) {
if isSkeleton {
continue
}
Expand All @@ -376,7 +376,7 @@ func (p *Packager) addComponent(index int, component types.ZarfComponent, isSkel
rel := filepath.Join(types.FilesFolder, strconv.Itoa(filesIdx), filepath.Base(file.Target))
dst := filepath.Join(componentPath.Base, rel)

if utils.IsURL(file.Source) {
if helpers.IsURL(file.Source) {
if isSkeleton {
continue
}
Expand Down Expand Up @@ -416,7 +416,7 @@ func (p *Packager) addComponent(index int, component types.ZarfComponent, isSkel
rel := filepath.Join(types.DataInjectionsFolder, strconv.Itoa(dataIdx), filepath.Base(data.Target.Path))
dst := filepath.Join(componentPath.Base, rel)

if utils.IsURL(data.Source) {
if helpers.IsURL(data.Source) {
if isSkeleton {
continue
}
Expand Down Expand Up @@ -455,7 +455,7 @@ func (p *Packager) addComponent(index int, component types.ZarfComponent, isSkel

// Copy manifests without any processing.
spinner.Updatef("Copying manifest %s", path)
if utils.IsURL(path) {
if helpers.IsURL(path) {
if isSkeleton {
continue
}
Expand Down Expand Up @@ -566,7 +566,7 @@ func (p *Packager) loadDifferentialData() error {
defer os.RemoveAll(tmpDir)

// Load the package spec of the package we're using as a 'reference' for the differential build
if utils.IsOCIURL(p.cfg.CreateOpts.DifferentialData.DifferentialPackagePath) {
if helpers.IsOCIURL(p.cfg.CreateOpts.DifferentialData.DifferentialPackagePath) {
err := p.SetOCIRemote(p.cfg.CreateOpts.DifferentialData.DifferentialPackagePath)
if err != nil {
return err
Expand Down Expand Up @@ -622,7 +622,7 @@ func (p *Packager) removeDifferentialComponentsFromPackage() error {

for idx, component := range p.cfg.Pkg.Components {
// if the component is imported from an OCI package and everything is the same, don't include this package
if utils.IsOCIURL(component.Import.URL) {
if helpers.IsOCIURL(component.Import.URL) {
if _, alsoExists := p.cfg.CreateOpts.DifferentialData.DifferentialOCIComponents[component.Import.URL]; alsoExists {

// If the component spec is not empty, we will still include it in the differential package
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/packager/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var (
func (p *Packager) Deploy() error {
message.Debug("packager.Deploy()")

if utils.IsOCIURL(p.cfg.DeployOpts.PackagePath) {
if helpers.IsOCIURL(p.cfg.DeployOpts.PackagePath) {
err := p.SetOCIRemote(p.cfg.DeployOpts.PackagePath)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion src/pkg/packager/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/defenseunicorns/zarf/src/internal/packager/sbom"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/mholt/archiver/v3"
)

Expand All @@ -24,7 +25,7 @@ func (p *Packager) Inspect(includeSBOM bool, outputSBOM string, inspectPublicKey
}

// Handle OCI packages that have been published to a registry
if utils.IsOCIURL(p.cfg.DeployOpts.PackagePath) {
if helpers.IsOCIURL(p.cfg.DeployOpts.PackagePath) {

message.Debugf("Pulling layers %v from %s", partialPaths, p.cfg.DeployOpts.PackagePath)

Expand Down
3 changes: 2 additions & 1 deletion src/pkg/packager/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/defenseunicorns/zarf/src/config/lang"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand All @@ -33,7 +34,7 @@ func (p *Packager) handlePackagePath() (partialPaths []string, err error) {
}

// Handle case where deploying remote package stored in an OCI registry
if utils.IsOCIURL(opts.PackagePath) {
if helpers.IsOCIURL(opts.PackagePath) {
p.cfg.DeployOpts.PackagePath = p.tmp.Base
requestedComponents := getRequestedComponentList(p.cfg.DeployOpts.Components)
layersToPull := []ocispec.Descriptor{}
Expand Down
5 changes: 3 additions & 2 deletions src/pkg/packager/prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/defenseunicorns/zarf/src/pkg/k8s"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/defenseunicorns/zarf/src/pkg/utils"
"github.com/defenseunicorns/zarf/src/pkg/utils/helpers"
"github.com/defenseunicorns/zarf/src/types"
"github.com/google/go-containerregistry/pkg/crane"
v1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -131,7 +132,7 @@ func (p *Packager) FindImages(baseDir, repoHelmChartPath string, kubeVersionOver

for idx, path := range chart.ValuesFiles {
dst := helm.StandardName(componentPath.Values, chart) + "-" + strconv.Itoa(idx)
if utils.IsURL(path) {
if helpers.IsURL(path) {
if err := utils.DownloadToFile(path, dst, component.CosignKeyPath); err != nil {
return nil, fmt.Errorf(lang.ErrDownloading, path, err.Error())
}
Expand Down Expand Up @@ -195,7 +196,7 @@ func (p *Packager) FindImages(baseDir, repoHelmChartPath string, kubeVersionOver
}
// Get all manifest files
for idx, f := range manifest.Files {
if utils.IsURL(f) {
if helpers.IsURL(f) {
mname := fmt.Sprintf("manifest-%s-%d.yaml", manifest.Name, idx)
destination := filepath.Join(componentPath.Manifests, mname)
if err := utils.DownloadToFile(f, destination, component.CosignKeyPath); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (p *Packager) Remove(packageName string) (err error) {
}

// If the user input is a path to a oci, pull the package
if utils.IsOCIURL(packageName) {
if helpers.IsOCIURL(packageName) {
err := p.SetOCIRemote(packageName)
if err != nil {
message.Fatalf(err, "Unable to set OCI remote: %s", err.Error())
Expand All @@ -52,7 +52,7 @@ func (p *Packager) Remove(packageName string) (err error) {
}

// If this came from a real package, read the package config and reset the packageName
if ZarfPackagePattern.MatchString(packageName) || ZarfInitPattern.MatchString(packageName) || utils.IsOCIURL(packageName) {
if ZarfPackagePattern.MatchString(packageName) || ZarfInitPattern.MatchString(packageName) || helpers.IsOCIURL(packageName) {
if err := p.readYaml(p.tmp.ZarfYaml); err != nil {
return err
}
Expand Down
42 changes: 42 additions & 0 deletions src/pkg/utils/helpers/url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Zarf Authors

// Package helpers provides generic helper functions with no external imports
package helpers

import (
"fmt"
"net/url"
)

// Nonstandard URL schemes or prefixes
const (
OCIURLScheme = "oci"
OCIURLPrefix = "oci://"
)

// IsURL is a helper function to check if a URL is valid.
func IsURL(source string) bool {
parsedURL, err := url.Parse(source)
return err == nil && parsedURL.Scheme != "" && parsedURL.Host != ""
}

// IsOCIURL returns true if the given URL is an OCI URL.
func IsOCIURL(source string) bool {
parsedURL, err := url.Parse(source)
return err == nil && parsedURL.Scheme == "oci"
}

// DoHostnamesMatch returns a boolean indicating if the hostname of two different URLs are the same.
func DoHostnamesMatch(url1 string, url2 string) (bool, error) {
parsedURL1, err := url.Parse(url1)
if err != nil {
return false, fmt.Errorf("unable to parse the url (%s): %w", url1, err)
}
parsedURL2, err := url.Parse(url2)
if err != nil {
return false, fmt.Errorf("unable to parse the url (%s): %w", url2, err)
}

return parsedURL1.Hostname() == parsedURL2.Hostname(), nil
}
Loading