Skip to content

Commit e3f2823

Browse files
authored
Enhancing Code Quality: Refactoring Credential Shell Scripts for Setup and Teardown (#315)
* Updated setup-for-wls-vm.sh and setup-for-wls-aks.sh scripts with necessary secrets for workflows. * Refactor environment variable file names for WLS VM and WLS AKS setups. (#123) * Refactored teardown script to remove redundant lines of code. * Update azure-credential-setup-wls-vm.sh and azure-credential-setup-wls-aks.sh scripts to create Azure Service Principal and set credentials as secret in the repository. * Refactor copyright information in credential setup and teardown scripts * Refactor setup and teardown scripts to use separate credentials params files.
1 parent 3d00723 commit e3f2823

18 files changed

+381
-631
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.sh text eol=lf
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
set -Eeuo pipefail
4+
5+
echo "Execute azure-credential-setup.sh - Start------------------------------------------"
6+
7+
## Create Azure Credentials
8+
SERVICE_PRINCIPAL_NAME_WLS_AKS="sp-${REPO_NAME}-wls-aks-$(date +%s)"
9+
echo "Creating Azure Service Principal with name: $SERVICE_PRINCIPAL_NAME_WLS_AKS"
10+
SUBSCRIPTION_ID=$(az account show --query id -o tsv| tr -d '\r\n')
11+
12+
AZURE_CREDENTIALS=$(az ad sp create-for-rbac --name ${SERVICE_PRINCIPAL_NAME_WLS_AKS} --role="Contributor" --scopes="/subscriptions/${SUBSCRIPTION_ID}" --sdk-auth --only-show-errors)
13+
SP_ID=$( az ad sp list --display-name $SERVICE_PRINCIPAL_NAME --query \[0\].id -o tsv | tr -d '\r\n')
14+
az role assignment create --assignee ${SP_ID} --scope="/subscriptions/${SUBSCRIPTION_ID}" --role "User Access Administrator"
15+
16+
## Set the Azure Credentials as a secret in the repository
17+
gh secret set "AZURE_CREDENTIALS" -b"${AZURE_CREDENTIALS}"
18+
gh variable set "SERVICE_PRINCIPAL_NAME_WLS_AKS" -b"${SERVICE_PRINCIPAL_NAME_WLS_AKS}"
19+
20+
echo "Execute azure-credential-setup.sh - End--------------------------------------------"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env bash
2+
3+
set -Eeuo pipefail
4+
5+
echo "Execute azure-credential-setup.sh - Start------------------------------------------"
6+
7+
## Create Azure Credentials
8+
SERVICE_PRINCIPAL_NAME_WLS_VM="sp-${REPO_NAME}-$(date +%s)"
9+
echo "Creating Azure Service Principal with name: $SERVICE_PRINCIPAL_NAME_WLS_VM"
10+
SUBSCRIPTION_ID=$(az account show --query id -o tsv| tr -d '\r\n')
11+
12+
SERVICE_PRINCIPAL=$(az ad sp create-for-rbac --name ${SERVICE_PRINCIPAL_NAME_WLS_VM} --role="Contributor" --scopes="/subscriptions/${SUBSCRIPTION_ID}" --sdk-auth --only-show-errors | base64 ${w0})
13+
AZURE_CREDENTIALS=$(echo $SERVICE_PRINCIPAL | base64 -d)
14+
15+
## Set the Azure Credentials as a secret in the repository
16+
gh secret set "AZURE_CREDENTIALS" -b"${AZURE_CREDENTIALS}"
17+
gh variable set "SERVICE_PRINCIPAL_NAME_WLS_VM" -b"${SERVICE_PRINCIPAL_NAME_WLS_VM}"
18+
19+
echo "Execute azure-credential-setup.sh - End--------------------------------------------"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
set -Eeuo pipefail
4+
5+
echo "Execute azure-credential-teardown.sh - Start------------------------------------------"
6+
7+
gh secret delete "AZURE_CREDENTIALS"
8+
SERVICE_PRINCIPAL_NAME_WLS_AKS=$(gh variable get "SERVICE_PRINCIPAL_NAME_WLS_AKS")
9+
az ad sp delete --id $(az ad sp list --display-name $SERVICE_PRINCIPAL_NAME_WLS_AKS --query "[].appId" -o tsv| tr -d '\r\n')
10+
11+
echo "Execute azure-credential-teardown.sh - End--------------------------------------------"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
set -Eeuo pipefail
4+
5+
echo "Execute azure-credential-teardown.sh - Start------------------------------------------"
6+
7+
gh secret delete "AZURE_CREDENTIALS"
8+
SERVICE_PRINCIPAL_NAME_WLS_VM=$(gh variable get "SERVICE_PRINCIPAL_NAME_WLS_VM")
9+
az ad sp delete --id $(az ad sp list --display-name $SERVICE_PRINCIPAL_NAME_WLS_VM --query "[].appId" -o tsv| tr -d '\r\n')
10+
11+
echo "Execute azure-credential-teardown.sh - End--------------------------------------------"
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env bash
2+
set -Eeuo pipefail
3+
4+
# ANSI color codes
5+
RED='\033[0;31m'
6+
NC='\033[0m' # No Color
7+
8+
echo "setup-credentials.sh - Start"
9+
10+
# Function to print error messages in red
11+
print_error() {
12+
local message=$1
13+
echo -e "${RED}Error: ${message}${NC}"
14+
}
15+
16+
check_parameters() {
17+
echo "Checking parameters..."
18+
local has_empty_value=0
19+
20+
while IFS= read -r line; do
21+
name=$(echo "$line" | yq -r '.name')
22+
value=$(echo "$line" | yq -r '.value')
23+
24+
if [ -z "$value" ] || [ "$value" == "null" ]; then
25+
print_error "The parameter '$name' has an empty/null value. Please provide a valid value."
26+
has_empty_value=1
27+
break
28+
else
29+
echo "Name: $name, Value: $value"
30+
fi
31+
done < <(yq eval -o=json '.[]' "$param_file" | jq -c '.')
32+
33+
echo "return $has_empty_value"
34+
return $has_empty_value
35+
}
36+
37+
# Function to set values from YAML
38+
set_values() {
39+
echo "Setting values..."
40+
yq eval -o=json '.[]' "$param_file" | jq -c '.' | while read -r line; do
41+
name=$(echo "$line" | jq -r '.name')
42+
value=$(echo "$line" | jq -r '.value')
43+
gh secret set "$name" -b"${value}"
44+
done
45+
}
46+
47+
# Main script execution
48+
main() {
49+
if check_parameters; then
50+
echo "All parameters are valid."
51+
set_values
52+
else
53+
echo "Parameter check failed. Exiting."
54+
exit 1
55+
fi
56+
57+
echo "setup-credentials.sh - Finish"
58+
}
59+
60+
# Run the main function
61+
main
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
set -Eeuo pipefail
3+
4+
echo "teardown-credentials.sh - Start"
5+
6+
# remove param the json
7+
yq eval -o=json '.[]' "$param_file" | jq -c '.' | while read -r line; do
8+
name=$(echo "$line" | jq -r '.name')
9+
value=$(echo "$line" | jq -r '.value')
10+
gh secret remove "$name"
11+
done
12+
13+
echo "teardown-credentials.sh - Finish"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# This file contains the parameters for the credentials used in the workflows.
2+
- name: ORC_SSOUSER
3+
value: ""
4+
description: "Oracle single sign-on userid."
5+
- name: ORC_SSOPSW
6+
value: ""
7+
description: "Password for Oracle single sign-on userid."
8+
- name: WDT_RUNTIMEPSW
9+
value: ""
10+
description: "Password for WebLogic Server and Runtime Deployment Tooling encryption."
11+
- name: WLS_PSW
12+
value: ${WDT_RUNTIMEPSW}
13+
description: "Password for WebLogic Server and Runtime Deployment Tooling encryption."
14+
# parameters for the credentials used in the workflows with default values.
15+
- name: WLS_USERNAME
16+
value: "weblogic"
17+
description: "WebLogic Server user name."
18+
- name: DB_PASSWORD
19+
value: "Secret123!"
20+
description: "Password for the database"
21+
- name: LOCATION
22+
value: "eastus"
23+
description: "Location of the resource group"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# This file contains the parameters for the credentials used in the workflows.
2+
- name: OTN_USERID
3+
value: ""
4+
description: Oracle single sign-on userid.
5+
- name: OTN_PASSWORD
6+
value: ""
7+
description: Password for Oracle single sign-on userid.
8+
- name: WLS_PSW
9+
value: ""
10+
description: Password for WebLogic Server.
11+
# Git credentials
12+
- name: USER_EMAIL
13+
value: ""
14+
description: User Email of GitHub acount to access GitHub repository.
15+
- name: USER_NAME
16+
value: ""
17+
description: User name of GitHub account
18+
- name: GIT_TOKEN
19+
value: ""
20+
description: GitHub token to access GitHub repository.
21+
# parameters for the credentials used in the workflows with default values.
22+
- name: LOCATION
23+
value: "eastus"
24+
description: Location of the resource group
25+
# Optional parameters:
26+
# if you want to use optional parameters, please uncomment the following lines
27+
#- name: ELK_URI
28+
# value: ""
29+
# description: URI (hostname:port) for Elastic server, leave blank if you don't want to integrate ELK.
30+
#- name: ELK_USER_NAME
31+
# value: ""
32+
# description: Account password for Elastic server, leave blank if you don't want to integrate ELK.
33+
#- name: ELK_PSW
34+
# value: ""
35+
# description: Account password for Elastic server, leave blank if you don't want to integrate ELK.

.github/resource/pre-check.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Check environment and tools required to run the script
2+
3+
# ANSI color codes
4+
GREEN='\033[0;32m'
5+
NC='\033[0m' # No Color
6+
7+
## Check if the required tools are installed and logged in
8+
echo -e "${GREEN}To run this script, you need to have the following tools installed:${NC}"
9+
echo -e "${GREEN}1. yq${NC}"
10+
echo -e "${GREEN}2. Github CLI (gh)${NC}"
11+
echo -e "${GREEN}3. Azure CLI (az)${NC}"
12+
echo -e "${GREEN}And you need to be logged in to GitHub CLI (gh), and Azure CLI (az).${NC}"
13+
14+
echo "Checking if the required tools are installed..."
15+
echo "Checking progress started..."
16+
17+
if ! command -v yq &> /dev/null; then
18+
echo "Check required tools and environment failed."
19+
echo "yq is not installed. Please install it to proceed."
20+
exit 1
21+
fi
22+
echo "1/6...yq is installed."
23+
24+
if ! command -v jq &> /dev/null; then
25+
echo "Check required tools and environment failed."
26+
echo "jq is not installed. Please install it to proceed."
27+
exit 1
28+
fi
29+
echo "2/6...jq is installed."
30+
31+
# Check gh installed
32+
if ! command -v gh &> /dev/null; then
33+
echo "Check required tools and environment failed."
34+
echo "GitHub CLI (gh) is not installed. Please install it to proceed."
35+
exit 1
36+
fi
37+
echo "3/6...GitHub CLI (gh) is installed."
38+
39+
40+
# Check if the GitHub CLI (gh) is logged in
41+
if ! gh auth status &> /dev/null; then
42+
echo "Check required tools and environment failed."
43+
echo "You are not logged in to GitHub CLI (gh). Please log in with `gh auth login` to proceed."
44+
exit 1
45+
fi
46+
echo "4/6...You are logged in to GitHub CLI (gh)."
47+
48+
# check if az is installed
49+
if ! command -v az &> /dev/null; then
50+
echo "Check required tools and environment failed."
51+
echo "Azure CLI (az) is not installed. Please install it to proceed."
52+
exit 1
53+
fi
54+
echo "5/6...Azure CLI (az) is installed."
55+
56+
57+
# check if az is logged in
58+
if ! az account show &> /dev/null; then
59+
echo "Check required tools and environment failed."
60+
echo "You are not logged in to Azure CLI (az). Please log in with command `az login` to proceed."
61+
exit 1
62+
fi
63+
echo "6/6...You are logged in to Azure CLI (az)."
64+
65+
echo "Checking progress completed..."

0 commit comments

Comments
 (0)