Skip to content

Commit a5613e0

Browse files
authored
Make authenticated calls whe ensuring options (#267)
Signed-off-by: Adolfo Garcia Veytia (puerco) <[email protected]>
1 parent 83d0107 commit a5613e0

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

internal/cmd/checklevelprov.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"google.golang.org/protobuf/encoding/protojson"
1515

1616
"github.com/slsa-framework/slsa-source-poc/pkg/attest"
17+
"github.com/slsa-framework/slsa-source-poc/pkg/auth"
1718
"github.com/slsa-framework/slsa-source-poc/pkg/ghcontrol"
1819
"github.com/slsa-framework/slsa-source-poc/pkg/policy"
1920
)
@@ -78,12 +79,19 @@ func addCheckLevelProv(parentCmd *cobra.Command) {
7879
}
7980

8081
func doCheckLevelProv(checkLevelProvArgs *checkLevelProvOpts) error {
81-
ghconnection := ghcontrol.NewGhConnection(checkLevelProvArgs.owner, checkLevelProvArgs.repository, ghcontrol.BranchToFullRef(checkLevelProvArgs.branch)).WithAuthToken(githubToken)
82+
t := githubToken
83+
var err error
84+
if t == "" {
85+
t, err = auth.New().ReadToken()
86+
if err != nil {
87+
return err
88+
}
89+
}
90+
ghconnection := ghcontrol.NewGhConnection(checkLevelProvArgs.owner, checkLevelProvArgs.repository, ghcontrol.BranchToFullRef(checkLevelProvArgs.branch)).WithAuthToken(t)
8291
ghconnection.Options.AllowMergeCommits = checkLevelProvArgs.allowMergeCommits
8392
ctx := context.Background()
8493

8594
prevCommit := checkLevelProvArgs.prevCommit
86-
var err error
8795
if prevCommit == "" {
8896
prevCommit, err = ghconnection.GetPriorCommit(ctx, checkLevelProvArgs.commit)
8997
if err != nil {

internal/cmd/options.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/carabiner-dev/vcslocator"
1313
"github.com/spf13/cobra"
1414

15+
"github.com/slsa-framework/slsa-source-poc/pkg/auth"
1516
"github.com/slsa-framework/slsa-source-poc/pkg/ghcontrol"
1617
"github.com/slsa-framework/slsa-source-poc/pkg/sourcetool/models"
1718
)
@@ -122,7 +123,16 @@ func (bo *branchOptions) EnsureDefaults() error {
122123
return nil
123124
}
124125

125-
gcx := ghcontrol.NewGhConnection(bo.owner, bo.repository, "").WithAuthToken(githubToken)
126+
t := githubToken
127+
var err error
128+
if t == "" {
129+
t, err = auth.New().ReadToken()
130+
if err != nil {
131+
return err
132+
}
133+
}
134+
135+
gcx := ghcontrol.NewGhConnection(bo.owner, bo.repository, "").WithAuthToken(t)
126136
branch, err := gcx.GetDefaultBranch(context.Background())
127137
if err != nil {
128138
return fmt.Errorf("reading repository default branch: %w", err)
@@ -181,7 +191,16 @@ func (co *commitOptions) EnsureDefaults() error {
181191
}
182192

183193
if co.commit == "" {
184-
gcx := ghcontrol.NewGhConnection(co.owner, co.repository, "").WithAuthToken(githubToken)
194+
t := githubToken
195+
var err error
196+
if t == "" {
197+
t, err = auth.New().ReadToken()
198+
if err != nil {
199+
return err
200+
}
201+
}
202+
203+
gcx := ghcontrol.NewGhConnection(co.owner, co.repository, "").WithAuthToken(t)
185204
digest, err := gcx.GetLatestCommit(context.Background(), co.branch)
186205
if err != nil {
187206
return fmt.Errorf("fetching last commit from %q: %w", co.branch, err)

0 commit comments

Comments
 (0)