Skip to content

Commit b834f95

Browse files
committed
fix(github): scope App token to triggering repo
Reissue a scoped token in SetClient when no extra scope config is present. The initial token is created before RepositoryIDs are populated, so it has access to all repos in the installation. After ScopeTokenToListOfRepos returns empty, fall back to GetAppToken which now uses the populated RepositoryIDs. Signed-off-by: Akshay Pant <akpant@redhat.com>
1 parent c37a213 commit b834f95

2 files changed

Lines changed: 34 additions & 5 deletions

File tree

pkg/provider/github/github.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,17 @@ func (v *Provider) SetClient(ctx context.Context, run *params.Run, event *info.E
363363
// If Global and Repo level configurations are not provided then lets not override the provider token.
364364
if token != "" {
365365
event.Provider.Token = token
366+
} else if len(v.RepositoryIDs) > 0 {
367+
// We need to keep the token unscoped until ScopeTokenToListOfRepos so that CreateToken can
368+
// look up the extra repos from the configmap.
369+
// Token is scoped to only the calling repo if no additional scoping repos are configured
370+
// so that no unwanted remote tasks are executed.
371+
ns := info.GetNS(ctx)
372+
scopedToken, err := v.GetAppToken(ctx, run.Clients.Kube, event.Provider.URL, event.InstallationID, ns)
373+
if err != nil {
374+
return fmt.Errorf("failed to scope token to triggering repository: %w", err)
375+
}
376+
event.Provider.Token = scopedToken
366377
}
367378
}
368379

pkg/provider/github/parse_payload.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,29 @@ type Payload struct {
131131
Installation struct {
132132
ID *int64 `json:"id"`
133133
} `json:"installation"`
134+
Repository struct {
135+
ID *int64 `json:"id"`
136+
} `json:"repository"`
134137
}
135138

136-
func getInstallationIDFromPayload(payload string) (int64, error) {
139+
func getInstallationAndRepoIDFromPayload(payload string) (int64, int64, error) {
137140
var data Payload
138141
err := json.Unmarshal([]byte(payload), &data)
139142
if err != nil {
140-
return -1, err
143+
return -1, -1, err
141144
}
145+
146+
var installationID int64 = -1
147+
var repoID int64 = -1
142148
if data.Installation.ID != nil {
143-
return *data.Installation.ID, nil
149+
installationID = *data.Installation.ID
150+
}
151+
152+
if data.Repository.ID != nil {
153+
repoID = *data.Repository.ID
144154
}
145-
return -1, nil
155+
156+
return installationID, repoID, nil
146157
}
147158

148159
// ParsePayload will parse the payload and return the event
@@ -177,7 +188,7 @@ func (v *Provider) ParsePayload(ctx context.Context, run *params.Run, request *h
177188
return nil, err
178189
}
179190

180-
installationIDFrompayload, err := getInstallationIDFromPayload(payload)
191+
installationIDFrompayload, repoIDFromPayload, err := getInstallationAndRepoIDFromPayload(payload)
181192
if err != nil {
182193
return nil, err
183194
}
@@ -189,6 +200,10 @@ func (v *Provider) ParsePayload(ctx context.Context, run *params.Run, request *h
189200
}
190201
}
191202

203+
if repoIDFromPayload > 0 {
204+
v.RepositoryIDs = []int64{repoIDFromPayload}
205+
}
206+
192207
eventInt, err := github.ParseWebHook(event.EventType, []byte(payload))
193208
if err != nil {
194209
return nil, err
@@ -537,6 +552,7 @@ func (v *Provider) handleReRequestEvent(ctx context.Context, event *github.Check
537552
// fine because you can't do a rereq without being a github owner?
538553
runevent.Sender = event.GetSender().GetLogin()
539554
v.userType = event.GetSender().GetType()
555+
v.RepositoryIDs = []int64{event.GetRepo().GetID()}
540556
return runevent, nil
541557
}
542558
runevent.PullRequestNumber = event.GetCheckRun().GetCheckSuite().PullRequests[0].GetNumber()
@@ -584,6 +600,7 @@ func (v *Provider) handleCheckSuites(ctx context.Context, event *github.CheckSui
584600
// fine because you can't do a rereq without being a github owner?
585601
runevent.Sender = event.GetSender().GetLogin()
586602
v.userType = event.GetSender().GetType()
603+
v.RepositoryIDs = []int64{event.GetRepo().GetID()}
587604
return runevent, nil
588605
}
589606
runevent.PullRequestNumber = event.GetCheckSuite().PullRequests[0].GetNumber()
@@ -699,6 +716,7 @@ func (v *Provider) handleCommitCommentEvent(ctx context.Context, event *github.C
699716
runevent.BaseURL = runevent.HeadURL
700717
runevent.TriggerTarget = triggertype.Push
701718
v.userType = event.GetSender().GetType()
719+
v.RepositoryIDs = []int64{event.GetRepo().GetID()}
702720

703721
repo, err := MatchEventURLRepo(ctx, v.Run, runevent, "")
704722
if err != nil {

0 commit comments

Comments
 (0)