Skip to content

Commit

Permalink
git: set token only for main remote access
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
(cherry picked from commit 5bf6429)
Signed-off-by: Sebastiaan van Stijn <[email protected]>
  • Loading branch information
tonistiigi authored and thaJeztah committed Feb 22, 2021
1 parent 3da6bef commit 99bc88e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion source/git/gitsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func (gs *gitSourceHandler) getAuthToken(ctx context.Context, g session.Group) e
if s.token {
dt = []byte("basic " + base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("x-access-token:%s", dt))))
}
gs.auth = []string{"-c", "http.extraheader=Authorization: " + string(dt)}
gs.auth = []string{"-c", "http." + tokenScope(gs.src.Remote) + ".extraheader=Authorization: " + string(dt)}
break
}
return nil
Expand Down Expand Up @@ -631,3 +631,14 @@ func argsNoDepth(args []string) []string {
}
return out
}

func tokenScope(remote string) string {
// generally we can only use the token for fetching main remote but in case of github.com we do best effort
// to try reuse same token for all github.com remotes. This is the same behavior actions/checkout uses
for _, pfx := range []string{"https://github.com/", "https://www.github.com/"} {
if strings.HasPrefix(remote, pfx) {
return pfx
}
}
return remote
}

0 comments on commit 99bc88e

Please sign in to comment.