|
14 | 14 | limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -import { AbstractConnectionPlugin } from "../../abstract_connection_plugin"; |
18 | | -import { HostInfo } from "../../host_info"; |
19 | | -import { SamlUtils } from "../../utils/saml_utils"; |
20 | | -import { IamAuthUtils, TokenInfo } from "../../utils/iam_auth_utils"; |
21 | 17 | import { PluginService } from "../../plugin_service"; |
22 | 18 | import { CredentialsProviderFactory } from "./credentials_provider_factory"; |
23 | | -import { RdsUtils } from "../../utils/rds_utils"; |
24 | | -import { WrapperProperties } from "../../wrapper_property"; |
25 | | -import { logger } from "../../../logutils"; |
26 | | -import { Messages } from "../../utils/messages"; |
27 | | -import { AwsWrapperError } from "../../utils/errors"; |
28 | | -import { ClientWrapper } from "../../client_wrapper"; |
29 | | -import { TelemetryCounter } from "../../utils/telemetry/telemetry_counter"; |
30 | | -import { RegionUtils } from "../../utils/region_utils"; |
31 | | -import { CanReleaseResources } from "../../can_release_resources"; |
| 19 | +import { BaseSamlAuthPlugin } from "./saml_auth_plugin"; |
| 20 | +import { IamAuthUtils } from "../../utils/iam_auth_utils"; |
32 | 21 |
|
33 | | -export class OktaAuthPlugin extends AbstractConnectionPlugin implements CanReleaseResources { |
34 | | - protected static readonly tokenCache = new Map<string, TokenInfo>(); |
35 | | - private static readonly subscribedMethods = new Set<string>(["connect", "forceConnect"]); |
36 | | - protected pluginService: PluginService; |
37 | | - protected rdsUtils = new RdsUtils(); |
38 | | - private readonly credentialsProviderFactory: CredentialsProviderFactory; |
39 | | - private readonly fetchTokenCounter: TelemetryCounter; |
40 | | - |
41 | | - constructor(pluginService: PluginService, credentialsProviderFactory: CredentialsProviderFactory) { |
42 | | - super(); |
43 | | - this.pluginService = pluginService; |
44 | | - this.credentialsProviderFactory = credentialsProviderFactory; |
45 | | - this.fetchTokenCounter = this.pluginService.getTelemetryFactory().createCounter("oktaAuth.fetchToken.count"); |
46 | | - } |
47 | | - |
48 | | - public getSubscribedMethods(): Set<string> { |
49 | | - return OktaAuthPlugin.subscribedMethods; |
50 | | - } |
51 | | - |
52 | | - connect( |
53 | | - hostInfo: HostInfo, |
54 | | - props: Map<string, any>, |
55 | | - isInitialConnection: boolean, |
56 | | - connectFunc: () => Promise<ClientWrapper> |
57 | | - ): Promise<ClientWrapper> { |
58 | | - return this.connectInternal(hostInfo, props, connectFunc); |
59 | | - } |
60 | | - |
61 | | - forceConnect( |
62 | | - hostInfo: HostInfo, |
63 | | - props: Map<string, any>, |
64 | | - isInitialConnection: boolean, |
65 | | - connectFunc: () => Promise<ClientWrapper> |
66 | | - ): Promise<ClientWrapper> { |
67 | | - return this.connectInternal(hostInfo, props, connectFunc); |
68 | | - } |
69 | | - |
70 | | - async connectInternal(hostInfo: HostInfo, props: Map<string, any>, connectFunc: () => Promise<ClientWrapper>): Promise<ClientWrapper> { |
71 | | - SamlUtils.checkIdpCredentialsWithFallback(props); |
72 | | - |
73 | | - const host = IamAuthUtils.getIamHost(props, hostInfo); |
74 | | - const port = IamAuthUtils.getIamPort(props, hostInfo, this.pluginService.getDialect().getDefaultPort()); |
75 | | - const region = RegionUtils.getRegion(props.get(WrapperProperties.IAM_REGION.name), host); |
76 | | - |
77 | | - const cacheKey = IamAuthUtils.getCacheKey(port, WrapperProperties.DB_USER.get(props), host, region); |
78 | | - const tokenInfo = OktaAuthPlugin.tokenCache.get(cacheKey); |
79 | | - |
80 | | - const isCachedToken = tokenInfo !== undefined && !tokenInfo.isExpired(); |
81 | | - |
82 | | - if (isCachedToken) { |
83 | | - logger.debug(Messages.get("AuthenticationToken.useCachedToken", tokenInfo.token)); |
84 | | - WrapperProperties.PASSWORD.set(props, tokenInfo.token); |
85 | | - } else { |
86 | | - await this.updateAuthenticationToken(hostInfo, props, region, cacheKey, host); |
87 | | - } |
88 | | - WrapperProperties.USER.set(props, WrapperProperties.DB_USER.get(props)); |
89 | | - this.pluginService.updateConfigWithProperties(props); |
90 | | - |
91 | | - try { |
92 | | - return await connectFunc(); |
93 | | - } catch (e: any) { |
94 | | - if (!this.pluginService.isLoginError(e as Error) || !isCachedToken) { |
95 | | - logger.debug(Messages.get("Authentication.connectError", e.message)); |
96 | | - throw e; |
97 | | - } |
98 | | - try { |
99 | | - await this.updateAuthenticationToken(hostInfo, props, region, cacheKey, host); |
100 | | - return await connectFunc(); |
101 | | - } catch (e: any) { |
102 | | - throw new AwsWrapperError(Messages.get("SamlAuthPlugin.unhandledError", e.message)); |
103 | | - } |
104 | | - } |
105 | | - } |
106 | | - |
107 | | - public async updateAuthenticationToken(hostInfo: HostInfo, props: Map<string, any>, region: string, cacheKey: string, iamHost): Promise<void> { |
108 | | - const tokenExpirationSec = WrapperProperties.IAM_TOKEN_EXPIRATION.get(props); |
109 | | - if (tokenExpirationSec < 0) { |
110 | | - throw new AwsWrapperError(Messages.get("AuthenticationToken.tokenExpirationLessThanZero")); |
111 | | - } |
112 | | - const tokenExpiry = Date.now() + tokenExpirationSec * 1000; |
113 | | - const port = IamAuthUtils.getIamPort(props, hostInfo, this.pluginService.getDialect().getDefaultPort()); |
114 | | - this.fetchTokenCounter.inc(); |
115 | | - const token = await IamAuthUtils.generateAuthenticationToken( |
116 | | - iamHost, |
117 | | - port, |
118 | | - region, |
119 | | - WrapperProperties.DB_USER.get(props), |
120 | | - await this.credentialsProviderFactory.getAwsCredentialsProvider(hostInfo.host, region, props), |
121 | | - this.pluginService |
122 | | - ); |
123 | | - logger.debug(Messages.get("AuthenticationToken.generatedNewToken", token)); |
124 | | - WrapperProperties.PASSWORD.set(props, token); |
125 | | - this.pluginService.updateConfigWithProperties(props); |
126 | | - OktaAuthPlugin.tokenCache.set(cacheKey, new TokenInfo(token, tokenExpiry)); |
127 | | - } |
128 | | - |
129 | | - releaseResources(): Promise<void> { |
130 | | - OktaAuthPlugin.tokenCache.clear(); |
131 | | - return; |
| 22 | +export class OktaAuthPlugin extends BaseSamlAuthPlugin { |
| 23 | + constructor(pluginService: PluginService, credentialsProviderFactory: CredentialsProviderFactory, iamAuthUtils: IamAuthUtils = new IamAuthUtils()) { |
| 24 | + super(pluginService, credentialsProviderFactory, "oktaAuth.fetchToken.count", iamAuthUtils); |
132 | 25 | } |
133 | 26 | } |
0 commit comments