Skip to content

Commit 4797bbf

Browse files
authored
chore: run pre-commit (#290)
* chore: run pre-commit * chore: ensure files are linted * chore: verified pr workflow * chore: rename pre-commit group
1 parent 11b5676 commit 4797bbf

25 files changed

Lines changed: 213 additions & 164 deletions

File tree

.docs/architecture.excalidraw.svg

Lines changed: 1 addition & 1 deletion
Loading

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ updates:
5757
schedule:
5858
interval: weekly
5959
groups:
60-
github-actions:
60+
pre-commit:
6161
patterns:
6262
- "*"
6363
commit-message:

.github/workflows/deploy.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ on:
1414
description: "Environment to deploy to"
1515
type: environment
1616

17-
permissions:
18-
id-token: write # Required for OIDC authentication w/ AWS
19-
contents: read
20-
2117
concurrency:
2218
group: ${{ inputs.environment }}
2319
cancel-in-progress: false
@@ -26,8 +22,11 @@ run-name: Deploy to ${{ inputs.environment }} environment by @${{ github.actor }
2622

2723
jobs:
2824
deploy:
25+
permissions:
26+
contents: read
27+
id-token: write # Required for OIDC authentication w/ AWS
2928
runs-on: ubuntu-latest
30-
environment:
29+
environment:
3130
name: ${{ inputs.environment }}
3231
url: ${{ vars.HOSTNAME }}
3332

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,33 @@ name: Diff CDK Stack
33
on:
44
pull_request:
55

6-
permissions:
7-
id-token: write # Required for OIDC authentication w/ AWS
8-
contents: read
9-
106
run-name: Diff ${{ github.ref }}
117

128
jobs:
9+
lint:
10+
runs-on: ubuntu-latest
11+
environment: dev
12+
permissions:
13+
contents: read
14+
15+
steps:
16+
- name: Checkout the repository
17+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7.0.1
18+
19+
- name: Setup UV
20+
uses: astral-sh/setup-uv@ae62891fec2bb8e7d6c99fc78c9fec3a63790f8d # v10.0.0
21+
22+
- name: Install dependencies
23+
run: |
24+
uv sync --locked
25+
26+
- name: Run pre-commit
27+
run: |
28+
uv run pre-commit run --all-files
1329
diff:
1430
runs-on: ubuntu-latest
1531
environment: dev
32+
needs: [lint]
1633
permissions:
1734
contents: read
1835
pull-requests: write

.github/workflows/scorecard.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ on:
77
schedule:
88
- cron: "30 16 * * 1" # Monday 10:30/11:30 CT
99

10-
permissions: read-all # Default all to readonly
11-
1210
jobs:
1311
analysis:
1412
name: Scorecard analysis
1513
runs-on: ubuntu-latest
1614
permissions:
15+
contents: read
1716
security-events: write # Allow upload to Security dashboard
1817
id-token: write # Access GitHub's OIDC token to verify authenticity of result for publish
1918

README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ For a private client (ie a client that runs within the frontend, such as single
5151

5252
The generated client secret will be stored in AWS Secrets Manager in the same account & region as the deployment. The name of this secret will follow the following convention: `veda-keycloak-$stage-client-$clientId` (eg a Grafana client in the production deployment will generate the following secret: `veda-keycloak-prod-client-grafana`). The generated secret contains the following information:
5353

54-
* `id`: OAuth Client ID
55-
* `secret`: OAuth Client Secret
56-
* `auth_url`: URL of the OAuth [authorization endpoint](https://datatracker.ietf.org/doc/html/rfc6749#section-3.1)
57-
* `token_url`: URL of the OAuth [token endpoint](https://datatracker.ietf.org/doc/html/rfc6749#section-3.2)
58-
* `userinfo_url`: URL of the OIDC [user info endpoint](https://openid.net/specs/openid-connect-core-1_0.html#UserInfo)
54+
- `id`: OAuth Client ID
55+
- `secret`: OAuth Client Secret
56+
- `auth_url`: URL of the OAuth [authorization endpoint](https://datatracker.ietf.org/doc/html/rfc6749#section-3.1)
57+
- `token_url`: URL of the OAuth [token endpoint](https://datatracker.ietf.org/doc/html/rfc6749#section-3.2)
58+
- `userinfo_url`: URL of the OIDC [user info endpoint](https://openid.net/specs/openid-connect-core-1_0.html#UserInfo)
5959

6060
A minimum example of a private client (note `publicClient: false` and `secret`):
6161

@@ -78,7 +78,6 @@ clients:
7878

7979
<summary>Consider also using an environment variable for URLs for greater flexibility</summary>
8080

81-
8281
```yaml
8382
clients:
8483
- clientId: grafana
@@ -458,14 +457,13 @@ VEDA Keycloak includes a custom `EmailSenderProvider` based on Keycloak’s `Def
458457
> [!TIP]
459458
> See the Service Provider Interfaces section in the [Server Developer Guide](https://www.keycloak.org/docs/latest/server_development/#_providers) for more details about how to create custom themes.
460459

461-
462-
463460
### Themes
464461

465462
> [!TIP]
466463
> See the theme section in the [Server Developer Guide](https://www.keycloak.org/docs/latest/server_development/#_themes) for more details about how to create custom themes.
467464

468465
### SES Relay
466+
469467
The AWS account that includes the SES `openveda.cloud` identity does not permit creating SMTP credentials for AWS SES for security reasons. However, Keycloak expects to talk to an SMTP server for sending transactional emails such as verification, password reset, and notification messages.
470468

471469
To bridge this gap, we deploy a small SMTP relay service as an ECS Fargate service into the same VPC as Keycloak:

bin/apply-config.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
#!/usr/bin/env python3
22

33
"""
4-
This script invokes a Lambda function to apply ECS configuration changes, waits for the ECS task
5-
to finish, and fetches its logs from CloudWatch Logs.
4+
This script invokes a Lambda function to apply ECS configuration changes,
5+
waits for the ECS task to finish, and fetches its logs from CloudWatch Logs.
66
77
Usage:
88
python apply_config.py <lambdaArn> <configEnvironmentJson>
99
1010
Example:
11-
python apply_config.py arn:aws:lambda:us-east-1:123456789012:function:applyConfig '{"key":"value"}'
11+
python apply_config.py \
12+
arn:aws:lambda:us-east-1:123456789012:function:applyConfig '{"key":"value"}'
1213
"""
1314

14-
import sys
1515
import json
16+
import sys
1617
import time
1718
import traceback
19+
1820
import boto3
1921

2022

cdk/app.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
#!/usr/bin/env -S uv run --script
2-
import os
32
import logging
3+
import os
44

55
from aws_cdk import (
66
App,
77
DefaultStackSynthesizer,
88
PermissionsBoundary,
99
)
10-
1110
from lib.keycloak import KeycloakStack
12-
from lib.utils import get_oauth_secrets, get_private_client_ids, get_send_email_addresses, get_application_role_arns
1311
from lib.settings import Settings
12+
from lib.utils import (
13+
get_application_role_arns,
14+
get_oauth_secrets,
15+
get_private_client_ids,
16+
get_send_email_addresses,
17+
)
1418

1519
logging.basicConfig(
1620
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
@@ -43,7 +47,7 @@
4347
"No private client IDs found in %s",
4448
settings.keycloak_config_cli_config_dir,
4549
)
46-
50+
4751
send_email_addresses = get_send_email_addresses()
4852
if send_email_addresses:
4953
logging.info(
@@ -57,7 +61,9 @@
5761
if application_role_arns:
5862
logging.info(
5963
"Found application role ARNs in environment: %s",
60-
", ".join(f"{key}: {', '.join(arns)}" for key, arns in application_role_arns.items()),
64+
", ".join(
65+
f"{key}: {', '.join(arns)}" for key, arns in application_role_arns.items()
66+
),
6167
)
6268
else:
6369
logging.warning("No application role ARNs found in the environment.")

cdk/lib/keycloak/config.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22
import textwrap
33

44
from aws_cdk import (
5-
Duration,
65
CfnOutput,
6+
Duration,
77
Stack,
88
aws_ecr_assets as ecr_assets,
99
aws_ecs as ecs,
1010
aws_iam as iam,
11-
aws_lambda as _lambda,
1211
aws_kms as kms,
12+
aws_lambda as _lambda,
1313
aws_secretsmanager as secretsmanager,
1414
)
1515
from constructs import Construct
1616

1717

1818
class KeycloakConfig(Construct):
1919
"""
20-
Responsible for creating infrastructure to apply configuration to a Keycloak instance.
20+
Responsible for creating infrastructure
21+
to apply configuration to a Keycloak instance.
2122
"""
2223

2324
def __init__(
@@ -57,7 +58,7 @@ def __init__(
5758
secret = secretsmanager.Secret(
5859
self,
5960
f"{client_slug}-client-secret",
60-
# WARNING: Changing this construct (name, id, template) will cause new client
61+
# WARNING: Changing construct (name, id, template) will cause new client
6162
# secrets to be generated!
6263
secret_name=f"{Stack.of(self).stack_name}-client-{client_slug}",
6364
encryption_key=kms_key,
@@ -67,9 +68,15 @@ def __init__(
6768
secret_string_template=json.dumps(
6869
{
6970
"id": client_slug,
70-
"auth_url": f"{hostname}/realms/{realm}/protocol/openid-connect/auth",
71-
"token_url": f"{hostname}/realms/{realm}/protocol/openid-connect/token",
72-
"userinfo_url": f"{hostname}/realms/{realm}/protocol/openid-connect/userinfo",
71+
"auth_url": (
72+
f"{hostname}/realms/{realm}/protocol/openid-connect/auth",
73+
),
74+
"token_url": (
75+
f"{hostname}/realms/{realm}/protocol/openid-connect/token"
76+
),
77+
"userinfo_url": (
78+
f"{hostname}/realms/{realm}/protocol/openid-connect/userinfo"
79+
),
7380
},
7481
separators=(",", ":"),
7582
),
@@ -81,16 +88,19 @@ def __init__(
8188
secret.add_to_resource_policy(
8289
iam.PolicyStatement(
8390
effect=iam.Effect.ALLOW,
84-
principals=[iam.ArnPrincipal(arn) for arn in application_role_arn],
91+
principals=[
92+
iam.ArnPrincipal(arn) for arn in application_role_arn
93+
],
8594
actions=["secretsmanager:GetSecretValue"],
8695
resources=["*"],
87-
8896
)
8997
)
9098
kms_key.add_to_resource_policy(
9199
iam.PolicyStatement(
92100
effect=iam.Effect.ALLOW,
93-
principals=[iam.ArnPrincipal(arn) for arn in application_role_arn],
101+
principals=[
102+
iam.ArnPrincipal(arn) for arn in application_role_arn
103+
],
94104
actions=["kms:Decrypt", "kms:DescribeKey"],
95105
resources=["*"],
96106
)
@@ -105,7 +115,8 @@ def __init__(
105115
)
106116
imported_client_secrets.append((client_slug, imported_secret))
107117

108-
# Create env vars from secrets for each client, e.g. GRAFANA_CLIENT_ID, GRAFANA_CLIENT_SECRET
118+
# Create env vars from secrets for each client
119+
# e.g. GRAFANA_CLIENT_ID, GRAFANA_CLIENT_SECRET
109120
task_client_secrets = {}
110121
for client_slug, secret in created_client_secrets + imported_client_secrets:
111122
for key in ["id", "secret"]:
@@ -163,10 +174,12 @@ def __init__(
163174
containerOverrides: [
164175
{{
165176
name: "{container_name}",
166-
environment: Object.entries(event).map(([name, value]) => ({{
167-
name,
168-
value: String(value),
169-
}})),
177+
environment: Object.entries(event).map(
178+
([name, value]) => ({{
179+
name,
180+
value: String(value),
181+
}})
182+
),
170183
}},
171184
],
172185
}},

cdk/lib/keycloak/database.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
from constructs import Construct
21
from aws_cdk import (
32
RemovalPolicy,
4-
aws_rds as rds,
53
aws_ec2 as ec2,
4+
aws_rds as rds,
65
)
6+
from constructs import Construct
77

88

99
class KeycloakDatabase(Construct):
10-
1110
def __init__(
1211
self,
1312
scope: Construct,
@@ -27,7 +26,8 @@ def __init__(
2726
:param database_name: Name of the database to create
2827
:param instance_identifier: Optional identifier for the RDS instance
2928
:param is_production: Whether the database is in production
30-
:param kwargs: Additional DatabaseInstanceProps (except 'engine', which is set to Postgres)
29+
:param kwargs: Additional DatabaseInstanceProps
30+
(except 'engine', which is set to Postgres)
3131
"""
3232
super().__init__(scope, construct_id)
3333

0 commit comments

Comments
 (0)