-
Notifications
You must be signed in to change notification settings - Fork 0
80 lines (63 loc) · 2.69 KB
/
key.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
name: Rotate Storage
on:
workflow_dispatch:
inputs:
description: "Target Azure Environment to deploy to."
required: true
type: choice # This ensures you get a dropdown
options: # Options must be provided for the 'choice' type
- dev
- staging
- prod
default: dev
employee_Id:
description: "8 Digit employee ID (required only for prod)"
required: true
default: "NA"
jobs:
RotateStorageKeys:
name: Rotate Storage Account Keys if older than 300 days
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Azure CLI Login
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- name: Check Storage Account Keys Age and Rotate if necessary
id: rotate_keys
run: |
# Set variables
resource_group="${{secrets.AZURE_RESOURCE_GROUP}}"
storage_account="${{secrets.AZURE_STORAGE_ACCOUNT}}"
key_age_limit=30 # in days
# Function to check and rotate a key if necessary
check_and_rotate_key() {
key_index=$1
key_name=$2
# Get the key creation date
key_creation_date=$(az storage account keys list --resource-group $resource_group --account-name $storage_account --query "[$key_index].creationTime" --output tsv)
# Convert creation date to a Unix timestamp
key_creation_timestamp=$(date -d $key_creation_date +%s)
# Get the current date as a Unix timestamp
current_date=$(date +%s)
# Calculate the difference in days
age_in_days=$(( (current_date - key_creation_timestamp) / 86400 ))
# Check if the key is older than the defined limit
if [ $age_in_days -ge $key_age_limit ]; then
echo "$key_name is $age_in_days days old, triggering rotation."
# # Trigger key rotation
az storage account keys renew --resource-group $resource_group --account-name $storage_account --key $key_name
echo "$key_name rotated successfully."
# Set output variable to indicate key rotation
echo "::set-output name=KeyRotated::true"
else
echo "$key_name is $age_in_days days old, no action required."
echo "::set-output name=KeyRotated::false"
fi
}
# Check and rotate the primary key if necessary
check_and_rotate_key 0 "primary"
# Check and rotate the secondary key if necessary
check_and_rotate_key 1 "secondary"