-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (109 loc) · 5.81 KB
/
Copy pathmain.py
File metadata and controls
129 lines (109 loc) · 5.81 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import sys
import time
import warnings
from glueops.setup_logging import configure as go_configure_logging
import vault_k8s_utils.utils as vault_k8s_utils
import secret_backend.config as secret_config
from secret_backend.config import BackupNotFoundError
# configure logger
logger = go_configure_logging(
name='VAULT_INIT',
level=os.getenv('PYTHON_LOG_LEVEL', 'INFO')
)
# To ignore https warnings
warnings.filterwarnings("ignore")
vault_status_url_path = '/v1/sys/health'
vault_unseal_url_path = '/v1/sys/unseal'
if __name__ == "__main__":
# Read values from environment variables if available, otherwise use default values
namespace = os.getenv("NAMESPACE", "glueops-core-vault")
vault_sts_name = os.getenv("VAULT_STS_NAME", "vault")
vault_k8s_service_name = os.getenv("VAULT_K8S_SERVICE_NAME", "vault-internal")
service_port = os.getenv("SERVICE_PORT", "8200")
vault_label_selector = os.getenv("VAULT_LABEL","app.kubernetes.io/name=vault")
reconcile_period = int(os.getenv("RECONCILE_PERIOD", "10"))
pause_reconcile = os.getenv("PAUSE_RECONCILE","false")
vault_key_shares = int(os.getenv("VAULT_KEY_SHARES","1"))
vault_key_threshold = int(os.getenv("VAULT_KEY_THRESHOLD","1"))
file_key = os.getenv("VAULT_SECRET_FILE", "vault_access.json")
restore_enabled = os.getenv("ENABLE_RESTORE","false").lower()
if restore_enabled == "true" and not secret_config.captain_domain:
logger.error("CAPTAIN_DOMAIN must be set when ENABLE_RESTORE is true")
sys.exit(1)
vaultClient = vault_k8s_utils.VaultManager(namespace,vault_sts_name,vault_k8s_service_name,service_port,vault_label_selector)
# Start the control loop to watch the vault pods
while(True):
# Pause reconciling if PAUSE_RECONCILE is true
if(pause_reconcile == "true"):
logger.info("Reconciling have been paused on vault servers...")
time.sleep(reconcile_period)
continue
logger.info("Reconciling on vault server...")
vault_pods = vaultClient.get_vault_pods()
if(len(vault_pods) == 0):
logger.info("No vault pods found with the label "+vault_label_selector+" in "+namespace)
time.sleep(reconcile_period)
continue
all_pods_running = True
for pod in vault_pods:
if(pod.status.phase == "Running"):
continue
else:
all_pods_running = False
break
if(all_pods_running == False):
logger.info("Not all vault pods are up and running....Retrying after 5 seconds")
time.sleep(reconcile_period)
continue
else:
logger.info("All vaults pods are up and running")
backup_found = False
backup_restored = False
# check vault status
if(not vaultClient.isVaultIntialized()):
# Check if a backup already exists, if yes restore
try:
latest_backup = secret_config.getLatestBackupfromS3()
config_file_exist = secret_config.configFileExists()
except BackupNotFoundError as e:
logger.error(str(e))
sys.exit(1)
except Exception as e:
logger.error(f"Failed to check for existing backups/keys in S3: {str(e)}. Skipping initialization to avoid data loss.")
time.sleep(reconcile_period)
continue
if restore_enabled=="true" and latest_backup and config_file_exist:
backup_found = True
logger.info("Backup found, intializing vault...")
temp_file_name = secret_config.file_key.replace("vault_access.json","vault_access_temp.json")
vaultClient.initializeVault(vault_key_shares,vault_key_threshold,temp_file_name)
else:
if(restore_enabled=="false"):
logger.info("Vault restore disabled, initializing vault from scratch..")
else:
missing = []
if not latest_backup:
missing.append("backup")
if not config_file_exist:
missing.append("config file")
logger.info(f"Missing {' and '.join(missing)} in S3, initializing vault from scratch..")
vaultClient.initializeVault(vault_key_shares,vault_key_threshold,file_key)
else:
logger.info('Vault is already Initialized')
secret_file = file_key
if restore_enabled=="true" and backup_found and not backup_restored:
secret_file = file_key.replace("vault_access.json","vault_access_temp.json")
for i in range(0,len(vault_pods)):
#check if each vault server in the cluster is unsealed
if(vaultClient.isVaultSealed("https://"+vault_sts_name+"-"+str(i)+"."+vault_k8s_service_name+"."+namespace+":"+service_port+vault_status_url_path)):
vaultClient.vaultUnseal("https://"+vault_sts_name+"-"+str(i)+"."+vault_k8s_service_name+"."+namespace+":"+service_port+vault_unseal_url_path,vault_key_threshold,secret_file)
else:
logger.info(vault_sts_name+"-"+str(i)+" is already unsealed")
if restore_enabled=="true" and backup_found and not backup_restored:
logger.info(f"Restoring vault from backup: {latest_backup['Key']}")
vaultClient.restoreVaultfromS3(latest_backup['Key'])
backup_restored = True
if(vaultClient.vaultHealthCheck() != None):
logger.info("Vault cluster is up and running")
time.sleep(reconcile_period)