Skip to content

Commit

Permalink
feat: add admin user generation to deploy script
Browse files Browse the repository at this point in the history
  • Loading branch information
tschoffelen committed Oct 6, 2024
1 parent dda3513 commit e536de4
Show file tree
Hide file tree
Showing 5 changed files with 944 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/deploy-script/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
],
"dependencies": {
"@aws-sdk/client-apigatewayv2": "^3.645.0",
"@aws-sdk/client-dynamodb": "^3.665.0",
"@aws-sdk/lib-dynamodb": "^3.665.0",
"bcryptjs": "^2.4.3",
"boxen": "^8.0.1",
"chalk": "^5.3.0",
"degit": "^2.8.4",
Expand Down
12 changes: 9 additions & 3 deletions packages/deploy-script/src/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ApiGatewayV2Client,
GetApisCommand,
} from "@aws-sdk/client-apigatewayv2";
import { checkHasUsers, createAdminUser } from "./users";

const exec = (command, options = {}) => {
const child = child_process.exec(command, {
Expand Down Expand Up @@ -85,8 +86,13 @@ const deploy = async (answers) => {
await exec("yarn deploy", { cwd: tmpPath });

// Create user
console.log(chalk.blue("Creating user..."));
// TODO: create user account in DDB
let adminPassword;
if (await checkHasUsers()) {
console.log(chalk.blue("User already exists"));
} else {
console.log(chalk.blue("Creating user..."));
adminPassword = await createAdminUser();
}

// Run auto-trace
console.log(chalk.blue("Auto tracing lambdas..."));
Expand All @@ -99,7 +105,7 @@ const deploy = async (answers) => {
});
} catch (e) {}

return { endpoint };
return { endpoint, adminPassword };
};

export default deploy;
4 changes: 2 additions & 2 deletions packages/deploy-script/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const questions = [
];
const answers = await inquirer.prompt(questions);

const { endpoint } = await deploy({
const { endpoint, adminPassword } = await deploy({
...answers,
tracerToken,
});
Expand All @@ -68,7 +68,7 @@ const domain = answers.CUSTOM_DOMAIN || endpoint;
console.log(
"\n\n" +
boxen(
`${chalk.green("Done!")} You can now access your TraceStack instance at \n${chalk.underline(chalk.bold(`https://${domain}`))}`,
`${chalk.green("Done!")} You can now access your TraceStack instance at \n${chalk.underline(chalk.bold(`https://${domain}`))}${adminPassword ? `\n\Username: admin\nPassword: ${adminPassword}` : ""}`,
{ padding: 1, borderStyle: "round" },
),
);
57 changes: 57 additions & 0 deletions packages/deploy-script/src/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import crypto from "crypto";
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import {
DeleteCommand,
DynamoDBDocumentClient,
PutCommand,
QueryCommand,
UpdateCommand,
} from "@aws-sdk/lib-dynamodb";
import bcryptjs from "bcryptjs";

const translateConfig = {
marshallOptions: {
convertEmptyValues: false,
},
};

const dynamo = DynamoDBDocumentClient.from(
new DynamoDBClient(),
translateConfig,
);

export const checkHasUsers = async () => {
const { Items } = await dynamo.send(
new QueryCommand({
TableName: "trace-stack-dev",
KeyConditionExpression: "#type = :type",
ExpressionAttributeNames: {
"#type": "type",
},
ExpressionAttributeValues: {
":type": "user",
},
IndexName: "type-sk",
}),
);

return !!Items?.length;
};

export const createAdminUser = async () => {
const randomPassword = crypto.randomBytes(20).toString("hex");
await dynamo.send(
new PutCommand({
TableName: "trace-stack-dev",
Item: {
pk: "user#admin",
sk: "user#admin",
type: "user",
name: "admin",
passwordHash: bcryptjs.hashSync(randomPassword),
},
}),
);

return randomPassword;
};
Loading

0 comments on commit e536de4

Please sign in to comment.