Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ci)!: modifications for externally triggered deployments and upgrades #25

Merged
merged 47 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
b3031fc
Update github actions to support mono-repo
amarouane-ABDELHAK Oct 24, 2023
a76cf2c
Remove project prefix
amarouane-ABDELHAK Oct 24, 2023
23c30ed
Get the authstack name
amarouane-ABDELHAK Oct 25, 2023
44865c9
Get the authstack name
amarouane-ABDELHAK Oct 25, 2023
54cf23b
Get the authstack name
amarouane-ABDELHAK Oct 25, 2023
59cb757
Get the authstack name
amarouane-ABDELHAK Oct 25, 2023
07581fd
Get the authstack name
amarouane-ABDELHAK Oct 25, 2023
2384db7
Try output stackname
amarouane-ABDELHAK Oct 25, 2023
f139e97
Try output stackname
amarouane-ABDELHAK Oct 25, 2023
46f1465
Try output stackname
amarouane-ABDELHAK Oct 25, 2023
ba5d28e
Try output stackname
amarouane-ABDELHAK Oct 25, 2023
a4617c3
Try output stackname
amarouane-ABDELHAK Oct 25, 2023
f3c33a6
Try output stackname
amarouane-ABDELHAK Oct 25, 2023
236f892
Add cloudformation outputs
amarouane-ABDELHAK Oct 26, 2023
2b32e2d
Add cloudformation outputs of veda auth
amarouane-ABDELHAK Oct 26, 2023
6718928
add client secret output
amarouane-ABDELHAK Oct 27, 2023
a5ae4c0
Move output to main module
amarouane-ABDELHAK Oct 30, 2023
ee5d75b
Merge branch 'main' into update-for-mono-repo
slesaad Feb 21, 2024
73f575d
Fix CfnOutput source
slesaad Feb 21, 2024
e2364cf
🤦
slesaad Feb 21, 2024
a016075
Add newline
slesaad Feb 21, 2024
393d670
Update action versions
slesaad Feb 23, 2024
7848094
Remove client secret cdk output
slesaad Feb 26, 2024
f46d058
Rename ghgc to veda
slesaad Feb 26, 2024
4f2ab4f
Remove accidental change in README
slesaad Feb 26, 2024
ad95748
fix: update aws-cdk=lib and aws_cdk.aws_cognito_identitypool_alpha ve…
Mar 4, 2024
f6adfaa
fix: update aws_cdk.aws_cognito_identitypool_alpha to use minimum ver…
Mar 4, 2024
73e108d
Merge pull request #22 from NASA-IMPACT/fix/upgrade-aws-cdk-lib-versi…
botanical Mar 4, 2024
77d2a43
Merge pull request #21 from NASA-IMPACT/update-for-mono-repo
anayeaye Mar 4, 2024
92ba2ba
ci: verbose cdk diff
anayeaye Mar 4, 2024
6a9cdc2
ci: add latest aws sdk default setting
anayeaye Mar 4, 2024
006703d
cdk context
anayeaye Mar 4, 2024
3a8c58a
ci: basic lower env deployment on push
anayeaye Mar 4, 2024
bd93406
ci: less verbose
anayeaye Mar 4, 2024
376ddc4
temp override to test workflow irl
anayeaye Mar 4, 2024
90e9879
try deduplicate programmatic client export names
anayeaye Mar 4, 2024
22e0c97
fix: cfn export programmatic client secret in app.py only
anayeaye Mar 4, 2024
541b843
fix: stop creating the unused additional workflows service client sec…
anayeaye Mar 4, 2024
3b756fe
remove temporary cicd deploy on push to fix branch
anayeaye Mar 4, 2024
2a5b152
fix(ci): run cdk diff on staging for prs into main and on dev for prs…
anayeaye Mar 4, 2024
685a940
fix pre deploy check for branch based stack
anayeaye Mar 4, 2024
e9127fa
invalid yml
anayeaye Mar 4, 2024
9332671
pr yml should use base_ref not ref; both pr and cicd updated to node 20
anayeaye Mar 4, 2024
7adb071
try add back service client secret
anayeaye Mar 5, 2024
40ab628
export sdk secret name
anayeaye Mar 5, 2024
90a1203
remove duplicate veda_ keys
anayeaye Mar 5, 2024
ed29ead
Merge pull request #24 from NASA-IMPACT/fix/ci-for-dev
anayeaye Mar 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions .github/actions/cdk-deploy/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ runs:
using: "composite"
steps:
- name: Install node and related deps
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 17.3.0
node-version: 20

