diff --git a/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/.terraform.lock.hcl b/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/.terraform.lock.hcl index 21363b30..470fca6d 100644 --- a/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/.terraform.lock.hcl +++ b/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/.terraform.lock.hcl @@ -44,6 +44,25 @@ provider "registry.terraform.io/hashicorp/kubernetes" { ] } +provider "registry.terraform.io/hashicorp/null" { + version = "3.2.4" + hashes = [ + "h1:L5V05xwp/Gto1leRryuesxjMfgZwjb7oool4WS1UEFQ=", + "zh:59f6b52ab4ff35739647f9509ee6d93d7c032985d9f8c6237d1f8a59471bbbe2", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:795c897119ff082133150121d39ff26cb5f89a730a2c8c26f3a9c1abf81a9c43", + "zh:7b9c7b16f118fbc2b05a983817b8ce2f86df125857966ad356353baf4bff5c0a", + "zh:85e33ab43e0e1726e5f97a874b8e24820b6565ff8076523cc2922ba671492991", + "zh:9d32ac3619cfc93eb3c4f423492a8e0f79db05fec58e449dee9b2d5873d5f69f", + "zh:9e15c3c9dd8e0d1e3731841d44c34571b6c97f5b95e8296a45318b94e5287a6e", + "zh:b4c2ab35d1b7696c30b64bf2c0f3a62329107bd1a9121ce70683dec58af19615", + "zh:c43723e8cc65bcdf5e0c92581dcbbdcbdcf18b8d2037406a5f2033b1e22de442", + "zh:ceb5495d9c31bfb299d246ab333f08c7fb0d67a4f82681fbf47f2a21c3e11ab5", + "zh:e171026b3659305c558d9804062762d168f50ba02b88b231d20ec99578a6233f", + "zh:ed0fe2acdb61330b01841fa790be00ec6beaac91d41f311fb8254f74eb6a711f", + ] +} + provider "registry.terraform.io/hashicorp/time" { version = "0.12.1" constraints = "0.12.1" diff --git a/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/data.tf b/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/data.tf index 53a96be5..45f482d7 100644 --- a/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/data.tf +++ b/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/data.tf @@ -100,3 +100,17 @@ data "aws_security_groups" "venue_proxy_sg" { Service = "U-CS" } } + +data "aws_region" "current" {} + +data "aws_ssm_parameter" "unity_client_id" { + name = "/sps/processing/workflows/unity_client_id" +} + +data "aws_ssm_parameter" "unity_password" { + name = "/sps/processing/workflows/unity_password" +} + +data "aws_ssm_parameter" "unity_username" { + name = "/sps/processing/workflows/unity_username" +} diff --git a/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/locals.tf b/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/locals.tf index 42a50002..8402c200 100644 --- a/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/locals.tf +++ b/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/locals.tf @@ -14,4 +14,5 @@ locals { Stack = "" } load_balancer_port = 5001 + region = data.aws_region.current.name } diff --git a/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/main.tf b/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/main.tf index 0f2ebe22..9153ba39 100644 --- a/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/main.tf +++ b/terraform-unity/modules/terraform-unity-sps-ogc-processes-api/main.tf @@ -456,7 +456,7 @@ resource "aws_ssm_parameter" "ogc_processes_api_health_check_endpoint" { description = "The URL of the OGC Processes REST API." type = "String" value = jsonencode({ - "componentCategory": "processing" + "componentCategory" : "processing" "componentName" : "OGC API" "componentType" : "api" "description" : "A standards-compliant programming interface for Application deployment, job execution and job tracking. May be used to execute jobs in batches." @@ -510,3 +510,33 @@ resource "aws_lambda_invocation" "unity_proxy_lambda_invocation" { ])) } } + +resource "null_resource" "check_ogc_api_status" { + provisioner "local-exec" { + command = "./check_ogc_api_status.sh" + working_dir = "${path.module}/../../../utils" + environment = { + OGC_PROCESSES_API = nonsensitive(aws_ssm_parameter.ogc_processes_api_url.value) + TOKEN_URL = "https://cognito-idp.${local.region}.amazonaws.com" + UNITY_CLIENTID = nonsensitive(data.aws_ssm_parameter.unity_client_id.value) + UNITY_PASSWORD = nonsensitive(data.aws_ssm_parameter.unity_password.value) + UNITY_USERNAME = nonsensitive(data.aws_ssm_parameter.unity_username.value) + } + } + depends_on = [aws_api_gateway_deployment.ogc-api-gateway-deployment, aws_ssm_parameter.ogc_processes_api_url] +} + +resource "null_resource" "register_ogc_processes" { + provisioner "local-exec" { + command = "./post_deployment_terraform.sh" + working_dir = "${path.module}/../../../utils" + environment = { + OGC_PROCESSES_API = nonsensitive(aws_ssm_parameter.ogc_processes_api_url.value) + TOKEN_URL = "https://cognito-idp.${local.region}.amazonaws.com" + UNITY_CLIENTID = nonsensitive(data.aws_ssm_parameter.unity_client_id.value) + UNITY_PASSWORD = nonsensitive(data.aws_ssm_parameter.unity_password.value) + UNITY_USERNAME = nonsensitive(data.aws_ssm_parameter.unity_username.value) + } + } + depends_on = [null_resource.check_ogc_api_status] +} diff --git a/utils/check_ogc_api_status.sh b/utils/check_ogc_api_status.sh new file mode 100755 index 00000000..c647d481 --- /dev/null +++ b/utils/check_ogc_api_status.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# Remove trailing slash from API URL if present +OGC_PROCESSES_API="${OGC_PROCESSES_API%/}" +echo $OGC_PROCESSES_API +echo $TOKEN_URL + +# Retrieve limited-lifetime token +echo "Fetching Cognito token..." +payload="{\"AuthParameters\":{\"USERNAME\":\"$UNITY_USERNAME\",\"PASSWORD\":\"$UNITY_PASSWORD\"},\"AuthFlow\":\"USER_PASSWORD_AUTH\",\"ClientId\":\"$UNITY_CLIENTID\"}" + +token_response=$(curl -X POST \ + -H "X-Amz-Target: AWSCognitoIdentityProviderService.InitiateAuth" \ + -H "Content-Type: application/x-amz-json-1.1" \ + --data $payload \ + $TOKEN_URL) + +token=$(echo $token_response | jq -r '.AuthenticationResult.AccessToken') +echo "Cognito token retrieved." + +# Poll onto OGC API is available +response_status=0 +while [ $response_status -ne 200 ]; do + response_status=$(curl -s -o /dev/null -k -X GET -H "Authorization: Bearer ${token}" -w "%{http_code}" "${OGC_PROCESSES_API}/processes") + echo "response_status=$response_status" + sleep 10 +done + +exit 0 diff --git a/utils/post_deployment_terraform.sh b/utils/post_deployment_terraform.sh new file mode 100755 index 00000000..281e9cbd --- /dev/null +++ b/utils/post_deployment_terraform.sh @@ -0,0 +1,58 @@ +#!/bin/bash +#set -ex + +# Script to execute post-deployment operations. +# Pre-Requisites: +# - SPS has been deployed successfully to a given venue +# - The user has valid Cognito credentials for the target venue + +# Syntax: +# cd unity-sps/utils +# export UNITY_USERNAME="....." +# export UNITY_PASSWORD="....." +# export UNITY_CLIENTID="...." +# export OGC_PROCESSES_API=https://.........execute-api.us-west-2.amazonaws.com/dev/ogc/api (NO trailing slash!) +# export TOKEN_URL=https://cognito-idp.{region}.amazonaws.com (where region is the AWS region executing in) + +# Remove trailing slash from API URL if present +OGC_PROCESSES_API="${OGC_PROCESSES_API%/}" + +# Retrieve limited-lifetime token +echo "Fetching Cognito token..." +payload="{\"AuthParameters\":{\"USERNAME\":\"$UNITY_USERNAME\",\"PASSWORD\":\"$UNITY_PASSWORD\"},\"AuthFlow\":\"USER_PASSWORD_AUTH\",\"ClientId\":\"$UNITY_CLIENTID\"}" + +token_response=$(curl -X POST \ + -H "X-Amz-Target: AWSCognitoIdentityProviderService.InitiateAuth" \ + -H "Content-Type: application/x-amz-json-1.1" \ + --data $payload \ + $TOKEN_URL) + +token=$(echo $token_response | jq -r '.AuthenticationResult.AccessToken') +echo "Cognito token retrieved." + +# list of processes to be registered +declare -a procs=("cwl_dag.json" "karpenter_test.json" "appgen_dag.json" "cwl_dag_modular.json" "db_cleanup_dag.json") + +for proc in "${procs[@]}" +do + echo " " + proc_name=$(echo "$proc" | sed "s/.json//") + + # unregister process (in case it was already registered) + echo "Unregistering process: $proc_name" + curl -k -X DELETE \ + -H "Authorization: Bearer ${token}" \ + -H "Content-Type: application/json; charset=utf-8" \ + "${OGC_PROCESSES_API}/processes/${proc_name}" + + # register process + echo "Registering process: $proc_name" + curl -k -X POST \ + -H "Authorization: Bearer ${token}" \ + -H "Expect:" \ + -H "Content-Type: application/json; charset=utf-8" \ + --data-binary @"../ogc-application-packages/$proc" \ + "${OGC_PROCESSES_API}/processes" + echo " " + +done