improvement: address feedback

This commit is contained in:
Scott Wilson
2024-10-31 10:24:30 -07:00
parent b693c035ce
commit 7dd6eac20a
5 changed files with 50 additions and 39 deletions

View File

@@ -182,9 +182,9 @@ export const DynamicSecretSnowflakeSchema = z.object({
orgId: z.string().trim().min(1),
username: z.string().trim().min(1),
password: z.string().trim().min(1),
creationStatement: z.string().trim(),
revocationStatement: z.string().trim(),
renewStatement: z.string().trim()
creationStatement: z.string().trim().min(1),
revocationStatement: z.string().trim().min(1),
renewStatement: z.string().trim().optional()
});
export const AzureEntraIDSchema = z.object({

View File

@@ -3,6 +3,7 @@ import { customAlphabet } from "nanoid";
import snowflake from "snowflake-sdk";
import { z } from "zod";
import { BadRequestError } from "@app/lib/errors";
import { alphaNumericNanoId } from "@app/lib/nanoid";
import { DynamicSecretSnowflakeSchema, TDynamicProviderFns } from "./models";
@@ -41,15 +42,7 @@ export const SnowflakeProvider = (): TDynamicProviderFns => {
application: "Infisical"
});
await new Promise((resolve, reject) => {
client.connect((err) => {
if (err) {
return reject(err);
}
return resolve(true);
});
});
await client.connectAsync(noop);
return client;
};
@@ -58,9 +51,20 @@ export const SnowflakeProvider = (): TDynamicProviderFns => {
const providerInputs = await validateProviderInputs(inputs);
const client = await getClient(providerInputs);
const isValidConnection = await client.isValidAsync();
let isValidConnection: boolean;
client.destroy(noop);
try {
isValidConnection = await Promise.race([
client.isValidAsync(),
new Promise((resolve) => {
setTimeout(resolve, 10000);
}).then(() => {
throw new BadRequestError({ message: "Unable to establish connection - verify credentials" });
})
]);
} finally {
client.destroy(noop);
}
return isValidConnection;
};
@@ -72,15 +76,15 @@ export const SnowflakeProvider = (): TDynamicProviderFns => {
const username = generateUsername();
const password = generatePassword();
const expiration = getDaysToExpiry(new Date(expireAt));
const creationStatement = handlebars.compile(providerInputs.creationStatement, { noEscape: true })({
username,
password,
expiration
});
try {
const expiration = getDaysToExpiry(new Date(expireAt));
const creationStatement = handlebars.compile(providerInputs.creationStatement, { noEscape: true })({
username,
password,
expiration
});
await new Promise((resolve, reject) => {
client.execute({
sqlText: creationStatement,
@@ -105,9 +109,9 @@ export const SnowflakeProvider = (): TDynamicProviderFns => {
const client = await getClient(providerInputs);
const revokeStatement = handlebars.compile(providerInputs.revocationStatement)({ username });
try {
const revokeStatement = handlebars.compile(providerInputs.revocationStatement)({ username });
await new Promise((resolve, reject) => {
client.execute({
sqlText: revokeStatement,
@@ -132,14 +136,13 @@ export const SnowflakeProvider = (): TDynamicProviderFns => {
const client = await getClient(providerInputs);
const expiration = getDaysToExpiry(new Date(expireAt));
const renewStatement = handlebars.compile(providerInputs.renewStatement)({
username,
expiration
});
try {
const expiration = getDaysToExpiry(new Date(expireAt));
const renewStatement = handlebars.compile(providerInputs.renewStatement)({
username,
expiration
});
await new Promise((resolve, reject) => {
client.execute({
sqlText: renewStatement,

View File

@@ -226,7 +226,7 @@ export type TDynamicSecretProvider =
password: string;
creationStatement: string;
revocationStatement: string;
renewStatement: string;
renewStatement?: string;
};
};
export type TCreateDynamicSecretDTO = {

View File

@@ -29,7 +29,7 @@ const formSchema = z.object({
password: z.string().trim().min(1),
creationStatement: z.string().trim().min(1),
revocationStatement: z.string().trim().min(1),
renewStatement: z.string().trim().min(1)
renewStatement: z.string().trim().optional()
}),
defaultTTL: z.string().superRefine((val, ctx) => {
const valMs = ms(val);
@@ -51,7 +51,11 @@ const formSchema = z.object({
if (valMs > 24 * 60 * 60 * 1000)
ctx.addIssue({ code: z.ZodIssueCode.custom, message: "TTL must be less than a day" });
}),
name: z.string().refine((val) => val.toLowerCase() === val, "Must be lowercase")
name: z
.string()
.trim()
.min(1)
.refine((val) => val.toLowerCase() === val, "Must be lowercase")
});
type TForm = z.infer<typeof formSchema>;
@@ -105,7 +109,7 @@ export const SnowflakeInputForm = ({
} catch (err) {
createNotification({
type: "error",
text: "Failed to create dynamic secret"
text: err instanceof Error ? err.message : "Failed to create dynamic secret"
});
}
};

View File

@@ -28,9 +28,9 @@ const formSchema = z.object({
orgId: z.string().min(1),
username: z.string().min(1),
password: z.string().min(1),
creationStatement: z.string().min(1),
revocationStatement: z.string().min(1),
renewStatement: z.string().optional()
creationStatement: z.string().trim().min(1),
revocationStatement: z.string().trim().min(1),
renewStatement: z.string().trim().optional()
})
.partial(),
defaultTTL: z.string().superRefine((val, ctx) => {
@@ -53,7 +53,11 @@ const formSchema = z.object({
if (valMs > 24 * 60 * 60 * 1000)
ctx.addIssue({ code: z.ZodIssueCode.custom, message: "TTL must be less than a day" });
}),
newName: z.string().refine((val) => val.toLowerCase() === val, "Must be lowercase")
newName: z
.string()
.trim()
.min(1)
.refine((val) => val.toLowerCase() === val, "Must be lowercase")
});
type TForm = z.infer<typeof formSchema>;
@@ -114,7 +118,7 @@ export const EditDynamicSecretSnowflakeForm = ({
} catch (err) {
createNotification({
type: "error",
text: "Failed to update dynamic secret"
text: err instanceof Error ? err.message : "Failed to create dynamic secret"
});
}
};