Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: add support for creating user in case they don't exist

adilsona93cfac0270b
diff --git a/backend/src/ee/services/secret-rotation-v2/snowflake-user-key-pair/snowflake-user-key-pair-rotation-fns.ts b/backend/src/ee/services/secret-rotation-v2/snowflake-user-key-pair/snowflake-user-key-pair-rotation-fns.ts
index 9dfb175944..1a69d69d08 100644
--- a/backend/src/ee/services/secret-rotation-v2/snowflake-user-key-pair/snowflake-user-key-pair-rotation-fns.ts
+++ b/backend/src/ee/services/secret-rotation-v2/snowflake-user-key-pair/snowflake-user-key-pair-rotation-fns.ts
@@ -122,11 +122,30 @@ export const snowflakeUserKeyPairRotationFactory: TRotationFactory<
}
};
+ // Create the target user as a key-pair-only SERVICE account if it does not already exist. We check
+ // first (rather than CREATE USER IF NOT EXISTS) so managing a pre-existing user still needs only
+ // ALTER privileges; the CREATE USER privilege is required only when a user is actually created.
+ const ensureUserExists = async () => {
+ await runSnowflake(async (client) => {
+ const escapedUsername = username.replace(/'/g, "''");
+ const rows = await executeSnowflakeSql<Record<string, unknown>>(client, `SHOW USERS LIKE '${escapedUsername}'`);
+ // SHOW ... LIKE is case-insensitive and treats _/% as wildcards, so exact-match the name.
+ const exists = rows.some((row) => String(row.name ?? row.NAME ?? "") === username);
+ if (!exists) {
+ await executeSnowflakeSql(
+ client,
+ `CREATE USER ${quoteSnowflakeIdent(username)} TYPE = SERVICE COMMENT = 'Created by Infisical secret rotation'`
+ );
+ }
+ }, `Failed to ensure Snowflake user "${username}" exists`);
+ };
+
const issueCredentials: TRotationFactoryIssueCredentials<TSnowflakeUserKeyPairRotationGeneratedCredentials> = async (
callback
) => {
// On creation the first credential always occupies slot 0 (activeIndex defaults to 0).
const keyPair = await generateRsaKeyPair();
+ await ensureUserExists();
await setPublicKeyForSlot(slotForIndex(0), keyPair);
return callback(keyPair);
};
diff --git a/backend/src/lib/api-docs/constants.ts b/backend/src/lib/api-docs/constants.ts
index 4632759888..33d30e1f7e 100644
--- a/backend/src/lib/api-docs/constants.ts
+++ b/backend/src/lib/api-docs/constants.ts
@@ -3566,7 +3566,8 @@ export const SecretRotations = {
serviceAccountUserId: "The user ID of the Fireworks service account to create the API key for."
},
SNOWFLAKE_USER_KEY_PAIR: {
- username: "The Snowflake user whose RSA key pair will be rotated."
+ username:
+ "The Snowflake user whose RSA key pair will be rotated. If the user does not exist, it is created as a key-pair-only SERVICE user."
}
},
SECRETS_MAPPING: {
diff --git a/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/SnowflakeUserKeyPairRotationParametersFields.tsx b/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/SnowflakeUserKeyPairRotationParametersFields.tsx
index 8929cc81f8..ffd15661a0 100644
--- a/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/SnowflakeUserKeyPairRotationParametersFields.tsx
+++ b/frontend/src/components/secret-rotations-v2/forms/SecretRotationV2ParametersFields/SnowflakeUserKeyPairRotationParametersFields.tsx
@@ -4,7 +4,8 @@ import { faCircleInfo } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { TSecretRotationV2Form } from "@app/components/secret-rotations-v2/forms/schemas";
-import { FilterableSelect, FormControl, Tooltip } from "@app/components/v2";
+import { FormControl, Tooltip } from "@app/components/v2";
+import { CreatableSelect } from "@app/components/v2/CreatableSelect";
import { useSnowflakeConnectionListUsers } from "@app/hooks/api/appConnections/snowflake";
import { TSnowflakeUser } from "@app/hooks/api/appConnections/snowflake/types";
import { SecretRotation } from "@app/hooks/api/secretRotationsV2";
@@ -36,8 +37,10 @@ export const SnowflakeUserKeyPairRotationParametersFields = () => {
className="max-w-md"
content={
<>
- Ensure that your connection&#39;s role has the necessary privileges to list users
- and to alter the selected user&#39;s RSA public key.
+ Select an existing Snowflake user, or type a new username to create it. New users
+ are created as key-pair-only SERVICE users. Ensure your connection&#39;s role has
+ the privileges to list users, alter the user&#39;s RSA public key, and (for new
+ users) create users. If the keys are rotated the user will continue to exist.
</>
}
>
@@ -48,17 +51,20 @@ export const SnowflakeUserKeyPairRotationParametersFields = () => {
</Tooltip>
}
>
- <FilterableSelect
+ <CreatableSelect
menuPlacement="top"
isLoading={isUsersPending && Boolean(connectionId)}
isDisabled={!connectionId}
- value={users?.find((user) => user.name === value) ?? null}
+ value={users?.find((user) => user.name === value) ?? (value ? { name: value } : null)}
onChange={(option) => {
const selectedUser = option as SingleValue<TSnowflakeUser>;
onChange(selectedUser?.name ?? null);
}}
+ onCreateOption={(inputValue) => onChange(inputValue)}
+ getNewOptionData={(inputValue) => ({ name: inputValue })}
+ isValidNewOption={(inputValue) => inputValue.trim().length > 0}
options={users}
- placeholder="Select a user..."
+ placeholder="Select or enter a user..."
getOptionLabel={(option) => option.name}
getOptionValue={(option) => option.name}
/>