Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 81296ed6 authored by Arkadiusz SZCZECINSKI's avatar Arkadiusz SZCZECINSKI
Browse files

:sparkles: added registration of a new Netbox into Vault

parent d3ab6fa6
No related branches found
No related tags found
1 merge request!148Draft: Netbox password rotation service
Pipeline #274090 passed
...@@ -5,7 +5,9 @@ check_vault_connection() { ...@@ -5,7 +5,9 @@ check_vault_connection() {
local response local response
response=$(curl -s -o /dev/null -w "%{http_code}" "${VAULT_ADDR}/v1/sys/health") response=$(curl -s -o /dev/null -w "%{http_code}" "${VAULT_ADDR}/v1/sys/health")
if [[ "$response" -ne 200 && "$response" -ne 429 && "$response" -ne 472 && "$response" -ne 473 && "$response" -ne 501 ]]; then if [[ "$response" -eq 200 || "$response" -eq 429 || "$response" -eq 472 || "$response" -eq 473 || "$response" -eq 501 ]]; then
echo "[$(date)] SUCCESS: Vault is reachable."
else
echo "[$(date)] ERROR: Vault is unreachable. Response code: $response" echo "[$(date)] ERROR: Vault is unreachable. Response code: $response"
exit 1 exit 1
fi fi
...@@ -16,7 +18,6 @@ check_vault_password() { ...@@ -16,7 +18,6 @@ check_vault_password() {
local response local response
response=$(curl -s \ response=$(curl -s \
-H "X-Vault-Token: $VAULT_TOKEN" \ -H "X-Vault-Token: $VAULT_TOKEN" \
-H "X-Vault-Namespace: ${VAULT_NAMESPACE}" \
-X GET "${VAULT_ADDR}/v1/${VAULT_LOGIN_MOUNT_POINT}/data/${VAULT_PATH}/${NETBOX_USERNAME}") -X GET "${VAULT_ADDR}/v1/${VAULT_LOGIN_MOUNT_POINT}/data/${VAULT_PATH}/${NETBOX_USERNAME}")
if echo "$response" | jq -e '.data' > /dev/null; then if echo "$response" | jq -e '.data' > /dev/null; then
...@@ -32,12 +33,14 @@ check_vault_password() { ...@@ -32,12 +33,14 @@ check_vault_password() {
# Function to get default password and store it in Vault # Function to get default password and store it in Vault
register_default_password() { register_default_password() {
DEFAULT_PASSWORD=$(curl -s \ DEFAULT_PASSWORD=$(curl -s \
-H "X-Vault-Token: $VAULT_TOKEN" \ -H "X-Vault-Token: ${VAULT_TOKEN}" \
-H "X-Vault-Namespace: ${VAULT_NAMESPACE}" \ -H "X-Vault-Namespace: ${VAULT_NAMESPACE}" \
-X GET "${VAULT_ADDR}/v1/${VAULT_LOGIN_MOUNT_POINT}/data/${DEFAULT_VAULT_PATH}/${NETBOX_USERNAME}" | jq -r '.data.password') --tlsv1.2 \
--request GET \
"${VAULT_ADDR}/v1/${VAULT_LOGIN_MOUNT_POINT}/data/${DEFAULT_VAULT_PATH}" | jq -r '.data.data.password')
if [[ -z "$DEFAULT_PASSWORD" || "$DEFAULT_PASSWORD" == "null" ]]; then if [[ -z "$DEFAULT_PASSWORD" || "$DEFAULT_PASSWORD" == "null" ]]; then
echo "[$(date)] ERROR: Failed to retrieve default password from ${VAULT_LOGIN_MOUNT_POINT}/${DEFAULT_VAULT_PATH}/${NETBOX_USERNAME}. Exiting." echo "[$(date)] ERROR: Failed to retrieve default password from ${VAULT_ADDR}/v1/${VAULT_LOGIN_MOUNT_POINT}/data/${DEFAULT_VAULT_PATH}. Exiting."
exit 1 exit 1
fi fi
...@@ -47,23 +50,26 @@ register_default_password() { ...@@ -47,23 +50,26 @@ register_default_password() {
-H "X-Vault-Namespace: ${VAULT_NAMESPACE}" \ -H "X-Vault-Namespace: ${VAULT_NAMESPACE}" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
-X POST \ -X POST \
--data "{\"password\": \"${DEFAULT_PASSWORD}\"}" \ --data "{\"data\": {\"password\": \"${DEFAULT_PASSWORD}\"}}" \
"${VAULT_ADDR}/v1/${VAULT_LOGIN_MOUNT_POINT}/data/${VAULT_PATH}/${NETBOX_USERNAME}" "${VAULT_ADDR}/v1/${VAULT_LOGIN_MOUNT_POINT}/data/${VAULT_PATH}/${NETBOX_USERNAME}"
echo "[$(date)] Default password stored successfully." echo "[$(date)] Default password stored successfully."
} }
# Validate Vault connection before proceeding
check_vault_connection
while true; do while true; do
# Get the current day of the week (7=Sunday) and current hour # Get the current day of the week (7=Sunday) and current hour
DAY=$(date +%u) # 7 = Sunday on some systems, use 7 if needed DAY=$(date +%u) # 7 = Sunday on some systems, use 7 if needed
HOUR=$(date +%H) HOUR=$(date +%H)
MIN=$(date +%M) MIN=$(date +%M)
# Validate Vault connection before proceeding
check_vault_connection
# Check if it's Sunday (7) and the time is 02:00 AM # Check if it's Sunday (7) and the time is 02:00 AM
if [[ "$DAY" -eq 7 && "$HOUR" -eq 02 ]]; then if [[ "$DAY" -eq 7 && "$HOUR" -eq 02 ]]; then
# Validate Vault connection before proceeding
check_vault_connection
echo "[$(date)] Executing password rotation script." echo "[$(date)] Executing password rotation script."
JSON_DATA="{\"role_id\":\"${VAULT_ROLE_ID}\",\"secret_id\":\"${VAULT_SECRET_ID}\"}" JSON_DATA="{\"role_id\":\"${VAULT_ROLE_ID}\",\"secret_id\":\"${VAULT_SECRET_ID}\"}"
...@@ -100,7 +106,7 @@ while true; do ...@@ -100,7 +106,7 @@ while true; do
} }
}" }"
echo "[$(date)] Password rotation completed successfully." # echo "[$(date)] Password rotation completed successfully."
# Sleep for 1 hour to avoid multiple executions in the same minute # Sleep for 1 hour to avoid multiple executions in the same minute
sleep 3600 sleep 3600
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment