-
Notifications
You must be signed in to change notification settings - Fork 0
/
grafana-cognito-auth.ts
496 lines (476 loc) · 14.2 KB
/
grafana-cognito-auth.ts
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import * as cdk from "aws-cdk-lib"
import * as cognito from "aws-cdk-lib/aws-cognito"
import * as ecs from "aws-cdk-lib/aws-ecs"
import * as sm from "aws-cdk-lib/aws-secretsmanager"
import * as ssm from "aws-cdk-lib/aws-ssm"
import * as logs from "aws-cdk-lib/aws-logs"
import * as constructs from "constructs"
export interface GrafanaCognitoAuthProps {
/**
* The Cognito User Pool to use for authentication
*/
userPool: cognito.IUserPool
/**
* The domain of the Cognito user pool
*
* @example "auth.example.com"
*/
cognitoDomain: string
/**
* The domain Grafana is being served from
*
* @example "grafana.example.com"
*/
grafanaDomain: string
/**
* The sub-path Grafana is being served from, if any
* @example "/grafana/"
*/
grafanaSubPath?: string
/**
* Authorization rules for the different
* built-in Grafana roles. Each role can be assigned based
* on a Cognito user emails and/or Cognito user pool groups.
*
* @remarks
* The construct does not set up the Grafana user pool groups for you.
*
* @example
* ```typescript
* {
* authorization: {
* serverAdmins: {
* emails: ["[email protected]"],
* },
* viewers: {
* groups: ["grafana-default"]
* }
* }
* }
* ```
*/
authorization: {
/**
* @default - users that are not assigned a role based on
* their email or group membership is not granted access
*/
defaultRole?: "GrafanaAdmin" | "Admin" | "Editor" | "Viewer"
/**
* The principals to grant the `GrafanaAdmin` role
*/
serverAdmins: {
emails?: string[]
groups?: string[]
}
/**
* The principals to grant the `Admin` role
*/
admins?: {
emails?: string[]
groups?: string[]
}
/**
* The principals to grant the `Editor` role
*/
editors?: {
emails?: string[]
groups?: string[]
}
/**
* The principals to grant the `Viewer` role
*/
viewers?: {
emails?: string[]
groups?: string[]
}
}
}
// The default Grafana configuration used by this construct
// (with some best-effort documentation)
type GrafanaConfig = {
security: {
disableInitialAdminCreation: boolean
}
auth: {
/**
* Configuration for using Cognito as OAuth provider
*/
genericOauth: {
enabled: boolean
name: string
allowSignUp: boolean
autoLogin: boolean
usePkce: boolean
useRefreshToken: boolean
clientId: string | sm.ISecret | ssm.IParameter
clientSecret: sm.ISecret | ssm.IParameter
scopes: string
authUrl: string
tokenUrl: string
apiUrl: string
signoutRedirectUrl: string
allowAssignGrafanaAdmin: boolean
roleAttributeStrict: boolean
/**
* JMES path that returns the Grafana role
* an authenticated user should be assigned
*/
roleAttributePath: string
nameAttributePath: string
loginAttributePath: string
}
basic: {
enabled: boolean
}
anonymous: {
enabled: boolean
}
disableLoginForm: boolean
disableSignoutMenu: boolean
}
users: {
/**
* Default role for new users
*/
autoAssignOrgRole: string
}
server: {
/**
* The full URL to Grafana
*/
rootUrl: string
/**
* Whether Grafana is being served from a sub-path or not
*/
serveFromSubPath: boolean
}
}
// A type that allows everything in GrafanaConfig (as optional),
// as well as arbitrary key-value pairs
type FlexibleGrafanaConfig = {
[K in keyof GrafanaConfig]?: GrafanaConfig[K] extends object
? {
[P in keyof GrafanaConfig[K]]?: GrafanaConfig[K][P] extends object
? {
[Q in keyof GrafanaConfig[K][P]]?: GrafanaConfig[K][P][Q]
}
: GrafanaConfig[K][P]
}
: GrafanaConfig[K]
} & Record<string, unknown>
type GrafanaConfigValue =
| { [key: string]: GrafanaConfigValue }
| string
| number
| boolean
| sm.ISecret
| ssm.IParameter
/**
* Environment variables that can be used to customize Grafana
*/
type GrafanaEnvironment = {
/**
* Environment variables containing string values
*/
strings: {
[key: string]: string
}
/**
* Environment variables containing SSM parameters
*/
parameters: {
[key: string]: ssm.IParameter
}
/**
* Environment variables containing Secrets Manager secrets
*/
secrets: {
[key: string]: sm.ISecret
}
}
/**
* Type guard to check if a value is a pure JavaScript object.
*/
const isPureObject = (v: unknown): v is Record<string, unknown> =>
Object.prototype.toString.call(v) === "[object Object]" &&
typeof v === "object" &&
v !== null &&
!Array.isArray(v) &&
v.constructor === Object &&
Object.getPrototypeOf(v) === Object.prototype
/**
* Type guard to check if a value is a Secrets Manager secret.
*/
const isSecret = (v: unknown): v is sm.ISecret =>
typeof v === "object" && v !== null && !Array.isArray(v) && "secretName" in v
/**
* Type guard to check if a value is an SSM parameter.
*/
const isParameter = (v: unknown): v is ssm.IParameter =>
typeof v === "object" &&
v !== null &&
!Array.isArray(v) &&
"parameterName" in v
/**
* Type guard to check if a value is a valid Grafana configuration value.
*/
const isGrafanaConfigValue = (v: unknown): v is GrafanaConfigValue =>
isPureObject(v) ||
isSecret(v) ||
isParameter(v) ||
typeof v === "string" ||
typeof v === "number" ||
typeof v === "boolean"
/**
* Creates a Cognito user pool client and prepares all the
* required Grafana configuration in order to use Cognito as
* the OAuth2 provider.
*
* This allows you to control access to Grafana through Cognito,
* and automatically map Grafana roles (e.g., `Editor`, `Viewer`, etc.)
* to users based on their email and/or Cognito group membership.
*/
export class GrafanaCognitoAuth extends constructs.Construct {
/**
* The Cognito User Pool Client created for Grafana
*/
public readonly userPoolClient: cognito.IUserPoolClient
/**
* Pre-configured container definition that can be used in an ECS
* task definition
*/
public readonly containerDefinitionOpts: ecs.ContainerDefinitionOptions
/**
* Environment variables that can be used to configure Grafana
*/
public readonly environment: GrafanaEnvironment
constructor(
scope: constructs.Construct,
id: string,
props: GrafanaCognitoAuthProps,
) {
super(scope, id)
if (
!props.authorization.serverAdmins.emails?.length &&
!props.authorization.serverAdmins.groups?.length
) {
throw new Error(
"At least one Grafana server administrator must be configured",
)
}
if (cdk.Token.isUnresolved(props.grafanaSubPath)) {
throw new Error("The Grafana sub-path can not be a CDK token")
}
const grafanaUrl = `https://${props.grafanaDomain}${
props.grafanaSubPath ? "/" + props.grafanaSubPath.replace(/^\//, "") : ""
}`
const grafanaUrlWithoutTrailingSlash = grafanaUrl.replace(/\/+$/, "")
// NOTE: We need to do some special handling
// here as the Grafana domain may be a CDK token
const encodedLogoutUrl =
encodeURIComponent("https://") +
props.grafanaDomain +
encodeURIComponent(
props.grafanaSubPath
? "/" + props.grafanaSubPath.replace(/^\//, "")
: "",
)
const cognitoUrl = `https://${props.cognitoDomain}`
this.userPoolClient = new cognito.UserPoolClient(this, "UserPoolClient", {
userPool: props.userPool,
generateSecret: true,
preventUserExistenceErrors: true,
writeAttributes: new cognito.ClientAttributes().withStandardAttributes({
// NOTE: Cognito requires email to be writable
email: true,
}),
oAuth: {
flows: {
authorizationCodeGrant: true,
},
logoutUrls: [grafanaUrl],
callbackUrls: [`${grafanaUrlWithoutTrailingSlash}/login/generic_oauth`],
},
})
const roleAttributePath = GrafanaCognitoAuth.generateRoleAttributePath(
props.authorization,
)
const clientSecret = new sm.Secret(this, "ClientSecret", {
secretStringValue: this.userPoolClient.userPoolClientSecret,
})
const grafanaConfig: GrafanaConfig = {
security: {
disableInitialAdminCreation: true,
},
server: {
rootUrl: grafanaUrl,
serveFromSubPath: !!props.grafanaSubPath,
},
users: {
autoAssignOrgRole: "None",
},
auth: {
disableLoginForm: true,
disableSignoutMenu: false,
basic: {
enabled: false,
},
anonymous: {
enabled: false,
},
genericOauth: {
enabled: true,
name: "Amazon Cognito",
clientId: this.userPoolClient.userPoolClientId,
clientSecret: clientSecret,
scopes: "openid profile email phone aws.cognito.signin.user.admin",
authUrl: `${cognitoUrl}/oauth2/authorize`,
tokenUrl: `${cognitoUrl}/oauth2/token`,
apiUrl: `${cognitoUrl}/oauth2/userInfo`,
signoutRedirectUrl: `${cognitoUrl}/logout?client_id=${this.userPoolClient.userPoolClientId}&logout_uri=${encodedLogoutUrl}`,
allowAssignGrafanaAdmin: true,
usePkce: true,
useRefreshToken: true,
autoLogin: false,
allowSignUp: true,
roleAttributeStrict: true,
roleAttributePath,
loginAttributePath: "username",
nameAttributePath: "name || preferred_username || email || username",
},
},
}
this.environment =
GrafanaCognitoAuth.generateEnvironmentVariables(grafanaConfig)
this.containerDefinitionOpts = {
image: ecs.ContainerImage.fromRegistry(
// renovate: datasource=docker depName=grafana/grafana-oss
"grafana/grafana-oss:11.4.0@sha256:d8ea37798ccc41061a62ab080f2676dda6bf7815558499f901bdb0f533a456fb",
),
logging: ecs.LogDriver.awsLogs({
streamPrefix: "ecs",
logRetention: logs.RetentionDays.TWO_WEEKS,
}),
portMappings: [{ containerPort: 3000 }],
environment: this.environment.strings,
secrets: {
...Object.fromEntries(
Object.entries(this.environment.secrets).map(([key, secret]) => [
key,
ecs.Secret.fromSecretsManager(secret),
]),
),
...Object.fromEntries(
Object.entries(this.environment.parameters).map(
([key, parameter]) => [key, ecs.Secret.fromSsmParameter(parameter)],
),
),
},
}
}
/**
* Generate a JMES path that Grafana can use to map Cognito groups
* and user emails to built-in Grafana roles.
*/
public static generateRoleAttributePath(
authorization: GrafanaCognitoAuthProps["authorization"],
): string {
const mappings = [
{ roleName: "GrafanaAdmin", principals: authorization.serverAdmins },
{ roleName: "Admin", principals: authorization.admins },
{ roleName: "Editor", principals: authorization.editors },
{ roleName: "Viewer", principals: authorization.viewers },
]
const defaultRole = authorization.defaultRole
? `'${authorization.defaultRole}'`
: "null"
const rules = mappings
.filter(
(role) =>
role.principals &&
(role.principals.emails?.length || role.principals.groups?.length),
)
.map((role) => {
const roleRules = []
if (role.principals?.groups?.length) {
roleRules.push(
role.principals.groups
// NOTE: We fallback to an empty array if the `cognito:groups` claim is
// missing from the JWT, which will be the case for users who
// aren't assigned to any groups.
.map((g) => `contains("cognito:groups" || \`[]\`, '${g}')`)
.join(" || "),
)
}
if (role.principals?.emails?.length) {
roleRules.push(
`contains([${role.principals.emails
.map((u) => `'${u}'`)
.join(",")}], email)`,
)
}
return `(${roleRules.join(" || ")} && '${role.roleName}')`
})
return rules.length
? rules.join(" || ") + " || " + defaultRole
: defaultRole
}
/**
* Generate environment variables that can be used to configure Grafana
* by transforming arbitrarily nested objects to variables in the format
* used by Grafana (e.g., `GF_MY_GRAFANA_VARIABLE`). The construct uses this
* function internally, but it can be used by consumers to override values,
* create their own config, etc.
*
* You can supply JSON primitives, Secrets Manager secrets and SSM parameters.
*
* The following resources can be used to determine available configuration values:
* - https://github.com/grafana/grafana/blob/main/conf/defaults.ini
* - https://grafana.com/docs/grafana/latest/setup-grafana/configure-grafana/
*
* @example
* ```typescript
* GrafanaCognitoAuth.generateEnvironmentVariables({
* hello: {
* world: ":-)"
* }
* })
* // returns { strings: GF_HELLO_WORLD: ":-)"}
* ```
*/
public static generateEnvironmentVariables(
config: FlexibleGrafanaConfig,
): GrafanaEnvironment {
const prefix = "GF"
const variables: GrafanaEnvironment = {
strings: {},
parameters: {},
secrets: {},
}
if (!isGrafanaConfigValue(config)) {
throw new Error(
"The Grafana configuration can only contain JSON primitives, Secrets Manager secrets and SSM parameters",
)
}
const stack: Array<[string[], GrafanaConfigValue]> = [[[], config]]
while (stack.length > 0) {
const [path, value] = stack.pop()!
if (isPureObject(value)) {
for (const [key, subValue] of Object.entries(value)) {
const formattedKey = key.replace(/([A-Z])/g, "_$1").toUpperCase()
stack.push([[...path, formattedKey], subValue])
}
} else {
const name = `${prefix}_${path.join("_")}`
if (isSecret(value)) {
variables.secrets[name] = value
} else if (isParameter(value)) {
variables.parameters[name] = value
} else {
variables.strings[name] = value.toString()
}
}
}
return variables
}
}