From ac14579c42fc35001d3894a78a45de2aefe4d2d7 Mon Sep 17 00:00:00 2001 From: santoshkal Date: Thu, 20 Jun 2024 19:56:36 +0530 Subject: [PATCH] Update: Reading of OCI URLs for cuemods from a const instead of a .env file This behaviour is for testing the commands and would be updated to read the URLs for all commands from a .env file stored in a repo Signed-off-by: santoshkal --- cmd/cuemod_init.go | 13 ++++++------- pkg/oci/constants.go | 3 +++ pkg/oci/ociClient.go | 3 +-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cmd/cuemod_init.go b/cmd/cuemod_init.go index 8eba005..0d94fbe 100644 --- a/cmd/cuemod_init.go +++ b/cmd/cuemod_init.go @@ -7,7 +7,6 @@ import ( "fmt" "os" - "github.com/intelops/genval/pkg/cuecore" "github.com/intelops/genval/pkg/oci" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -37,7 +36,7 @@ and provide the directory to --policy flag in cue command. for validating and generating the Kubernetes resources. # Curently, available flags for cuemod init are: ---tool=k8s:1.29 +--tool=k8s:1.30 --tool=argocd:2.10.4 --tool=tektoncd:0.58.0 --too=crosplane:1.15.0 @@ -83,11 +82,11 @@ func runInitCmd(cmd *cobra.Command, args []string) error { if initArgs.tool == "" { return errors.New("atleast one tool needs to be provided to initialize") } - desiredTool, ociURL, err := cuecore.ParseTools(initArgs.tool) + + ociURL, err := oci.FetchPolicyFromRegistry(initArgs.tool) if err != nil { - log.Errorf("Error prsing provided tool %s: %v", initArgs.tool, err) + return fmt.Errorf("error fetching module for '%v': %v", initArgs.tool, err) } - // key := "" verified, err := oci.VerifyArifact(context.Background(), ociURL, initArgs.key) if err != nil { return fmt.Errorf("error varifying artifact: %v", err) @@ -106,7 +105,7 @@ func runInitCmd(cmd *cobra.Command, args []string) error { if input == "y" { fmt.Println("Proceeding...") - if err := oci.CreateWorkspace(desiredTool, ociURL, initArgs.creds); err != nil { + if err := oci.CreateWorkspace(initArgs.tool, ociURL, initArgs.creds); err != nil { log.Errorf("Error creating workspace: %v", err) } log.Infof("Workspace verified and created") @@ -116,7 +115,7 @@ func runInitCmd(cmd *cobra.Command, args []string) error { } else { fmt.Println("Invalid input. Please enter 'y' or 'n'.") } - } else if err := oci.CreateWorkspace(desiredTool, ociURL, initArgs.creds); err != nil { + } else if err := oci.CreateWorkspace(initArgs.tool, ociURL, initArgs.creds); err != nil { log.Errorf("Error creating workspace: %v", err) } return nil diff --git a/pkg/oci/constants.go b/pkg/oci/constants.go index a419f91..f410ed9 100644 --- a/pkg/oci/constants.go +++ b/pkg/oci/constants.go @@ -21,11 +21,13 @@ const ( URLPrefix = "oci://" + // TODO: Move all the URLs to a .env file to read from // OCI URLs for Rego policies DockerfilePolicies = URLPrefix + "ghcr.io/intelops/policyhub/genval/dockerfile_policies:v0.0.1" InfrafilePolicies = URLPrefix + "ghcr.io/intelops/policyhub/genval/infrafile_policies:v0.0.1" TerraformPolicies = URLPrefix + "ghcr.io/intelops/policyhub/genval/terraform_policies:v0.0.1" InputPolicies = URLPrefix + "ghcr.io/intelops/policyhub/genval/input_policies:v0.0.1" + k8sLatestModule = URLPrefix + "ghcr.io/intelops/policyhub/genval/k8s-cuemods:v0.0.1" ) // FetchPolicyFromRegistry fetches the policy based on the command provided @@ -35,6 +37,7 @@ func FetchPolicyFromRegistry(cmd string) (string, error) { "infrafile": InfrafilePolicies, "terraform": TerraformPolicies, "inputPolicy": InputPolicies, + "k8s:1.30": k8sLatestModule, } policy, ok := policies[cmd] diff --git a/pkg/oci/ociClient.go b/pkg/oci/ociClient.go index fbaa425..fffa4d0 100644 --- a/pkg/oci/ociClient.go +++ b/pkg/oci/ociClient.go @@ -17,7 +17,6 @@ import ( "github.com/google/go-containerregistry/pkg/authn" "github.com/google/go-containerregistry/pkg/crane" - "github.com/google/go-containerregistry/pkg/name" "github.com/google/go-containerregistry/pkg/v1/remote" "github.com/google/go-containerregistry/pkg/v1/remote/transport" "github.com/intelops/genval/pkg/cuecore" @@ -41,7 +40,7 @@ func ParseAnnotations(args []string) (map[string]string, error) { // CheckTagAndPullArchive checks for provided tag to be available in the remote, if available pulls the archive // and stores it in the specified directory and retuens an error if encountered. func CheckTagAndPullArchive(url, tool, creds string, archivePath *os.File) error { - ref, err := name.ParseReference(url) + ref, err := ParseOCIReference(url) if err != nil { return fmt.Errorf("error parsing url %s: %v", url, err) }