- uses: actions/cache@v3
with:
Expand All @@ -30,7 +30,7 @@ runs:
run: npm install -g aws-cdk@2

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: "3.10"
cache: "pip"
Expand All @@ -49,7 +49,13 @@ runs:
working-directory: ${{ inputs.dir }}
env:
AWS_DEFAULT_REGION: us-west-2
run: ./scripts/get-env.sh ${{ inputs.env_aws_secret_name }}
run: |
if [[ -z "${{ inputs.script_path }}" ]]; then
./scripts/sync-env.sh ${{ inputs.env_aws_secret_name }}
else
python ${{ inputs.script_path }} --secret-id ${{ inputs.env_aws_secret_name }}
fi


- name: Deploy
id: deploy_auth_stack
Expand Down
69 changes: 32 additions & 37 deletions .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ permissions:
on:
push:
branches:
- make-mcp-ready
- main
- dev

jobs:
define-environment:
Expand All @@ -19,19 +20,17 @@ jobs:
run: |
if [ "${{ github.ref }}" = "refs/heads/main" ]; then
echo "env_name=staging" >> $GITHUB_OUTPUT
echo "secret_name=veda-auth-staging" >> $GITHUB_OUTPUT
echo "secret_name=veda-auth-uah-env" >> $GITHUB_OUTPUT
elif [ "${{ github.ref }}" = "refs/heads/dev" ]; then
echo "env_name=development" >> $GITHUB_OUTPUT
echo "secret_name=veda-auth-dev" >> $GITHUB_OUTPUT
elif [ "${{ github.ref }}" = "refs/heads/production" ]; then
echo "env_name=production" >> $GITHUB_OUTPUT
echo "secret_name=veda-auth-production" >> $GITHUB_OUTPUT
echo "env_name=dev" >> $GITHUB_OUTPUT
echo "secret_name=veda-auth-dev-env" >> $GITHUB_OUTPUT
fi
- name: Print the environment
run: echo "The environment is ${{ steps.define_environment.outputs.env_name }}"

outputs:
env_name: ${{ steps.define_environment.outputs.env_name }}
secret_name: ${{ steps.define_environment.outputs.secret_name }}

deploy:
name: Deploy to ${{ needs.define-environment.outputs.env_name }} 🚀
Expand All @@ -42,50 +41,46 @@ jobs:
concurrency: ${{ needs.define-environment.outputs.env_name }}

steps:
- name: Checkout
uses: actions/checkout@v3
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
lfs: "true"
submodules: "recursive"

python-version: '3.9'

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 20

- name: Configure awscli
uses: aws-actions/configure-aws-credentials@v3
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-west-2

- name: Install node and related deps
uses: actions/setup-node@v3
with:
node-version: 17.3.0


- uses: actions/cache@v3
with:
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}

- name: Install AWS CDK
shell: bash
- name: Install CDK
run: npm install -g aws-cdk@2

- name: Set up Python
uses: actions/setup-python@v4

- uses: actions/cache@v3
with:
python-version: "3.10"
cache: "pip"
cache-dependency-path: requirements.txt

path: ${{ env.pythonLocation }}
key: ${{ env.pythonLocation }}-${{ hashFiles('setup.py') }}

- name: Install python dependencies
run: |
pip install \
-r requirements.txt \

- name: Get environment configuration from aws secrets
run: ./scripts/get-env.sh ${{ needs.define-environment.outputs.secret_name }}

