Skip to content

Commit 54afe7e

Browse files
committed
Handle CRC service accounts.
No-Issue Signed-off-by: James Tanner <tanner.jc@gmail.com>
1 parent 3b2dc46 commit 54afe7e

File tree

3 files changed

+137
-13
lines changed

3 files changed

+137
-13
lines changed

galaxy_ng/app/auth/auth.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,32 @@ def authenticate(self, request):
4343
return None
4444

4545
header = self._decode_header(request.META[self.header])
46+
identity = header.get("identity")
47+
if identity is None:
48+
raise AuthenticationFailed
4649

47-
try:
48-
identity = header['identity']
49-
account = identity['account_number']
50-
51-
user = identity['user']
52-
username = user['username']
53-
except KeyError:
50+
identity_type = identity.get("type", "User")
51+
if identity_type == "User":
52+
try:
53+
identity = header['identity']
54+
account = identity['account_number']
55+
56+
user = identity['user']
57+
username = user['username']
58+
except KeyError:
59+
raise AuthenticationFailed
60+
elif identity_type == "ServiceAccount":
61+
try:
62+
service_account = identity['service_account']
63+
# service-account-<uuid4> is too long for the username field
64+
username = service_account['username'].replace('service-account-', '')
65+
# make this the same?
66+
account = username
67+
# all other attributes for service accounts is null
68+
user = {}
69+
except KeyError:
70+
raise AuthenticationFailed
71+
else:
5472
raise AuthenticationFailed
5573

5674
email = user.get('email', '')

galaxy_ng/tests/integration/utils/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def wait_for_task_ui_client(gc, task):
8888
if state == 'completed':
8989
break
9090
time.sleep(SLEEP_SECONDS_POLLING)
91-
assert state == 'completed'
91+
assert state == 'completed', ds
9292

9393

9494
def wait_for_namespace_tasks_gk(gc, timeout=300):

profiles/insights/proxy/main.go

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ type User struct {
4646
IsOrgAdmin bool `json:"is_org_admin"`
4747
}
4848

