@@ -46,6 +46,11 @@ type User struct {
46
46
IsOrgAdmin bool `json:"is_org_admin"`
47
47
}
48
48
49
+ type ServiceAccount struct {
50
+ ClientId string `json:"client_id"`
51
+ Username string `json:"username"`
52
+ }
53
+
49
54
type Account struct {
50
55
AccountNumber int `json:"account_number"`
51
56
User User `json:"user"`
@@ -60,6 +65,24 @@ type XRHItentity struct {
60
65
Entitlements Entitlement `json:"entitlements"`
61
66
}
62
67
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
+
63
86
var accounts = map [string ]Account {
64
87
"jdoe" : {
65
88
AccountNumber : 6089719 ,
@@ -103,6 +126,13 @@ var accounts = map[string]Account{
103
126
},
104
127
}
105
128
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
+
106
136
func randomString (length int ) string {
107
137
rand .Seed (time .Now ().UnixNano ())
108
138
b := make ([]byte , length )
@@ -149,19 +179,95 @@ func userToIentityHeader(account Account) string {
149
179
return base64 .StdEncoding .EncodeToString ([]byte (data ))
150
180
}
151
181
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
+
152
252
func setRHIdentityHeader (req * http.Request ) {
153
253
auth_header := req .Header .Get ("Authorization" )
154
254
155
255
if auth_header != "" {
156
256
if strings .Contains (auth_header , "Basic" ) {
257
+
157
258
user , pass , _ := req .BasicAuth ()
158
259
159
260
fmt .Printf ("Authenticating with basic auth: %s:%s\n " , user , pass )
160
261
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
+ }
165
271
}
166
272
167
273
} else if strings .Contains (auth_header , "Bearer" ) {
@@ -378,7 +484,7 @@ func main() {
378
484
data , _ := ioutil .ReadAll (upstreamServerResponse .Body )
379
485
modified := downloadUrlReg .ReplaceAll (data , replacementURL )
380
486
381
- fmt .Printf ("MODIFIED DATA: %s\n " , modified )
487
+ // fmt.Printf("MODIFIED DATA: %s\n", modified)
382
488
383
489
// Write the response
384
490
rw .WriteHeader (upstreamServerResponse .StatusCode )
0 commit comments