pip install -r requirements.txt

- name: Get environment configuration for target branch
run: |
./scripts/get-env.sh ${{ needs.define-environment.outputs.secret_name }}
- name: Deploy
env:
AWS_DEFAULT_REGION: us-west-2
CDK_DEFAULT_REGION: us-west-2
run: cdk deploy --all --require-approval never
run: |
echo $STAGE
cdk deploy --require-approval never --outputs-file ${HOME}/cdk-outputs.json
19 changes: 19 additions & 0 deletions .github/workflows/gitflow-enforcer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Gitflow enforcer 🚀

on:
pull_request:
branches:
- main
- dev
- production
types: [ opened, reopened, edited, synchronize ]

jobs:
gitflow-enforcer:
runs-on: ubuntu-latest
steps:
- name: Check branch
if: github.base_ref == 'main' && github.head_ref != 'dev' || github.base_ref == 'production' && github.head_ref != 'main'
run: |
echo "ERROR: You can only merge to main from dev and to production from main"
exit 1
33 changes: 31 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
name: Pull Request - Preview CDK Diff

permissions:
id-token: write
contents: read

on: [pull_request]

jobs:
define-environment:
name: Set ✨ environment ✨ based on the branch 🌳
runs-on: ubuntu-latest
steps:
- name: Set the environment
id: define_environment
run: |
if [ "${{ github.base_ref }}" == "main" ]; then
echo "env_name=staging" >> $GITHUB_OUTPUT
echo "secret_name=veda-auth-uah-env" >> $GITHUB_OUTPUT
elif [ "${{ github.base_ref }}" == "dev" ]; then
echo "env_name=dev" >> $GITHUB_OUTPUT
echo "secret_name=veda-auth-dev-env" >> $GITHUB_OUTPUT
fi
- name: Print the environment
run: echo "The environment is ${{ steps.define_environment.outputs.env_name }}"

outputs:
env_name: ${{ steps.define_environment.outputs.env_name }}
secret_name: ${{ steps.define_environment.outputs.secret_name }}


predeploy:
name: Pre-deploy cdk diff for ${{ needs.define-environment.outputs.env_name }} 🚀
needs: [define-environment]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -15,7 +43,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 17
node-version: 20

- name: Configure awscli
uses: aws-actions/configure-aws-credentials@v3
Expand Down Expand Up @@ -43,7 +71,8 @@ jobs:

- name: Get environment configuration for target branch
run: |
./scripts/get-env.sh "veda-auth-uah-env"
./scripts/get-env.sh ${{ needs.define-environment.outputs.secret_name }}

- name: Pre deployment CDK diff
run: |
echo $STAGE
Expand Down
12 changes: 9 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python3
import subprocess

from aws_cdk import App, Tags, DefaultStackSynthesizer
from aws_cdk import App, CfnOutput, Tags, DefaultStackSynthesizer

from infra.stack import AuthStack, BucketPermissions

Expand All @@ -11,7 +11,7 @@

stack = AuthStack(
app,
f"veda-auth-stack-{app_settings.stage}",
f"{app_settings.app_name}-{app_settings.stage}",
app_settings,
synthesizer=DefaultStackSynthesizer(
qualifier=app_settings.bootstrap_qualifier
Expand Down Expand Up @@ -98,7 +98,13 @@
)

# Programmatic Clients
stack.add_programmatic_client("veda-sdk")
client = stack.add_programmatic_client(f"{app_settings.app_name}-{app_settings.stage}-veda-sdk")
CfnOutput(
stack,
"client_id",
export_name=f"{app_settings.app_name}-{app_settings.stage}-client-id",
value=client.user_pool_client_id,
)

# Frontend Clients
# stack.add_frontend_client('veda-dashboard')
Expand Down
5 changes: 4 additions & 1 deletion cdk.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"app": "python3 app.py"
"app": "python3 app.py",
"context": {
"@aws-cdk/customresources:installLatestAwsSdkDefault": false
}
}
5 changes: 5 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@


