11locals {
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
6532resource "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
10143resource "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
14357data "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
15667resource "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+
16681data "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
17991resource "aws_lambda_function" "sso_callback" {
0 commit comments