Skip to content

Commit ca0f984

Browse files
committed
add json format
1 parent e9fbe9d commit ca0f984

4 files changed

Lines changed: 46 additions & 10 deletions

File tree

‎.github/CODEOWNERS‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ internal/codespaces/ @cli/codespaces
55

66
# Limit Package Security team ownership to the attestation command package and related integration tests
77
pkg/cmd/attestation/ @cli/package-security
8+
pkg/cmd/release/attestation @cli/package-security
9+
pkg/cmd/release/verify @cli/package-security
10+
pkg/cmd/release/verify-asset @cli/package-security
11+
812
test/integration/attestation-cmd @cli/package-security
913

1014
pkg/cmd/attestation/verification/embed/tuf-repo.github.com/ @cli/tuf-root-reviewers

‎pkg/cmd/release/attestation/options.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package attestation
33
import (
44
"fmt"
55
"net/http"
6+
"path/filepath"
67
"strings"
78

89
"github.com/cli/cli/v2/internal/gh"
@@ -47,8 +48,15 @@ type AttestOptions struct {
4748
Hostname string
4849
EC verification.EnforcementCriteria
4950
// Tenant is only set when tenancy is used
50-
Tenant string
51-
FilePath string
51+
Tenant string
52+
AssetFilePath string
53+
}
54+
55+
// Clean cleans the file path option values
56+
func (opts *AttestOptions) Clean() {
57+
if opts.AssetFilePath != "" {
58+
opts.AssetFilePath = filepath.Clean(opts.AssetFilePath)
59+
}
5260
}
5361

5462
// AreFlagsValid checks that the provided flag combination is valid

‎pkg/cmd/release/verify-asset/verify-asset.go‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func NewCmdVerifyAsset(f *cmdutil.Factory, runF func(*attestation.AttestOptions)
3131
return cmdutil.FlagErrorf("You must specify a tag and a file path")
3232
}
3333

34-
opts.TagName = args[0]
35-
opts.FilePath = args[1]
34+
tagName := args[0]
35+
assetFilePath := args[1]
3636

3737
httpClient, err := f.HttpClient()
3838
if err != nil {
@@ -46,8 +46,8 @@ func NewCmdVerifyAsset(f *cmdutil.Factory, runF func(*attestation.AttestOptions)
4646
hostname, _ := ghauth.DefaultHost()
4747

4848
*opts = attestation.AttestOptions{
49-
TagName: opts.TagName,
50-
FilePath: opts.FilePath,
49+
TagName: tagName,
50+
AssetFilePath: assetFilePath,
5151
Repo: baseRepo.RepoOwner() + "/" + baseRepo.RepoName(),
5252
APIClient: api.NewLiveClient(httpClient, hostname, logger),
5353
Limit: 10,
@@ -93,15 +93,17 @@ func NewCmdVerifyAsset(f *cmdutil.Factory, runF func(*attestation.AttestOptions)
9393
return verifyAssetRun(opts)
9494
},
9595
}
96+
cmdutil.AddFormatFlags(cmd, &opts.Exporter)
97+
9698
return cmd
9799
}
98100

99101
func verifyAssetRun(opts *attestation.AttestOptions) error {
100102
ctx := context.Background()
101-
fileName := getFileName(opts.FilePath)
103+
fileName := getFileName(opts.AssetFilePath)
102104

103105
// calculate the digest of the file
104-
fileDigest, err := artifact.NewDigestedArtifact(nil, opts.FilePath, "sha256")
106+
fileDigest, err := artifact.NewDigestedArtifact(nil, opts.AssetFilePath, "sha256")
105107
if err != nil {
106108
opts.Logger.Println(opts.Logger.ColorScheme.Red("✗ Failed to calculate file digest"))
107109
return err
@@ -141,7 +143,7 @@ func verifyAssetRun(opts *attestation.AttestOptions) error {
141143
}
142144

143145
if len(filteredAttestations) == 0 {
144-
opts.Logger.Printf(opts.Logger.ColorScheme.Red("Release %s does not contain %s (%s)\n"), opts.TagName, opts.FilePath, fileDigest.DigestWithAlg())
146+
opts.Logger.Printf(opts.Logger.ColorScheme.Red("Release %s does not contain %s (%s)\n"), opts.TagName, opts.AssetFilePath, fileDigest.DigestWithAlg())
145147
return nil
146148
}
147149

@@ -152,10 +154,20 @@ func verifyAssetRun(opts *attestation.AttestOptions) error {
152154

153155
if err != nil {
154156
opts.Logger.Println(opts.Logger.ColorScheme.Red(errMsg))
155-
opts.Logger.Printf(opts.Logger.ColorScheme.Red("Release %s does not contain %s (%s)\n"), opts.TagName, opts.FilePath, fileDigest.DigestWithAlg())
157+
opts.Logger.Printf(opts.Logger.ColorScheme.Red("Release %s does not contain %s (%s)\n"), opts.TagName, opts.AssetFilePath, fileDigest.DigestWithAlg())
156158
return err
157159
}
158160

161+
// If an exporter is provided with the --json flag, write the results to the terminal in JSON format
162+
if opts.Exporter != nil {
163+
// print the results to the terminal as an array of JSON objects
164+
if err = opts.Exporter.Write(opts.Logger.IO, verified); err != nil {
165+
opts.Logger.Println(opts.Logger.ColorScheme.Red("✗ Failed to write JSON output"))
166+
return err
167+
}
168+
return nil
169+
}
170+
159171
opts.Logger.Printf("The following %s matched the policy criteria\n\n", text.Pluralize(len(verified), "attestation"))
160172
opts.Logger.Println(opts.Logger.ColorScheme.Green("✓ Verification succeeded!\n"))
161173
opts.Logger.Printf("Attestation found matching release %s (%s)\n", opts.TagName, releaseRefDigest.DigestWithAlg())

‎pkg/cmd/release/verify/verify.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ func NewCmdVerify(f *cmdutil.Factory, runF func(*attestation.AttestOptions) erro
9494
return verifyRun(opts)
9595
},
9696
}
97+
cmdutil.AddFormatFlags(cmd, &opts.Exporter)
98+
9799
return cmd
98100
}
99101

@@ -137,6 +139,16 @@ func verifyRun(opts *attestation.AttestOptions) error {
137139
return err
138140
}
139141

142+
// If an exporter is provided with the --json flag, write the results to the terminal in JSON format
143+
if opts.Exporter != nil {
144+
// print the results to the terminal as an array of JSON objects
145+
if err = opts.Exporter.Write(opts.Logger.IO, verified); err != nil {
146+
opts.Logger.Println(opts.Logger.ColorScheme.Red("✗ Failed to write JSON output"))
147+
return err
148+
}
149+
return nil
150+
}
151+
140152
opts.Logger.Printf("The following %s matched the policy criteria\n\n", text.Pluralize(len(verified), "attestation"))
141153
opts.Logger.Println(opts.Logger.ColorScheme.Green("✓ Verification succeeded!\n"))
142154

0 commit comments

Comments
 (0)