diff --git a/.github/workflows/seed-admin.yml b/.github/workflows/seed-admin.yml new file mode 100644 index 0000000..40204bb --- /dev/null +++ b/.github/workflows/seed-admin.yml @@ -0,0 +1,107 @@ +name: Seed Admin + +# Manual, one-off: creates the first Admin account against the deployed +# stage. Admins are intentionally NOT reachable through /auth/signup (see +# docs/decisions.md) — this workflow is the sanctioned way to create one, +# using the same AWS credentials the Deploy workflow already has, so no +# local AWS CLI setup is needed. Every subsequent Admin can be promoted by +# an existing Admin directly in Cognito/DynamoDB. +on: + workflow_dispatch: + inputs: + email: + description: "Admin email address" + required: true + name: + description: "Admin display name" + required: true + password: + description: "Admin password (min 8 chars, upper+lower+digit)" + required: true + +permissions: + contents: read + +env: + AWS_REGION: ${{ vars.AWS_REGION || 'eu-west-1' }} + STAGE: ${{ vars.STAGE || 'dev' }} + +jobs: + seed-admin: + name: seed-admin + runs-on: ubuntu-latest + steps: + # Belt-and-suspenders: workflow_dispatch inputs aren't secrets, but + # masking keeps the password out of the step logs regardless. + - name: Mask password in logs + run: echo "::add-mask::${{ inputs.password }}" + + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up pnpm + uses: pnpm/action-setup@v4 + with: + version: 10.7.1 + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: 20 + cache: pnpm + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ env.AWS_REGION }} + + - name: Resolve resource identifiers from stack outputs + id: resolve + run: | + user_pool_id=$(aws cloudformation describe-stacks \ + --stack-name "EventTicketing-${STAGE}-Api" \ + --query "Stacks[0].Outputs[?OutputKey=='UserPoolId'].OutputValue" \ + --output text) + if [ -z "$user_pool_id" ] || [ "$user_pool_id" = "None" ]; then + echo "::error::Could not resolve UserPoolId from EventTicketing-${STAGE}-Api outputs — has Deploy run successfully for this stage?" + exit 1 + fi + echo "user_pool_id=$user_pool_id" >> "$GITHUB_OUTPUT" + + users_table_name=$(aws cloudformation describe-stacks \ + --stack-name "EventTicketing-${STAGE}-Database" \ + --query "Stacks[0].Outputs[?OutputKey=='UsersTableName'].OutputValue" \ + --output text) + if [ -z "$users_table_name" ] || [ "$users_table_name" = "None" ]; then + echo "::error::Could not resolve UsersTableName from EventTicketing-${STAGE}-Database outputs — has Deploy run successfully for this stage?" + exit 1 + fi + echo "users_table_name=$users_table_name" >> "$GITHUB_OUTPUT" + + - name: Run seed-admin + env: + USER_POOL_ID: ${{ steps.resolve.outputs.user_pool_id }} + USERS_TABLE_NAME: ${{ steps.resolve.outputs.users_table_name }} + ADMIN_EMAIL: ${{ inputs.email }} + ADMIN_NAME: ${{ inputs.name }} + ADMIN_PASSWORD: ${{ inputs.password }} + run: | + pnpm run seed:admin -- \ + --email "$ADMIN_EMAIL" \ + --name "$ADMIN_NAME" \ + --password "$ADMIN_PASSWORD" + + - name: Summary + if: success() + run: | + { + echo "### Admin account created" + echo "- Stage: ${STAGE}" + echo "- Email: ${{ inputs.email }}" + echo "- They can now sign in via POST /auth/login as an Admin." + } >> "$GITHUB_STEP_SUMMARY" diff --git a/README.md b/README.md index d3d5f6d..207d8f8 100644 --- a/README.md +++ b/README.md @@ -143,12 +143,24 @@ starts in sandbox mode: ### Creating the first Admin Admins are never self-service (see -[docs/decisions.md](docs/decisions.md#auth-amazon-cognito-over-hand-rolled-jwt)). -After the `Auth` and `Database` stacks deploy, grab their outputs and run: +[docs/decisions.md](docs/decisions.md#auth-amazon-cognito-over-hand-rolled-jwt)) +and the `Deploy` workflow never creates one automatically — after a fresh +deploy, no Admin account exists until you explicitly create one. + +**Recommended**: run the **[Seed Admin](../../actions/workflows/seed-admin.yml)** +GitHub Actions workflow (`.github/workflows/seed-admin.yml`) — +`Actions → Seed Admin → Run workflow`, fill in email/name/password. It +resolves `UserPoolId`/`UsersTableName` from the deployed stacks' outputs +itself and uses the same AWS credentials `Deploy` already has, so no local +AWS setup is needed. + +Alternatively, run it locally once you have AWS CLI credentials configured +and the stack outputs (`aws cloudformation describe-stacks --stack-name +EventTicketing--Api` / `...-Database`): ```bash -USER_POOL_ID= \ -USERS_TABLE_NAME= \ +USER_POOL_ID= \ +USERS_TABLE_NAME= \ AWS_REGION= \ pnpm run seed:admin -- --email admin@example.com --name "Site Admin" --password 'Str0ngP@ssw0rd' ``` diff --git a/infrastructure/lib/stacks/database-stack.ts b/infrastructure/lib/stacks/database-stack.ts index 47350f3..ddf9dc4 100644 --- a/infrastructure/lib/stacks/database-stack.ts +++ b/infrastructure/lib/stacks/database-stack.ts @@ -1,4 +1,4 @@ -import { RemovalPolicy, Stack, StackProps } from 'aws-cdk-lib'; +import { CfnOutput, RemovalPolicy, Stack, StackProps } from 'aws-cdk-lib'; import { AttributeType, BillingMode, ProjectionType, Table, TableEncryption } from 'aws-cdk-lib/aws-dynamodb'; import { Construct } from 'constructs'; import { StageConfig } from '../config/environment'; @@ -72,5 +72,10 @@ export class DatabaseStack extends Stack { sortKey: { name: 'createdAt', type: AttributeType.STRING }, projectionType: ProjectionType.ALL, }); + + // Consumed by the Seed Admin workflow (.github/workflows/seed-admin.yml) + // so it resolves the table name from the stack instead of duplicating + // the naming convention above. + new CfnOutput(this, 'UsersTableName', { value: this.usersTable.tableName }); } }