"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSecretFromArn = exports.loadEnvVars = void 0;
const axios_1 = __importDefault(require("axios"));
const envs_1 = require("../envs");
const isSecretManagerArn = (value) => {
    return /^arn:aws:secretsmanager/.test(value);
};
const loadEnvVars = async (envVars) => {
    const environmentVariables = {};
    const missingVariables = [];
    for (const varName of Object.values(envVars)) {
        const name = varName;
        const value = process.env[name];
        if (!value) {
            missingVariables.push(name);
        }
        else {
            environmentVariables[name] = value;
        }
    }
    if (missingVariables.length) {
        throw Error(`Missing env vars ${missingVariables.join(', ')}`);
    }
    for (const [name, value] of Object.entries(environmentVariables)) {
        if (isSecretManagerArn(value)) {
            const secret = await (0, exports.getSecretFromArn)(value);
            environmentVariables[name] = secret;
        }
    }
    return environmentVariables;
};
exports.loadEnvVars = loadEnvVars;
const getSecretFromArn = async (secretsArn) => {
    const AWS_SESSION_TOKEN = (0, envs_1.maybeGetEnvParam)('AWS_SESSION_TOKEN');
    const getSecretUrl = `http://localhost:2773/secretsmanager/get?secretId=${secretsArn}`;
    const getSecretResult = await axios_1.default.get(getSecretUrl, {
        headers: {
            "X-Aws-Parameters-Secrets-Token": AWS_SESSION_TOKEN
        }
    });
    return getSecretResult.data.SecretString;
};
exports.getSecretFromArn = getSecretFromArn;
//# sourceMappingURL=secrets-manager.js.map