Skip to content

Commit bc852fd

Browse files
If custom server url exists, use that instead of the default one. (#1776)
* If custom server url exists, use that instead of the default one. Signed-off-by: Fredrik Skogman <[email protected]> * Name variable baseURL to avoid linter errors. Signed-off-by: Fredrik Skogman <[email protected]> --------- Signed-off-by: Fredrik Skogman <[email protected]>
1 parent 77325fa commit bc852fd

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

pkg/identity/github/principal.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"context"
1919
"crypto/x509"
2020
"errors"
21+
"fmt"
2122
"net/url"
2223

2324
"github.com/coreos/go-oidc/v3/oidc"
@@ -105,6 +106,7 @@ func WorkflowPrincipalFromIDToken(_ context.Context, token *oidc.IDToken) (ident
105106
WorkflowSha string `json:"workflow_sha"`
106107
RunID string `json:"run_id"`
107108
RunAttempt string `json:"run_attempt"`
109+
Enterprise string `json:"enterprise"`
108110
}
109111
if err := token.Claims(&claims); err != nil {
110112
return nil, err
@@ -159,10 +161,16 @@ func WorkflowPrincipalFromIDToken(_ context.Context, token *oidc.IDToken) (ident
159161
return nil, errors.New("missing run_attempt claim in ID token")
160162
}
161163

164+
baseURL := `https://github.com/`
165+
166+
if claims.Enterprise != "" {
167+
baseURL = fmt.Sprintf("https://%s.ghe.com/", claims.Enterprise)
168+
}
169+
162170
return &workflowPrincipal{
163171
subject: token.Subject,
164172
issuer: token.Issuer,
165-
url: `https://github.com/`,
173+
url: baseURL,
166174
sha: claims.Sha,
167175
eventName: claims.EventName,
168176
repository: claims.Repository,

pkg/identity/github/principal_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,53 @@ func TestWorkflowPrincipalFromIDToken(t *testing.T) {
8484
},
8585
WantErr: false,
8686
},
87+
`Valid token, custom base url authenticates with correct claims`: {
88+
Claims: map[string]interface{}{
89+
"aud": "sigstore",
90+
"event_name": "push",
91+
"exp": 0,
92+
"iss": "https://token.actions.githubusercontent.com",
93+
"job_workflow_ref": "sigstore/fulcio/.github/workflows/foo.yaml@refs/heads/main",
94+
"job_workflow_sha": "example-sha",
95+
"ref": "refs/heads/main",
96+
"repository": "sigstore/fulcio",
97+
"repository_id": "12345",
98+
"repository_owner": "username",
99+
"repository_owner_id": "345",
100+
"repository_visibility": "public",
101+
"run_attempt": "1",
102+
"run_id": "42",
103+
"runner_environment": "cloud-hosted",
104+
"sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
105+
"sub": "repo:sigstore/fulcio:ref:refs/heads/main",
106+
"workflow": "foo",
107+
"workflow_ref": "sigstore/other/.github/workflows/foo.yaml@refs/heads/main",
108+
"workflow_sha": "example-sha-other",
109+
"enterprise": "test",
110+
},
111+
ExpectPrincipal: workflowPrincipal{
112+
issuer: "https://token.actions.githubusercontent.com",
113+
subject: "repo:sigstore/fulcio:ref:refs/heads/main",
114+
url: "https://test.ghe.com/",
115+
jobWorkflowRef: "sigstore/fulcio/.github/workflows/foo.yaml@refs/heads/main",
116+
sha: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
117+
eventName: "push",
118+
repository: "sigstore/fulcio",
119+
workflow: "foo",
120+
ref: "refs/heads/main",
121+
jobWorkflowSha: "example-sha",
122+
runnerEnvironment: "cloud-hosted",
123+
repositoryID: "12345",
124+
repositoryOwner: "username",
125+
repositoryOwnerID: "345",
126+
repositoryVisibility: "public",
127+
workflowRef: "sigstore/other/.github/workflows/foo.yaml@refs/heads/main",
128+
workflowSha: "example-sha-other",
129+
runID: "42",
130+
runAttempt: "1",
131+
},
132+
WantErr: false,
133+
},
87134
`Token missing job_workflow_ref claim should be rejected`: {
88135
Claims: map[string]interface{}{
89136
"aud": "sigstore",

0 commit comments

Comments
 (0)