infisical-autoenv/index.js

92 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-11-05 11:50:53 -07:00
import fs from "node:fs";
import os from "node:os";
2024-08-03 21:10:14 -06:00
import { InfisicalClient } from "@infisical/sdk";
2023-11-05 17:41:45 -07:00
import { loadConfig, upsertConfig } from "./configurator.js";
2023-11-05 11:50:53 -07:00
const envpath = `${os.homedir()}/.env`;
//upsertConfig(); //TODO Build Configuration Generator
const config = loadConfig();
2024-08-03 21:10:14 -06:00
const siteUrl = config.infisicalInstance;
2023-11-05 11:50:53 -07:00
const projects = config.projects;
const pendingSecrets = [];
2023-11-05 17:41:45 -07:00
async function getProjectSecrets(
2024-08-03 21:10:14 -06:00
projectName,
projectId,
2023-11-05 17:41:45 -07:00
environment,
2024-08-03 21:10:14 -06:00
clientId,
clientSecret,
2023-11-05 17:41:45 -07:00
path = "/",
includeImports = true,
) {
2024-08-03 21:10:14 -06:00
const client = new InfisicalClient({
siteUrl,
auth: { universalAuth: { clientId, clientSecret } },
});
const secrets = await client.listSecrets({
projectId,
2023-11-05 17:41:45 -07:00
environment,
path,
includeImports,
attachToProcessEnv: false,
});
2024-08-03 21:10:14 -06:00
return secrets.map((s) => ({ ...s, project: projectName, path }));
2023-11-05 11:50:53 -07:00
}
2023-11-05 17:41:45 -07:00
function mapToFile(proj, secrets) {
const filename = `infisical-autoenv-${proj}`;
const date = new Date();
const dateString =
date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
const paths = Object.keys(secrets);
const lines = paths.map(
(p) =>
`# ---${p}--- ${dateString}\n` +
secrets[p]
2024-08-03 21:10:14 -06:00
.map((s) => `export ${s.secretKey}=${s.secretValue}`)
2023-11-05 17:41:45 -07:00
.join("\n"),
);
const fileData = lines.join("\n");
fs.writeFileSync(`${envpath}/${filename}`, fileData);
2023-11-05 11:50:53 -07:00
}
2023-11-05 17:41:45 -07:00
for (var p of projects) {
for (var e of p.environments) {
2024-08-03 21:10:14 -06:00
if (!process.env[e.clientIdEnvar])
throw Error(`${e.clientIdEnvar} could not be found!`);
if (!process.env[e.clientSecretEnvar])
throw Error(`${e.clientSecretEnvar} could not be found!`);
2023-11-05 17:41:45 -07:00
for (var path of e.paths) {
pendingSecrets.push(
2024-08-03 21:10:14 -06:00
getProjectSecrets(
p.name,
p.projectId,
e.slug,
process.env[e.clientIdEnvar],
process.env[e.clientSecretEnvar],
path,
),
2023-11-05 17:41:45 -07:00
);
}
}
2023-11-05 11:50:53 -07:00
}
2023-11-05 17:41:45 -07:00
const loadedSecrets = await Promise.all(pendingSecrets);
const filteredSecrets = loadedSecrets.flat(2).filter((s) => !s.isFallback);
2023-11-05 11:50:53 -07:00
2023-11-05 17:41:45 -07:00
const proj = projects.map((p) => ({
name: p.name,
paths: p.environments.map((envs) => envs.paths).flat(2),
}));
2023-11-05 11:50:53 -07:00
2023-11-05 17:41:45 -07:00
const projectSecrets = {};
for (var p of proj) {
projectSecrets[p.name] = {};
for (var path of p.paths) {
projectSecrets[p.name][path] = filteredSecrets.filter(
(s) => s.project === p.name && s.path === path,
);
}
mapToFile(p.name, projectSecrets[p.name]);
}