mirror of
https://github.com/Infisical/infisical.git
synced 2026-01-09 15:38:03 -05:00
Add a dedicated auth func
This commit is contained in:
@@ -15,9 +15,6 @@ import { TRedisAccountCredentials, TRedisResourceConnectionDetails } from "./red
|
|||||||
|
|
||||||
const EXTERNAL_REQUEST_TIMEOUT = 10 * 1000;
|
const EXTERNAL_REQUEST_TIMEOUT = 10 * 1000;
|
||||||
|
|
||||||
const TEST_CONNECTION_USERNAME = "infisical-gateway-connection-test";
|
|
||||||
const TEST_CONNECTION_PASSWORD = "infisical-gateway-connection-test-password";
|
|
||||||
|
|
||||||
export interface RedisResourceConnection {
|
export interface RedisResourceConnection {
|
||||||
/**
|
/**
|
||||||
* Check and see if the connection is good or not.
|
* Check and see if the connection is good or not.
|
||||||
@@ -28,6 +25,14 @@ export interface RedisResourceConnection {
|
|||||||
*/
|
*/
|
||||||
validate: (connectOnly: boolean) => Promise<void>;
|
validate: (connectOnly: boolean) => Promise<void>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Authenticate with the provided credentials.
|
||||||
|
*
|
||||||
|
* @param credentials the username and password to authenticate with
|
||||||
|
* @returns Promise to be resolved when authentication succeeds, otherwise an error will be errbacked
|
||||||
|
*/
|
||||||
|
authenticate: (credentials: TRedisAccountCredentials) => Promise<void>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close the connection.
|
* Close the connection.
|
||||||
*
|
*
|
||||||
@@ -40,11 +45,9 @@ const makeRedisConnection = (
|
|||||||
proxyPort: number,
|
proxyPort: number,
|
||||||
config: {
|
config: {
|
||||||
connectionDetails: TRedisResourceConnectionDetails;
|
connectionDetails: TRedisResourceConnectionDetails;
|
||||||
username?: string;
|
|
||||||
password?: string;
|
|
||||||
}
|
}
|
||||||
): RedisResourceConnection => {
|
): RedisResourceConnection => {
|
||||||
const { connectionDetails, username, password } = config;
|
const { connectionDetails } = config;
|
||||||
const { sslEnabled, sslRejectUnauthorized, sslCertificate } = connectionDetails;
|
const { sslEnabled, sslRejectUnauthorized, sslCertificate } = connectionDetails;
|
||||||
|
|
||||||
let client: Redis | null = null;
|
let client: Redis | null = null;
|
||||||
@@ -69,8 +72,8 @@ const makeRedisConnection = (
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
validate: async (connectOnly) => {
|
validate: async (connectOnly) => {
|
||||||
|
client = createClient();
|
||||||
try {
|
try {
|
||||||
client = createClient();
|
|
||||||
await client.ping();
|
await client.ping();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (connectOnly) {
|
if (connectOnly) {
|
||||||
@@ -94,6 +97,29 @@ const makeRedisConnection = (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
authenticate: async (credentials) => {
|
||||||
|
client = createClient();
|
||||||
|
try {
|
||||||
|
const result = await client.auth(credentials.username, credentials.password, () => {});
|
||||||
|
if (result !== "OK") {
|
||||||
|
throw new BadRequestError({
|
||||||
|
message: `Authentication failed: Redis returned ${result as string} status`
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof BadRequestError) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
throw new BadRequestError({
|
||||||
|
message: `Unable to authenticate Redis connection: ${(error as Error).message || String(error)}`
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
if (client) {
|
||||||
|
await client.quit();
|
||||||
|
client = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
close: async () => {
|
close: async () => {
|
||||||
if (client) {
|
if (client) {
|
||||||
await client.quit();
|
await client.quit();
|
||||||
@@ -107,8 +133,6 @@ export const executeWithGateway = async <T>(
|
|||||||
config: {
|
config: {
|
||||||
connectionDetails: TRedisResourceConnectionDetails;
|
connectionDetails: TRedisResourceConnectionDetails;
|
||||||
gatewayId: string;
|
gatewayId: string;
|
||||||
username?: string;
|
|
||||||
password?: string;
|
|
||||||
},
|
},
|
||||||
gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">,
|
gatewayV2Service: Pick<TGatewayV2ServiceFactory, "getPlatformConnectionDetailsByGatewayId">,
|
||||||
operation: (connection: RedisResourceConnection) => Promise<T>
|
operation: (connection: RedisResourceConnection) => Promise<T>
|
||||||
@@ -183,13 +207,11 @@ export const redisResourceFactory: TPamResourceFactory<TRedisResourceConnectionD
|
|||||||
await executeWithGateway(
|
await executeWithGateway(
|
||||||
{
|
{
|
||||||
connectionDetails,
|
connectionDetails,
|
||||||
gatewayId,
|
gatewayId
|
||||||
username: credentials.username,
|
|
||||||
password: credentials.password
|
|
||||||
},
|
},
|
||||||
gatewayV2Service,
|
gatewayV2Service,
|
||||||
async (client) => {
|
async (client) => {
|
||||||
await client.validate(false);
|
await client.authenticate(credentials);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
return credentials;
|
return credentials;
|
||||||
|
|||||||
Reference in New Issue
Block a user