diff --git a/cmd/kosli/attestation.go b/cmd/kosli/attestation.go index 5ecb4d384..381ced6dc 100644 --- a/cmd/kosli/attestation.go +++ b/cmd/kosli/attestation.go @@ -24,6 +24,8 @@ If ^--commit^ is only defaulted from CI and ^--repo-root^ is left at ^.^, a miss prints a warning and the attestation is sent without commit info. Note that when the attestation is reported for an artifact that does not yet exist in Kosli, ^--commit^ is required to facilitate binding the attestation to the right artifact. +Use ^--no-commit-info^ when the job that attests runs in a different repository from the one the trail belongs to: +the attestation is then sent without commit info and without the repository info defaulted from the CI environment. ` + commitDetailsDesc const commitRequiredDesc = `This command always requires ^--commit^ (defaulted in some CIs) and access to the git repository containing it. @@ -75,6 +77,7 @@ type CommonAttestationOptions struct { repoProvider string repoURLExplicit bool repoNameExplicit bool + noCommitInfo bool // flags lets run tell a passed flag from a defaulted one. flags *pflag.FlagSet // commitRequiredFor completes "the commit is required to ..."; empty when @@ -103,6 +106,20 @@ func (o *CommonAttestationOptions) run(args []string, payload *CommonAttestation } } + if o.noCommitInfo { + if o.commitRequiredFor != "" { + return fmt.Errorf("--no-commit-info cannot be used: the commit is required to %s", o.commitRequiredFor) + } + if o.explicit("commit") { + return fmt.Errorf("--no-commit-info cannot be combined with --commit, whether passed as a flag or set through KOSLI_COMMIT or the config file") + } + payload.GitRepoInfo, err = o.explicitGitRepoInfo() + if err != nil { + return err + } + return o.loadUserDataURLsAndAnnotations(payload) + } + if o.commitSHA != "" { payload.Commit, err = commitInfoRequest{ repoRoot: o.srcRepoRoot, @@ -128,6 +145,36 @@ func (o *CommonAttestationOptions) run(args []string, payload *CommonAttestation } payload.GitRepoInfo = mergeGitRepoInfo(payload.GitRepoInfo, o.repoID, o.repoName, o.repoURL, o.repoProvider, o.repoNameExplicit) + return o.loadUserDataURLsAndAnnotations(payload) +} + +func (o *CommonAttestationOptions) explicit(flag string) bool { + return o.flags != nil && o.flags.Changed(flag) +} + +// explicitGitRepoInfo builds the repo info from the repo flags that were +// passed, leaving out any the CI environment only defaulted: those describe +// the repository the job runs in. +func (o *CommonAttestationOptions) explicitGitRepoInfo() (*gitview.GitRepoInfo, error) { + passed := func(flag, value string) string { + if o.explicit(flag) { + return value + } + return "" + } + repoID, repoName := passed("repo-id", o.repoID), passed("repository", o.repoName) + repoURL, repoProvider := passed("repo-url", o.repoURL), passed("repo-provider", o.repoProvider) + if repoID == "" && repoName == "" && repoURL == "" && repoProvider == "" { + return nil, nil + } + if err := validateRepoFlags(repoURL, repoProvider, true); err != nil { + return nil, err + } + return mergeGitRepoInfo(nil, repoID, repoName, repoURL, repoProvider, true), nil +} + +func (o *CommonAttestationOptions) loadUserDataURLsAndAnnotations(payload *CommonAttestationPayload) error { + var err error payload.UserData, err = LoadJsonData(o.userDataFilePath) if err != nil { return fmt.Errorf("failed to load user data. %s", err) diff --git a/cmd/kosli/attestationCommitInfo_test.go b/cmd/kosli/attestationCommitInfo_test.go index 018773dab..787210d22 100644 --- a/cmd/kosli/attestationCommitInfo_test.go +++ b/cmd/kosli/attestationCommitInfo_test.go @@ -186,6 +186,103 @@ func (suite *AttestationCommitInfoTestSuite) TestCommandsNeedingTheCommitFailWit suite.Contains(err.Error(), "the commit is required to find pull requests") } +// inRepoB runs f as in a GitHub Actions job of a repository other than the +// one the trail belongs to, checked out at the working directory. +func (suite *AttestationCommitInfoTestSuite) inRepoB(f func()) { + suite.T().Chdir("../..") + suite.T().Setenv("GITHUB_SERVER_URL", "https://github.com") + suite.T().Setenv("GITHUB_REPOSITORY", "acme/repo-b") + suite.T().Setenv("GITHUB_REPOSITORY_ID", "222") + suite.T().Setenv("GITHUB_RUN_ID", "9") + suite.inCI(suite.headHash, f) +} + +func (suite *AttestationCommitInfoTestSuite) TestCIDefaultsAreSentWithoutNoCommitInfo() { + suite.inRepoB(func() { + _, out, _, _, err := executeCommandC(suite.attestGeneric("")) + suite.Require().NoError(err) + suite.Contains(out, `"sha1": "`+suite.headHash+`"`) + suite.Contains(out, `"name": "acme/repo-b"`) + }) +} + +func (suite *AttestationCommitInfoTestSuite) TestNoCommitInfoDropsCommitAndCIRepoInfo() { + suite.inRepoB(func() { + _, out, _, _, err := executeCommandC(suite.attestGeneric("--no-commit-info")) + suite.Require().NoError(err) + suite.Contains(out, "THIS IS A DRY-RUN") + suite.NotContains(out, "git_commit_info") + suite.NotContains(out, "repo_info") + suite.NotContains(out, "[warning]") + // Where the attestation was sent from stays true, whichever repository it is about. + suite.Contains(out, `"origin_url": "https://github.com/acme/repo-b/actions/runs/9"`) + }) +} + +func (suite *AttestationCommitInfoTestSuite) TestNoCommitInfoWithoutRepositoryDoesNotWarn() { + suite.inRepoB(func() { + suite.T().Chdir(suite.T().TempDir()) + _, out, _, _, err := executeCommandC(suite.attestGeneric("--no-commit-info")) + suite.Require().NoError(err) + suite.NotContains(out, "git_commit_info") + suite.NotContains(out, "[warning]") + }) +} + +func (suite *AttestationCommitInfoTestSuite) TestNoCommitInfoKeepsExplicitRepoFlags() { + suite.inRepoB(func() { + _, out, _, _, err := executeCommandC(suite.attestGeneric( + "--no-commit-info --repository acme/repo-a --repo-url https://github.com/acme/repo-a --repo-id 111 --repo-provider github")) + suite.Require().NoError(err) + suite.NotContains(out, "git_commit_info") + suite.Contains(out, `"name": "acme/repo-a"`) + suite.Contains(out, `"url": "https://github.com/acme/repo-a"`) + suite.Contains(out, `"id": "111"`) + suite.NotContains(out, `"222"`) + }) +} + +// One explicit repo flag must not be completed from the CI's values for the +// repository the attestation is being kept apart from. +func (suite *AttestationCommitInfoTestSuite) TestNoCommitInfoDoesNotMixExplicitAndCIRepoInfo() { + suite.inRepoB(func() { + _, out, _, _, err := executeCommandC(suite.attestGeneric("--no-commit-info --repository acme/repo-a")) + suite.Require().NoError(err) + suite.NotContains(out, "repo_info") + suite.NotContains(out, `"222"`) + }) +} + +func (suite *AttestationCommitInfoTestSuite) TestNoCommitInfoWithExplicitCommitFails() { + suite.inRepoB(func() { + _, _, _, _, err := executeCommandC(suite.attestGeneric("--no-commit-info --commit " + suite.headHash)) + suite.Require().Error(err) + suite.Equal("--no-commit-info cannot be combined with --commit, whether passed as a flag or set through KOSLI_COMMIT or the config file", err.Error()) + }) +} + +func (suite *AttestationCommitInfoTestSuite) TestNoCommitInfoWithCommitFromEnvVarFails() { + suite.inRepoB(func() { + suite.T().Setenv("KOSLI_COMMIT", suite.headHash) + _, _, _, _, err := executeCommandC(suite.attestGeneric("--no-commit-info")) + suite.Require().Error(err) + suite.Contains(err.Error(), "set through KOSLI_COMMIT or the config file") + }) +} + +func (suite *AttestationCommitInfoTestSuite) TestNoCommitInfoFailsOnCommandsNeedingTheCommit() { + suite.inRepoB(func() { + for _, tc := range []struct{ cmd, need string }{ + {"attest pullrequest github --no-commit-info --name foo --flow f --trail t --github-token tok --github-org o --repository r" + suite.defaultKosliArguments, "find pull requests"}, + {"attest jira --no-commit-info --name foo --flow f --trail t --jira-base-url https://x.atlassian.net --jira-username u --jira-api-token tok" + suite.defaultKosliArguments, "search for Jira issue keys"}, + } { + _, _, _, _, err := executeCommandC(tc.cmd) + suite.Require().Error(err, tc.cmd) + suite.Equal("--no-commit-info cannot be used: the commit is required to "+tc.need, err.Error(), tc.cmd) + } + }) +} + func TestAttestationCommitInfoTestSuite(t *testing.T) { suite.Run(t, new(AttestationCommitInfoTestSuite)) } diff --git a/cmd/kosli/flags.go b/cmd/kosli/flags.go index 7aadb1284..ad8c699f3 100644 --- a/cmd/kosli/flags.go +++ b/cmd/kosli/flags.go @@ -103,6 +103,7 @@ func addAttestationFlags(cmd *cobra.Command, o *CommonAttestationOptions, payloa cmd.Flags().StringVarP(&payload.ArtifactFingerprint, "fingerprint", "F", "", attestationFingerprintFlag) cmd.Flags().StringVarP(&o.commitSHA, "commit", "g", DefaultValueForCommit(ci, false), commitFlagDesc) cmd.Flags().StringSliceVar(&o.redactedCommitInfo, "redact-commit-info", []string{}, attestationRedactCommitInfoFlag) + cmd.Flags().BoolVar(&o.noCommitInfo, "no-commit-info", false, attestationNoCommitInfoFlag) cmd.Flags().StringVarP(&payload.OriginURL, "origin-url", "o", DefaultValue(ci, "build-url"), attestationOriginUrlFlag) cmd.Flags().StringVarP(&o.attestationNameTemplate, "name", "n", "", attestationNameFlag) cmd.Flags().StringToStringVar(&o.externalFingerprints, "external-fingerprint", map[string]string{}, externalFingerprintFlag) diff --git a/cmd/kosli/root.go b/cmd/kosli/root.go index ee82364c0..8fb4104d7 100644 --- a/cmd/kosli/root.go +++ b/cmd/kosli/root.go @@ -327,6 +327,7 @@ Paths the list already matches stay excluded whatever is later added there, so k showUnchangedArtifactsFlag = "[defaulted] Show the unchanged artifacts present in both snapshots within the diff output." attestationFingerprintFlag = "[conditional] The SHA256 fingerprint of the artifact to attach the attestation to. Only required if the attestation is for an artifact and --artifact-type and artifact name/path are not used." attestationCommitFlag = "[conditional] The git commit for which the attestation is associated to. Becomes required when reporting an attestation for an artifact before reporting it to Kosli. (defaulted in some CIs: https://docs.kosli.com/integrations/ci_cd ). If both --commit and --repo-root are left at their defaults and the commit cannot be read from the repository, a warning is printed and the attestation is sent without commit info." + attestationNoCommitInfoFlag = "[optional] Send the attestation without git commit info and without the repository info defaulted from the CI environment. Use it when the job that attests runs in a different repository from the one the trail belongs to. Repository flags that are passed explicitly are still sent. Cannot be combined with --commit." attestationRedactCommitInfoFlag = "[optional] The list of commit info to be redacted before sending to Kosli. Allowed values are one or more of [author, message, branch]." attestationOriginUrlFlag = "[optional] The url pointing to where the attestation came from or is related. (defaulted to the CI url in some CIs: https://docs.kosli.com/integrations/ci_cd/#defaulted-kosli-command-flags-from-ci-variables )." attestationNameFlag = "The name of the attestation as declared in the flow or trail yaml template." diff --git a/cmd/kosli/testdata/empty-flag-audit-coverage.json b/cmd/kosli/testdata/empty-flag-audit-coverage.json index b121145bd..ce30451e8 100644 --- a/cmd/kosli/testdata/empty-flag-audit-coverage.json +++ b/cmd/kosli/testdata/empty-flag-audit-coverage.json @@ -128,6 +128,7 @@ "fingerprint": "string", "flow": "string", "name": "string", + "no-commit-info": "bool", "origin-url": "string", "redact-commit-info": "stringSlice", "registry-password": "string", @@ -157,6 +158,7 @@ "fingerprint": "string", "flow": "string", "name": "string", + "no-commit-info": "bool", "origin-url": "string", "redact-commit-info": "stringSlice", "registry-password": "string", @@ -184,6 +186,7 @@ "fingerprint": "string", "flow": "string", "name": "string", + "no-commit-info": "bool", "origin-url": "string", "redact-commit-info": "stringSlice", "registry-password": "string", @@ -251,6 +254,7 @@ "jira-trailer": "string", "jira-username": "string", "name": "string", + "no-commit-info": "bool", "origin-url": "string", "redact-commit-info": "stringSlice", "registry-password": "string", @@ -277,6 +281,7 @@ "fingerprint": "string", "flow": "string", "name": "string", + "no-commit-info": "bool", "origin-url": "string", "redact-commit-info": "stringSlice", "registry-password": "string", @@ -306,6 +311,7 @@ "flow": "string", "name": "string", "new-compliance-status": "bool", + "no-commit-info": "bool", "origin-url": "string", "original-attestation-type": "string", "reason": "string", @@ -337,6 +343,7 @@ "fingerprint": "string", "flow": "string", "name": "string", + "no-commit-info": "bool", "origin-url": "string", "project": "string", "redact-commit-info": "stringSlice", @@ -369,6 +376,7 @@ "fingerprint": "string", "flow": "string", "name": "string", + "no-commit-info": "bool", "origin-url": "string", "redact-commit-info": "stringSlice", "registry-password": "string", @@ -399,6 +407,7 @@ "github-org": "string", "github-token": "string", "name": "string", + "no-commit-info": "bool", "origin-url": "string", "redact-commit-info": "stringSlice", "registry-password": "string", @@ -429,6 +438,7 @@ "gitlab-org": "string", "gitlab-token": "string", "name": "string", + "no-commit-info": "bool", "origin-url": "string", "redact-commit-info": "stringSlice", "registry-password": "string", @@ -459,6 +469,7 @@ "flow": "string", "key": "string", "name": "string", + "no-commit-info": "bool", "origin-url": "string", "redact-commit-info": "stringSlice", "registry-password": "string", @@ -486,6 +497,7 @@ "fingerprint": "string", "flow": "string", "name": "string", + "no-commit-info": "bool", "origin-url": "string", "redact-commit-info": "stringSlice", "registry-password": "string", @@ -515,6 +527,7 @@ "flow": "string", "max-wait": "int", "name": "string", + "no-commit-info": "bool", "origin-url": "string", "pull-request": "string", "redact-commit-info": "stringSlice", diff --git a/cmd/kosli/testdata/output/docs/mintlify/snyk.md b/cmd/kosli/testdata/output/docs/mintlify/snyk.md index a4c7fbaee..aa0042ae4 100644 --- a/cmd/kosli/testdata/output/docs/mintlify/snyk.md +++ b/cmd/kosli/testdata/output/docs/mintlify/snyk.md @@ -37,6 +37,8 @@ If `--commit` is only defaulted from CI and `--repo-root` is left at `.`, a miss prints a warning and the attestation is sent without commit info. Note that when the attestation is reported for an artifact that does not yet exist in Kosli, `--commit` is required to facilitate binding the attestation to the right artifact. +Use `--no-commit-info` when the job that attests runs in a different repository from the one the trail belongs to: +the attestation is then sent without commit info and without the repository info defaulted from the CI environment. You can optionally redact some of the git commit data sent to Kosli using `--redact-commit-info`. To record repository information, all three of `--repo-id`, `--repo-url`, and `--repository` must be set together. These are automatically set in GitHub Actions, GitLab CI, Bitbucket Pipelines, and Azure DevOps. @@ -58,6 +60,7 @@ In other CI systems, set them explicitly to capture repository metadata. | `-f`, `--flow` | string | The Kosli flow name. | | `-h`, `--help` | bool | help for snyk | | `-n`, `--name` | string | The name of the attestation as declared in the flow or trail yaml template. | +| `--no-commit-info` | bool | [optional] Send the attestation without git commit info and without the repository info defaulted from the CI environment. Use it when the job that attests runs in a different repository from the one the trail belongs to. Repository flags that are passed explicitly are still sent. Cannot be combined with `--commit`. | | `-o`, `--origin-url` | string | [optional] The url pointing to where the attestation came from or is related. (defaulted to the CI url in some CIs: [docs](/integrations/ci_cd/#defaulted-kosli-command-flags-from-ci-variables) ). | | `--redact-commit-info` | strings | [optional] The list of commit info to be redacted before sending to Kosli. Allowed values are one or more of [author, message, branch]. | | `--registry-password` | string | [conditional] The container registry password or access token. Only required if you want to read container image SHA256 digest from a remote container registry and it is not already accessible via Docker/Podman auth files or a credential helper. | diff --git a/hack/empty-flag-audit/categories.json b/hack/empty-flag-audit/categories.json index 3ccde9d44..7c1fb7598 100644 --- a/hack/empty-flag-audit/categories.json +++ b/hack/empty-flag-audit/categories.json @@ -98,6 +98,7 @@ "namespaces-regex": "filter", "new-compliance-status": "switch", "no-assert": "switch", + "no-commit-info": "switch", "organization": "identity", "origin-url": "location", "original-attestation-type": "identity", diff --git a/hack/empty-flag-audit/spec.json b/hack/empty-flag-audit/spec.json index 3aa93c955..1b2a9d6a5 100644 --- a/hack/empty-flag-audit/spec.json +++ b/hack/empty-flag-audit/spec.json @@ -24,6 +24,7 @@ "fingerprint", "flow", "name", + "no-commit-info", "origin-url", "redact-commit-info", "registry-password", @@ -62,7 +63,8 @@ "repo-url": "http://example.com", "repository": "probe-repository", "trail": "{trail}", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "true" }, "setup": [ { @@ -575,6 +577,7 @@ "fingerprint", "flow", "name", + "no-commit-info", "origin-url", "redact-commit-info", "registry-password", @@ -615,7 +618,8 @@ "repository": "probe-repository", "trail": "{trail}", "type": "{name}", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "true" }, "setup": [ { @@ -709,6 +713,7 @@ "fingerprint", "flow", "name", + "no-commit-info", "origin-url", "redact-commit-info", "registry-password", @@ -748,7 +753,8 @@ "repo-url": "http://example.com", "repository": "probe-repository", "trail": "{trail}", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "true" }, "setup": [ { @@ -843,6 +849,7 @@ "jira-trailer", "jira-username", "name", + "no-commit-info", "origin-url", "redact-commit-info", "registry-password", @@ -890,7 +897,8 @@ "repo-url": "http://example.com", "repository": "probe-repository", "trail": "{trail}", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "false" }, "setup": [ { @@ -948,6 +956,7 @@ "fingerprint", "flow", "name", + "no-commit-info", "origin-url", "redact-commit-info", "registry-password", @@ -989,7 +998,8 @@ "results-dir": "cmd/kosli/testdata/junit", "trail": "{trail}", "upload-results": "true", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "true" }, "setup": [ { @@ -1071,6 +1081,7 @@ "fingerprint", "flow", "name", + "no-commit-info", "origin-url", "redact-commit-info", "registry-password", @@ -1112,7 +1123,8 @@ "scan-results": "cmd/kosli/testdata/snyk_scan_example.json", "trail": "{trail}", "upload-results": "true", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "true" }, "setup": [ { @@ -1171,6 +1183,7 @@ "flow", "max-wait", "name", + "no-commit-info", "origin-url", "pull-request", "redact-commit-info", @@ -1225,7 +1238,8 @@ "sonar-server-url": "probe-sonar-server-url", "sonar-working-dir": "probe-sonar-working-dir", "trail": "{trail}", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "true" }, "setup": [ { @@ -3787,6 +3801,7 @@ "flow", "name", "new-compliance-status", + "no-commit-info", "origin-url", "original-attestation-type", "reason", @@ -3829,7 +3844,8 @@ "repo-url": "http://example.com", "repository": "probe-repository", "trail": "{trail}", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "true" }, "setup": [ { @@ -4133,6 +4149,7 @@ "fingerprint", "flow", "name", + "no-commit-info", "origin-url", "project", "redact-commit-info", @@ -4175,7 +4192,8 @@ "repo-url": "http://example.com", "repository": "probe-repository", "trail": "{trail}", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "false" }, "setup": [ { @@ -4242,6 +4260,7 @@ "fingerprint", "flow", "name", + "no-commit-info", "origin-url", "redact-commit-info", "registry-password", @@ -4284,7 +4303,8 @@ "repo-url": "http://example.com", "repository": "probe-repository", "trail": "{trail}", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "false" }, "setup": [ { @@ -4349,6 +4369,7 @@ "github-org", "github-token", "name", + "no-commit-info", "origin-url", "redact-commit-info", "registry-password", @@ -4390,7 +4411,8 @@ "repo-url": "http://example.com", "repository": "probe-repository", "trail": "{trail}", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "false" }, "setup": [ { @@ -4456,6 +4478,7 @@ "gitlab-org", "gitlab-token", "name", + "no-commit-info", "origin-url", "redact-commit-info", "registry-password", @@ -4497,7 +4520,8 @@ "repo-url": "http://example.com", "repository": "probe-repository", "trail": "{trail}", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "false" }, "setup": [ { @@ -5082,6 +5106,7 @@ "flow", "key", "name", + "no-commit-info", "origin-url", "redact-commit-info", "registry-password", @@ -5125,7 +5150,8 @@ "repository": "probe-repository", "sbom-file": "cmd/kosli/testdata/sbom/cyclonedx.json", "trail": "{trail}", - "user-data": "cmd/kosli/testdata/person-schema.json" + "user-data": "cmd/kosli/testdata/person-schema.json", + "no-commit-info": "true" }, "setup": [ {