Skip to content

Commit

Permalink
fix(secrets): store server address on fs
Browse files Browse the repository at this point in the history
  • Loading branch information
masontikhonov committed Jul 3, 2024
1 parent 1e24d5b commit cb4b05b
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 65 deletions.
24 changes: 9 additions & 15 deletions lib/addNewMask.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
const { secretsServerAddress } = require('./logger');
const { getServerAddress } = require('./helpers');

async function updateMasks(secret) {
try {
const serverAddress = await secretsServerAddress;
console.log(`server address: ${serverAddress}`);
const serverAddress = await getServerAddress();
console.debug(`server address: ${serverAddress}`);
const url = new URL('secrets', serverAddress);

const { default: got } = await import('got');
/** @type {import('got').Got} */
const httpClient = got.extend({
prefixUrl: serverAddress,
responseType: 'json',
throwHttpErrors: false,
const { default: httpClient } = await import('got');
const response = await httpClient.post(url, {
json: secret,
});
const response = await httpClient.post(
'secrets',
{ json: secret },
);

if (!response.ok) {
if (response.statusCode !== 201) {
console.error(`could not create mask for secret: ${secret.key}, because server responded with: ${response.statusCode}\n\n${JSON.stringify(response.body)}`);
process.exit(1);
}
console.log(`successfully updated masks with secret: ${secret.key}`);
process.exit(0);
} catch (error) {
console.log(`could not create mask for secret: ${secret.key}, due to error: ${error}`);
console.error(`could not create mask for secret: ${secret.key}, due to error: ${error}`);
process.exit(1);
}
}
Expand Down
5 changes: 5 additions & 0 deletions lib/const.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const serverAddressPath = '/root/cf-runtime/SERVER_ADDRESS';

module.exports = {
serverAddressPath,
};
24 changes: 23 additions & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const { stat } = require('fs/promises');
const { stat, writeFile, readFile } = require('fs/promises');
const path = require('path');
const logger = require('cf-logs').Logger('codefresh:containerLogger');
const getPromiseWithResolvers = require('core-js-pure/es/promise/with-resolvers');
const { BuildFinishedSignalFilename } = require('./enums');
const { serverAddressPath } = require('./const');

const checkFileInterval = 1000;

Expand Down Expand Up @@ -34,11 +35,32 @@ function watchForBuildFinishedSignal() {
return deferred.promise;
}

const persistServerAddress = async (serverAddress) => {
try {
await writeFile(serverAddressPath, serverAddress, { encoding: 'utf8' });
} catch (error) {
logger.error(`Failed to persist server address: ${error}`);
throw error;
}
};

const getServerAddress = async () => {
try {
const serverAddress = await readFile(serverAddressPath, { encoding: 'utf8' });
return serverAddress;
} catch (error) {
logger.error(`Failed to read server address: ${error}`);
throw error;
}
};

module.exports = {
/**
* Polyfill of `Promise.withResolvers`, TC39 Stage 4 proposal.
* @see https://github.com/tc39/proposal-promise-with-resolvers
*/
getPromiseWithResolvers,
watchForBuildFinishedSignal,
persistServerAddress,
getServerAddress,
};
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path');
const path = require('node:path');
const cflogs = require('cf-logs');

const loggerOptions = {
Expand All @@ -12,7 +12,7 @@ const loggerOptions = {
};
cflogs.init(loggerOptions);

const { Logger } = require('./logger');
const Logger = require('./logger');
const { watchForBuildFinishedSignal } = require('./helpers');

const taskLoggerConfig = JSON.parse(process.env.TASK_LOGGER_CONFIG);
Expand Down
12 changes: 3 additions & 9 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ const { ContainerStatus } = require('./enums');
const { LoggerStrategy } = require('./enums');
const { ContainerHandlingStatus } = require('./enums');
const ContainerLogger = require('./ContainerLogger');
const { getPromiseWithResolvers } = require('./helpers');
const { getPromiseWithResolvers, persistServerAddress } = require('./helpers');

const initialState = {
pid: process.pid, status: 'init', lastLogsDate: new Date(), failedHealthChecks: [], restartCounter: 0, containers: {}
};

const deferredServerAddress = getPromiseWithResolvers();

class Logger {

constructor({
Expand Down Expand Up @@ -384,11 +382,10 @@ class Logger {
});

const address = await server.listen({ host, port });
deferredServerAddress.resolve(address);
await persistServerAddress(address);
logger.info(`listening for engine updates on ${address}`);
} catch (error) {
logger.error(`could not start server for engine updates due to error: ${error}`);
deferredServerAddress.reject(error);
throw error;
}
}
Expand Down Expand Up @@ -432,7 +429,4 @@ class Logger {
}
}

module.exports = {
Logger,
secretsServerAddress: deferredServerAddress.promise,
};
module.exports = Logger;
Loading

0 comments on commit cb4b05b

Please sign in to comment.