Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimized bash scripts #119

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 27 additions & 41 deletions scripts/common_utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,52 +14,38 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# modified by Adam Krawczyk - jamallorock

############################### utilities #################################

function check_running() {

check_pod=$1
prometheus_ns="monitoring"
kubectl_cmd="kubectl -n ${prometheus_ns}"
set -euo pipefail # Exit immediately on error

echo "Info: Waiting for ${check_pod} to come up..."
err_wait=0
while true;
do
sleep 2
${kubectl_cmd} get pods | grep ${check_pod}
pod_stat=$(${kubectl_cmd} get pods | grep ${check_pod} | awk '{ print $3 }')
case "${pod_stat}" in
"Running")
echo "Info: ${check_pod} deploy succeeded: ${pod_stat}"
err=0
break;
;;
"Error")
# On Error, wait for 10 seconds before exiting.
err_wait=$(( err_wait + 1 ))
if [ ${err_wait} -gt 5 ]; then
echo "Error: ${check_pod} deploy failed: ${pod_stat}"
err=-1
break;
fi
;;
*)
sleep 2
;;
esac
done
check_running() {
local check_pod="$1"
local prometheus_ns='monitoring'
local kubectl_cmd="kubectl -n ${prometheus_ns}"

${kubectl_cmd} get pods | grep ${check_pod}
echo
echo "Info: Waiting for ${check_pod} to become ready..."

# Use `kubectl wait` for more efficient waiting instead of polling
if ! "${kubectl_cmd}" get pod -l "app=${check_pod}" --no-headers | grep -q '.'; then
>&2 echo "Error: No pods found for app=${check_pod}. Exiting."
exit 1
fi
if ! ${kubectl_cmd} wait --for=condition=Ready pod -l "app=${check_pod}" --timeout=60s; then
>&2 echo "Error: ${check_pod} failed to become ready within timeout."
exit 1
fi
echo "Info: ${check_pod} is now running."
"${kubectl_cmd}" get pods -l "app=${check_pod}"
echo
}

# Check error code from last command, exit on error
# Check error code from the last command, exit on failure
check_err() {
err=$?
if [ ${err} -ne 0 ]; then
echo "$*"
exit -1
fi
}
local err=$?
if [ "${err}" -ne 0 ]; then
>&2 echo "Error: $*"
exit 1 # Use standard Unix exit code
fi
}
72 changes: 43 additions & 29 deletions scripts/generate-certs.sh
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
#!/usr/bin/env bash

set -e
set -o pipefail
set -euo pipefail # Improved: added 'u' to catch errors from undeclared variables

# 📍 Setting directories
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
OUTPUT_DIR="${SCRIPT_DIR}/../issuers/testdata"
CA_DIR="${OUTPUT_DIR}/ca"

if [ -f "${OUTPUT_DIR}/pkcs7.pem" ] && [ -f "${OUTPUT_DIR}/x509.pem" ]; then
printf 'Certificates already exist, skipping generation...\n'
# 📍 Checking if certificates already exist
if [[ -f "${OUTPUT_DIR}/pkcs7.pem" && -f "${OUTPUT_DIR}/x509.pem" ]]; then
echo "✅ Certificates already exist. Skipping generation..."
exit 0
fi

set -x
# 📍 Setting default values for CA certificate
COUNTRY="US"
STATE="YourState"
CITY="YourCity"
ORG="YourOrganization"
ORG_UNIT="YourOrganizationalUnit"
COMMON_NAME="adcs-issuer Test CA"
KEY_SIZE="4096"
DAYS_VALID="3650" # 10 years

mkdir -pv "${OUTPUT_DIR}/ca"
echo "🔧 Creating directories..."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "🔧 Creating directories..."
echo '🔧 Creating directories...'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change without impact. Doesn't understand why it changed to single.

mkdir -pv "${CA_DIR}"

# Create the CA key
openssl genrsa -out "${OUTPUT_DIR}/ca/ca.key" 2048
# Create a configuration file for the Root CA
# 📍 Generating CA private key
echo "🔑 Generating CA private key (${KEY_SIZE} bits)..."
openssl genrsa -out "${CA_DIR}/ca.key" "${KEY_SIZE}"

# Create CA config
cat > "${OUTPUT_DIR}/ca/ca.cnf" << EOF
# 📍 Creating CA configuration
echo "📜 Creating CA configuration..."
cat > "${CA_DIR}/ca.cnf" << EOF
[req]
default_bits = 4096
default_bits = ${KEY_SIZE}
prompt = no
default_md = sha256
distinguished_name = dn
x509_extensions = v3_ca

[dn]
C = US
ST = YourState
L = YourCity
O = YourOrganization
OU = YourOrganizationalUnit
CN = adcs-issuer Test CA
C = ${COUNTRY}
ST = ${STATE}
L = ${CITY}
O = ${ORG}
OU = ${ORG_UNIT}
CN = ${COMMON_NAME}

[v3_ca]
subjectKeyIdentifier = hash
Expand All @@ -43,16 +55,18 @@ basicConstraints = critical, CA:TRUE
keyUsage = critical, digitalSignature, keyCertSign
EOF

# Generate the CA cert
# 📍 Generating CA certificate
echo "📜 Generating CA certificate (valid for ${DAYS_VALID} days)..."
openssl req -x509 -new -nodes \
-key "${OUTPUT_DIR}/ca/ca.key" \
-key "${CA_DIR}/ca.key" \
-sha256 \
-days 3650 \
-out "${OUTPUT_DIR}/ca/ca.pem" \
-config "${OUTPUT_DIR}/ca/ca.cnf"

# This is probably wrong, but it seems the test
# just compares equality of the parsed pkcs7.pem to the raw x509.pem...
# TODO: review
cp -v "${OUTPUT_DIR}/ca/ca.pem" "${OUTPUT_DIR}/pkcs7.pem"
cp -v "${OUTPUT_DIR}/ca/ca.pem" "${OUTPUT_DIR}/x509.pem"
-days "${DAYS_VALID}" \
-out "${CA_DIR}/ca.pem" \
-config "${CA_DIR}/ca.cnf"

# 📍 Copying certificates to test files
echo "📂 Copying certificates..."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "📂 Copying certificates..."
echo '📂 Copying certificates...'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change without impact. Doesn't understand why it changed to single,

cp -v "${CA_DIR}/ca.pem" "${OUTPUT_DIR}/pkcs7.pem"
cp -v "${CA_DIR}/ca.pem" "${OUTPUT_DIR}/x509.pem"

echo "✅ Certificates successfully generated!"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
echo "✅ Certificates successfully generated!"
echo '✅ Certificates successfully generated!'

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change without impact. Doesn't understand why it changed to single.

Loading