@updating-secrets/infisical-adapter
Version:
Infisical adapter for the updating-secrets package.
85 lines (84 loc) • 3.35 kB
JavaScript
import { getObjectTypedEntries, stringifyWithJson5, } from '@augment-vir/common';
/**
* A mock implementation of `InfisicalSDK` from the
* [@infisical/sdk](https://www.npmjs.com/package/@infisical/sdk) package. This only mocks what is
* necessary for the infisical adapter to work.
*
* @category Mocks
*/
export class MockInfisicalSdk {
mockSecrets;
/** Keeps track of whether this SDK has been authorized or not. */
isAuthorized = false;
constructor(
/** Mock secrets that will be used in `secrets().listSecrets()` */
mockSecrets) {
this.mockSecrets = mockSecrets;
}
/** Mock of `InfisicalSDK.auth()` */
auth() {
return {
/** Mock of `InfisicalSDK.auth().universalAuth` */
universalAuth: {
/** Mock of `InfisicalSDK.auth().universalAuth.login()` */
login: (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
...params) => {
this.isAuthorized = true;
return Promise.resolve(this);
},
/** Mock of `InfisicalSDK.auth().universalAuth.renew()` */
renew() {
throw new Error('Not mocked.');
},
},
/** Mock of `InfisicalSDK.auth().accessToken` */
accessToken: (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
...params) => {
this.isAuthorized = true;
return this;
},
/** Mock of `InfisicalSDK.auth().awsIamAuth` */
awsIamAuth: {
/** Mock of `InfisicalSDK.auth().awsIamAuth.login()` */
login: (
// eslint-disable-next-line @typescript-eslint/no-unused-vars
...params) => {
this.isAuthorized = true;
return Promise.resolve(this);
},
/** Mock of `InfisicalSDK.auth().awsIamAuth.renew()` */
renew() {
throw new Error('Not mocked.');
},
},
};
}
/** Mock of `InfisicalSDK.secrets()` */
secrets() {
return {
/** Mock of `InfisicalSDK.secrets().listSecrets()` */
listSecrets: ({ projectId, environment }) => {
if (!this.isAuthorized) {
throw new Error('Mock Infisical SDK client not authorized.');
}
const secrets = this.mockSecrets.find((mock) => mock.projectId === projectId && mock.env === environment)?.secrets;
if (!secrets) {
throw new Error('Invalid project.');
}
return Promise.resolve({
secrets: getObjectTypedEntries(secrets).map(([secretKey, mockDefinition,]) => {
return {
secretPath: mockDefinition.folderPath || '/',
secretKey,
secretValue: mockDefinition.preJson
? stringifyWithJson5(mockDefinition.preJson)
: mockDefinition.rawString || '',
};
}),
});
},
};
}
}