Skip to content

Commit 0e095e7

Browse files
authored
changes to secure dbt docs using microsoft sso (#31)
* changes to secure dbt docs using microsoft sso * updates to dbt sso modules * dynamic temp dir * added support for multiple static sites * changes to fix lambda deployment
1 parent 9e4666f commit 0e095e7

File tree

22 files changed

+1115
-1
lines changed

22 files changed

+1115
-1
lines changed

.gitignore

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,4 +593,10 @@ payload.zip
593593
*.crt
594594
*.cert
595595
*.key
596-
*.ovpn
596+
*.ovpn
597+
598+
# NPM
599+
package-lock.json
600+
.node_install*
601+
temp/
602+
artifacts/
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Microsoft Entra ID SSO for CloudFront
2+
3+
A Terraform module to implement Microsoft Entra ID (Azure AD) authentication for CloudFront static websites using Lambda@Edge.
4+
5+
## Architecture
6+
7+
![Authentication Flow](auth-flow-diagram.svg)
8+
9+
## How It Works
10+
11+
1. User requests protected content from CloudFront
12+
2. Auth Lambda checks for valid session cookie
13+
3. If no valid session, user is redirected to Microsoft login
14+
4. User authenticates with Microsoft Entra ID
15+
5. Microsoft returns authorization code to callback URL
16+
6. Callback Lambda exchanges code for access and ID tokens
17+
7. Microsoft returns tokens to Callback Lambda
18+
8. Callback Lambda sets secure cookie and redirects to original URL
19+
9. Authenticated user receives protected content
20+
21+
## Quick Start
22+
23+
### 1. Register Microsoft Entra ID Application
24+
25+
1. Create app registration in Microsoft Entra ID / Azure AD
26+
2. Set redirect URL to `https://your-domain.com/callback`
27+
3. Note the client ID, tenant ID, and create a client secret
28+
29+
### 2. Store Credentials
30+
31+
Create a secret in AWS Secrets Manager:
32+
33+
```json
34+
{
35+
"tenant": "your-tenant-id",
36+
"client_id": "your-client-id",
37+
"client_secret": "your-client-secret",
38+
"redirect_uri": "https://your-domain.com/callback"
39+
}
40+
```
41+
42+
### 3. Deploy Module
43+
44+
```hcl
45+
module "sso_auth" {
46+
source = "path/to/cloudfront-microsoft-sso"
47+
48+
name_prefix = "example"
49+
app_code = "docs"
50+
lambda_runtime = "nodejs18.x"
51+
sso_config_arn = aws_secretsmanager_secret.sso_config.arn
52+
}
53+
```
54+
55+
### 4. Configure CloudFront
56+
57+
```hcl
58+
resource "aws_cloudfront_distribution" "distribution" {
59+
# ... other configuration ...
60+
61+
default_cache_behavior {
62+
# ... other settings ...
63+
64+
lambda_function_association {
65+
event_type = "viewer-request"
66+
lambda_arn = module.sso_auth.authenticator_lambda_arn
67+
include_body = false
68+
}
69+
}
70+
71+
ordered_cache_behavior {
72+
path_pattern = "/callback*"
73+
# ... other settings ...
74+
75+
lambda_function_association {
76+
event_type = "viewer-request"
77+
lambda_arn = module.sso_auth.callback_lambda_arn
78+
include_body = false
79+
}
80+
}
81+
}
82+
```
83+
84+
## Module Inputs
85+
86+
| Name | Description | Type | Required |
87+
|------|-------------|------|----------|
88+
| name_prefix | Prefix for resource names | string | Yes |
89+
| app_code | Application identifier | string | Yes |
90+
| lambda_runtime | Node.js runtime | string | Yes |
91+
| sso_config_arn | Secret ARN | string | Yes |
92+
93+
## Module Outputs
94+
95+
| Name | Description |
96+
|------|-------------|
97+
| authenticator_lambda_arn | Authenticator Lambda ARN |
98+
| callback_lambda_arn | Callback Lambda ARN |
99+
100+
## Troubleshooting
101+
102+
- **403 Errors**: Check IAM permissions and Lambda execution role
103+
- **Redirect Issues**: Verify redirect URI matches exactly across all configurations
104+
- **Cookie Problems**: Ensure CloudFront distribution and cookie domain match
105+
106+
## Security Features
107+
108+
- HTTP-only secure cookies
109+
- Credentials stored in AWS Secrets Manager
110+
- Edge authentication with Lambda@Edge
111+
- No client-side credential exposure
Lines changed: 119 additions & 0 deletions
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
resource_name_prefix = var.use_env_code_flag ? "${lower(var.env_code)}-${lower(var.project_code)}" : "${lower(var.project_code)}"
3+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# dbt docs
2+
module "dbt_sso_auth" {
3+
source = "../modules/cloudfront-microsoft-sso"
4+
5+
name_prefix = local.resource_name_prefix
6+
app_code = "dbt-docs"
7+
enable_auth_flag = true
8+
9+
lambda_runtime = "nodejs18.x"
10+
sso_config_arn = aws_secretsmanager_secret.dbt_sso_config.arn
11+
}
12+
13+
# elementary data
14+
module "elementary_sso_auth" {
15+
source = "../modules/cloudfront-microsoft-sso"
16+
17+
name_prefix = local.resource_name_prefix
18+
app_code = "elementary-data"
19+
enable_auth_flag = true
20+
21+
lambda_runtime = "nodejs18.x"
22+
sso_config_arn = aws_secretsmanager_secret.elementary_sso_config.arn
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
output "dbt__cloudfront_distribution__domain_name" {
2+
value = module.dbt_sso_auth.cloudfront_distribution__domain_name
3+
}
4+
5+
output "dbt__aws_s3_bucket__arn" {
6+
value = module.dbt_sso_auth.aws_s3_bucket__arn
7+
}
8+
9+
output "dbt__secret_arn" {
10+
description = "The ARN of the SSO secret"
11+
value = aws_secretsmanager_secret.dbt_sso_config.arn
12+
}
13+
14+
output "elementary__cloudfront_distribution__domain_name" {
15+
value = module.elementary_sso_auth.cloudfront_distribution__domain_name
16+
}
17+
18+
output "elementary__aws_s3_bucket__arn" {
19+
value = module.elementary_sso_auth.aws_s3_bucket__arn
20+
}
21+
22+
output "elementary__secret_arn" {
23+
description = "The ARN of the SSO secret"
24+
value = aws_secretsmanager_secret.elementary_sso_config.arn
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "4.59.0"
6+
}
7+
}
8+
}
9+
10+
provider "aws" {
11+
region = var.aws_region
12+
profile = "terraform"
13+
14+
default_tags {
15+
tags = {
16+
"environment" = "${lower(var.env_code)}"
17+
"created_by" = "terraform"
18+
}
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
resource "aws_secretsmanager_secret" "dbt_sso_config" {
2+
name = "dbt-sso-secret"
3+
description = "SSO config (tenant, client_id, client_secret, redirect_uri)"
4+
recovery_window_in_days = 0
5+
}
6+
7+
resource "aws_secretsmanager_secret_version" "dbt_sso_config_version" {
8+
secret_id = aws_secretsmanager_secret.dbt_sso_config.id
9+
secret_string = jsonencode({
10+
tenant = var.dbt_sso_tenant_id
11+
client_id = var.dbt_sso_client_id
12+
client_secret = var.dbt_sso_client_secret
13+
redirect_uri = var.dbt_sso_redirect_uri
14+
})
15+
}
16+
17+
resource "aws_secretsmanager_secret" "elementary_sso_config" {
18+
name = "elementary-sso-secret"
19+
description = "SSO config (tenant, client_id, client_secret, redirect_uri)"
20+
recovery_window_in_days = 0
21+
}
22+
23+
resource "aws_secretsmanager_secret_version" "elementary_sso_config_version" {
24+
secret_id = aws_secretsmanager_secret.elementary_sso_config.id
25+
secret_string = jsonencode({
26+
tenant = var.dbt_sso_tenant_id
27+
client_id = var.dbt_sso_client_id
28+
client_secret = var.dbt_sso_client_secret
29+
redirect_uri = var.elementary_sso_redirect_uri
30+
})
31+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
env_code = "dev"
2+
project_code = "entechlog"
3+
aws_region = "us-east-1"
4+
use_env_code_flag = true
5+
6+
dbt_sso_tenant_id = ""
7+
dbt_sso_client_id = ""
8+
dbt_sso_client_secret = ""
9+
dbt_sso_redirect_uri = ""
10+
elementary_sso_redirect_uri = ""
11+
12+
lambda_runtime = "nodejs18.x"

0 commit comments

Comments
 (0)