Skip to content
Open
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
47 changes: 47 additions & 0 deletions cmd/kosli/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
97 changes: 97 additions & 0 deletions cmd/kosli/attestationCommitInfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
1 change: 1 addition & 0 deletions cmd/kosli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/kosli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
13 changes: 13 additions & 0 deletions cmd/kosli/testdata/empty-flag-audit-coverage.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"fingerprint": "string",
"flow": "string",
"name": "string",
"no-commit-info": "bool",
"origin-url": "string",
"redact-commit-info": "stringSlice",
"registry-password": "string",
Expand Down Expand Up @@ -157,6 +158,7 @@
"fingerprint": "string",
"flow": "string",
"name": "string",
"no-commit-info": "bool",
"origin-url": "string",
"redact-commit-info": "stringSlice",
"registry-password": "string",
Expand Down Expand Up @@ -184,6 +186,7 @@
"fingerprint": "string",
"flow": "string",
"name": "string",
"no-commit-info": "bool",
"origin-url": "string",
"redact-commit-info": "stringSlice",
"registry-password": "string",
Expand Down Expand Up @@ -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",
Expand All @@ -277,6 +281,7 @@
"fingerprint": "string",
"flow": "string",
"name": "string",
"no-commit-info": "bool",
"origin-url": "string",
"redact-commit-info": "stringSlice",
"registry-password": "string",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -337,6 +343,7 @@
"fingerprint": "string",
"flow": "string",
"name": "string",
"no-commit-info": "bool",
"origin-url": "string",
"project": "string",
"redact-commit-info": "stringSlice",
Expand Down Expand Up @@ -369,6 +376,7 @@
"fingerprint": "string",
"flow": "string",
"name": "string",
"no-commit-info": "bool",
"origin-url": "string",
"redact-commit-info": "stringSlice",
"registry-password": "string",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -459,6 +469,7 @@
"flow": "string",
"key": "string",
"name": "string",
"no-commit-info": "bool",
"origin-url": "string",
"redact-commit-info": "stringSlice",
"registry-password": "string",
Expand Down Expand Up @@ -486,6 +497,7 @@
"fingerprint": "string",
"flow": "string",
"name": "string",
"no-commit-info": "bool",
"origin-url": "string",
"redact-commit-info": "stringSlice",
"registry-password": "string",
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions cmd/kosli/testdata/output/docs/mintlify/snyk.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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. |
Expand Down
1 change: 1 addition & 0 deletions hack/empty-flag-audit/categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading
Loading