Skip to content

Commit

Permalink
Update secrets.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHougaard committed Jan 22, 2024
1 parent ae05a1c commit 633f450
Showing 1 changed file with 119 additions and 30 deletions.
149 changes: 119 additions & 30 deletions languages/node/examples/secrets.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,157 @@
import { InfisicalClient, LogLevel } from "../src";
import { InfisicalClient, ListSecretsOptions, LogLevel } from "../src";

// Just need a random string for testing, nothing crazy.
const randomStr = () => Date.now().toString(36);
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));

// Make sure to change these values.
const projectId = "PROJECT_ID";
const environment = "dev";
const TEST_SECRET_NAME = "DATABASE_URL";

const MACHINE_IDENTITY_CLIENT_SECRET = "CLIENT_SECRET";
const MACHINE_IDENTITY_CLIENT_ID = "CLIENT_ID";

const uncachedClient = new InfisicalClient({
clientId: "CLIENT_ID",
clientSecret: "CLIENT_SECRET",
siteUrl: "http://localhost:8080", // This is optional. You can remove it and it will default to https://app.infisical.com.
cacheTtl: 0, // Default is 300 seconds (5 minutes).
clientId: MACHINE_IDENTITY_CLIENT_ID,
clientSecret: MACHINE_IDENTITY_CLIENT_SECRET,
// siteUrl: "http://localhost:8080", // This is optional. You can remove it and it will default to https://app.infisical.com.
cacheTtl: 0, // Default is 300 seconds (5 minutes). (0 means no caching)
logLevel: LogLevel.Error // Optional, default is LogLevel.Error.
});

const cachedClient = new InfisicalClient({
clientId: "CLIENT_ID",
clientSecret: "CLIENT_SECRET",
siteUrl: "http://localhost:8080", // This is optional. You can remove it and it will default to https://app.infisical.com.
clientId: MACHINE_IDENTITY_CLIENT_ID,
clientSecret: MACHINE_IDENTITY_CLIENT_SECRET,
// siteUrl: "http://localhost:8080", // This is optional. You can remove it and it will default to https://app.infisical.com.
cacheTtl: 300, // Default is 300 seconds (5 minutes).
logLevel: LogLevel.Error // Optional, default is LogLevel.Error.
});

// Make sure to change these values.
const projectId = "YOUR_PROJECT_ID";
const environment = "dev";

async function main() {
await testCacheSpeeds();
await testListSecrets();
await testCreateSecret();
await testDeleteSecret();
await testUpdateSecret();
}

async function testUpdateSecret() {
console.log("⏱️ Updating secret");
const newValue = randomStr();

// Get a secret, and update it afterwards, as an example.
const secretOptions = {
projectId: projectId,
environment: environment,
secretName: "TEST"
} as const;
const updatedSecret = await uncachedClient.updateSecret({
secretName: TEST_SECRET_NAME,
environment,
projectId,
secretValue: newValue
});

if (updatedSecret.secretValue !== newValue) {
throw new Error("❌ testUpdate: secretValue was not the same");
}

const secret = await cachedClient.getSecret(secretOptions);
console.log("Fetched secret", secret);
console.log("✅ Secret updated");
}

const updatedSecret = await cachedClient.updateSecret({
...secretOptions,
secretValue: "NEW VALUE"
async function testDeleteSecret() {
console.log("⏱️ Deleting secret");

const secretName = randomStr();
const secretValue = randomStr();
await uncachedClient.createSecret({
secretName,
secretValue,
projectId,
environment
});
console.log("Updated secret", updatedSecret);

const deletedSecret = await uncachedClient.deleteSecret({
secretName,
environment,
projectId
});

if (deletedSecret.secretKey !== secretName) {
throw new Error("❌ testDelete: secretName was not the same");
}

console.log("✅ Secret deleted");
}

async function testCreateSecret() {
console.log("⏱️ Creating secret");

const secretName = randomStr();
const secretValue = randomStr();
const createdSecret = await uncachedClient.createSecret({
secretName,
secretValue,
projectId,
environment
});

if (createdSecret.secretKey !== secretName) {
throw new Error("❌ testCreate: secretName was not the same");
}

await uncachedClient.deleteSecret({
secretName,
environment,
projectId
});

console.log("✅ Secret created");
}

async function testListSecrets() {
console.log("⏱️ Listing secrets");

const secretName = randomStr();
const secretValue = randomStr();
await uncachedClient.createSecret({
secretName,
secretValue,
projectId,
environment
});

const secrets = await uncachedClient.listSecrets({
projectId,
environment
});

if (!secrets.length) {
throw new Error("❌ testList: secrets.length was 0");
}

await uncachedClient.deleteSecret({
secretName,
environment,
projectId
});

console.log("✅ Secrets listed");
}

async function testCacheSpeeds() {
console.log("Testing cache speeds...");

await uncachedClient.getSecret({ projectId, environment, secretName: "TEST" });
await cachedClient.getSecret({ projectId, environment, secretName: "TEST" });
await uncachedClient.getSecret({ projectId, environment, secretName: TEST_SECRET_NAME });
await cachedClient.getSecret({ projectId, environment, secretName: TEST_SECRET_NAME });

const startUncached = Date.now();
for (let i = 0; i < 10; i++) await uncachedClient.getSecret({ projectId, environment, secretName: "TEST" }).then(console.log);
for (let i = 0; i < 10; i++) await uncachedClient.getSecret({ projectId, environment, secretName: TEST_SECRET_NAME });
const endUncached = Date.now();

const startCached = Date.now();
for (let i = 0; i < 10; i++) await cachedClient.getSecret({ projectId, environment, secretName: "TEST" });
for (let i = 0; i < 10; i++) await cachedClient.getSecret({ projectId, environment, secretName: TEST_SECRET_NAME });
const endCached = Date.now();

console.log(`Uncached: ${endUncached - startUncached}ms`);
console.log(`Cached: ${endCached - startCached}ms\n`);

const percentage = (endUncached - startUncached) / (endCached - startCached);
console.log(`Cached fetched the same secret 10 times, ${percentage.toFixed(2)}x faster than uncached`);
console.log(`Cached fetched the same secret 10 times, ${percentage.toFixed(2)}x faster than uncached\n\n`);
}

main();

0 comments on commit 633f450

Please sign in to comment.