/* eslint-disable consistent-return */ import { writeFile } from 'fs' import colors from 'colors' import { Response } from './types' import { GetAWSSecret } from '../GetAWSSecret' import { getConfigFile } from '../../helpers' import { WappiConfigFile } from '../../types' let shownWarning = false export async function ServerUtils_SetAuthKeys(path: string): Promise { const { serverProvider }: WappiConfigFile = await getConfigFile() try { const AwsSecretName = process.env.AWS_AUTH_KEY_SECRET_NAME || 'authKeys' const regionName = process.env.AWS_AUTH_KEY_SECRET_REGION || 'ap-southeast-2' let secret = { current: process.env.AUTH_KEY_CURRENT, previous: process.env.AUTH_KEY_PREVIOUS, } if (serverProvider && serverProvider === 'AWS') { try { secret = await GetAWSSecret(AwsSecretName, regionName) } catch (error) { if (!shownWarning) { console.info(colors.black('\n... -.- .. .--.'), colors.grey.italic(` Unavailable AWS secret: ${AwsSecretName} Check your computers AWS credentials or add the secret to your account. Using variables from .env: AUTH_KEY_CURRENT & AUTH_KEY_PREVIOUS These variables will not be rotated and is a fallback. It is recommended that you create an AWS secret that rotates at the same rate as your authRotation setting`)) } shownWarning = true } } const data = `${secret.current}\n${secret.previous}\n` // Write data in 'Output.txt' . writeFile(path, data, (err) => { // In case of a error throw err. if (err) throw err }) return { success: true } } catch (err) { console.error(err) return { error: 'Fatal error in ServerUtils_SetAuthKeys. Investigate.', success: false, } } }