Skip to content

Commit b06e48b

Browse files
authored
Handle missing permissions more gracefully (#292)
* Add access denied error Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]> * Controls: Handle missing access to repos Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]> --------- Signed-off-by: Adolfo García Veytia (Puerco) <[email protected]>
1 parent 6292801 commit b06e48b

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

internal/cmd/setup.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,14 @@ a fork of the repository you want to protect.
448448
fmt.Printf("\n ℹ️ Controls already enabled on %s\n\n", opts.GetRepository().Path)
449449
return nil
450450
}
451+
452+
if errors.Is(err, models.ErrRepositoryAccessDenied) {
453+
fmt.Printf("\n 🔐 %s sourcetool does not have access to %s\n\n", colorHiRed("Error:"), opts.GetRepository().Path)
454+
fmt.Println()
455+
fmt.Printf("Please run %s again and grant the app access\n", w("sourcetool auth login"))
456+
fmt.Println("to the repository or organization.")
457+
return nil
458+
}
451459
return fmt.Errorf("configuring controls: %w", err)
452460
}
453461

pkg/ghcontrol/checklevel.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"fmt"
99
"log"
10+
"net/http"
1011
"slices"
1112
"time"
1213

@@ -249,7 +250,7 @@ func (ghc *GitHubConnection) EnableBranchRules(ctx context.Context) error {
249250
}
250251

251252
// Create the SLSA ruleset
252-
if _, _, err := ghc.Client().Repositories.CreateRuleset(ctx, ghc.Owner(), ghc.Repo(), github.RepositoryRuleset{
253+
if _, resp, err := ghc.Client().Repositories.CreateRuleset(ctx, ghc.Owner(), ghc.Repo(), github.RepositoryRuleset{
253254
Name: "SLSA Branch Controls",
254255
Target: github.Ptr(github.RulesetTargetBranch),
255256
Enforcement: EnforcementActive,
@@ -265,6 +266,9 @@ func (ghc *GitHubConnection) EnableBranchRules(ctx context.Context) error {
265266
NonFastForward: &github.EmptyRuleParameters{},
266267
},
267268
}); err != nil {
269+
if resp.StatusCode == http.StatusNotFound {
270+
return models.ErrRepositoryAccessDenied
271+
}
268272
return fmt.Errorf("creating branch protection ruleset: %w", err)
269273
}
270274

@@ -290,7 +294,7 @@ func (ghc *GitHubConnection) EnableTagRules(ctx context.Context) error {
290294
}
291295

292296
// Create the SLSA ruleset
293-
if _, _, err := ghc.Client().Repositories.CreateRuleset(ctx, ghc.Owner(), ghc.Repo(), github.RepositoryRuleset{
297+
if _, resp, err := ghc.Client().Repositories.CreateRuleset(ctx, ghc.Owner(), ghc.Repo(), github.RepositoryRuleset{
294298
Name: "SLSA Tag Controls",
295299
Target: github.Ptr(github.RulesetTargetTag),
296300
Enforcement: EnforcementActive,
@@ -309,6 +313,9 @@ func (ghc *GitHubConnection) EnableTagRules(ctx context.Context) error {
309313
},
310314
},
311315
}); err != nil {
316+
if resp.StatusCode == http.StatusNotFound {
317+
return models.ErrRepositoryAccessDenied
318+
}
312319
return fmt.Errorf("creating tag protection ruleset: %w", err)
313320
}
314321

pkg/sourcetool/models/models.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import (
1818
"github.com/slsa-framework/source-tool/pkg/slsa"
1919
)
2020

21-
var ErrProtectionAlreadyInPlace = errors.New("controls already in place in the repository")
21+
var (
22+
ErrProtectionAlreadyInPlace = errors.New("controls already in place in the repository")
23+
ErrRepositoryAccessDenied = errors.New("access to repository denied")
24+
)
2225

2326
// AttestationStorageReader abstracts an attestation storage system where
2427
// sourcetool can read VSAs and provenance attestations.

0 commit comments

Comments
 (0)