From 672aceb72d0ff69aa21926279b025f793cc571a0 Mon Sep 17 00:00:00 2001 From: Pratham Soni Date: Thu, 20 Jun 2024 09:31:49 -0500 Subject: [PATCH 01/43] table reload when doczy pipeline has fully run --- streamlit/interface_1.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/streamlit/interface_1.py b/streamlit/interface_1.py index 25666d5..861e195 100644 --- a/streamlit/interface_1.py +++ b/streamlit/interface_1.py @@ -265,7 +265,6 @@ with buttons[1]: st.error("Select at least one Group No. for every Contract") else: #Resets the Data Editor with Blank Values - update_dataframe() with st.spinner('Running...'): # csv_buf = StringIO() # additional_info.to_csv(csv_buf, header=True, index=False) @@ -282,6 +281,9 @@ with buttons[1]: response = requests.post(doczy_pipeline, json = myobj) if response.status_code >= 200 and response.status_code < 300: st.write("Success") + #Updates and Reruns the Code after everything on the interface has completed + update_dataframe() + st.experimental_rerun() # st.write(myobj) else: st.write("Failed") From 045737c186096e1069b5f36ffe3ccf187cda5d7f Mon Sep 17 00:00:00 2001 From: Pratham Soni Date: Thu, 20 Jun 2024 16:01:12 -0500 Subject: [PATCH 02/43] update dataframe on pipeline run --- streamlit/interface_1.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/streamlit/interface_1.py b/streamlit/interface_1.py index c788354..4bf6831 100644 --- a/streamlit/interface_1.py +++ b/streamlit/interface_1.py @@ -15,6 +15,11 @@ from constants import USER_LIST (REDIRECT_URI, create_batch_url, doczy_pipeline) = util.load_page_details(1) +def update_dataframe(): + new_df = pd.DataFrame(columns=['Contract Name', 'Unique Key','Pricing Before Carveouts' + , 'Contract Related', 'Provider', 'Timeline', 'Carveout Indicator', 'Carveout Methodology']) + st.session_state['dataframe'] = new_df + user_list = USER_LIST st.set_page_config(layout = "wide") # Sidebar contents @@ -272,7 +277,7 @@ with buttons[1]: st.write("Success") #Updates and Reruns the Code after everything on the interface has completed update_dataframe() - st.experimental_rerun() + st.rerun() # st.write(myobj) else: st.write("Failed") From 700f4cfbe70f38f8d284278cc09a9c162baacb1b Mon Sep 17 00:00:00 2001 From: Umang Mistry Date: Thu, 20 Jun 2024 18:11:46 -0500 Subject: [PATCH 03/43] Removed API gateway authorization --- .../modules/aws-textract-pipeline-part3/main.tf | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/textract-pipeline/terraform/modules/aws-textract-pipeline-part3/main.tf b/textract-pipeline/terraform/modules/aws-textract-pipeline-part3/main.tf index 2eadfb6..68b9e16 100644 --- a/textract-pipeline/terraform/modules/aws-textract-pipeline-part3/main.tf +++ b/textract-pipeline/terraform/modules/aws-textract-pipeline-part3/main.tf @@ -909,7 +909,8 @@ resource "aws_api_gateway_method" "create_batch_method" { rest_api_id = "${aws_api_gateway_rest_api.project_api.id}" resource_id = "${aws_api_gateway_resource.create_batch.id}" http_method = "POST" - authorization = "AWS_IAM" + #authorization = "AWS_IAM" + authorization = "NONE" depends_on = [ aws_api_gateway_rest_api.project_api, @@ -997,7 +998,8 @@ resource "aws_api_gateway_method" "s3_folder_detail_method" { rest_api_id = "${aws_api_gateway_rest_api.project_api.id}" resource_id = "${aws_api_gateway_resource.s3_folder_detail.id}" http_method = "POST" - authorization = "AWS_IAM" + #authorization = "AWS_IAM" + authorization = "NONE" } # API Gateway - s3 folder detail - create method response resource "aws_api_gateway_method_response" "s3_folder_detail_method_response_200" { @@ -1059,7 +1061,8 @@ resource "aws_api_gateway_method" "trigger_pipeline_method" { rest_api_id = "${aws_api_gateway_rest_api.project_api.id}" resource_id = "${aws_api_gateway_resource.trigger_pipeline.id}" http_method = "POST" - authorization = "AWS_IAM" + #authorization = "AWS_IAM" + authorization = "NONE" } # API Gateway - trigger pipeline - create method response resource "aws_api_gateway_method_response" "trigger_pipeline_method_response_200" { From 0c2eab2bea9138e33668057dba9508c483a17342 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 15:30:57 +0200 Subject: [PATCH 04/43] [DOC-536] Smart Git diff compare for terraform modules --- git_diff_changes.py | 90 +++++++++++++++++++++++++++++++++++++++++++++ terraform.py | 14 +++++-- 2 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 git_diff_changes.py diff --git a/git_diff_changes.py b/git_diff_changes.py new file mode 100644 index 0000000..3d8013d --- /dev/null +++ b/git_diff_changes.py @@ -0,0 +1,90 @@ +import os +import subprocess +import requests + +BITBUCKET_REPO_OWNER = "aarete" +BITBUCKET_REPO_SLUG = "doczy.ai" +BB_PIPELINE_BRANCH = os.getenv("BB_PIPELINE_BRANCH", "DEV") +BITBUCKET_AUTH_HEADER = os.getenv("BITBUCKET_AUTH_HEADER") + +from terminal import run_command, prepare_logger, decorate_warn + +LOGGER = prepare_logger() + + +def check_bitbucket_auth_header(): + if os.getenv("BITBUCKET_CI") == "true" and not BITBUCKET_AUTH_HEADER: + raise EnvironmentError("Error: BITBUCKET_AUTH_HEADER is not set.") + + +def get_last_successful_commit(module): + if os.getenv("BITBUCKET_CI") != "true": + rc, result = run_command("git rev-parse HEAD", log_output=True, log_cmd=True, log_prefix=f"[{module}]") + return result + + for commit in subprocess.check_output(["git", "log", "--format=%h", "-n", "30"]).decode().split(): + url = f"https://api.bitbucket.org/2.0/repositories/{BITBUCKET_REPO_OWNER}/{BITBUCKET_REPO_SLUG}/commit/{commit}/statuses/" + LOGGER.info(f"commit={commit}, url={url}") + basic_url_headers = { + BITBUCKET_AUTH_HEADER.split(": ")[0]: BITBUCKET_AUTH_HEADER.split(": ")[1] + } + response = requests.get(url, headers=basic_url_headers).json() + LOGGER.info(f"response={response}") + + for item in response.get('values', []): + name = item['name'] + if "security/snyk" in name or "iacbot" in name: + continue + commit_state = item['state'] + ref_name = item['refname'] + updated_on = item['updated_on'] + + LOGGER.info( + f"COMMIT={commit}, COMMIT_STATE={commit_state}, REF_NAME={ref_name}, NAME={name}, UPDATED_ON={updated_on}") + + if commit_state == "SUCCESSFUL" and ref_name.startswith(BB_PIPELINE_BRANCH): + return commit + + return commit + + +def get_changes_output(module, last_successful_commit): + if os.getenv("BITBUCKET_CI") == "true": + _, result = run_command(f"git diff --dirstat=files,0 {last_successful_commit} | sed -E 's/^[ 0-9.]+% //g'", + log_output=True, log_cmd=True, log_prefix=f"[{module}]") + return result + else: + _, result = run_command("git diff --dirstat=files,0 HEAD~1 | sed -E 's/^[ 0-9.]+% //g'", + log_output=True, log_cmd=True, log_prefix=f"[{module}]") + return result + + +def is_deploy_module(module, dir_prefix): + check_bitbucket_auth_header() + if os.getenv("IS_DEPLOY_ALL") == "true": + return True + if os.getenv("SKIP_DEPLOY"): + return False + if module == os.getenv("MODULE_NAME"): + return True + elif os.getenv("MODULE_NAME"): + return False + + changes_output = get_changes_output(module, get_last_successful_commit(module)) + + LOGGER.info(f"changes_output={changes_output}") + + special_checks = [ + ("textract-pipeline/terraform", "textract-pipeline/src"), + ] + + for mod, pattern in special_checks: + pattern_found = any(pattern in change for change in changes_output) + if module == mod and pattern_found: + LOGGER.info(decorate_warn(f"Marking {module} as changed based on the special pattern={pattern}")) + return True + + if f"{dir_prefix}{module}/" in changes_output: + return True + + return False diff --git a/terraform.py b/terraform.py index 0d6faaf..3ab19f9 100644 --- a/terraform.py +++ b/terraform.py @@ -1,4 +1,5 @@ -from terminal import run_command, prepare_logger +from terminal import run_command, prepare_logger, decorate_white_bold, decorate_warn +from git_diff_changes import is_deploy_module import os import argparse @@ -99,7 +100,13 @@ tf_root_module_path = os.getenv('TF_ROOT_MODULE_PATH') if not module_name else m def do_terraform(stack_path): - LOGGER.info(f"stack_path={stack_path}") + LOGGER.info(f"[{stack_path}]: stack_path={stack_path}") + is_deploy_module_path = is_deploy_module(stack_path, '') + LOGGER.info(f"[{stack_path}]: is_deploy_module_path={decorate_white_bold(is_deploy_module_path)}") + if not is_deploy_module_path: + LOGGER.info(decorate_warn(f"[{stack_path}]: Module is not found in git changes. Skipping deploy.")) + return True, "OK" + absolute_path = change_dir(stack_path) # TF INIT state_environment = "dev-new" if environment == "dev" else environment @@ -224,4 +231,5 @@ if __name__ == "__main__": paths = find_paths_with_file('provider.tf') for path in paths: LOGGER.warning(f"Discovered: {path}") - execute_with_dependencies(paths) + # TODO finish later if we want multi modulue deploy in one go + # execute_with_dependencies(paths) From 03024b7bdd3d042c83b6ad2cb4e59dc2d8a273d9 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 15:33:57 +0200 Subject: [PATCH 05/43] [DOC-536] Smart Git diff compare for terraform modules --- bitbucket-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index ba20121..e1ff9ac 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -239,7 +239,7 @@ pipelines: - snowflake/**/*.sql - feature/terraform-refactor: + feature/DOC-536-smart-git-diff: - parallel: - step: name: Plan devops-pipeline on DEV From eff9d9dea0b736bc268b5c95860e30e1f99826d4 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 15:35:18 +0200 Subject: [PATCH 06/43] [DOC-536] Smart Git diff compare for terraform modules --- bitbucket-pipelines.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index e1ff9ac..6fdce6e 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -247,39 +247,39 @@ pipelines: oidc: true script: - *aws-context-dev - - python terraform.py plan --environment=dev --module=devops-pipeline/other-resources + - BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=dev --module=devops-pipeline/other-resources - step: name: Plan streamlit-server on DEV image: hugree/bitbucket-aws-python38-tf154:latest oidc: true script: - *aws-context-dev - - python terraform.py plan --environment=dev --module=streamlit-server + - BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=dev --module=streamlit-server - step: name: Plan textract-pipeline on DEV image: hugree/bitbucket-aws-python38-tf154:latest oidc: true script: - *aws-context-dev - - python terraform.py plan --environment=dev --module=textract-pipeline/terraform + - BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=dev --module=textract-pipeline/terraform - step: name: Plan devops-pipeline on UAT image: hugree/bitbucket-aws-python38-tf154:latest oidc: true script: - *aws-context-uat - - python terraform.py plan --environment=uat --module=devops-pipeline/other-resources + - BB_PIPELINE_BRANCH="UAT" python terraform.py plan --environment=uat --module=devops-pipeline/other-resources - step: name: Plan streamlit-server on UAT image: hugree/bitbucket-aws-python38-tf154:latest oidc: true script: - *aws-context-uat - - python terraform.py plan --environment=uat --module=streamlit-server + - BB_PIPELINE_BRANCH="UAT" python terraform.py plan --environment=uat --module=streamlit-server - step: name: Plan textract-pipeline on UAT image: hugree/bitbucket-aws-python38-tf154:latest oidc: true script: - *aws-context-uat - - python terraform.py plan --environment=uat --module=textract-pipeline/terraform + - BB_PIPELINE_BRANCH="UAT" python terraform.py plan --environment=uat --module=textract-pipeline/terraform From eb68c1755c63cdc5ffc30a27c3e77800217b33bc Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 15:39:01 +0200 Subject: [PATCH 07/43] [DOC-536] Smart Git diff compare for terraform modules --- bitbucket-pipelines.yml | 6 ++++++ requirements.txt | 1 + 2 files changed, 7 insertions(+) create mode 100644 requirements.txt diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 6fdce6e..aa319ed 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -247,6 +247,7 @@ pipelines: oidc: true script: - *aws-context-dev + - pip install -r requirements.txt - BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=dev --module=devops-pipeline/other-resources - step: name: Plan streamlit-server on DEV @@ -254,6 +255,7 @@ pipelines: oidc: true script: - *aws-context-dev + - pip install -r requirements.txt - BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=dev --module=streamlit-server - step: name: Plan textract-pipeline on DEV @@ -261,6 +263,7 @@ pipelines: oidc: true script: - *aws-context-dev + - pip install -r requirements.txt - BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=dev --module=textract-pipeline/terraform - step: name: Plan devops-pipeline on UAT @@ -268,6 +271,7 @@ pipelines: oidc: true script: - *aws-context-uat + - pip install -r requirements.txt - BB_PIPELINE_BRANCH="UAT" python terraform.py plan --environment=uat --module=devops-pipeline/other-resources - step: name: Plan streamlit-server on UAT @@ -275,6 +279,7 @@ pipelines: oidc: true script: - *aws-context-uat + - pip install -r requirements.txt - BB_PIPELINE_BRANCH="UAT" python terraform.py plan --environment=uat --module=streamlit-server - step: name: Plan textract-pipeline on UAT @@ -282,4 +287,5 @@ pipelines: oidc: true script: - *aws-context-uat + - pip install -r requirements.txt - BB_PIPELINE_BRANCH="UAT" python terraform.py plan --environment=uat --module=textract-pipeline/terraform diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f229360 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests From a20e693f4423572d304b798d649f25108a1e732f Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 15:44:21 +0200 Subject: [PATCH 08/43] [DOC-536] Smart Git diff compare for terraform modules --- git_diff_changes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git_diff_changes.py b/git_diff_changes.py index 3d8013d..948c28a 100644 --- a/git_diff_changes.py +++ b/git_diff_changes.py @@ -29,7 +29,7 @@ def get_last_successful_commit(module): BITBUCKET_AUTH_HEADER.split(": ")[0]: BITBUCKET_AUTH_HEADER.split(": ")[1] } response = requests.get(url, headers=basic_url_headers).json() - LOGGER.info(f"response={response}") + LOGGER.debug(f"response={response}") for item in response.get('values', []): name = item['name'] From dd76bfec792ccc2cd6fb3a8eaafb8ceb42447eb1 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 15:46:47 +0200 Subject: [PATCH 09/43] [DOC-536] Smart Git diff compare for terraform modules --- streamlit-server/main.tf | 3 --- 1 file changed, 3 deletions(-) diff --git a/streamlit-server/main.tf b/streamlit-server/main.tf index 3e0540b..0213521 100644 --- a/streamlit-server/main.tf +++ b/streamlit-server/main.tf @@ -220,6 +220,3 @@ resource "aws_instance" "streamlit_server" { Environment = var.environment } } - - - From a83d4185fc2838fa2e00ab7b276a02c662ff99de Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 16:53:34 +0200 Subject: [PATCH 10/43] [DOC-536] Smart Git diff compare for terraform modules --- bitbucket-pipelines.yml | 10 ++++++++++ snowsql-cicd.sh | 39 +++++++++++++++++++++++++++++++++++++++ snowsql-query.sql | 1 + 3 files changed, 50 insertions(+) create mode 100755 snowsql-cicd.sh create mode 100644 snowsql-query.sql diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index ba20121..038c076 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -283,3 +283,13 @@ pipelines: script: - *aws-context-uat - python terraform.py plan --environment=uat --module=textract-pipeline/terraform + + + feature/DOC-535-snowsql-in-cicd: + - step: + image: hugree/snowflake-snowsql:latest + name: Run Snowsql in CICD + oidc: true + script: + - *aws-context-dev + - bash snowsql-cicd.sh dev \ No newline at end of file diff --git a/snowsql-cicd.sh b/snowsql-cicd.sh new file mode 100755 index 0000000..a233842 --- /dev/null +++ b/snowsql-cicd.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +set -euo pipefail + +ENV_GROUP=$1 + +readonly SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )" + +#echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}, SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}, SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" + +#export GIT_ROOT=$(git rev-parse --show-toplevel) +#if [ "$(type -t changes)" != 'function' ]; then +# source "${GIT_ROOT}/infra/scripts/bootstrap-diff-changes.sh" +#fi +# +#if is_deploy_module "streamlit"; then +# echo -e "${GREEN}# Streamlit module changes have been detected. Executing the upload command...${NC}" +#else +# echo -e "${YELLOW}# There are no Streamlit module changes. Skipping the upload command...${NC}" +# exit 0 +#fi + +echo "# Getting snowflake creds from AWS Secret Manager..." +snowflake_secret_name="doczy-${ENV_GROUP}-db-svc-acc" +snowflake_secret_value=$(aws secretsmanager get-secret-value --secret-id "$snowflake_secret_name" --query 'SecretString' --output text) + +SNOWFLAKE_ACCOUNT=$(echo $snowflake_secret_value | jq -r '.account_locator') +SNOWFLAKE_DATABASE=$(echo $snowflake_secret_value | jq -r '.database') +SNOWFLAKE_USER=$(echo $snowflake_secret_value | jq -r '.user') +SNOWFLAKE_PASSWORD=$(echo $snowflake_secret_value | jq -r '.password') +SNOWFLAKE_ROLE=$(echo $snowflake_secret_value | jq -r '.role') + +echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}" +echo "SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}" +echo "SNOWFLAKE_USER=${SNOWFLAKE_USER}" +echo "SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" +export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} + +snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" diff --git a/snowsql-query.sql b/snowsql-query.sql new file mode 100644 index 0000000..60d93f3 --- /dev/null +++ b/snowsql-query.sql @@ -0,0 +1 @@ +SELECT DISTINCT client_name, bucket_name FROM STG.CLIENT_LOGS \ No newline at end of file From 27545157ed1f81d68657a8bdfe316dfaf7aa1f93 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 16:56:12 +0200 Subject: [PATCH 11/43] [DOC-536] Smart Git diff compare for terraform modules --- snowsql-cicd.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/snowsql-cicd.sh b/snowsql-cicd.sh index a233842..7264255 100755 --- a/snowsql-cicd.sh +++ b/snowsql-cicd.sh @@ -20,6 +20,7 @@ readonly SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )" # exit 0 #fi +set -x echo "# Getting snowflake creds from AWS Secret Manager..." snowflake_secret_name="doczy-${ENV_GROUP}-db-svc-acc" snowflake_secret_value=$(aws secretsmanager get-secret-value --secret-id "$snowflake_secret_name" --query 'SecretString' --output text) @@ -27,9 +28,11 @@ snowflake_secret_value=$(aws secretsmanager get-secret-value --secret-id "$snowf SNOWFLAKE_ACCOUNT=$(echo $snowflake_secret_value | jq -r '.account_locator') SNOWFLAKE_DATABASE=$(echo $snowflake_secret_value | jq -r '.database') SNOWFLAKE_USER=$(echo $snowflake_secret_value | jq -r '.user') -SNOWFLAKE_PASSWORD=$(echo $snowflake_secret_value | jq -r '.password') SNOWFLAKE_ROLE=$(echo $snowflake_secret_value | jq -r '.role') +set +x +SNOWFLAKE_PASSWORD=$(echo $snowflake_secret_value | jq -r '.password') + echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}" echo "SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}" echo "SNOWFLAKE_USER=${SNOWFLAKE_USER}" From 207404ec1d509ba90ab8a5153889ed50de3bfbb3 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 16:59:38 +0200 Subject: [PATCH 12/43] [DOC-536] Smart Git diff compare for terraform modules --- bitbucket-pipelines.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 038c076..3dbc0ae 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -292,4 +292,7 @@ pipelines: oidc: true script: - *aws-context-dev + - aws sts get-caller-identity --output text + - echo $AWS_ACCESS_KEY_ID + - echo $AWS_SECRET_ACCESS_KEY - bash snowsql-cicd.sh dev \ No newline at end of file From c24b08ccbfa52abc8c6dc6d185e8c63b638e3727 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 17:03:16 +0200 Subject: [PATCH 13/43] [DOC-536] Smart Git diff compare for terraform modules --- bitbucket-pipelines.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 3dbc0ae..14d531b 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -291,8 +291,10 @@ pipelines: name: Run Snowsql in CICD oidc: true script: - - *aws-context-dev +# - *aws-context-dev + - export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600) + - export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId') + - export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey') + - export AWS_SESSION_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken') - aws sts get-caller-identity --output text - - echo $AWS_ACCESS_KEY_ID - - echo $AWS_SECRET_ACCESS_KEY - bash snowsql-cicd.sh dev \ No newline at end of file From 08263d227cde4810e5bc2b372c298b06dff6e16b Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 17:03:47 +0200 Subject: [PATCH 14/43] [DOC-536] Smart Git diff compare for terraform modules --- bitbucket-pipelines.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 14d531b..81b887c 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -292,6 +292,7 @@ pipelines: oidc: true script: # - *aws-context-dev + - export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO:role/$OIDC_ROLE - export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600) - export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId') - export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey') From f1216783edada59fdec4136cad22ac7276b29ded Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 17:07:07 +0200 Subject: [PATCH 15/43] [DOC-536] Smart Git diff compare for terraform modules --- snowsql-cicd.sh | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/snowsql-cicd.sh b/snowsql-cicd.sh index 7264255..d119a97 100755 --- a/snowsql-cicd.sh +++ b/snowsql-cicd.sh @@ -20,18 +20,23 @@ readonly SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )" # exit 0 #fi -set -x -echo "# Getting snowflake creds from AWS Secret Manager..." -snowflake_secret_name="doczy-${ENV_GROUP}-db-svc-acc" -snowflake_secret_value=$(aws secretsmanager get-secret-value --secret-id "$snowflake_secret_name" --query 'SecretString' --output text) +#set -x +#echo "# Getting snowflake creds from AWS Secret Manager..." +#snowflake_secret_name="doczy-${ENV_GROUP}-db-svc-acc" +#snowflake_secret_value=$(aws secretsmanager get-secret-value --secret-id "$snowflake_secret_name" --query 'SecretString' --output text) +# +#SNOWFLAKE_ACCOUNT=$(echo $snowflake_secret_value | jq -r '.account_locator') +#SNOWFLAKE_DATABASE=$(echo $snowflake_secret_value | jq -r '.database') +#SNOWFLAKE_USER=$(echo $snowflake_secret_value | jq -r '.user') +#SNOWFLAKE_ROLE=$(echo $snowflake_secret_value | jq -r '.role') +# +#set +x +#SNOWFLAKE_PASSWORD=$(echo $snowflake_secret_value | jq -r '.password') -SNOWFLAKE_ACCOUNT=$(echo $snowflake_secret_value | jq -r '.account_locator') -SNOWFLAKE_DATABASE=$(echo $snowflake_secret_value | jq -r '.database') -SNOWFLAKE_USER=$(echo $snowflake_secret_value | jq -r '.user') -SNOWFLAKE_ROLE=$(echo $snowflake_secret_value | jq -r '.role') - -set +x -SNOWFLAKE_PASSWORD=$(echo $snowflake_secret_value | jq -r '.password') +SNOWFLAKE_ACCOUNT=OQ11564.us-east-2.aws +SNOWFLAKE_DATABASE=DOCZY_DEV +SNOWFLAKE_USER=aws_svc_account +SNOWFLAKE_ROLE=DEVADMIN echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}" echo "SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}" From 4e317e3c33f6325aa70ab0d561acc90b31133963 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 17:09:07 +0200 Subject: [PATCH 16/43] [DOC-536] Smart Git diff compare for terraform modules --- snowsql-cicd.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snowsql-cicd.sh b/snowsql-cicd.sh index d119a97..2bf4315 100755 --- a/snowsql-cicd.sh +++ b/snowsql-cicd.sh @@ -42,6 +42,6 @@ echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}" echo "SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}" echo "SNOWFLAKE_USER=${SNOWFLAKE_USER}" echo "SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" -export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} +export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD_DEV} snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" From 3d1b2efc0c9fa530798a3314a7f002fd9998a1aa Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Fri, 21 Jun 2024 17:28:32 +0200 Subject: [PATCH 17/43] [DOC-535] Snowsql test --- snowsql-cicd.sh | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/snowsql-cicd.sh b/snowsql-cicd.sh index 2bf4315..4af0186 100755 --- a/snowsql-cicd.sh +++ b/snowsql-cicd.sh @@ -7,7 +7,7 @@ ENV_GROUP=$1 readonly SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )" #echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}, SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}, SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" - +# #export GIT_ROOT=$(git rev-parse --show-toplevel) #if [ "$(type -t changes)" != 'function' ]; then # source "${GIT_ROOT}/infra/scripts/bootstrap-diff-changes.sh" @@ -20,28 +20,26 @@ readonly SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )" # exit 0 #fi -#set -x -#echo "# Getting snowflake creds from AWS Secret Manager..." -#snowflake_secret_name="doczy-${ENV_GROUP}-db-svc-acc" -#snowflake_secret_value=$(aws secretsmanager get-secret-value --secret-id "$snowflake_secret_name" --query 'SecretString' --output text) -# -#SNOWFLAKE_ACCOUNT=$(echo $snowflake_secret_value | jq -r '.account_locator') -#SNOWFLAKE_DATABASE=$(echo $snowflake_secret_value | jq -r '.database') -#SNOWFLAKE_USER=$(echo $snowflake_secret_value | jq -r '.user') -#SNOWFLAKE_ROLE=$(echo $snowflake_secret_value | jq -r '.role') -# -#set +x -#SNOWFLAKE_PASSWORD=$(echo $snowflake_secret_value | jq -r '.password') +echo "# Getting snowflake creds from AWS Secret Manager..." +snowflake_secret_name="doczy-${ENV_GROUP}-db-svc-acc" +snowflake_secret_value=$(aws secretsmanager get-secret-value --secret-id "$snowflake_secret_name" --query 'SecretString' --output text) -SNOWFLAKE_ACCOUNT=OQ11564.us-east-2.aws -SNOWFLAKE_DATABASE=DOCZY_DEV -SNOWFLAKE_USER=aws_svc_account -SNOWFLAKE_ROLE=DEVADMIN +SNOWFLAKE_ACCOUNT=$(echo $snowflake_secret_value | jq -r '.account_locator') +SNOWFLAKE_DATABASE=$(echo $snowflake_secret_value | jq -r '.database') +SNOWFLAKE_USER=$(echo $snowflake_secret_value | jq -r '.user') +SNOWFLAKE_ROLE=$(echo $snowflake_secret_value | jq -r '.role') + +SNOWFLAKE_PASSWORD=$(echo $snowflake_secret_value | jq -r '.password') + +#SNOWFLAKE_ACCOUNT=OQ11564.us-east-2.aws +#SNOWFLAKE_DATABASE=DOCZY_DEV +#SNOWFLAKE_USER=aws_svc_account +#SNOWFLAKE_ROLE=DEVADMIN echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}" echo "SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}" echo "SNOWFLAKE_USER=${SNOWFLAKE_USER}" echo "SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" -export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD_DEV} +export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" From 4f1459f1ec4e9879863452ea816838bc70307937 Mon Sep 17 00:00:00 2001 From: Pratham Soni Date: Fri, 21 Jun 2024 15:58:39 -0500 Subject: [PATCH 18/43] adding nonce to URL auth --- streamlit/security.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/streamlit/security.py b/streamlit/security.py index d759570..d8e542e 100644 --- a/streamlit/security.py +++ b/streamlit/security.py @@ -4,6 +4,7 @@ import requests import boto3 from botocore.exceptions import ClientError import json +import uuid # Replace with your own values CLIENT_ID = 'effafe90-7ed7-43a3-ab03-19a0be2f1758' @@ -55,9 +56,11 @@ CLIENT_SECRET = get_secret() app = msal.ConfidentialClientApplication(CLIENT_ID, authority=AUTHORITY, client_credential=CLIENT_SECRET) +#Create NONCE from UUID +nonce = str(uuid.uuid4()) def get_auth_url(REDIRECT_URI): - auth_url = app.get_authorization_request_url(SCOPE, redirect_uri=REDIRECT_URI) + auth_url = app.get_authorization_request_url(SCOPE, redirect_uri=REDIRECT_URI, nonce=nonce ) #added nonce for masking token return auth_url @st.cache_data From 9d5dc1492e327ac6670450061e5bad101d4b9cd5 Mon Sep 17 00:00:00 2001 From: Pratham Soni Date: Fri, 21 Jun 2024 16:23:30 -0500 Subject: [PATCH 19/43] undo sso changes --- streamlit/security.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/streamlit/security.py b/streamlit/security.py index d8e542e..d7465d2 100644 --- a/streamlit/security.py +++ b/streamlit/security.py @@ -4,7 +4,6 @@ import requests import boto3 from botocore.exceptions import ClientError import json -import uuid # Replace with your own values CLIENT_ID = 'effafe90-7ed7-43a3-ab03-19a0be2f1758' @@ -56,11 +55,9 @@ CLIENT_SECRET = get_secret() app = msal.ConfidentialClientApplication(CLIENT_ID, authority=AUTHORITY, client_credential=CLIENT_SECRET) -#Create NONCE from UUID -nonce = str(uuid.uuid4()) def get_auth_url(REDIRECT_URI): - auth_url = app.get_authorization_request_url(SCOPE, redirect_uri=REDIRECT_URI, nonce=nonce ) #added nonce for masking token + auth_url = app.get_authorization_request_url(SCOPE, redirect_uri=REDIRECT_URI ) return auth_url @st.cache_data From f8dafeb5140549332b8a163080ebcdd12b4728f2 Mon Sep 17 00:00:00 2001 From: Pratham Soni Date: Fri, 21 Jun 2024 16:24:56 -0500 Subject: [PATCH 20/43] interface_1 change to st.download_button --- streamlit/interface_1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/streamlit/interface_1.py b/streamlit/interface_1.py index f41cf97..162273f 100644 --- a/streamlit/interface_1.py +++ b/streamlit/interface_1.py @@ -249,7 +249,7 @@ if client: buttons = st.columns([0.8, 0.2]) with buttons[0]: - st.download_button("Download Table", csv, "file.csv", "text/csv", key='download-csv') + st.download_button("Download Table", "file.csv", "text/csv", key='download-csv') with buttons[1]: if st.button("Run Doczy.AI Pipeline"): if not st.session_state.contract_count == len(edited_df): From 18274a4622160e9248e7ca906e5adbd1609913d7 Mon Sep 17 00:00:00 2001 From: "AARETE\\agupta" Date: Fri, 21 Jun 2024 16:25:20 -0500 Subject: [PATCH 21/43] UI 1 Fix --- streamlit/interface_1.py | 67 +++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/streamlit/interface_1.py b/streamlit/interface_1.py index 465ea9b..a714956 100644 --- a/streamlit/interface_1.py +++ b/streamlit/interface_1.py @@ -97,7 +97,10 @@ if client: batch_list = [] for prefix in batch_objects['CommonPrefixes']: - batch_list.append(prefix['Prefix'][:-1].split('/')[-1]) + batch_name = prefix['Prefix'][:-1].split('/')[-1] + batch_objects2 = s3_client.list_objects_v2(Bucket=client_bucket, Prefix="contracts-landing-zone/"+batch_name+"/", Delimiter='/') + if 'Contents' in batch_objects2 and len(batch_objects2['Contents'] > 0): + batch_list.append(prefix['Prefix'][:-1].split('/')[-1]) # Hardcoded batch_list for testing purposes # batch_list = ['batch_020524103737', 'batch_090524131433', 'batch_090524131607', 'batch_100524123000', 'batch_130524064322', @@ -252,36 +255,36 @@ if client: "contract_list": contract_list } -buttons = st.columns([0.8, 0.2]) -with buttons[0]: - st.download_button("Download Table", csv, "file.csv", "text/csv", key='download-csv') -with buttons[1]: - if st.button("Run Doczy.AI Pipeline"): - if not st.session_state.contract_count == len(edited_df): - st.error("Select at least one Group No. for every Contract") - else: - #Resets the Data Editor with Blank Values - with st.spinner('Running...'): - # csv_buf = StringIO() - # additional_info.to_csv(csv_buf, header=True, index=False) - # csv_buf.seek(0) - # s3_client.put_object(Bucket='doczy-dev-infra-raw-data-ingestion', Body=csv_buf.getvalue(), Key='training_interface/request_submission.csv') - # csv_buf = StringIO() - # edited_df.to_csv(csv_buf, header=True, index=False) - # csv_buf.seek(0) - # s3_client.put_object(Bucket='doczy-dev-infra-raw-data-ingestion', Body=csv_buf.getvalue(), Key='training_interface/contract_config.csv') - # try: - # save_to_sf('load_request_and_contract_submissions', request_submission_file_name = "request_submission.csv", contract_config_file_name = "contract_config.csv") - # except Exception as e: - # st.write(e) - response = requests.post(doczy_pipeline, json = myobj) - if response.status_code >= 200 and response.status_code < 300: - st.write("Success") - #Updates and Reruns the Code after everything on the interface has completed - update_dataframe() - st.rerun() - # st.write(myobj) + buttons = st.columns([0.8, 0.2]) + with buttons[0]: + st.download_button("Download Table", csv, "file.csv", "text/csv", key='download-csv') + with buttons[1]: + if st.button("Run Doczy.AI Pipeline"): + if not st.session_state.contract_count == len(edited_df): + st.error("Select at least one Group No. for every Contract") else: - st.write("Failed") - # st.write(response.text) + #Resets the Data Editor with Blank Values + with st.spinner('Running...'): + # csv_buf = StringIO() + # additional_info.to_csv(csv_buf, header=True, index=False) + # csv_buf.seek(0) + # s3_client.put_object(Bucket='doczy-dev-infra-raw-data-ingestion', Body=csv_buf.getvalue(), Key='training_interface/request_submission.csv') + # csv_buf = StringIO() + # edited_df.to_csv(csv_buf, header=True, index=False) + # csv_buf.seek(0) + # s3_client.put_object(Bucket='doczy-dev-infra-raw-data-ingestion', Body=csv_buf.getvalue(), Key='training_interface/contract_config.csv') + # try: + # save_to_sf('load_request_and_contract_submissions', request_submission_file_name = "request_submission.csv", contract_config_file_name = "contract_config.csv") + # except Exception as e: + # st.write(e) + response = requests.post(doczy_pipeline, json = myobj) + if response.status_code >= 200 and response.status_code < 300: + st.write("Success") + #Updates and Reruns the Code after everything on the interface has completed + update_dataframe() + st.rerun() + # st.write(myobj) + else: + st.write("Failed") + # st.write(response.text) From a94e644e101e6e672291c28dd50dd59201bd0840 Mon Sep 17 00:00:00 2001 From: "AARETE\\agupta" Date: Fri, 21 Jun 2024 16:35:18 -0500 Subject: [PATCH 22/43] UI 2 Snowflake Query changed --- streamlit/interface_2.py | 2 +- streamlit/multipage/pages/Interface_2.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/streamlit/interface_2.py b/streamlit/interface_2.py index ec38e22..26a5e88 100644 --- a/streamlit/interface_2.py +++ b/streamlit/interface_2.py @@ -196,7 +196,7 @@ if client: fields = fields[fields['PRIORITY'] == 'E'] if st.button("Show Results"): - query = 'select * from "DOCZY_PIPELINE_RAW_OUTPUT"' + query = 'select * from "DOCZY_PIPELINE_RAW_OUTPUT_AC"' cur.execute(query) df2 = pd.DataFrame.from_records(iter(cur), columns=[x[0] for x in cur.description]) diff --git a/streamlit/multipage/pages/Interface_2.py b/streamlit/multipage/pages/Interface_2.py index 7323247..85a8a81 100644 --- a/streamlit/multipage/pages/Interface_2.py +++ b/streamlit/multipage/pages/Interface_2.py @@ -196,7 +196,7 @@ if client: fields = fields[fields['PRIORITY'] == 'E'] if st.button("Show Results"): - query = 'select * from "DOCZY_PIPELINE_RAW_OUTPUT"' + query = 'select * from "DOCZY_PIPELINE_RAW_OUTPUT_AC"' cur.execute(query) df2 = pd.DataFrame.from_records(iter(cur), columns=[x[0] for x in cur.description]) From 89d2deb515317a0003b5aacb5bf494c8d989640a Mon Sep 17 00:00:00 2001 From: Pratham Soni Date: Mon, 24 Jun 2024 10:13:32 -0500 Subject: [PATCH 23/43] session state for SSO auth --- streamlit/security.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/streamlit/security.py b/streamlit/security.py index d7465d2..2433203 100644 --- a/streamlit/security.py +++ b/streamlit/security.py @@ -2,6 +2,7 @@ import streamlit as st import msal import requests import boto3 +import secrets from botocore.exceptions import ClientError import json @@ -57,7 +58,11 @@ app = msal.ConfidentialClientApplication(CLIENT_ID, authority=AUTHORITY, client_ def get_auth_url(REDIRECT_URI): - auth_url = app.get_authorization_request_url(SCOPE, redirect_uri=REDIRECT_URI ) + #Generates a random stat parameter + state = secrets.token_urlsafe(16) + #Stores the state param in the session state + st.session_state['state'] = state + auth_url = app.get_authorization_request_url(SCOPE, redirect_uri=REDIRECT_URI, state=state) return auth_url @st.cache_data @@ -75,11 +80,13 @@ def get_user_info(access_token): def handle_redirect(REDIRECT_URI): if not st.session_state.get('access_token'): + query_params = st.experimental_get_query_params() code = st.query_params.get('code') - if code: - access_token = get_token_from_code(code, REDIRECT_URI) + state=query_params.get('state') + if code and state and state[0] == st.session_state.get('state'): + access_token = get_token_from_code(code[0], REDIRECT_URI) st.session_state['access_token'] = access_token - st.session_state + clear_url() From d80f3699cd6da32193ba665fb93efb7a2c054338 Mon Sep 17 00:00:00 2001 From: Pratham Soni Date: Mon, 24 Jun 2024 10:52:12 -0500 Subject: [PATCH 24/43] revert SSO changes --- streamlit/security.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/streamlit/security.py b/streamlit/security.py index 2433203..d7465d2 100644 --- a/streamlit/security.py +++ b/streamlit/security.py @@ -2,7 +2,6 @@ import streamlit as st import msal import requests import boto3 -import secrets from botocore.exceptions import ClientError import json @@ -58,11 +57,7 @@ app = msal.ConfidentialClientApplication(CLIENT_ID, authority=AUTHORITY, client_ def get_auth_url(REDIRECT_URI): - #Generates a random stat parameter - state = secrets.token_urlsafe(16) - #Stores the state param in the session state - st.session_state['state'] = state - auth_url = app.get_authorization_request_url(SCOPE, redirect_uri=REDIRECT_URI, state=state) + auth_url = app.get_authorization_request_url(SCOPE, redirect_uri=REDIRECT_URI ) return auth_url @st.cache_data @@ -80,13 +75,11 @@ def get_user_info(access_token): def handle_redirect(REDIRECT_URI): if not st.session_state.get('access_token'): - query_params = st.experimental_get_query_params() code = st.query_params.get('code') - state=query_params.get('state') - if code and state and state[0] == st.session_state.get('state'): - access_token = get_token_from_code(code[0], REDIRECT_URI) + if code: + access_token = get_token_from_code(code, REDIRECT_URI) st.session_state['access_token'] = access_token - clear_url() + st.session_state From 39a83798bc9a976e8c7a31b542c8e032cfc501e4 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 12:28:49 +0200 Subject: [PATCH 25/43] [DOC-535] Snowsql test --- bitbucket-pipelines.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 81b887c..1acbe09 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -291,11 +291,11 @@ pipelines: name: Run Snowsql in CICD oidc: true script: -# - *aws-context-dev - - export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO:role/$OIDC_ROLE - - export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600) - - export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId') - - export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey') - - export AWS_SESSION_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken') + - *aws-context-dev +# - export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO:role/$OIDC_ROLE +# - export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600) +# - export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId') +# - export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey') +# - export AWS_SESSION_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken') - aws sts get-caller-identity --output text - bash snowsql-cicd.sh dev \ No newline at end of file From 9e5ad4979820750ea864e9c723b3cff22d12c85b Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 14:54:44 +0200 Subject: [PATCH 26/43] [DOC-535] Snowsql test --- bitbucket-pipelines.yml | 15 +++--- textract-pipeline/terraform/main.tf | 17 +++++++ textract-pipeline/terraform/snowsql-cicd.sh | 48 +++++++++++++++++++ textract-pipeline/terraform/snowsql-query.sql | 1 + 4 files changed, 74 insertions(+), 7 deletions(-) create mode 100755 textract-pipeline/terraform/snowsql-cicd.sh create mode 100644 textract-pipeline/terraform/snowsql-query.sql diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 1acbe09..9d303ed 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -291,11 +291,12 @@ pipelines: name: Run Snowsql in CICD oidc: true script: - - *aws-context-dev -# - export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO:role/$OIDC_ROLE -# - export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600) -# - export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId') -# - export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey') -# - export AWS_SESSION_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken') +# - *aws-context-dev + - export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO:role/$OIDC_ROLE + - export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600) + - export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId') + - export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey') + - export AWS_SESSION_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken') - aws sts get-caller-identity --output text - - bash snowsql-cicd.sh dev \ No newline at end of file +# - bash snowsql-cicd.sh dev + - IS_DEPLOY_ALL=true python terraform.py apply --environment=dev --module=textract-pipeline/terraform --apply-extra-args="-target=null_resource.snowsql_client_create" diff --git a/textract-pipeline/terraform/main.tf b/textract-pipeline/terraform/main.tf index d3f8114..07a5403 100644 --- a/textract-pipeline/terraform/main.tf +++ b/textract-pipeline/terraform/main.tf @@ -101,6 +101,23 @@ module "textract_pipeline" { text_creation_lambda_additional_layer_arn = var.text_creation_lambda_additional_layer_arn } +resource "null_resource" "snowsql_client_create" { + + # for each for multiple resources + for_each = var.client_list + + triggers = { + is_changed = uuid() + } + + provisioner "local-exec" { + environment = { + } + command = "./snowsql-cicd.sh dev ${each.value.client_name}" + } +} + + # Create lambda_role resource "aws_iam_role" "lambda_role" { name = local.lambda_role_name diff --git a/textract-pipeline/terraform/snowsql-cicd.sh b/textract-pipeline/terraform/snowsql-cicd.sh new file mode 100755 index 0000000..1dfa76b --- /dev/null +++ b/textract-pipeline/terraform/snowsql-cicd.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash + +set -euo pipefail + +ENV_GROUP=$1 +CLIENT_NAME=$2 + +echo "ENV_GROUP=${ENV_GROUP}, CLIENT_NAME=${CLIENT_NAME}" + +readonly SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )" + +#echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}, SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}, SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" +# +#export GIT_ROOT=$(git rev-parse --show-toplevel) +#if [ "$(type -t changes)" != 'function' ]; then +# source "${GIT_ROOT}/infra/scripts/bootstrap-diff-changes.sh" +#fi +# +#if is_deploy_module "streamlit"; then +# echo -e "${GREEN}# Streamlit module changes have been detected. Executing the upload command...${NC}" +#else +# echo -e "${YELLOW}# There are no Streamlit module changes. Skipping the upload command...${NC}" +# exit 0 +#fi + +echo "# Getting snowflake creds from AWS Secret Manager..." +snowflake_secret_name="doczy-${ENV_GROUP}-db-svc-acc" +snowflake_secret_value=$(aws secretsmanager get-secret-value --secret-id "$snowflake_secret_name" --query 'SecretString' --output text) + +SNOWFLAKE_ACCOUNT=$(echo $snowflake_secret_value | jq -r '.account_locator') +SNOWFLAKE_DATABASE=$(echo $snowflake_secret_value | jq -r '.database') +SNOWFLAKE_USER=$(echo $snowflake_secret_value | jq -r '.user') +SNOWFLAKE_ROLE=$(echo $snowflake_secret_value | jq -r '.role') + +SNOWFLAKE_PASSWORD=$(echo $snowflake_secret_value | jq -r '.password') + +#SNOWFLAKE_ACCOUNT=OQ11564.us-east-2.aws +#SNOWFLAKE_DATABASE=DOCZY_DEV +#SNOWFLAKE_USER=aws_svc_account +#SNOWFLAKE_ROLE=DEVADMIN + +echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}" +echo "SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}" +echo "SNOWFLAKE_USER=${SNOWFLAKE_USER}" +echo "SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" +export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} + +snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" diff --git a/textract-pipeline/terraform/snowsql-query.sql b/textract-pipeline/terraform/snowsql-query.sql new file mode 100644 index 0000000..60d93f3 --- /dev/null +++ b/textract-pipeline/terraform/snowsql-query.sql @@ -0,0 +1 @@ +SELECT DISTINCT client_name, bucket_name FROM STG.CLIENT_LOGS \ No newline at end of file From f04aae9a73742f448290bc050f0321134b1aeee5 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 15:04:51 +0200 Subject: [PATCH 27/43] [DOC-535] Snowsql test --- bitbucket-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 9d303ed..63f6051 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -287,7 +287,7 @@ pipelines: feature/DOC-535-snowsql-in-cicd: - step: - image: hugree/snowflake-snowsql:latest + image: terraform-with-snowsql:latest name: Run Snowsql in CICD oidc: true script: From 2b988f7fc054dd638338f13f26c5a8544544592a Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 15:06:55 +0200 Subject: [PATCH 28/43] [DOC-535] Snowsql test --- bitbucket-pipelines.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 63f6051..6553773 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -287,7 +287,7 @@ pipelines: feature/DOC-535-snowsql-in-cicd: - step: - image: terraform-with-snowsql:latest + image: hugree/terraform-with-snowsql:latest name: Run Snowsql in CICD oidc: true script: From ed31dce8f72bb0c1e2b17cd4257f7976035207ca Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 15:16:50 +0200 Subject: [PATCH 29/43] [DOC-535] Snowsql test --- textract-pipeline/terraform/snowsql-cicd.sh | 2 ++ textract-pipeline/terraform/snowsql-query.sql | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/textract-pipeline/terraform/snowsql-cicd.sh b/textract-pipeline/terraform/snowsql-cicd.sh index 1dfa76b..eaa1b28 100755 --- a/textract-pipeline/terraform/snowsql-cicd.sh +++ b/textract-pipeline/terraform/snowsql-cicd.sh @@ -46,3 +46,5 @@ echo "SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" + +#snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('001','CLIENT_NAME','doczyai-use2-d-cn1')" diff --git a/textract-pipeline/terraform/snowsql-query.sql b/textract-pipeline/terraform/snowsql-query.sql index 60d93f3..fce5dd1 100644 --- a/textract-pipeline/terraform/snowsql-query.sql +++ b/textract-pipeline/terraform/snowsql-query.sql @@ -1 +1,2 @@ -SELECT DISTINCT client_name, bucket_name FROM STG.CLIENT_LOGS \ No newline at end of file +SELECT DISTINCT client_id, client_name, bucket_name FROM STG.CLIENT_LOGS +--INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('001','CLIENT_NAME','doczyai-use2-d-cn1') From 07ce5fddda4bd85717b5b35d12126b301d0578fb Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 15:20:12 +0200 Subject: [PATCH 30/43] [DOC-535] Snowsql test --- textract-pipeline/terraform/snowsql-cicd.sh | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/textract-pipeline/terraform/snowsql-cicd.sh b/textract-pipeline/terraform/snowsql-cicd.sh index eaa1b28..0cdad3c 100755 --- a/textract-pipeline/terraform/snowsql-cicd.sh +++ b/textract-pipeline/terraform/snowsql-cicd.sh @@ -45,6 +45,5 @@ echo "SNOWFLAKE_USER=${SNOWFLAKE_USER}" echo "SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} -snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" - -#snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('001','CLIENT_NAME','doczyai-use2-d-cn1')" +#snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" +snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('001','CLIENT_NAME','doczyai-use2-d-${CLIENT_NAME}')" From 4fd8758c7a6d4bc06db7155ecea74667d8d93c3c Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 15:24:49 +0200 Subject: [PATCH 31/43] [DOC-535] Snowsql test --- textract-pipeline/terraform/snowsql-cicd.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/textract-pipeline/terraform/snowsql-cicd.sh b/textract-pipeline/terraform/snowsql-cicd.sh index 0cdad3c..f9bcdf8 100755 --- a/textract-pipeline/terraform/snowsql-cicd.sh +++ b/textract-pipeline/terraform/snowsql-cicd.sh @@ -45,5 +45,5 @@ echo "SNOWFLAKE_USER=${SNOWFLAKE_USER}" echo "SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} -#snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" -snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('001','CLIENT_NAME','doczyai-use2-d-${CLIENT_NAME}')" +snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" +#snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('001','CLIENT_NAME','doczyai-use2-d-${CLIENT_NAME}')" From bb518543a8b6a70377ae57b488da51139457fb2a Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 16:11:07 +0200 Subject: [PATCH 32/43] [DOC-535] Snowsql test --- bitbucket-pipelines.yml | 18 +++++++++++++++++- textract-pipeline/terraform/snowsql-cicd.sh | 3 ++- textract-pipeline/terraform/snowsql-query.sql | 2 -- textract-pipeline/terraform/variables.tf | 5 ----- textract-pipeline/terraform/vars-dev.tfvars | 8 +++++++- textract-pipeline/terraform/vars-uat.tfvars | 6 ++++++ 6 files changed, 32 insertions(+), 10 deletions(-) delete mode 100644 textract-pipeline/terraform/snowsql-query.sql diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 6553773..41000ea 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -286,9 +286,10 @@ pipelines: feature/DOC-535-snowsql-in-cicd: + - parallel: - step: image: hugree/terraform-with-snowsql:latest - name: Run Snowsql in CICD + name: Run Snowsql on DEV oidc: true script: # - *aws-context-dev @@ -300,3 +301,18 @@ pipelines: - aws sts get-caller-identity --output text # - bash snowsql-cicd.sh dev - IS_DEPLOY_ALL=true python terraform.py apply --environment=dev --module=textract-pipeline/terraform --apply-extra-args="-target=null_resource.snowsql_client_create" + + - step: + image: hugree/terraform-with-snowsql:latest + name: Run Snowsql on UAT + oidc: true + script: + # - *aws-context-dev + - export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO_UAT:role/$OIDC_ROLE_UAT + - export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600) + - export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId') + - export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey') + - export AWS_SESSION_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken') + - aws sts get-caller-identity --output text + # - bash snowsql-cicd.sh dev + - IS_DEPLOY_ALL=true python terraform.py apply --environment=uat --module=textract-pipeline/terraform --apply-extra-args="-target=null_resource.snowsql_client_create" diff --git a/textract-pipeline/terraform/snowsql-cicd.sh b/textract-pipeline/terraform/snowsql-cicd.sh index f9bcdf8..84e0880 100755 --- a/textract-pipeline/terraform/snowsql-cicd.sh +++ b/textract-pipeline/terraform/snowsql-cicd.sh @@ -45,5 +45,6 @@ echo "SNOWFLAKE_USER=${SNOWFLAKE_USER}" echo "SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} -snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" +#snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" +snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "SELECT DISTINCT client_id, client_name, bucket_name FROM STG.CLIENT_LOGS" #snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('001','CLIENT_NAME','doczyai-use2-d-${CLIENT_NAME}')" diff --git a/textract-pipeline/terraform/snowsql-query.sql b/textract-pipeline/terraform/snowsql-query.sql deleted file mode 100644 index fce5dd1..0000000 --- a/textract-pipeline/terraform/snowsql-query.sql +++ /dev/null @@ -1,2 +0,0 @@ -SELECT DISTINCT client_id, client_name, bucket_name FROM STG.CLIENT_LOGS ---INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('001','CLIENT_NAME','doczyai-use2-d-cn1') diff --git a/textract-pipeline/terraform/variables.tf b/textract-pipeline/terraform/variables.tf index 73ee0fc..c29c60c 100644 --- a/textract-pipeline/terraform/variables.tf +++ b/textract-pipeline/terraform/variables.tf @@ -19,11 +19,6 @@ variable "client_list" { type = map(object({ client_name = string })) - default = { - client1 = { - client_name = "cn1" - } - } } variable "aws_account_id" { diff --git a/textract-pipeline/terraform/vars-dev.tfvars b/textract-pipeline/terraform/vars-dev.tfvars index 02453a3..eee4077 100644 --- a/textract-pipeline/terraform/vars-dev.tfvars +++ b/textract-pipeline/terraform/vars-dev.tfvars @@ -1,2 +1,8 @@ aws_account_id = 660131068782 -text_creation_lambda_additional_layer_arn = "arn:aws:lambda:us-east-2:336392948345:layer:AWSSDKPandas-Python312:8" \ No newline at end of file +text_creation_lambda_additional_layer_arn = "arn:aws:lambda:us-east-2:336392948345:layer:AWSSDKPandas-Python312:8" + +client_list = { + client1 = { + client_name = "cn1" + } +} \ No newline at end of file diff --git a/textract-pipeline/terraform/vars-uat.tfvars b/textract-pipeline/terraform/vars-uat.tfvars index b6e909a..8223bd2 100644 --- a/textract-pipeline/terraform/vars-uat.tfvars +++ b/textract-pipeline/terraform/vars-uat.tfvars @@ -1,3 +1,9 @@ aws_account_id = 975049960860 # TODO provide new layer arn for UAT? 336392948345 was used in DEV but not sure what account that is?! text_creation_lambda_additional_layer_arn = "arn:aws:lambda:us-east-2:336392948345:layer:AWSSDKPandas-Python312:8" + +client_list = { + client1 = { + client_name = "cn1" + } +} \ No newline at end of file From a791b89125aaf712478801eed63c37c097d4bf8e Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 17:06:46 +0200 Subject: [PATCH 33/43] [DOC-535] Snowsql test --- bitbucket-pipelines.yml | 16 ++++++++-------- textract-pipeline/terraform/main.tf | 17 ++++++++++------- textract-pipeline/terraform/snowsql-cicd.sh | 19 ++++++++++++++++--- textract-pipeline/terraform/variables.tf | 3 ++- textract-pipeline/terraform/vars-dev.tfvars | 3 ++- textract-pipeline/terraform/vars-uat.tfvars | 3 ++- 6 files changed, 40 insertions(+), 21 deletions(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 41000ea..fc0e6e8 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -292,13 +292,13 @@ pipelines: name: Run Snowsql on DEV oidc: true script: -# - *aws-context-dev - - export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO:role/$OIDC_ROLE - - export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600) - - export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId') - - export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey') - - export AWS_SESSION_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken') - - aws sts get-caller-identity --output text + - *aws-context-dev +# - export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO:role/$OIDC_ROLE +# - export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600) +# - export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId') +# - export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey') +# - export AWS_SESSION_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken') +# - aws sts get-caller-identity --output text # - bash snowsql-cicd.sh dev - IS_DEPLOY_ALL=true python terraform.py apply --environment=dev --module=textract-pipeline/terraform --apply-extra-args="-target=null_resource.snowsql_client_create" @@ -307,7 +307,7 @@ pipelines: name: Run Snowsql on UAT oidc: true script: - # - *aws-context-dev + # - *aws-context-uat - export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO_UAT:role/$OIDC_ROLE_UAT - export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600) - export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId') diff --git a/textract-pipeline/terraform/main.tf b/textract-pipeline/terraform/main.tf index 07a5403..4c7a8ac 100644 --- a/textract-pipeline/terraform/main.tf +++ b/textract-pipeline/terraform/main.tf @@ -7,11 +7,6 @@ terraform { } backend "s3" { - bucket = "doczyai-use2-d-infra-s3-terraform-state" # Parameterize using -backend-config flag with "terraform init" - key = "terraform/textract-pipeline/terraform.tfstate" # Parameterize - region = "us-east-2" # Parameterize - # profile = "doczyai" # Parameterize - dynamodb_table = "doczyai-use2-d-infra-dyd-terraform-lock" # Parameterize encrypt = true } } @@ -66,7 +61,11 @@ locals { Environment = var.environment Terraform = "true" } + + s3_prefix = "${local.global_prefix}-s3" + processing_s3_bucket_name = "${local.s3_prefix}-textract-processing-001" } + provider "aws" { default_tags { @@ -80,7 +79,7 @@ module "textract_pipeline" { # for each for multiple resources for_each = var.client_list - client_name = each.value.client_name + client_name = each.value.client_id aws_account_id = var.aws_account_id @@ -113,7 +112,11 @@ resource "null_resource" "snowsql_client_create" { provisioner "local-exec" { environment = { } - command = "./snowsql-cicd.sh dev ${each.value.client_name}" +# doczyai-use2-u-cn1-s3-textract-processing-001 +# doczyai-use2-d-infra-s3-textract-processing-001 +# doczyai-use2-d-cn1-s3-textract-processing-001 + + command = "./snowsql-cicd.sh dev ${each.value.client_id} ${each.value.full_name} ${replace(local.processing_s3_bucket_name, "infra", each.value.client_id)}" } } diff --git a/textract-pipeline/terraform/snowsql-cicd.sh b/textract-pipeline/terraform/snowsql-cicd.sh index 84e0880..f2753b7 100755 --- a/textract-pipeline/terraform/snowsql-cicd.sh +++ b/textract-pipeline/terraform/snowsql-cicd.sh @@ -3,9 +3,11 @@ set -euo pipefail ENV_GROUP=$1 -CLIENT_NAME=$2 +CLIENT_ID=$2 +FULL_NAME=$3 +BUCKET_NAME=$4 -echo "ENV_GROUP=${ENV_GROUP}, CLIENT_NAME=${CLIENT_NAME}" +echo "ENV_GROUP=${ENV_GROUP}, CLIENT_ID=${CLIENT_ID}, FULL_NAME=${FULL_NAME}, BUCKET_NAME=${BUCKET_NAME}" readonly SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )" @@ -47,4 +49,15 @@ export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} #snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "SELECT DISTINCT client_id, client_name, bucket_name FROM STG.CLIENT_LOGS" -#snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('001','CLIENT_NAME','doczyai-use2-d-${CLIENT_NAME}')" + +#QUERY="INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('${CLIENT_ID}','${FULL_NAME}','${BUCKET_NAME}')" +#echo "QUERY=${QUERY}" +#snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "${QUERY}" + +#INSERT INTO STG.CLIENT_LOGS (CLIENT_ID, CLIENT_NAME, BUCKET_NAME) +#SELECT '001', 'CLIENT_NAME', 'doczyai-use2-d-CLIENT_NAME' +#WHERE NOT EXISTS ( +# SELECT 1 +# FROM STG.CLIENT_LOGS +# WHERE CLIENT_ID = '001' +#); \ No newline at end of file diff --git a/textract-pipeline/terraform/variables.tf b/textract-pipeline/terraform/variables.tf index c29c60c..c4fb014 100644 --- a/textract-pipeline/terraform/variables.tf +++ b/textract-pipeline/terraform/variables.tf @@ -17,7 +17,8 @@ variable "environment" { variable "client_list" { type = map(object({ - client_name = string + client_id = string + full_name = string })) } diff --git a/textract-pipeline/terraform/vars-dev.tfvars b/textract-pipeline/terraform/vars-dev.tfvars index eee4077..4ca3365 100644 --- a/textract-pipeline/terraform/vars-dev.tfvars +++ b/textract-pipeline/terraform/vars-dev.tfvars @@ -3,6 +3,7 @@ text_creation_lambda_additional_layer_arn = "arn:aws:lambda:us-east-2:3363929483 client_list = { client1 = { - client_name = "cn1" + client_id = "cn1" + full_name = "Centene" } } \ No newline at end of file diff --git a/textract-pipeline/terraform/vars-uat.tfvars b/textract-pipeline/terraform/vars-uat.tfvars index 8223bd2..b7365e0 100644 --- a/textract-pipeline/terraform/vars-uat.tfvars +++ b/textract-pipeline/terraform/vars-uat.tfvars @@ -4,6 +4,7 @@ text_creation_lambda_additional_layer_arn = "arn:aws:lambda:us-east-2:3363929483 client_list = { client1 = { - client_name = "cn1" + client_id = "cn1" + full_name = "Centene" } } \ No newline at end of file From 6bfc495c919a12bd2d0b66cbefcbc75773188dee Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 17:10:34 +0200 Subject: [PATCH 34/43] [DOC-535] Snowsql test --- textract-pipeline/terraform/snowsql-cicd.sh | 8 +++++--- textract-pipeline/terraform/vars-dev.tfvars | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/textract-pipeline/terraform/snowsql-cicd.sh b/textract-pipeline/terraform/snowsql-cicd.sh index f2753b7..8a71548 100755 --- a/textract-pipeline/terraform/snowsql-cicd.sh +++ b/textract-pipeline/terraform/snowsql-cicd.sh @@ -48,11 +48,13 @@ echo "SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} #snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" + +QUERY="INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('${CLIENT_ID}','${FULL_NAME}','${BUCKET_NAME}')" +echo "QUERY=${QUERY}" +snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "${QUERY}" + snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "SELECT DISTINCT client_id, client_name, bucket_name FROM STG.CLIENT_LOGS" -#QUERY="INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('${CLIENT_ID}','${FULL_NAME}','${BUCKET_NAME}')" -#echo "QUERY=${QUERY}" -#snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "${QUERY}" #INSERT INTO STG.CLIENT_LOGS (CLIENT_ID, CLIENT_NAME, BUCKET_NAME) #SELECT '001', 'CLIENT_NAME', 'doczyai-use2-d-CLIENT_NAME' diff --git a/textract-pipeline/terraform/vars-dev.tfvars b/textract-pipeline/terraform/vars-dev.tfvars index 4ca3365..68214b0 100644 --- a/textract-pipeline/terraform/vars-dev.tfvars +++ b/textract-pipeline/terraform/vars-dev.tfvars @@ -5,5 +5,9 @@ client_list = { client1 = { client_id = "cn1" full_name = "Centene" + }, + client2 = { + client_id = "fc" + full_name = "Fidelis" } } \ No newline at end of file From dadb1219feb69e09ec3b939c7c23fb82a2aa13ee Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 17:26:06 +0200 Subject: [PATCH 35/43] [DOC-535] Snowsql test --- textract-pipeline/terraform/snowsql-cicd.sh | 30 ++++++++++++--------- textract-pipeline/terraform/vars-dev.tfvars | 4 --- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/textract-pipeline/terraform/snowsql-cicd.sh b/textract-pipeline/terraform/snowsql-cicd.sh index 8a71548..ab6697e 100755 --- a/textract-pipeline/terraform/snowsql-cicd.sh +++ b/textract-pipeline/terraform/snowsql-cicd.sh @@ -49,17 +49,23 @@ export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} #snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" -QUERY="INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('${CLIENT_ID}','${FULL_NAME}','${BUCKET_NAME}')" -echo "QUERY=${QUERY}" -snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "${QUERY}" - -snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "SELECT DISTINCT client_id, client_name, bucket_name FROM STG.CLIENT_LOGS" +#QUERY="INSERT INTO STG.CLIENT_LOGS(CLIENT_ID, CLIENT_NAME, BUCKET_NAME) VALUES ('${CLIENT_ID}','${FULL_NAME}','${BUCKET_NAME}')" +#echo "QUERY=${QUERY}" +#snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "${QUERY}" -#INSERT INTO STG.CLIENT_LOGS (CLIENT_ID, CLIENT_NAME, BUCKET_NAME) -#SELECT '001', 'CLIENT_NAME', 'doczyai-use2-d-CLIENT_NAME' -#WHERE NOT EXISTS ( -# SELECT 1 -# FROM STG.CLIENT_LOGS -# WHERE CLIENT_ID = '001' -#); \ No newline at end of file +INSERT_QUERY=$(cat < Date: Tue, 25 Jun 2024 17:28:35 +0200 Subject: [PATCH 36/43] [DOC-535] Snowsql test --- textract-pipeline/terraform/snowsql-cicd.sh | 22 ++------------------- textract-pipeline/terraform/vars-dev.tfvars | 4 ++++ 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/textract-pipeline/terraform/snowsql-cicd.sh b/textract-pipeline/terraform/snowsql-cicd.sh index ab6697e..559dcaa 100755 --- a/textract-pipeline/terraform/snowsql-cicd.sh +++ b/textract-pipeline/terraform/snowsql-cicd.sh @@ -11,20 +11,6 @@ echo "ENV_GROUP=${ENV_GROUP}, CLIENT_ID=${CLIENT_ID}, FULL_NAME=${FULL_NAME}, BU readonly SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )" -#echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}, SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}, SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" -# -#export GIT_ROOT=$(git rev-parse --show-toplevel) -#if [ "$(type -t changes)" != 'function' ]; then -# source "${GIT_ROOT}/infra/scripts/bootstrap-diff-changes.sh" -#fi -# -#if is_deploy_module "streamlit"; then -# echo -e "${GREEN}# Streamlit module changes have been detected. Executing the upload command...${NC}" -#else -# echo -e "${YELLOW}# There are no Streamlit module changes. Skipping the upload command...${NC}" -# exit 0 -#fi - echo "# Getting snowflake creds from AWS Secret Manager..." snowflake_secret_name="doczy-${ENV_GROUP}-db-svc-acc" snowflake_secret_value=$(aws secretsmanager get-secret-value --secret-id "$snowflake_secret_name" --query 'SecretString' --output text) @@ -36,11 +22,6 @@ SNOWFLAKE_ROLE=$(echo $snowflake_secret_value | jq -r '.role') SNOWFLAKE_PASSWORD=$(echo $snowflake_secret_value | jq -r '.password') -#SNOWFLAKE_ACCOUNT=OQ11564.us-east-2.aws -#SNOWFLAKE_DATABASE=DOCZY_DEV -#SNOWFLAKE_USER=aws_svc_account -#SNOWFLAKE_ROLE=DEVADMIN - echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}" echo "SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}" echo "SNOWFLAKE_USER=${SNOWFLAKE_USER}" @@ -53,7 +34,6 @@ export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} #echo "QUERY=${QUERY}" #snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -q "${QUERY}" - INSERT_QUERY=$(cat < Date: Tue, 25 Jun 2024 17:34:35 +0200 Subject: [PATCH 37/43] [DOC-535] Snowsql test --- .gitignore | 3 +++ textract-pipeline/terraform/main.tf | 2 +- textract-pipeline/terraform/snowsql-cicd.sh | 8 ++++---- textract-pipeline/terraform/vars-dev.tfvars | 2 +- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index e49dd36..dd5df53 100644 --- a/.gitignore +++ b/.gitignore @@ -71,3 +71,6 @@ streamlit/results.csv # env streamlit/venv + +*.tfplan +*.pem \ No newline at end of file diff --git a/textract-pipeline/terraform/main.tf b/textract-pipeline/terraform/main.tf index 4c7a8ac..15d3f1b 100644 --- a/textract-pipeline/terraform/main.tf +++ b/textract-pipeline/terraform/main.tf @@ -116,7 +116,7 @@ resource "null_resource" "snowsql_client_create" { # doczyai-use2-d-infra-s3-textract-processing-001 # doczyai-use2-d-cn1-s3-textract-processing-001 - command = "./snowsql-cicd.sh dev ${each.value.client_id} ${each.value.full_name} ${replace(local.processing_s3_bucket_name, "infra", each.value.client_id)}" + command = "./snowsql-cicd.sh dev ${each.value.client_id} '${each.value.full_name}' ${replace(local.processing_s3_bucket_name, "infra", each.value.client_id)}" } } diff --git a/textract-pipeline/terraform/snowsql-cicd.sh b/textract-pipeline/terraform/snowsql-cicd.sh index 559dcaa..1f854be 100755 --- a/textract-pipeline/terraform/snowsql-cicd.sh +++ b/textract-pipeline/terraform/snowsql-cicd.sh @@ -2,10 +2,10 @@ set -euo pipefail -ENV_GROUP=$1 -CLIENT_ID=$2 -FULL_NAME=$3 -BUCKET_NAME=$4 +ENV_GROUP="$1" +CLIENT_ID="$2" +FULL_NAME="$3" +BUCKET_NAME="$4" echo "ENV_GROUP=${ENV_GROUP}, CLIENT_ID=${CLIENT_ID}, FULL_NAME=${FULL_NAME}, BUCKET_NAME=${BUCKET_NAME}" diff --git a/textract-pipeline/terraform/vars-dev.tfvars b/textract-pipeline/terraform/vars-dev.tfvars index a217776..9abd0f5 100644 --- a/textract-pipeline/terraform/vars-dev.tfvars +++ b/textract-pipeline/terraform/vars-dev.tfvars @@ -7,7 +7,7 @@ client_list = { full_name = "Centene" }, client1 = { - client_id = "cicd-demo" + client_id = "cicd-demo2" full_name = "CICD Demo" } } \ No newline at end of file From 2366a8aa7ebac84a048fc43c4e6740cb54f00b6c Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 17:58:33 +0200 Subject: [PATCH 38/43] [DOC-535] Snowsql test --- bitbucket-pipelines.yml | 42 +++++++++++++------------------------- snowsql-cicd.sh | 45 ----------------------------------------- snowsql-query.sql | 1 - 3 files changed, 14 insertions(+), 74 deletions(-) delete mode 100755 snowsql-cicd.sh delete mode 100644 snowsql-query.sql diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index fc0e6e8..4e1aad3 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -287,32 +287,18 @@ pipelines: feature/DOC-535-snowsql-in-cicd: - parallel: - - step: - image: hugree/terraform-with-snowsql:latest - name: Run Snowsql on DEV - oidc: true - script: - - *aws-context-dev -# - export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO:role/$OIDC_ROLE -# - export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600) -# - export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId') -# - export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey') -# - export AWS_SESSION_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken') -# - aws sts get-caller-identity --output text -# - bash snowsql-cicd.sh dev - - IS_DEPLOY_ALL=true python terraform.py apply --environment=dev --module=textract-pipeline/terraform --apply-extra-args="-target=null_resource.snowsql_client_create" + - step: + image: hugree/terraform-with-snowsql:latest + name: Run Snowsql on DEV + oidc: true + script: + - *aws-context-dev + - IS_DEPLOY_ALL=true python terraform.py apply --environment=dev --module=textract-pipeline/terraform --apply-extra-args="-target=null_resource.snowsql_client_create" - - step: - image: hugree/terraform-with-snowsql:latest - name: Run Snowsql on UAT - oidc: true - script: - # - *aws-context-uat - - export AWS_ROLE_ARN=arn:aws:iam::$AWS_ACCOUNT_NO_UAT:role/$OIDC_ROLE_UAT - - export STS_OUTPUT=$(aws sts assume-role-with-web-identity --role-arn $AWS_ROLE_ARN --role-session-name BitbucketPipeline --web-identity-token "$BITBUCKET_STEP_OIDC_TOKEN" --duration-seconds 3600) - - export AWS_ACCESS_KEY_ID=$(echo $STS_OUTPUT | jq -r '.Credentials.AccessKeyId') - - export AWS_SECRET_ACCESS_KEY=$(echo $STS_OUTPUT | jq -r '.Credentials.SecretAccessKey') - - export AWS_SESSION_TOKEN=$(echo $STS_OUTPUT | jq -r '.Credentials.SessionToken') - - aws sts get-caller-identity --output text - # - bash snowsql-cicd.sh dev - - IS_DEPLOY_ALL=true python terraform.py apply --environment=uat --module=textract-pipeline/terraform --apply-extra-args="-target=null_resource.snowsql_client_create" + - step: + image: hugree/terraform-with-snowsql:latest + name: Run Snowsql on UAT + oidc: true + script: + - *aws-context-uat + - IS_DEPLOY_ALL=true python terraform.py apply --environment=uat --module=textract-pipeline/terraform --apply-extra-args="-target=null_resource.snowsql_client_create" diff --git a/snowsql-cicd.sh b/snowsql-cicd.sh deleted file mode 100755 index 4af0186..0000000 --- a/snowsql-cicd.sh +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/env bash - -set -euo pipefail - -ENV_GROUP=$1 - -readonly SCRIPT_DIR="$( cd "$( dirname "$0" )" && pwd )" - -#echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}, SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}, SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" -# -#export GIT_ROOT=$(git rev-parse --show-toplevel) -#if [ "$(type -t changes)" != 'function' ]; then -# source "${GIT_ROOT}/infra/scripts/bootstrap-diff-changes.sh" -#fi -# -#if is_deploy_module "streamlit"; then -# echo -e "${GREEN}# Streamlit module changes have been detected. Executing the upload command...${NC}" -#else -# echo -e "${YELLOW}# There are no Streamlit module changes. Skipping the upload command...${NC}" -# exit 0 -#fi - -echo "# Getting snowflake creds from AWS Secret Manager..." -snowflake_secret_name="doczy-${ENV_GROUP}-db-svc-acc" -snowflake_secret_value=$(aws secretsmanager get-secret-value --secret-id "$snowflake_secret_name" --query 'SecretString' --output text) - -SNOWFLAKE_ACCOUNT=$(echo $snowflake_secret_value | jq -r '.account_locator') -SNOWFLAKE_DATABASE=$(echo $snowflake_secret_value | jq -r '.database') -SNOWFLAKE_USER=$(echo $snowflake_secret_value | jq -r '.user') -SNOWFLAKE_ROLE=$(echo $snowflake_secret_value | jq -r '.role') - -SNOWFLAKE_PASSWORD=$(echo $snowflake_secret_value | jq -r '.password') - -#SNOWFLAKE_ACCOUNT=OQ11564.us-east-2.aws -#SNOWFLAKE_DATABASE=DOCZY_DEV -#SNOWFLAKE_USER=aws_svc_account -#SNOWFLAKE_ROLE=DEVADMIN - -echo "SNOWFLAKE_ACCOUNT=${SNOWFLAKE_ACCOUNT}" -echo "SNOWFLAKE_DATABASE=${SNOWFLAKE_DATABASE}" -echo "SNOWFLAKE_USER=${SNOWFLAKE_USER}" -echo "SNOWFLAKE_ROLE=${SNOWFLAKE_ROLE}" -export SNOWSQL_PWD=${SNOWFLAKE_PASSWORD} - -snowsql -a "${SNOWFLAKE_ACCOUNT}" -u "${SNOWFLAKE_USER}" -d "${SNOWFLAKE_DATABASE}" -r "${SNOWFLAKE_ROLE}" -o exit_on_error=true -f "${SCRIPT_DIR}/snowsql-query.sql" diff --git a/snowsql-query.sql b/snowsql-query.sql deleted file mode 100644 index 60d93f3..0000000 --- a/snowsql-query.sql +++ /dev/null @@ -1 +0,0 @@ -SELECT DISTINCT client_name, bucket_name FROM STG.CLIENT_LOGS \ No newline at end of file From 1cf4bdadb0e8b9a012f4fed71ce106e9268e27b5 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 18:00:53 +0200 Subject: [PATCH 39/43] [DOC-535] Snowsql test --- textract-pipeline/terraform/vars-dev.tfvars | 4 ---- 1 file changed, 4 deletions(-) diff --git a/textract-pipeline/terraform/vars-dev.tfvars b/textract-pipeline/terraform/vars-dev.tfvars index 9abd0f5..2de759a 100644 --- a/textract-pipeline/terraform/vars-dev.tfvars +++ b/textract-pipeline/terraform/vars-dev.tfvars @@ -6,8 +6,4 @@ client_list = { client_id = "cn1" full_name = "Centene" }, - client1 = { - client_id = "cicd-demo2" - full_name = "CICD Demo" - } } \ No newline at end of file From 812d448e45cb18f9866da28fa7c4cf71d4cce75f Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Tue, 25 Jun 2024 18:03:25 +0200 Subject: [PATCH 40/43] [DOC-535] cleanup cicd for terraform --- bitbucket-pipelines.yml | 34 ---------------------------------- 1 file changed, 34 deletions(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 4e1aad3..8ad0ee1 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -126,13 +126,6 @@ pipelines: script: - *aws-context-dev - python terraform.py apply --environment=dev --module=textract-pipeline/terraform - condition: - changesets: - includePaths: - - textract-pipeline/src/* - - textract-pipeline/src/**/* - - textract-pipeline/terraform/* - - textract-pipeline/terraform/**/* - step: name: Apply devops-pipeline on DEV image: hugree/bitbucket-aws-python38-tf154:latest @@ -140,11 +133,6 @@ pipelines: script: - *aws-context-dev - python terraform.py apply --environment=dev --module=devops-pipeline/other-resources - condition: - changesets: - includePaths: - - devops-pipeline/other-resources/* - - devops-pipeline/other-resources/**/* - step: name: Apply streamlit-server on DEV image: hugree/bitbucket-aws-python38-tf154:latest @@ -152,11 +140,6 @@ pipelines: script: - *aws-context-dev - python terraform.py apply --environment=dev --module=streamlit-server - condition: - changesets: - includePaths: - - streamlit-server/* - - streamlit-server/**/* # UAT and PROD pipelines will be updated with Streamlit steps once it is tested and ready @@ -168,13 +151,6 @@ pipelines: script: - *aws-context-uat - python terraform.py apply --environment=uat --module=textract-pipeline/terraform - condition: - changesets: - includePaths: - - textract-pipeline/src/* - - textract-pipeline/src/**/* - - textract-pipeline/terraform/* - - textract-pipeline/terraform/**/* - step: name: Apply devops-pipeline on UAT image: hugree/bitbucket-aws-python38-tf154:latest @@ -182,11 +158,6 @@ pipelines: script: - *aws-context-uat - python terraform.py apply --environment=uat --module=devops-pipeline/other-resources - condition: - changesets: - includePaths: - - devops-pipeline/other-resources/* - - devops-pipeline/other-resources/**/* - step: name: Apply streamlit-server on UAT image: hugree/bitbucket-aws-python38-tf154:latest @@ -194,11 +165,6 @@ pipelines: script: - *aws-context-uat - python terraform.py apply --environment=uat --module=streamlit-server - condition: - changesets: - includePaths: - - streamlit-server/* - - streamlit-server/**/* - step: <<: *snowflake_deploy condition: From 9356fb0dd740f9760d6280abb7c80f1a7426b7fb Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Wed, 26 Jun 2024 11:25:27 +0200 Subject: [PATCH 41/43] [DOC-535] fix missing pythong install --- bitbucket-pipelines.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 377d3c2..2ce920c 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -125,6 +125,7 @@ pipelines: oidc: true script: - *aws-context-dev + - pip install -r requirements.txt - python terraform.py apply --environment=dev --module=textract-pipeline/terraform - step: name: Apply devops-pipeline on DEV @@ -132,6 +133,7 @@ pipelines: oidc: true script: - *aws-context-dev + - pip install -r requirements.txt - python terraform.py apply --environment=dev --module=devops-pipeline/other-resources - step: name: Apply streamlit-server on DEV @@ -139,6 +141,7 @@ pipelines: oidc: true script: - *aws-context-dev + - pip install -r requirements.txt - python terraform.py apply --environment=dev --module=streamlit-server @@ -150,6 +153,7 @@ pipelines: oidc: true script: - *aws-context-uat + - pip install -r requirements.txt - python terraform.py apply --environment=uat --module=textract-pipeline/terraform - step: name: Apply devops-pipeline on UAT @@ -157,6 +161,7 @@ pipelines: oidc: true script: - *aws-context-uat + - pip install -r requirements.txt - python terraform.py apply --environment=uat --module=devops-pipeline/other-resources - step: name: Apply streamlit-server on UAT @@ -164,6 +169,7 @@ pipelines: oidc: true script: - *aws-context-uat + - pip install -r requirements.txt - python terraform.py apply --environment=uat --module=streamlit-server - step: <<: *snowflake_deploy From 8474791356d4b7060d6352f86530da51ef061fb2 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Wed, 26 Jun 2024 11:25:59 +0200 Subject: [PATCH 42/43] [DOC-535] fix missing pythong install --- bitbucket-pipelines.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index 2ce920c..f216093 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -271,6 +271,7 @@ pipelines: oidc: true script: - *aws-context-dev + - pip install -r requirements.txt - IS_DEPLOY_ALL=true python terraform.py apply --environment=dev --module=textract-pipeline/terraform --apply-extra-args="-target=null_resource.snowsql_client_create" - step: @@ -279,4 +280,5 @@ pipelines: oidc: true script: - *aws-context-uat + - pip install -r requirements.txt - IS_DEPLOY_ALL=true python terraform.py apply --environment=uat --module=textract-pipeline/terraform --apply-extra-args="-target=null_resource.snowsql_client_create" From 0559b239986f9f3606e528dcb7725bb645bc3af7 Mon Sep 17 00:00:00 2001 From: Grzegorz Huber Date: Wed, 26 Jun 2024 11:37:05 +0200 Subject: [PATCH 43/43] [DOC-535] fix wrong image for snowsql --- bitbucket-pipelines.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bitbucket-pipelines.yml b/bitbucket-pipelines.yml index f216093..e01bf18 100644 --- a/bitbucket-pipelines.yml +++ b/bitbucket-pipelines.yml @@ -121,7 +121,7 @@ pipelines: - airflow/dags/* - step: name: Apply textract-pipeline on DEV - image: hugree/bitbucket-aws-python38-tf154:latest + image: hugree/terraform-with-snowsql:latest oidc: true script: - *aws-context-dev @@ -149,7 +149,7 @@ pipelines: UAT: - step: name: Apply textract-pipeline on UAT - image: hugree/bitbucket-aws-python38-tf154:latest + image: hugree/terraform-with-snowsql:latest oidc: true script: - *aws-context-uat @@ -231,7 +231,7 @@ pipelines: - BB_PIPELINE_BRANCH="DEV" python terraform.py plan --environment=dev --module=streamlit-server - step: name: Plan textract-pipeline on DEV - image: hugree/bitbucket-aws-python38-tf154:latest + image: hugree/terraform-with-snowsql:latest oidc: true script: - *aws-context-dev