Skip to content

Commit

Permalink
Use base64 encoding for github and runner context
Browse files Browse the repository at this point in the history
Co-authored-by: Jeroen Knoops <jeroen.knoops@philips.com>
Signed-off-by: Marco Franssen <marco.franssen@philips.com>
  • Loading branch information
marcofranssen and JeroenKnoops committed Dec 24, 2021
1 parent 60802d7 commit 065eab8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
34 changes: 19 additions & 15 deletions cmd/slsa-provenance/cli/files_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli_test

import (
"encoding/base64"
"fmt"
"os"
"path"
Expand All @@ -21,6 +22,9 @@ func TestGenerateFilesCliOptions(t *testing.T) {
rootDir := path.Join(path.Dir(filename), "../../..")
provenanceFile := path.Join(rootDir, "bin/unittest-provenance.json")

base64GitHubContext := base64.StdEncoding.EncodeToString([]byte(githubContext))
base64RunnerContext := base64.StdEncoding.EncodeToString([]byte(runnerContext))

testCases := []struct {
name string
err error
Expand All @@ -46,7 +50,7 @@ func TestGenerateFilesCliOptions(t *testing.T) {
"--artifact-path",
path.Join(rootDir, "bin/slsa-provenance"),
"--github-context",
githubContext,
base64GitHubContext,
"--output-path",
provenanceFile,
},
Expand All @@ -58,9 +62,9 @@ func TestGenerateFilesCliOptions(t *testing.T) {
"--artifact-path",
unknownFile,
"--github-context",
githubContext,
base64GitHubContext,
"--runner-context",
runnerContext,
base64RunnerContext,
},
},
{
Expand All @@ -70,11 +74,11 @@ func TestGenerateFilesCliOptions(t *testing.T) {
"--artifact-path",
path.Join(rootDir, "bin/slsa-provenance"),
"--github-context",
githubContext,
base64GitHubContext,
"--output-path",
provenanceFile,
"--runner-context",
runnerContext,
base64RunnerContext,
},
},
{
Expand All @@ -84,11 +88,11 @@ func TestGenerateFilesCliOptions(t *testing.T) {
"--artifact-path",
path.Join(rootDir, "bin/slsa-provenance"),
"--github-context",
githubContext,
base64GitHubContext,
"--output-path",
provenanceFile,
"--runner-context",
runnerContext,
base64RunnerContext,
"--extra-materials",
path.Join(rootDir, "test-data/materials-valid.json"),
},
Expand All @@ -100,11 +104,11 @@ func TestGenerateFilesCliOptions(t *testing.T) {
"--artifact-path",
path.Join(rootDir, "bin/slsa-provenance"),
"--github-context",
githubContext,
base64GitHubContext,
"--output-path",
provenanceFile,
"--runner-context",
runnerContext,
base64RunnerContext,
"--extra-materials",
path.Join(rootDir, "test-data/materials-broken.not-json"),
},
Expand All @@ -116,11 +120,11 @@ func TestGenerateFilesCliOptions(t *testing.T) {
"--artifact-path",
path.Join(rootDir, "bin/slsa-provenance"),
"--github-context",
githubContext,
base64GitHubContext,
"--output-path",
provenanceFile,
"--runner-context",
runnerContext,
base64RunnerContext,
"--extra-materials",
fmt.Sprintf("%s,%s", path.Join(rootDir, "test-data/materials-valid.json"), unknownFile),
},
Expand All @@ -132,11 +136,11 @@ func TestGenerateFilesCliOptions(t *testing.T) {
"--artifact-path",
path.Join(rootDir, "bin/slsa-provenance"),
"--github-context",
githubContext,
base64GitHubContext,
"--output-path",
provenanceFile,
"--runner-context",
runnerContext,
base64RunnerContext,
"--extra-materials",
path.Join(rootDir, "test-data/materials-no-uri.json"),
},
Expand All @@ -148,11 +152,11 @@ func TestGenerateFilesCliOptions(t *testing.T) {
"--artifact-path",
path.Join(rootDir, "bin/slsa-provenance"),
"--github-context",
githubContext,
base64GitHubContext,
"--output-path",
provenanceFile,
"--runner-context",
runnerContext,
base64RunnerContext,
"--extra-materials",
path.Join(rootDir, "test-data/materials-no-digest.json"),
},
Expand Down
8 changes: 6 additions & 2 deletions cmd/slsa-provenance/cli/github-release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli_test

import (
"context"
"encoding/base64"
"os"
"path"
"runtime"
Expand Down Expand Up @@ -47,15 +48,18 @@ func TestProvenenaceGitHubRelease(t *testing.T) {
_, err = client.Repositories.DeleteRelease(ctx, owner, repo, releaseID)
}()

base64GitHubContext := base64.StdEncoding.EncodeToString([]byte(githubContext))
base64RunnerContext := base64.StdEncoding.EncodeToString([]byte(runnerContext))

output, err := executeCommand(cli.GitHubRelease(),
"--artifact-path",
artifactPath,
"--github-context",
githubContext,
base64GitHubContext,
"--output-path",
provenanceFile,
"--runner-context",
runnerContext,
base64RunnerContext,
"--tag-name",
"v0.0.0-generate-test",
)
Expand Down
13 changes: 11 additions & 2 deletions cmd/slsa-provenance/cli/options/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package options

import (
"encoding/base64"
"encoding/json"
"fmt"
"os"
Expand All @@ -24,8 +25,12 @@ func (o *GenerateOptions) GetGitHubContext() (*github.Context, error) {
if o.GitHubContext == "" {
return nil, RequiredFlagError("github-context")
}
decodedContext, err := base64.StdEncoding.DecodeString(o.GitHubContext)
if err != nil {
return nil, err
}
var gh github.Context
if err := json.Unmarshal([]byte(o.GitHubContext), &gh); err != nil {
if err := json.Unmarshal(decodedContext, &gh); err != nil {
return nil, fmt.Errorf("failed to unmarshal github context json: %w", err)
}
return &gh, nil
Expand All @@ -36,8 +41,12 @@ func (o *GenerateOptions) GetRunnerContext() (*github.RunnerContext, error) {
if o.RunnerContext == "" {
return nil, RequiredFlagError("runner-context")
}
decodedContext, err := base64.StdEncoding.DecodeString(o.RunnerContext)
if err != nil {
return nil, err
}
var runner github.RunnerContext
if err := json.Unmarshal([]byte(o.RunnerContext), &runner); err != nil {
if err := json.Unmarshal(decodedContext, &runner); err != nil {
return nil, fmt.Errorf("failed to unmarshal runner context json: %w", err)
}
return &runner, nil
Expand Down

0 comments on commit 065eab8

Please sign in to comment.