/
user.go
217 lines (185 loc) · 5.86 KB
/
user.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Creating a user in a random organization:
//
// Given a user named "Bob"
//
// Creating a user in a given organization:
//
// Given a user named "Jimmy" in organization "13639843"
//
// Logging into a user session:
//
// Given I am logged in as "Jimmy"
//
// Setting the Authorization header of the current user session:
//
// Given I set the Authorization header to "Bearer ${agent_token}"
package cucumber
import (
"context"
"fmt"
"strings"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/pkg/client/keycloak"
"github.com/golang-jwt/jwt/v4"
"github.com/bf2fc6cc711aee1a0c2a/kas-fleet-manager/internal/kafka/compat"
"github.com/cucumber/godog"
)
func init() {
StepModules = append(StepModules, func(ctx *godog.ScenarioContext, s *TestScenario) {
ctx.Step(`^a user named "([^"]*)"$`, s.Suite.createUserNamed)
ctx.Step(`^a user named "([^"]*)" in organization "([^"]*)"$`, s.Suite.createUserNamedInOrganization)
ctx.Step(`^an org admin user named "([^"]*)"$`, s.Suite.createOrgAdminUserNamed)
ctx.Step(`^an org admin user named "([^"]*)" in organization "([^"]*)"$`, s.Suite.createOrgAdminUserNamedInOrganization)
ctx.Step(`^I am logged in as "([^"]*)"$`, s.iAmLoggedInAs)
ctx.Step(`^I set the "([^"]*)" header to "([^"]*)"$`, s.iSetTheHeaderTo)
ctx.Step(`^an admin user named "([^"]+)" with roles "([^"]+)"$`, s.Suite.createAdminUserNamed)
ctx.Step(`^I store userid for "([^"]+)" as \${([^}]*)}$`, s.storeUserId)
ctx.Step(`^I store orgid for "([^"]+)" as \${([^}]*)}$`, s.storeOrgId)
})
}
func (s *TestSuite) createUserNamed(name string) error {
// this value is taken from config/quota-management-list-configuration.yaml
s.Mu.Lock()
orgId := s.nextOrgId
s.nextOrgId += 1
s.Mu.Unlock()
return s.createUserNamedInOrganization(name, fmt.Sprintf("%d", orgId))
}
func (s *TestSuite) createUserNamedInOrganization(name string, orgid string) error {
// users are shared concurrently across scenarios.. so lock while we create the user...
s.Mu.Lock()
defer s.Mu.Unlock()
if s.users[name] != nil {
return nil
}
// setup pre-requisites to performing requests
account := s.Helper.NewAccountWithNameAndOrg(name, orgid)
token, err := s.Helper.AuthHelper.CreateSignedJWT(account, nil)
if err != nil {
return err
}
s.users[name] = &TestUser{
Name: name,
OrgId: orgid,
Token: token,
UserName: account.Username(),
Ctx: context.WithValue(context.Background(), compat.ContextAccessToken, token),
}
return nil
}
func (s *TestSuite) createOrgAdminUserNamed(name string) error {
// users are shared concurrently across scenarios.. so lock while we create the user...
s.Mu.Lock()
orgId := fmt.Sprint(s.nextOrgId)
s.nextOrgId++
defer s.Mu.Unlock()
if s.users[name] != nil {
return nil
}
// setup pre-requisites to performing requests
account := s.Helper.NewAccountWithNameAndOrg(name, orgId)
// add org admin claim
claims := jwt.MapClaims{}
claims["is_org_admin"] = true
token, err := s.Helper.AuthHelper.CreateSignedJWT(account, claims)
if err != nil {
return err
}
s.users[name] = &TestUser{
Name: name,
OrgId: orgId,
Token: token,
UserName: account.Username(),
Ctx: context.WithValue(context.Background(), compat.ContextAccessToken, token),
}
return nil
}
func (s *TestSuite) createOrgAdminUserNamedInOrganization(name string, orgid string) error {
// users are shared concurrently across scenarios.. so lock while we create the user...
s.Mu.Lock()
defer s.Mu.Unlock()
if s.users[name] != nil {
return nil
}
// check if org id is referencing another user's org id
if strings.HasPrefix(orgid, "${") && strings.HasSuffix(orgid, ".OrgId}") {
orgid = s.users[strings.TrimSuffix(strings.TrimPrefix(orgid, "${"), ".OrgId}")].OrgId
}
// setup pre-requisites to performing requests
account := s.Helper.NewAccountWithNameAndOrg(name, orgid)
// add org admin claim
claims := jwt.MapClaims{}
claims["is_org_admin"] = true
token, err := s.Helper.AuthHelper.CreateSignedJWT(account, claims)
if err != nil {
return err
}
s.users[name] = &TestUser{
Name: name,
OrgId: orgid,
Token: token,
UserName: account.Username(),
Ctx: context.WithValue(context.Background(), compat.ContextAccessToken, token),
}
return nil
}
func (s *TestSuite) createAdminUserNamed(name, roles string) error {
// users are shared concurrently across scenarios.. so lock while we create the user...
s.Mu.Lock()
defer s.Mu.Unlock()
if s.users[name] != nil {
return nil
}
var keycloakConfig *keycloak.KeycloakConfig
s.Helper.Env.MustResolveAll(&keycloakConfig)
// setup pre-requisites to performing requests
account := s.Helper.NewAllowedServiceAccount()
claims := jwt.MapClaims{
"iss": keycloakConfig.AdminAPISSORealm.ValidIssuerURI,
"realm_access": map[string][]string{
"roles": strings.Split(strings.TrimSpace(roles), ","),
},
"preferred_username": name,
}
token, err := s.Helper.AuthHelper.CreateSignedJWT(account, claims)
if err != nil {
return err
}
s.users[name] = &TestUser{
Name: name,
Token: token,
UserName: account.Username(),
Ctx: context.WithValue(context.Background(), compat.ContextAccessToken, token),
}
return nil
}
func (s *TestScenario) storeUserId(name, varName string) error {
s.Suite.Mu.Lock()
defer s.Suite.Mu.Unlock()
user := s.Suite.users[name]
if user != nil {
s.Variables[varName] = user.UserName
}
return nil
}
func (s *TestScenario) storeOrgId(name, varName string) error {
s.Suite.Mu.Lock()
defer s.Suite.Mu.Unlock()
user := s.Suite.users[name]
if user != nil {
s.Variables[varName] = user.OrgId
}
return nil
}
func (s *TestScenario) iAmLoggedInAs(name string) error {
s.Session().Header.Del("Authorization")
s.CurrentUser = name
return nil
}
func (s *TestScenario) iSetTheHeaderTo(name string, value string) error {
expanded, err := s.Expand(value, []string{})
if err != nil {
return err
}
s.Session().Header.Set(name, expanded)
return nil
}