Skip to content

Commit 04914fc

Browse files
committed
added support for multiple static sites
1 parent 230d8c4 commit 04914fc

File tree

7 files changed

+88
-129
lines changed

7 files changed

+88
-129
lines changed
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
# main.tf
2-
module "sso_auth" {
1+
# dbt docs
2+
module "dbt_sso_auth" {
33
source = "../modules/cloudfront-microsoft-sso"
44

55
name_prefix = local.resource_name_prefix
66
app_code = "dbt-docs"
77
enable_auth_flag = true
88

99
lambda_runtime = "nodejs18.x"
10-
sso_config_arn = aws_secretsmanager_secret.sso_config.arn
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
1123
}
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
output "dbt__cloudfront_distribution__domain_name" {
2-
value = module.sso_auth.cloudfront_distribution__domain_name
2+
value = module.dbt_sso_auth.cloudfront_distribution__domain_name
33
}
44

55
output "dbt__aws_s3_bucket__arn" {
6-
value = module.sso_auth.aws_s3_bucket__arn
6+
value = module.dbt_sso_auth.aws_s3_bucket__arn
77
}
88

99
output "dbt__secret_arn" {
1010
description = "The ARN of the SSO secret"
11-
value = aws_secretsmanager_secret.sso_config.arn
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
1225
}
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
1-
resource "aws_secretsmanager_secret" "sso_config" {
1+
resource "aws_secretsmanager_secret" "dbt_sso_config" {
22
name = "dbt-sso-secret"
33
description = "SSO config (tenant, client_id, client_secret, redirect_uri)"
44
recovery_window_in_days = 0
55
}
66

7-
resource "aws_secretsmanager_secret_version" "sso_config_version" {
8-
secret_id = aws_secretsmanager_secret.sso_config.id
7+
resource "aws_secretsmanager_secret_version" "dbt_sso_config_version" {
8+
secret_id = aws_secretsmanager_secret.dbt_sso_config.id
99
secret_string = jsonencode({
1010
tenant = var.dbt_sso_tenant_id
1111
client_id = var.dbt_sso_client_id
1212
client_secret = var.dbt_sso_client_secret
1313
redirect_uri = var.dbt_sso_redirect_uri
1414
})
1515
}
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+
}

dbt-docs/terraform/cloudfront-microsoft-sso/terraform.tfvars.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ dbt_sso_tenant_id = ""
77
dbt_sso_client_id = ""
88
dbt_sso_client_secret = ""
99
dbt_sso_redirect_uri = ""
10+
elementary_sso_redirect_uri = ""
1011

1112
lambda_runtime = "nodejs18.x"

dbt-docs/terraform/cloudfront-microsoft-sso/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,8 @@ variable "dbt_sso_redirect_uri" {
4343
type = string
4444
description = "Redirect URI for the SSO flow"
4545
}
46+
47+
variable "elementary_sso_redirect_uri" {
48+
type = string
49+
description = "Redirect URI for the SSO flow"
50+
}

dbt-docs/terraform/modules/cloudfront-microsoft-sso/lambda.tf

Lines changed: 30 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
locals {
2+
# Track code changes in .js/.json
23
sso_authenticator_files = fileset(local.sso_authenticator_dir, "*.{js,json}")
34
sso_authenticator_sha = sha256(join(",", [
45
for file in local.sso_authenticator_files : filesha256("${local.sso_authenticator_dir}/${file}")
@@ -16,141 +17,51 @@ locals {
1617

1718
# Then extract just the base name (everything before the last hyphen and random characters)
1819
secret_name = join("-", slice(split("-", local.full_secret_part), 0, length(split("-", local.full_secret_part)) - 1))
19-
20-
# Create minimal lambda handlers to ensure the archives are never empty
21-
minimal_authenticator_code = <<-EOT
22-
exports.handler = async (event) => {
23-
console.log('Authenticator handler called');
24-
return {
25-
statusCode: 500,
26-
body: JSON.stringify({ message: "Default handler - not properly configured" })
27-
};
28-
};
29-
EOT
30-
31-
minimal_callback_code = <<-EOT
32-
exports.handler = async (event) => {
33-
console.log('Callback handler called');
34-
return {
35-
statusCode: 500,
36-
body: JSON.stringify({ message: "Default handler - not properly configured" })
37-
};
38-
};
39-
EOT
40-
}
41-
42-
# Directly create the authenticator.js file to ensure it exists
43-
resource "local_file" "authenticator_js" {
44-
filename = "${local.temp_authenticator_dir}/authenticator.js"
45-
content = local.minimal_authenticator_code
46-
47-
# Create the directory if it doesn't exist
48-
provisioner "local-exec" {
49-
command = "mkdir -p ${dirname(self.filename)}"
50-
}
5120
}
5221

53-
# Directly create the callback-handler.js file to ensure it exists
54-
resource "local_file" "callback_handler_js" {
55-
filename = "${local.temp_callback_dir}/callback-handler.js"
56-
content = local.minimal_callback_code
57-
58-
# Create the directory if it doesn't exist
59-
provisioner "local-exec" {
60-
command = "mkdir -p ${dirname(self.filename)}"
22+
# Track changes to trigger rebuilds only when necessary
23+
resource "null_resource" "prepare_triggers" {
24+
triggers = {
25+
authenticator_dir_sha = local.sso_authenticator_sha
26+
callback_dir_sha = local.sso_callback_sha
27+
secret_name = local.secret_name
6128
}
6229
}
6330

6431
# Prepare authenticator code in temporary directory
6532
resource "null_resource" "prepare_authenticator" {
66-
triggers = {
67-
authenticator_dir = local.sso_authenticator_dir
68-
authenticator_dir_sha = local.sso_authenticator_sha
69-
secret_name = local.secret_name
70-
}
33+
# depends_on = [null_resource.prepare_triggers]
7134

35+
# Use single-line command to avoid line ending issues
7236
provisioner "local-exec" {
73-
command = <<-EOT
74-
echo "Preparing authenticator files..."
75-
76-
# Check if source directory exists and has files
77-
if [ -d "${local.sso_authenticator_dir}" ] && [ "$(ls -A ${local.sso_authenticator_dir})" ]; then
78-
echo "Copying from ${local.sso_authenticator_dir}"
79-
cp -f ${local.sso_authenticator_dir}/*.js ${local.temp_authenticator_dir}/ 2>/dev/null || true
80-
cp -f ${local.sso_authenticator_dir}/*.json ${local.temp_authenticator_dir}/ 2>/dev/null || true
81-
82-
# If authenticator.js exists, update the SECRET_NAME
83-
if [ -f "${local.temp_authenticator_dir}/authenticator.js" ]; then
84-
sed -i 's/const SECRET_NAME = "SECRET-NAME-PLACEHOLDER";/const SECRET_NAME = "${local.secret_name}";/g' ${local.temp_authenticator_dir}/authenticator.js
85-
echo "Updated SECRET_NAME in authenticator.js"
86-
fi
87-
else
88-
echo "WARNING: Source directory ${local.sso_authenticator_dir} does not exist or is empty"
89-
fi
90-
91-
# Verify files were copied
92-
echo "Files in destination directory:"
93-
ls -la ${local.temp_authenticator_dir}/
94-
EOT
37+
interpreter = ["/bin/bash", "-c"]
38+
command = "mkdir -p ${local.temp_authenticator_dir} && cp -r ${local.sso_authenticator_dir}/* ${local.temp_authenticator_dir}/ && sed -i 's/const SECRET_NAME = \"SECRET-NAME-PLACEHOLDER\";/const SECRET_NAME = \"${local.secret_name}\";/g' ${local.temp_authenticator_dir}/authenticator.js"
9539
}
96-
97-
depends_on = [local_file.authenticator_js]
9840
}
9941

10042
# Prepare callback code in temporary directory
10143
resource "null_resource" "prepare_callback" {
102-
triggers = {
103-
callback_dir = local.sso_callback_dir
104-
callback_dir_sha = local.sso_callback_sha
105-
secret_name = local.secret_name
106-
}
44+
# depends_on = [null_resource.prepare_triggers]
10745

46+
# Use single-line command to avoid line ending issues
10847
provisioner "local-exec" {
109-
command = <<-EOT
110-
echo "Preparing callback files..."
111-
112-
# Check if source directory exists and has files
113-
if [ -d "${local.sso_callback_dir}" ] && [ "$(ls -A ${local.sso_callback_dir})" ]; then
114-
echo "Copying from ${local.sso_callback_dir}"
115-
cp -f ${local.sso_callback_dir}/*.js ${local.temp_callback_dir}/ 2>/dev/null || true
116-
cp -f ${local.sso_callback_dir}/*.json ${local.temp_callback_dir}/ 2>/dev/null || true
117-
118-
# If callback-handler.js exists, update the SECRET_NAME
119-
if [ -f "${local.temp_callback_dir}/callback-handler.js" ]; then
120-
sed -i 's/const SECRET_NAME = "SECRET-NAME-PLACEHOLDER";/const SECRET_NAME = "${local.secret_name}";/g' ${local.temp_callback_dir}/callback-handler.js
121-
echo "Updated SECRET_NAME in callback-handler.js"
122-
fi
123-
else
124-
echo "WARNING: Source directory ${local.sso_callback_dir} does not exist or is empty"
125-
fi
126-
127-
# Verify files were copied
128-
echo "Files in destination directory:"
129-
ls -la ${local.temp_callback_dir}/
130-
EOT
48+
interpreter = ["/bin/bash", "-c"]
49+
command = "mkdir -p ${local.temp_callback_dir} && cp -r ${local.sso_callback_dir}/* ${local.temp_callback_dir}/ && sed -i 's/const SECRET_NAME = \"SECRET-NAME-PLACEHOLDER\";/const SECRET_NAME = \"${local.secret_name}\";/g' ${local.temp_callback_dir}/callback-handler.js"
13150
}
132-
133-
depends_on = [local_file.callback_handler_js]
13451
}
13552

136-
# Ensure tmp/artifacts directory exists
137-
resource "null_resource" "ensure_artifacts_dir" {
138-
provisioner "local-exec" {
139-
command = "mkdir -p tmp/artifacts"
140-
}
141-
}
53+
###############################
54+
# Package & deploy SSO Authenticator
55+
###############################
14256

14357
data "archive_file" "sso_authenticator" {
14458
type = "zip"
145-
source_dir = local.temp_authenticator_dir
146-
output_path = "tmp/artifacts/${local.instance_id}-authenticator.zip"
59+
source_dir = "${local.temp_authenticator_dir}"
60+
output_path = "${local.temp_authenticator_dir}/payload.zip"
61+
excludes = ["payload.zip"]
14762
output_file_mode = "0666"
14863

149-
depends_on = [
150-
local_file.authenticator_js,
151-
null_resource.prepare_authenticator,
152-
null_resource.ensure_artifacts_dir
153-
]
64+
depends_on = [null_resource.prepare_triggers, null_resource.prepare_authenticator]
15465
}
15566

15667
resource "aws_lambda_function" "sso_authenticator" {
@@ -163,17 +74,18 @@ resource "aws_lambda_function" "sso_authenticator" {
16374
publish = true
16475
}
16576

77+
###############################
78+
# Package & deploy SSO Callback
79+
###############################
80+
16681
data "archive_file" "sso_callback" {
16782
type = "zip"
168-
source_dir = local.temp_callback_dir
169-
output_path = "tmp/artifacts/${local.instance_id}-callback.zip"
83+
source_dir = "${local.temp_callback_dir}"
84+
output_path = "${local.temp_callback_dir}/payload.zip"
85+
excludes = ["payload.zip"]
17086
output_file_mode = "0666"
17187

172-
depends_on = [
173-
local_file.callback_handler_js,
174-
null_resource.prepare_callback,
175-
null_resource.ensure_artifacts_dir
176-
]
88+
depends_on = [null_resource.prepare_triggers, null_resource.prepare_callback]
17789
}
17890

17991
resource "aws_lambda_function" "sso_callback" {

dbt-docs/terraform/modules/cloudfront-microsoft-sso/locals.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ locals {
77

88
# Create unique temporary directories for each module instance
99
instance_id = sha256("${var.name_prefix}-${var.app_code}")
10-
temp_authenticator_dir = "/tmp/${local.instance_id}/authenticator"
11-
temp_callback_dir = "/tmp/${local.instance_id}/callback"
10+
temp_authenticator_dir = "${path.module}/temp/lambda/${local.instance_id}/authenticator"
11+
temp_callback_dir = "${path.module}/temp/lambda/${local.instance_id}/callback"
1212

1313
# file extensions to mime types mapping
1414
mime_type_mappings = {

0 commit comments

Comments
 (0)