Skip to content

Commit c7a71c9

Browse files
committed
refactor(credentials): read a reconnect's stored mode and cloud through one per-provider map
1 parent 8f5230d commit c7a71c9

2 files changed

Lines changed: 44 additions & 36 deletions

File tree

‎apps/sim/lib/credentials/application/prepare-workload-identity.ts‎

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ import { credentialOperations } from '@/lib/credentials/application/operations'
77
import { storedAwsAuthType } from '@/lib/credentials/aws-service-account.server'
88
import { storedDatabricksAuthType } from '@/lib/credentials/databricks-service-account.server'
99
import { isGoogleWorkloadIdentitySecret } from '@/lib/credentials/google-workload-identity'
10-
import {
11-
storedMicrosoftAppAuthType,
12-
storedMicrosoftAppCloud,
13-
} from '@/lib/credentials/microsoft-app-service-account.server'
10+
import { storedMicrosoftAppReconnectFields } from '@/lib/credentials/microsoft-app-service-account.server'
1411
import type { MicrosoftCloudId } from '@/lib/credentials/microsoft-clouds'
1512
import { storedSnowflakeAuthType } from '@/lib/credentials/snowflake-service-account.server'
1613
import {
@@ -55,22 +52,38 @@ export type PrepareWorkloadIdentityResult =
5552
expiresAt: string
5653
} & ReconnectAuthTypeField)
5754

55+
/** What a reconnect reopens on, read from a credential's stored secret. */
56+
interface StoredReconnectFields<P extends WorkloadIdentityProvider> {
57+
authType?: WorkloadIdentityProviderAuthType<P>
58+
/** A Microsoft app credential's national cloud; absent for a global one and other providers. */
59+
cloud?: MicrosoftCloudId
60+
}
61+
62+
function authTypeOnly<A extends string>(
63+
readAuthType: (decrypted: string) => A | undefined
64+
): (decrypted: string) => { authType?: A } {
65+
return (decrypted) => {
66+
const authType = readAuthType(decrypted)
67+
return authType ? { authType } : {}
68+
}
69+
}
70+
5871
/**
5972
* Every keyless provider also has key-based modes under the same provider ID, so a
60-
* reconnect reads the stored secret's mode and the dialog reopens on it rather than
61-
* on a default that would drop the credential's role, secret, or certificate.
73+
* reconnect reads the stored secret's mode (and a Microsoft app credential's cloud) and
74+
* the dialog reopens on it rather than on a default that would drop the credential's
75+
* role, secret, certificate, or cloud.
6276
*/
63-
const STORED_AUTH_TYPE: {
64-
[P in WorkloadIdentityProvider]: (
65-
decrypted: string
66-
) => WorkloadIdentityProviderAuthType<P> | undefined
77+
const STORED_RECONNECT_FIELDS: {
78+
[P in WorkloadIdentityProvider]: (decrypted: string) => StoredReconnectFields<P>
6779
} = {
68-
aws: storedAwsAuthType,
69-
databricks: storedDatabricksAuthType,
70-
snowflake: storedSnowflakeAuthType,
71-
microsoft: storedMicrosoftAppAuthType,
72-
google: (decrypted) =>
73-
isGoogleWorkloadIdentitySecret(decrypted) ? 'workload_identity' : 'json_key',
80+
aws: authTypeOnly(storedAwsAuthType),
81+
databricks: authTypeOnly(storedDatabricksAuthType),
82+
snowflake: authTypeOnly(storedSnowflakeAuthType),
83+
microsoft: storedMicrosoftAppReconnectFields,
84+
google: (decrypted) => ({
85+
authType: isGoogleWorkloadIdentitySecret(decrypted) ? 'workload_identity' : 'json_key',
86+
}),
7487
}
7588

7689
/**
@@ -99,11 +112,10 @@ async function storedReconnectFields(
99112
return {}
100113
}
101114
const { decrypted } = await decryptSecret(row.encryptedServiceAccountKey)
102-
const reconnectAuthType = STORED_AUTH_TYPE[provider](decrypted)
103-
const reconnectCloud = provider === 'microsoft' ? storedMicrosoftAppCloud(decrypted) : undefined
115+
const { authType, cloud } = STORED_RECONNECT_FIELDS[provider](decrypted)
104116
return {
105-
...(reconnectAuthType ? { reconnectAuthType } : {}),
106-
...(reconnectCloud ? { reconnectCloud } : {}),
117+
...(authType ? { reconnectAuthType: authType } : {}),
118+
...(cloud ? { reconnectCloud: cloud } : {}),
107119
}
108120
}
109121

‎apps/sim/lib/credentials/microsoft-app-service-account.server.ts‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -497,25 +497,21 @@ async function verifyDataverseApplicationUser(environmentUrl: string, token: str
497497
)
498498
}
499499

500-
/** The auth mode a decrypted Microsoft app credential secret holds, or undefined when it is not one. */
501-
export function storedMicrosoftAppAuthType(
502-
decrypted: string
503-
): 'federated' | 'client_secret' | 'certificate' | undefined {
504-
try {
505-
const parsed = storedSecretSchema.safeParse(JSON.parse(decrypted))
506-
return parsed.success ? parsed.data.config.authType : undefined
507-
} catch {
508-
return undefined
509-
}
510-
}
511-
512-
/** The stored national cloud, for a reconnect to reopen on; absent for a global credential. */
513-
export function storedMicrosoftAppCloud(decrypted: string): MicrosoftCloudId | undefined {
500+
/**
501+
* The auth mode and national cloud a decrypted Microsoft app credential secret holds, for a
502+
* reconnect to reopen on: empty when it is not one, and no cloud for a global credential.
503+
*/
504+
export function storedMicrosoftAppReconnectFields(decrypted: string): {
505+
authType?: MicrosoftAppConfig['authType']
506+
cloud?: MicrosoftCloudId
507+
} {
514508
try {
515509
const parsed = storedSecretSchema.safeParse(JSON.parse(decrypted))
516-
return parsed.success ? parsed.data.config.cloud : undefined
510+
if (!parsed.success) return {}
511+
const { authType, cloud } = parsed.data.config
512+
return cloud ? { authType, cloud } : { authType }
517513
} catch {
518-
return undefined
514+
return {}
519515
}
520516
}
521517

0 commit comments

Comments
 (0)