class Config(pydantic.BaseSettings):
# App name and deployment stage
app_name: Optional[str] = pydantic.Field(
"veda-auth-stack",
description="Optional app name used to name stack and resources",
)
stage: str = pydantic.Field(
description=" ".join(
[
Expand Down
37 changes: 15 additions & 22 deletions infra/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from config import Config


class BucketPermissions(str, Enum):
read_only = "r"
read_write = "wr"
Expand Down Expand Up @@ -44,9 +43,13 @@ def __init__(
else:
self.userpool = self._create_userpool()
self.domain = self._add_domain(self.userpool)

stack_name = Stack.of(self).stack_name

CfnOutput(
self,
"userpool_id",
export_name=f"{stack_name}-userpool-id",
value=self.userpool.user_pool_id,
)
if app_settings.cognito_groups or app_settings.data_managers_group:
self._group_precedence = 0

Expand All @@ -56,7 +59,7 @@ def __init__(
)
else:
auth_provider_client = self.add_programmatic_client(
"cognito-identity-pool-auth-provider",
f"{stack_name}-identity-provider",
name="Identity Pool Authentication Provider",
)
if app_settings.data_managers_role_arn:
Expand Down Expand Up @@ -224,12 +227,6 @@ def _create_secret(
secret_string_value=SecretValue.unsafe_plain_text(json.dumps(secret_dict)),
)

CfnOutput(
self,
f"{service_id}-secret-output",
export_name=f"{stack_name}-{service_id}-secret",
value=secret.secret_name,
)
CfnOutput(
self,
f"{service_id}-secret-arn-output",
Expand Down Expand Up @@ -323,22 +320,21 @@ def add_programmatic_client(
user_pool_client_name=name or service_id,
# disable_o_auth=True,
)
cognito_sdk_secret = self._create_secret(
self._create_secret(
service_id,
{
"flow": "user_password",
"cognito_domain": self.domain.base_url(),
"client_id": client.user_pool_client_id,
"veda_client_id": client.user_pool_client_id,
"veda_userpool_id": self.userpool.user_pool_id,
"userpool_id": self.userpool.user_pool_id,
},
)
stack_name = Stack.of(self).stack_name
CfnOutput(
self,
f"cognito-sdk-{service_id}-secret",
export_name=f"{stack_name}-cognito-sdk-secret",
value=cognito_sdk_secret.secret_name,
value=f"{stack_name}/{service_id}",
)

return client
Expand All @@ -363,28 +359,25 @@ def add_service_client(
user_pool_client_name=f"{service_id} Service Access",
disable_o_auth=False,
)
# temp: we are going provide client id, secret, and user pool id values twice in the secret (once with veda_ prefix)
service_client_secret = self._get_client_secret(client)
cognito_app_secret = self._create_secret(

self._create_secret(
service_id,
{
"flow": "client_credentials",
"cognito_domain": self.domain.base_url(),
"client_id": client.user_pool_client_id,
"client_secret": service_client_secret,
"client_secret": self._get_client_secret(client),
"userpool_id": self.userpool.user_pool_id,
"veda_client_id": client.user_pool_client_id,
"veda_client_secret": service_client_secret,
"veda_userpool_id": self.userpool.user_pool_id,
"scope": " ".join(scope.scope_name for scope in scopes),
},
)

stack_name = Stack.of(self).stack_name
CfnOutput(
self,
f"cognito-app-{service_id}-secret",
export_name=f"{stack_name}-cognito-app-secret",
value=cognito_app_secret.secret_name,
value=f"{stack_name}/{service_id}",
)

return client
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
aws-cdk-lib==2.35.0
aws_cdk.aws_cognito_identitypool_alpha==2.35.0a0
aws-cdk-lib==2.112.0
aws_cdk.aws_cognito_identitypool_alpha>=2.112.0a0
constructs>=10.0.0,<11.0.0
pydantic==1.9.1
black==22.3.0
Expand Down
Loading