-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgithub-oidc-auth-app_test.go
195 lines (175 loc) · 8.72 KB
/
github-oidc-auth-app_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"encoding/json"
"os"
"reflect"
"testing"
"github.com/golang-jwt/jwt/v5"
"github.com/google/go-github/v53/github"
)
var claims jwt.MapClaims = jwt.MapClaims{
"actor": "major-tom",
"actor_id": 2787414,
"aud": "api://ActionsOIDCGateway",
"base_ref": "",
"environment": "production",
"event_name": "workflow_dispatch",
"head_ref": "",
"iss": "https://token.actions.githubusercontent.com",
"job_workflow_ref": "major-tom/starman/.github/workflows/manual-test.yml@refs/heads/main",
"job_workflow_sha": "44216e5ae99f3653290b60b7f995bfe1c0f3aba0",
"ref": "refs/heads/main",
"ref_type": "branch",
"repository": "major-tom/starman",
"repository_id": 630836305,
"repository_owner": "major-tom",
"repository_owner_id": 2787414,
"repository_visibility": "public",
"run_attempt": 1,
"run_id": 4779904167,
"run_number": 12,
"runner_environment": "github-hosted",
"sub": "repo:major-tom/starman:ref:refs/heads/main",
"workflow": "Manual Test Workflow",
"workflow_ref": "major-tom/starman/.github/workflows/manual-test.yml@refs/heads/main",
"workflow_sha": "44216e5ae99f3653290b60b7f995bfe1c0f3aba0",
}
func TestExactRegexEntitlement(t *testing.T) {
entitlement := Entitlement{
Environment: "production",
Repository: "major-tom/starman",
RepositoryId: 630836305,
RepositoryOwner: "major-tom",
RepositoryOwnerId: 2787414,
Actor: "major-tom",
ActorId: 2787414,
Audience: "api://ActionsOIDCGateway",
BaseRef: "",
EventName: "workflow_dispatch",
HeadRef: "",
Issuer: "https://token.actions.githubusercontent.com",
JobWokflowRef: "major-tom/starman/.github/workflows/manual-test.yml@refs/heads/main",
JobWokflowSha: "44216e5ae99f3653290b60b7f995bfe1c0f3aba0",
Ref: "refs/heads/main",
RefType: "branch",
RunAttempt: 1,
RunId: 4779904167,
RunNumber: 12,
RunnerEnvironment: "github-hosted",
Subject: "repo:major-tom/starman:ref:refs/heads/main",
Visibility: "public",
Workflow: "Manual Test Workflow",
WorkflowRef: "major-tom/starman/.github/workflows/manual-test.yml@refs/heads/main",
WorkflowSha: "44216e5ae99f3653290b60b7f995bfe1c0f3aba0",
}
entitlementRegex := entitlement.regexString()
if entitlementRegex != "^actor:major-tom,actor_id:2787414,aud:api://ActionsOIDCGateway,base_ref:.*,environment:production,event_name:workflow_dispatch,head_ref:.*,iss:https://token.actions.githubusercontent.com,job_workflow_ref:major-tom/starman/.github/workflows/manual-test.yml@refs/heads/main,job_workflow_sha:44216e5ae99f3653290b60b7f995bfe1c0f3aba0,ref:refs/heads/main,ref_type:branch,repository:major-tom/starman,repository_id:630836305,repository_owner:major-tom,repository_owner_id:2787414,repository_visibility:public,run_attempt:1,run_id:4779904167,run_number:12,runner_environment:github-hosted,sub:repo:major-tom/starman:ref:refs/heads/main,workflow:Manual Test Workflow,workflow_ref:major-tom/starman/.github/workflows/manual-test.yml@refs/heads/main,workflow_sha:44216e5ae99f3653290b60b7f995bfe1c0f3aba0$" {
t.Error("Expected entitlementRegex to be ^actor:major-tom,actor_id:2787414,aud:api://ActionsOIDCGateway,base_ref:.*,environment:production,event_name:workflow_dispatch,head_ref:.*,iss:https://token.actions.githubusercontent.com,job_workflow_ref:major-tom/starman/.github/workflows/manual-test.yml@refs/heads/main,job_workflow_sha:44216e5ae99f3653290b60b7f995bfe1c0f3aba0,ref:refs/heads/main,ref_type:branch,repository:major-tom/starman,repository_id:630836305,repository_owner:major-tom,repository_owner_id:2787414,repository_visibility:public,run_attempt:1,run_id:4779904167,run_number:12,runner_environment:github-hosted,sub:repo:major-tom/starman:ref:refs/heads/main,workflow:Manual Test Workflow,workflow_ref:major-tom/starman/.github/workflows/manual-test.yml@refs/heads/main,workflow_sha:44216e5ae99f3653290b60b7f995bfe1c0f3aba0$, but got", entitlementRegex)
}
}
func TestFuzzyRegexEntitlement(t *testing.T) {
entitlement := Entitlement{
Environment: "production",
RepositoryOwner: "major-tom",
EventName: "workflow_dispatch",
Ref: "refs/heads/main",
Visibility: "public",
}
entitlementRegex := entitlement.regexString()
if entitlementRegex != "^actor:.*,actor_id:.*,aud:.*,base_ref:.*,environment:production,event_name:workflow_dispatch,head_ref:.*,iss:.*,job_workflow_ref:.*,job_workflow_sha:.*,ref:refs/heads/main,ref_type:.*,repository:.*,repository_id:.*,repository_owner:major-tom,repository_owner_id:.*,repository_visibility:public,run_attempt:.*,run_id:.*,run_number:.*,runner_environment:.*,sub:.*,workflow:.*,workflow_ref:.*,workflow_sha:.*$" {
t.Error("Expected entitlementRegex to be ^actor:.*,actor_id:.*,aud:.*,base_ref:.*,environment:production,event_name:workflow_dispatch,head_ref:.*,iss:.*,job_workflow_ref:.*,job_workflow_sha:.*,ref:refs/heads/main,ref_type:.*,repository:.*,repository_id:.*,repository_owner:major-tom,repository_owner_id:.*,repository_visibility:public,run_attempt:.*,run_id:.*,run_number:.*,runner_environment:.*,sub:.*,workflow:.*,workflow_ref:.*,workflow_sha:.*$, but got", entitlementRegex)
}
}
func TestComputeSimpleEntitlements(t *testing.T) {
//Load the sample config file as a string
b, err := os.ReadFile("test/precise-match.json")
if err != nil {
t.Fatal(err)
}
content := string(b)
var entitlements []Entitlement
err = json.Unmarshal([]byte(content), &entitlements)
if err != nil {
t.Fatal(err)
}
entitlementConfig := &EntitlementConfig{
Entitlements: entitlements,
}
// Compute the scope for the claims
scope := entitlementConfig.computeScopes(claims)
expectedRepoList := []string{"codespace-oddity"}
read := "read"
expectedPermissions := github.InstallationPermissions{
Contents: &read,
}
if !reflect.DeepEqual(scope.Repositories, expectedRepoList) {
t.Errorf("Expected scope.Repositories to be [codespace-oddity], but got %s", scope.Repositories)
}
if !reflect.DeepEqual(scope.Permissions, expectedPermissions) {
expPerms, _ := json.MarshalIndent(expectedPermissions, "", " ")
gotPerms, _ := json.MarshalIndent(scope.Permissions, "", " ")
t.Errorf("Expected scope.Permissions to be %s, but got %v", string(expPerms), string(gotPerms))
}
}
func TestComputeMultipleEntitlements(t *testing.T) {
//Load the sample config file as a string
b, err := os.ReadFile("test/multiple-match.json") // just pass the file name
if err != nil {
t.Fatal(err)
}
content := string(b) // convert content to a 'string'
var entitlements []Entitlement
err = json.Unmarshal([]byte(content), &entitlements)
if err != nil {
t.Fatal(err)
}
entitlementConfig := &EntitlementConfig{
Entitlements: entitlements,
}
expectedRepoList := []string{"codespace-oddity", "bootstrap"}
read := "read"
write := "write"
expectedPermissions := github.InstallationPermissions{
Contents: &write,
Checks: &read,
OrganizationProjects: &read,
}
// Compute the scope for the claims
scope := entitlementConfig.computeScopes(claims)
if !reflect.DeepEqual(scope.Repositories, expectedRepoList) {
t.Errorf("Expected scope.Repositories to be [codespace-oddity, bootstrap], but got %s", scope.Repositories)
}
if !reflect.DeepEqual(scope.Permissions, expectedPermissions) {
expPerms, _ := json.MarshalIndent(expectedPermissions, "", " ")
gotPerms, _ := json.MarshalIndent(scope.Permissions, "", " ")
t.Errorf("Expected scope.Permissions to be %s, but got %v", string(expPerms), string(gotPerms))
}
}
func TestNoEntitlements(t *testing.T) {
//Load the sample config file as a string
b, err := os.ReadFile("test/no-match.json")
if err != nil {
t.Fatal(err)
}
content := string(b)
var entitlements []Entitlement
err = json.Unmarshal([]byte(content), &entitlements)
if err != nil {
t.Fatal(err)
}
entitlementConfig := &EntitlementConfig{
Entitlements: entitlements,
}
// Compute the scope for the claims
scope := entitlementConfig.computeScopes(claims)
expectedRepoList := []string{}
expectedPermissions := github.InstallationPermissions{}
if !reflect.DeepEqual(scope.Repositories, expectedRepoList) {
t.Errorf("Expected scope.Repositories to be empty, but got %s", scope.Repositories)
}
if !reflect.DeepEqual(scope.Permissions, expectedPermissions) {
expPerms, _ := json.MarshalIndent(expectedPermissions, "", " ")
gotPerms, _ := json.MarshalIndent(scope.Permissions, "", " ")
t.Errorf("Expected scope.Permissions to be %s, but got %v", string(expPerms), string(gotPerms))
}
}