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

Add cred flag to pull and push commands #95

Merged
merged 3 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cmd/artifact_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type pullFlags struct {
dest string
verify bool
cosignKey string
creds string
}

var pullArgs pullFlags
Expand All @@ -69,7 +70,7 @@ func init() {
pullCmd.Flags().StringVarP(&pullArgs.dest, "dest", "d", "", "destination URL for pulling the artifact from")
pullCmd.Flags().BoolVarP(&pullArgs.verify, "verify", "v", false, "Set signature verification of the artifact using Sigstore cosign")
pullCmd.Flags().StringVarP(&pullArgs.cosignKey, "pub-key", "k", "", "Cosign public key for varifying the artifact")

pullCmd.Flags().StringVarP(&pullArgs.creds, "credentials", "c", "", "Credentials to authenticate with OCI registries ")
artifactCmd.AddCommand(pullCmd)
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/artifact_push.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type pushFlags struct {
annotations []string
sign bool
cosignKey string
creds string
}

var pushArgs pushFlags
Expand All @@ -82,6 +83,7 @@ func init() {
pushCmd.Flags().StringArrayVarP(&pushArgs.annotations, "annotations", "a", nil, "Set custom annotation in <key>=<value> format")
pushCmd.Flags().BoolVarP(&pushArgs.sign, "sign", "s", false, "If set to true, signs the artifact with cosign in keyless mode")
pushCmd.Flags().StringVarP(&pushArgs.cosignKey, "cosign-key", "k", "", "path to cosign private key")
pushCmd.Flags().StringVarP(&pushArgs.creds, "credentials", "c", "", "Credentials to authenticate with OCI registries ")
artifactCmd.AddCommand(pushCmd)
}

Expand Down Expand Up @@ -149,7 +151,6 @@ func runPushCmd(cmd *cobra.Command, args []string) error {
if err != nil {
log.Errorf("appending content to artifact failed: %v", err)
}
// TODO: Add userAgent header for HTTP requests made to OCI registry
spin := utils.StartSpinner("pushing artifact")
defer spin.Stop()
opts, err := oci.GetCreds()
Expand Down
22 changes: 14 additions & 8 deletions pkg/oci/ociClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,6 @@ func PullArtifact(ctx context.Context, dest, path string) error {
url := parts[0]
desiredTag := parts[1]

// TODO: Add userAgent header for HTTP requests made to OCI registry

opts, err := GetCreds()
if err != nil {
return fmt.Errorf("error getting credentials: %v", err)
Expand Down Expand Up @@ -283,13 +281,21 @@ func GetCreds() ([]crane.Option, error) {
return nil, errors.New("ARTIFACT_REGISTRY_PASSWORD environment variable not set")
}

if user == "" || pass == "" {
return nil, errors.New("username or password is empty")
}
token, tokenSet := os.LookupEnv("ARTIFACT_REGISTRY_TOKEN")

// Create authentication config
authConfig := authn.AuthConfig{Username: user, Password: pass}
opts = append(opts, crane.WithAuth(authn.FromConfig(authConfig)))
if tokenSet || token != "" {
// Token is set, use it
authConfig := authn.AuthConfig{RegistryToken: token}
opts = append(opts, crane.WithAuth(authn.FromConfig(authConfig)))
} else {
if user == "" || pass == "" {
return nil, errors.New("username or password is empty")
}

// Create authentication config
authConfig := authn.AuthConfig{Username: user, Password: pass}
opts = append(opts, crane.WithAuth(authn.FromConfig(authConfig)))
}
} else {
// Other error occurred while checking for Docker config file
return nil, fmt.Errorf("error checking Docker config at %s: %v", credPath, err)
Expand Down
Loading