VerifyCosign verifies an image(`rawRef`) with a cosign public key(`keyRef`) `hostsDirs` are used to resolve image `rawRef` Either --cosign-certificate-identity or --cosign-certificate-identity-regexp and either --cosign-certificate-oidc-issuer or --cosign-certificate-oidc-issuer-regexp must be set f
(ctx context.Context, rawRef string, keyRef string, hostsDirs []string, certIdentity string, certIdentityRegexp string, certOidcIssuer string, certOidcIssuerRegexp string)
| 65 | // `hostsDirs` are used to resolve image `rawRef` |
| 66 | // Either --cosign-certificate-identity or --cosign-certificate-identity-regexp and either --cosign-certificate-oidc-issuer or --cosign-certificate-oidc-issuer-regexp must be set for keyless flows. |
| 67 | func VerifyCosign(ctx context.Context, rawRef string, keyRef string, hostsDirs []string, |
| 68 | certIdentity string, certIdentityRegexp string, certOidcIssuer string, certOidcIssuerRegexp string) (string, error) { |
| 69 | digest, err := imgutil.ResolveDigest(ctx, rawRef, false, hostsDirs) |
| 70 | if err != nil { |
| 71 | log.G(ctx).WithError(err).Errorf("unable to resolve digest for an image %s: %v", rawRef, err) |
| 72 | return rawRef, err |
| 73 | } |
| 74 | ref := rawRef |
| 75 | if !strings.Contains(ref, "@") { |
| 76 | ref += "@" + digest |
| 77 | } |
| 78 | |
| 79 | log.G(ctx).Debugf("verifying image: %s", ref) |
| 80 | |
| 81 | cosignExecutable, err := exec.LookPath("cosign") |
| 82 | if err != nil { |
| 83 | log.G(ctx).WithError(err).Error("cosign executable not found in path $PATH") |
| 84 | log.G(ctx).Info("you might consider installing cosign from: https://docs.sigstore.dev/cosign/installation") |
| 85 | return ref, err |
| 86 | } |
| 87 | |
| 88 | cosignCmd := exec.Command(cosignExecutable, []string{"verify"}...) |
| 89 | cosignCmd.Env = os.Environ() |
| 90 | |
| 91 | // if key is empty, use keyless mode(experimental) |
| 92 | if keyRef != "" { |
| 93 | cosignCmd.Args = append(cosignCmd.Args, "--key", keyRef) |
| 94 | } else { |
| 95 | if certIdentity == "" && certIdentityRegexp == "" { |
| 96 | return ref, errors.New("--cosign-certificate-identity or --cosign-certificate-identity-regexp is required for Cosign verification in keyless mode") |
| 97 | } |
| 98 | if certIdentity != "" { |
| 99 | cosignCmd.Args = append(cosignCmd.Args, "--certificate-identity", certIdentity) |
| 100 | } |
| 101 | if certIdentityRegexp != "" { |
| 102 | cosignCmd.Args = append(cosignCmd.Args, "--certificate-identity-regexp", certIdentityRegexp) |
| 103 | } |
| 104 | if certOidcIssuer == "" && certOidcIssuerRegexp == "" { |
| 105 | return ref, errors.New("--cosign-certificate-oidc-issuer or --cosign-certificate-oidc-issuer-regexp is required for Cosign verification in keyless mode") |
| 106 | } |
| 107 | if certOidcIssuer != "" { |
| 108 | cosignCmd.Args = append(cosignCmd.Args, "--certificate-oidc-issuer", certOidcIssuer) |
| 109 | } |
| 110 | if certOidcIssuerRegexp != "" { |
| 111 | cosignCmd.Args = append(cosignCmd.Args, "--certificate-oidc-issuer-regexp", certOidcIssuerRegexp) |
| 112 | } |
| 113 | cosignCmd.Env = append(cosignCmd.Env, "COSIGN_EXPERIMENTAL=true") |
| 114 | } |
| 115 | |
| 116 | cosignCmd.Args = append(cosignCmd.Args, ref) |
| 117 | |
| 118 | log.G(ctx).Debugf("running %s %v", cosignExecutable, cosignCmd.Args) |
| 119 | |
| 120 | err = processCosignIO(cosignCmd) |
| 121 | if err != nil { |
| 122 | return ref, err |
| 123 | } |
| 124 | if err := cosignCmd.Wait(); err != nil { |
no test coverage detected
searching dependent graphs…