49+
type ServiceAccount struct {
50+
ClientId string `json:"client_id"`
51+
Username string `json:"username"`
52+
}
53+
4954
type Account struct {
5055
AccountNumber int `json:"account_number"`
5156
User User `json:"user"`
@@ -60,6 +65,24 @@ type XRHItentity struct {
6065
Entitlements Entitlement `json:"entitlements"`
6166
}
6267

68+
type XRHSVCItentity struct {
69+
Entitlements Entitlement `json:"entitlements"`
70+
Identity struct {
71+
AuthType string `json:"auth_type"`
72+
Internal struct {
73+
AuthTime int `json:"auth_time"`
74+
CrossAccess bool `json:"cross_access"`
75+
OrgID string `json:"org_id"`
76+
} `json:"internal"`
77+
OrgID string `json:"org_id"`
78+
Type string `json:"type"`
79+
ServiceAccount struct {
80+
ClientID string `json:"client_id"`
81+
Username string `json:"username"`
82+
} `json:"service_account"`
83+
} `json:"identity"`
84+
}
85+
6386
var accounts = map[string]Account{
6487
"jdoe": {
6588
AccountNumber: 6089719,
@@ -103,6 +126,13 @@ var accounts = map[string]Account{
103126
},
104127
}
105128

129+
var serviceAccounts = map[string]ServiceAccount {
130+
"service-account-b69eaf9e-e6a6-4f9e-805e-02987daddfbd": {
131+
Username: "service-account-b69eaf9e-e6a6-4f9e-805e-02987daddfbd",
132+
ClientId: "b69eaf9e-e6a6-4f9e-805e-02987daddfbd",
133+
},
134+
}
135+
106136
func randomString(length int) string {
107137
rand.Seed(time.Now().UnixNano())
108138
b := make([]byte, length)
@@ -149,19 +179,95 @@ func userToIentityHeader(account Account) string {
149179
return base64.StdEncoding.EncodeToString([]byte(data))
150180
}
151181

182+
func serviceAccountToIentityHeader(svc_account ServiceAccount) string {
183+
/*
184+
{
185+
"entitlements": {},
186+
"identity": {
187+
"auth_type": "jwt-auth",
188+
"internal": {
189+
"auth_time": 500,
190+
"cross_access": false,
191+
"org_id": "456"
192+
},
193+
"org_id": "456",
194+
"type": "ServiceAccount",
195+
"service_account": {
196+
"client_id": "b69eaf9e-e6a6-4f9e-805e-02987daddfbd",
197+
"username": "service-account-b69eaf9e-e6a6-4f9e-805e-02987daddfbd"
198+
}
199+
}
200+
}
201+
*/
202+
203+
data := XRHSVCItentity{
204+
Entitlements: Entitlement{
205+
Insights: map[string]bool{
206+
"is_entitled": true,
207+
"is_trial": false,
208+
},
209+
},
210+
Identity: struct {
211+
AuthType string `json:"auth_type"`
212+
Internal struct {
213+
AuthTime int `json:"auth_time"`
214+
CrossAccess bool `json:"cross_access"`
215+
OrgID string `json:"org_id"`
216+
} `json:"internal"`
217+
OrgID string `json:"org_id"`
218+
Type string `json:"type"`
219+
ServiceAccount struct {
220+
ClientID string `json:"client_id"`
221+
Username string `json:"username"`
222+
} `json:"service_account"`
223+
}{
224+
AuthType: "jwt-auth",
225+
Internal: struct {
226+
AuthTime int `json:"auth_time"`
227+
CrossAccess bool `json:"cross_access"`
228+
OrgID string `json:"org_id"`
229+
}{
230+
AuthTime: 500,
231+
CrossAccess: false,
232+
OrgID: "456",
233+
},
234+
OrgID: "456",
235+
Type: "ServiceAccount",
236+
ServiceAccount: struct {
237+
ClientID string `json:"client_id"`
238+
Username string `json:"username"`
239+
}{
240+
ClientID: svc_account.ClientId,
241+
Username: svc_account.Username,
242+
},
243+
},
244+
}
245+
jsonData, _ := json.MarshalIndent(data, "", " ")
246+
247+
fmt.Printf("Setting X-RH-IDENTITY: %s\n", string(jsonData))
248+
return base64.StdEncoding.EncodeToString([]byte(jsonData))
249+
250+
}
251+
152252
func setRHIdentityHeader(req *http.Request) {
153253
auth_header := req.Header.Get("Authorization")
154254

155255
if auth_header != "" {
156256
if strings.Contains(auth_header, "Basic") {
257+
157258
user, pass, _ := req.BasicAuth()
158259

159260
fmt.Printf("Authenticating with basic auth: %s:%s\n", user, pass)
160261

161-
if account, ok := accounts[user]; ok {
162-
req.Header.Set("X-RH-IDENTITY", userToIentityHeader(account))
163-
} else {
164-
fmt.Printf("User not found: %s", user)
262+
if svc_account, ok := serviceAccounts[user]; ok {
263+
req.Header.Set("X-RH-IDENTITY", serviceAccountToIentityHeader(svc_account))
264+
} else {
265+
266+
if account, ok := accounts[user]; ok {
267+
req.Header.Set("X-RH-IDENTITY", userToIentityHeader(account))
268+
} else {
269+
fmt.Printf("User not found: %s", user)
270+
}
165271
}
166272

167273
} else if strings.Contains(auth_header, "Bearer") {
@@ -378,7 +484,7 @@ func main() {
378484
data, _ := ioutil.ReadAll(upstreamServerResponse.Body)
379485
modified := downloadUrlReg.ReplaceAll(data, replacementURL)
380486

381-
fmt.Printf("MODIFIED DATA: %s\n", modified)
487+
//fmt.Printf("MODIFIED DATA: %s\n", modified)
382488

383489
// Write the response
384490
rw.WriteHeader(upstreamServerResponse.StatusCode)

0 commit comments

Comments
 (0)