-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
aws-sso-integration-service.ts
414 lines (341 loc) · 15.4 KB
/
aws-sso-integration-service.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
import { Repository } from "./repository";
import { AwsSsoRoleService, LoginResponse, SsoRoleSession } from "./session/aws/aws-sso-role-service";
import { AwsSsoIntegration } from "../models/aws-sso-integration";
import { formatDistance } from "date-fns";
import { INativeService } from "../interfaces/i-native-service";
import { AwsSsoOidcService } from "./aws-sso-oidc.service";
import { KeychainService } from "./keychain-service";
import { constants } from "../models/constants";
import SSO, {
AccountInfo,
GetRoleCredentialsRequest,
GetRoleCredentialsResponse,
ListAccountRolesRequest,
ListAccountsRequest,
LogoutRequest,
RoleInfo,
} from "aws-sdk/clients/sso";
import { SessionType } from "../models/session-type";
import { AwsSsoRoleSession } from "../models/aws-sso-role-session";
import { IBehaviouralNotifier } from "../interfaces/i-behavioural-notifier";
import { AwsSsoIntegrationTokenInfo } from "../models/aws-sso-integration-token-info";
import { SessionFactory } from "./session-factory";
import { BehaviouralSubjectService } from "./behavioural-subject-service";
const portalUrlValidationRegex = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)/;
export interface IntegrationCreationParams {
alias: string;
portalUrl: string;
region: string;
browserOpening: string;
}
export interface SsoSessionsDiff {
sessionsToDelete: AwsSsoRoleSession[];
sessionsToAdd: SsoRoleSession[];
}
export class AwsSsoIntegrationService {
private ssoPortal: SSO;
constructor(
private repository: Repository,
private awsSsoOidcService: AwsSsoOidcService,
private awsSsoRoleService: AwsSsoRoleService,
private keyChainService: KeychainService,
private sessionNotifier: IBehaviouralNotifier,
private nativeService: INativeService,
private sessionFactory: SessionFactory,
private behaviouralSubjectService: BehaviouralSubjectService
) {}
static validateAlias(alias: string): boolean | string {
return alias.trim() !== "" ? true : "Empty alias";
}
static validatePortalUrl(portalUrl: string): boolean | string {
return portalUrlValidationRegex.test(portalUrl) ? true : "Invalid portal URL";
}
createIntegration(creationParams: IntegrationCreationParams): void {
this.repository.addAwsSsoIntegration(creationParams.portalUrl, creationParams.alias, creationParams.region, creationParams.browserOpening);
}
updateAwsSsoIntegration(id: string, updateParams: IntegrationCreationParams): void {
this.repository.updateAwsSsoIntegration(id, updateParams.alias, updateParams.region, updateParams.portalUrl, updateParams.browserOpening);
}
getIntegration(id: string): AwsSsoIntegration {
return this.repository.getAwsSsoIntegration(id);
}
getIntegrations(): AwsSsoIntegration[] {
return this.repository.listAwsSsoIntegrations();
}
getOnlineIntegrations(): AwsSsoIntegration[] {
return this.getIntegrations().filter((integration) => this.isOnline(integration));
}
getOfflineIntegrations(): AwsSsoIntegration[] {
return this.getIntegrations().filter((integration) => !this.isOnline(integration));
}
isOnline(integration: AwsSsoIntegration): boolean {
const expiration = new Date(integration.accessTokenExpiration).getTime();
const now = this.getDate().getTime();
return !!integration.accessTokenExpiration && now < expiration;
}
remainingHours(integration: AwsSsoIntegration): string {
return formatDistance(new Date(integration.accessTokenExpiration), this.getDate(), { addSuffix: true });
}
async loginAndGetSessionsDiff(integrationId: string): Promise<SsoSessionsDiff> {
const awsSsoIntegration = this.repository.getAwsSsoIntegration(integrationId);
const region = awsSsoIntegration.region;
const portalUrl = awsSsoIntegration.portalUrl;
const accessToken = await this.getAccessToken(integrationId, region, portalUrl);
const onlineSessions = await this.getSessions(integrationId, accessToken, region);
const persistedSessions = this.repository.getAwsSsoIntegrationSessions(integrationId);
const sessionsToDelete: AwsSsoRoleSession[] = [];
for (const persistedSession of persistedSessions) {
const shouldBeDeleted = !onlineSessions.find((s) => {
const ssoRoleSession = persistedSession as unknown as SsoRoleSession;
return ssoRoleSession.sessionName === s.sessionName && ssoRoleSession.roleArn === s.roleArn && ssoRoleSession.email === s.email;
});
if (shouldBeDeleted) {
sessionsToDelete.push(persistedSession as AwsSsoRoleSession);
}
}
const sessionsToAdd = [];
for (const onlineSession of onlineSessions) {
const shouldBeCreated = !persistedSessions.find((persistedSession) => {
const session = persistedSession as unknown as SsoRoleSession;
return (
onlineSession.sessionName === session.sessionName && onlineSession.roleArn === session.roleArn && onlineSession.email === session.email
);
});
if (shouldBeCreated) {
sessionsToAdd.push(onlineSession);
}
}
return { sessionsToDelete, sessionsToAdd };
}
async syncSessions(integrationId: string): Promise<SsoSessionsDiff> {
const sessionsDiff = await this.loginAndGetSessionsDiff(integrationId);
for (const ssoRoleSession of sessionsDiff.sessionsToAdd) {
ssoRoleSession.awsSsoConfigurationId = integrationId;
await this.awsSsoRoleService.create(ssoRoleSession);
}
for (const ssoSession of sessionsDiff.sessionsToDelete) {
const sessionService = this.sessionFactory.getSessionService(ssoSession.type);
await sessionService.delete(ssoSession.sessionId);
}
return sessionsDiff;
}
async logout(integrationId: string): Promise<void> {
// Obtain region and access token
const integration: AwsSsoIntegration = this.repository.getAwsSsoIntegration(integrationId);
const region = integration.region;
const savedAccessToken = await this.getAccessTokenFromKeychain(integrationId);
// Configure Sso Portal Client
this.setupSsoPortalClient(region);
// Make a logout request to Sso
const logoutRequest: LogoutRequest = { accessToken: savedAccessToken };
try {
await this.ssoPortal.logout(logoutRequest).promise();
} catch (_) {
// logout request has to be handled in reject Promise by design
// Clean clients
this.ssoPortal = null;
// Delete access token and remove sso integration info from workspace
await this.keyChainService.deletePassword(constants.appName, this.getIntegrationAccessTokenKey(integrationId));
this.repository.unsetAwsSsoIntegrationExpiration(integrationId);
}
}
async getAccessToken(integrationId: string, region: string, portalUrl: string): Promise<string> {
const isAwsSsoAccessTokenExpired = await this.isAwsSsoAccessTokenExpired(integrationId);
if (isAwsSsoAccessTokenExpired) {
const loginResponse = await this.login(integrationId, region, portalUrl);
const integration: AwsSsoIntegration = this.repository.getAwsSsoIntegration(integrationId);
await this.configureAwsSso(
integrationId,
integration.alias,
region,
loginResponse.portalUrlUnrolled,
integration.browserOpening,
loginResponse.expirationTime.toISOString(),
loginResponse.accessToken
);
return loginResponse.accessToken;
} else {
return await this.getAccessTokenFromKeychain(integrationId);
}
}
async getRoleCredentials(accessToken: string, region: string, roleArn: string): Promise<GetRoleCredentialsResponse> {
this.setupSsoPortalClient(region);
const getRoleCredentialsRequest: GetRoleCredentialsRequest = {
accountId: roleArn.substring(13, 25),
roleName: roleArn.split("/")[1],
accessToken,
};
return this.ssoPortal.getRoleCredentials(getRoleCredentialsRequest).promise();
}
async getAwsSsoIntegrationTokenInfo(awsSsoIntegrationId: string): Promise<AwsSsoIntegrationTokenInfo> {
const accessToken = await this.keyChainService.getSecret(constants.appName, `aws-sso-integration-access-token-${awsSsoIntegrationId}`);
const expiration = this.repository.getAwsSsoIntegration(awsSsoIntegrationId)
? new Date(this.repository.getAwsSsoIntegration(awsSsoIntegrationId).accessTokenExpiration).getTime()
: undefined;
return { accessToken, expiration };
}
async isAwsSsoAccessTokenExpired(awsSsoIntegrationId: string): Promise<boolean> {
const awsSsoAccessTokenInfo = await this.getAwsSsoIntegrationTokenInfo(awsSsoIntegrationId);
return !awsSsoAccessTokenInfo.expiration || awsSsoAccessTokenInfo.expiration < Date.now();
}
async deleteIntegration(integrationId: string): Promise<void> {
await this.logout(integrationId);
this.repository.deleteAwsSsoIntegration(integrationId);
this.deleteDependentSessions(integrationId);
}
private async getSessions(integrationId: string, accessToken: string, region: string): Promise<SsoRoleSession[]> {
const accounts: AccountInfo[] = await this.listAccounts(accessToken, region);
const promiseArray: Promise<SsoRoleSession[]>[] = [];
accounts.forEach((account) => {
promiseArray.push(this.getSessionsFromAccount(integrationId, account, accessToken, region));
});
return new Promise((resolve, _) => {
Promise.all(promiseArray).then((sessionMatrix: SsoRoleSession[][]) => {
resolve(sessionMatrix.flat());
});
});
}
private async configureAwsSso(
integrationId: string,
alias: string,
region: string,
portalUrl: string,
browserOpening: string,
expirationTime: string,
accessToken: string
): Promise<void> {
this.repository.updateAwsSsoIntegration(integrationId, alias, region, portalUrl, browserOpening, expirationTime);
await this.keyChainService.saveSecret(constants.appName, this.getIntegrationAccessTokenKey(integrationId), accessToken);
}
private async getAccessTokenFromKeychain(integrationId: string | number): Promise<string> {
return await this.keyChainService.getSecret(constants.appName, this.getIntegrationAccessTokenKey(integrationId));
}
private getIntegrationAccessTokenKey(integrationId: string | number) {
return `aws-sso-integration-access-token-${integrationId}`;
}
private async login(integrationId: string | number, region: string, portalUrl: string): Promise<LoginResponse> {
const redirectClient = this.nativeService.followRedirects[this.getProtocol(portalUrl)];
portalUrl = await new Promise((resolve, _) => {
const request = redirectClient.request(portalUrl, (response) => resolve(response.responseUrl));
request.end();
});
const generateSsoTokenResponse = await this.awsSsoOidcService.login(integrationId, region, portalUrl);
return {
portalUrlUnrolled: portalUrl,
accessToken: generateSsoTokenResponse.accessToken,
region,
expirationTime: generateSsoTokenResponse.expirationTime,
};
}
private async removeSsoSessionsFromWorkspace(integrationId: string): Promise<void> {
const ssoSessions = this.repository.getAwsSsoIntegrationSessions(integrationId);
for (const ssoSession of ssoSessions) {
const sessionService = this.sessionFactory.getSessionService(ssoSession.type);
await sessionService.delete(ssoSession.sessionId);
}
}
private setupSsoPortalClient(region: string): void {
if (!this.ssoPortal) {
this.ssoPortal = new SSO({ region });
}
}
private async listAccounts(accessToken: string, region: string): Promise<AccountInfo[]> {
this.setupSsoPortalClient(region);
const listAccountsRequest: ListAccountsRequest = { accessToken, maxResults: 30 };
const accountList: AccountInfo[] = [];
return new Promise((resolve, _) => {
this.recursiveListAccounts(accountList, listAccountsRequest, resolve);
});
}
private recursiveListAccounts(accountList: AccountInfo[], listAccountsRequest: ListAccountsRequest, promiseCallback: any) {
this.ssoPortal
.listAccounts(listAccountsRequest)
.promise()
.then((response) => {
accountList.push(...response.accountList);
if (response.nextToken !== null) {
listAccountsRequest.nextToken = response.nextToken;
this.recursiveListAccounts(accountList, listAccountsRequest, promiseCallback);
} else {
promiseCallback(accountList);
}
});
}
private async getSessionsFromAccount(
integrationId: string,
accountInfo: AccountInfo,
accessToken: string,
region: string
): Promise<SsoRoleSession[]> {
this.setupSsoPortalClient(region);
const listAccountRolesRequest: ListAccountRolesRequest = {
accountId: accountInfo.accountId,
accessToken,
maxResults: 30, // TODO: find a proper value
};
const accountRoles: RoleInfo[] = [];
await new Promise((resolve, _) => {
this.recursiveListRoles(accountRoles, listAccountRolesRequest, resolve);
});
const awsSsoSessions: SsoRoleSession[] = [];
accountRoles.forEach((accountRole) => {
const oldSession = this.findOldSession(accountInfo, accountRole);
const awsSsoSession = {
email: accountInfo.emailAddress,
region: oldSession?.region || this.repository.getDefaultRegion() || constants.defaultRegion,
roleArn: `arn:aws:iam::${accountInfo.accountId}/${accountRole.roleName}`,
sessionName: accountInfo.accountName,
profileId: oldSession?.profileId || this.repository.getDefaultProfileId(),
awsSsoConfigurationId: integrationId,
};
awsSsoSessions.push(awsSsoSession);
});
return awsSsoSessions;
}
private recursiveListRoles(accountRoles: RoleInfo[], listAccountRolesRequest: ListAccountRolesRequest, promiseCallback: any) {
this.ssoPortal
.listAccountRoles(listAccountRolesRequest)
.promise()
.then((response) => {
accountRoles.push(...response.roleList);
if (response.nextToken !== null) {
listAccountRolesRequest.nextToken = response.nextToken;
this.recursiveListRoles(accountRoles, listAccountRolesRequest, promiseCallback);
} else {
promiseCallback(accountRoles);
}
});
}
private findOldSession(accountInfo: SSO.AccountInfo, accountRole: SSO.RoleInfo): { region: string; profileId: string } {
//TODO: use map and filter in order to make this method more readable
for (let i = 0; i < this.repository.getSessions().length; i++) {
const sess = this.repository.getSessions()[i];
if (sess.type === SessionType.awsSsoRole) {
if (
(sess as AwsSsoRoleSession).email === accountInfo.emailAddress &&
(sess as AwsSsoRoleSession).roleArn === `arn:aws:iam::${accountInfo.accountId}/${accountRole.roleName}`
) {
return { region: (sess as AwsSsoRoleSession).region, profileId: (sess as AwsSsoRoleSession).profileId };
}
}
}
return undefined;
}
private deleteDependentSessions(configurationId: string) {
const ssoSessions = this.repository.getSessions().filter((session) => (session as any).awsSsoConfigurationId === configurationId);
for (const session of ssoSessions) {
this.repository.deleteSession(session.sessionId);
}
this.behaviouralSubjectService.setSessions([...this.repository.getSessions()]);
}
private getProtocol(aliasedUrl: string): string {
let protocol = aliasedUrl.split("://")[0];
if (protocol.indexOf("http") === -1) {
protocol = "https";
}
return protocol;
}
private getDate(): Date {
return new Date();
}
}