Repository navigation
Expand file tree
/
Copy pathsetenv.py
More file actions
executable file
·513 lines (431 loc) · 21.1 KB
/
Copy pathsetenv.py
File metadata and controls
executable file
·513 lines (431 loc) · 21.1 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!env python3
import argparse
import csv
import base64
import json
import os
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
from datetime import datetime, timedelta
ENV_VAR_PREFIX = 'INPUT_'
TRUTHY_VALUES = {'1', 'true', 'yes', 'on'}
FALSY_VALUES = {'0', 'false', 'no', 'off', ''}
def get_env_var_name(dest):
return f'{ENV_VAR_PREFIX}{dest.upper()}'
def add_env_var_help(parser):
for action in parser._actions:
if not action.option_strings or action.dest == 'help':
continue
env_var_name = get_env_var_name(action.dest)
help_text = action.help or ''
if f'[env: {env_var_name}]' not in help_text:
action.help = f'{help_text} [env: {env_var_name}]'.strip()
def parse_env_bool(action, raw_value):
normalized = raw_value.strip().lower()
if normalized in TRUTHY_VALUES:
return True
if normalized in FALSY_VALUES:
return False
raise ValueError(f'Invalid boolean value for {get_env_var_name(action.dest)}: {raw_value!r}')
def parse_env_list(raw_value):
return [value for value in next(csv.reader([raw_value], skipinitialspace=True)) if value]
def apply_env_defaults(parser):
for action in parser._actions:
if not action.option_strings or action.dest == 'help':
continue
raw_value = os.getenv(get_env_var_name(action.dest))
if raw_value is None:
continue
if isinstance(action, argparse._StoreTrueAction):
try:
action.default = parse_env_bool(action, raw_value)
except ValueError as exc:
parser.error(str(exc))
action.required = False
continue
if isinstance(action, argparse._AppendAction):
env_values = parse_env_list(raw_value)
if env_values:
default_values = list(action.default) if isinstance(action.default, list) else []
action.default = default_values + env_values
action.required = False
continue
if raw_value != '':
action.default = raw_value
action.required = False
def build_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--name', required=True, metavar='my-terraform-config', help='Vault app_role_applications name')
parser.add_argument('--env', required=True, metavar='prod', help='Vault environment eg: prod')
parser.add_argument('--cluster', action='append', metavar='cluster:role:namespace', default=[], help='Can be used multiple times, exported in KUBE_CONFIG_PATH: ~/.kube/atlantis/config')
parser.add_argument('--azure', action='store_true', help='Get Azure credentials, exported as ARM_CLIENT_ID and ARM_CLIENT_SECRET')
parser.add_argument('--azure-tenant-id', help='Azure tenant ID. Required if --azure is set')
parser.add_argument('--azure-subscription-id', help='Azure subscription ID. Subscription ID for which your state is stored. Required if --azure is set')
parser.add_argument('--azure-resource-group', help='Azure resource group name. Resource group for which your state is stored. Required if --azure is set')
parser.add_argument('--azure-no-arm', action='store_true', help='Do not export ARM_CLIENT_ID and ARM_CLIENT_SECRET, only TF_VAR_azure_client_id and TF_VAR_azure_client_secret')
parser.add_argument('--gcp', action='append', metavar='my-project', default=[], help='GCP project names, creates TF_VAR_gcp_project_name for use in "credentials" in google provider')
parser.add_argument('--terraform-registry', action='store_true', help='Get Terraform registry token, expects to be found in vault under "token" in secret/applications/{name}/{env}/terraform-registry')
parser.add_argument('--eval', action='store_true', help='Output as export statements, for use with eval $()')
parser.add_argument('--new-line', action='store_true', help='Output as key=value separated by newline. If not set, output will be comma separated')
parser.add_argument('--debug', action='store_true', help='Print progress messages')
parser.add_argument('--token', help='Vault token, defaults to /vault/secrets/token')
parser.add_argument('--vault-role-id-name', default='TF_VAR_vault_role_id', help='Name of the environment variable for Vault role ID, defaults to "TF_VAR_vault_role_id"')
parser.add_argument('--vault-secret-id-name', default='TF_VAR_vault_secret_id', help='Name of the environment variable for Vault secret ID, defaults to "TF_VAR_vault_secret_id"')
parser.add_argument('--vault-secret-id-cidr', default=None, help='CIDR to use for Vault secret ID, defaults to the IP address from --myip-url with /32 suffix')
parser.add_argument('--wait-time', default='60', help='Time to wait in seconds after Azure credentials have been validated, defaults to 60')
parser.add_argument('--cache', action='store_true', help='Cache/ use cached credentials')
parser.add_argument('--cache-file', help='Path and name to cache file, defaults to /tmp/{repo_name}_{workflow_name}.cache.json when running in atlantis, or /tmp/{current_workdir}.cache.json when running from shell')
parser.add_argument('--secret', action='store_true', help='Every key/value pair in vault applications "setenv" secret is added to env vars')
parser.add_argument('--vault-secret', action='append', metavar='path:key:var_name', help='Get secret from vault, specify path to secret, key in secret and name of environment variable to export. Can be used multiple times, e.g. --vault-secret secret/applications/myapp/prod:mykey:MY_VAR_NAME. If using * as key, all keys in secret will be exported with the specified var_name as prefix, e.g.: --vault-secret secret/applications/myapp/prod:*:MY_PREFIX_ will export MY_PREFIX_key1, MY_PREFIX_key2 etc.')
parser.add_argument('--myip-url', default='http://icanhazip.com', help='URL to get current IP address, default is http://icanhazip.com')
add_env_var_help(parser)
apply_env_defaults(parser)
return parser
args = build_parser().parse_args()
vault_role = f"{args.name}-{args.env}"
try:
wait_time = float(args.wait_time)
except ValueError:
wait_time = 60
def status(message, error=False, flush=False):
if args.debug or error:
print(message, file=sys.stderr, flush=flush)
github_actions = False
cache_path = ''
VAULT_ADDR = os.getenv('VAULT_ADDR', 'http://127.0.0.1:8200')
VAULT_TOKEN = ""
if os.getenv('GITHUB_ACTIONS'):
github_actions = True
if args.token not in (None, ''):
status("Using --token as vault token", False)
VAULT_TOKEN = args.token
elif os.getenv('VAULT_TOKEN'):
status("Using VAULT_TOKEN environment variable as vault token", False)
VAULT_TOKEN = os.getenv('VAULT_TOKEN') or ''
elif os.path.exists('/vault/secrets/token'):
status("Using /vault/secrets/token as vault token", False)
with open('/vault/secrets/token', 'r') as f:
VAULT_TOKEN = f.read().strip()
def get_cache_filename():
# If cache file is specified, use it
if args.cache_file:
return args.cache_file
# When running from shell, use current workdir
if args.token:
return f'/tmp/{os.getcwd().split("/")[-1]}.cache.json'
path_parts = os.getcwd().split("/")
if not len(path_parts) >= 3:
status("Could not generate cache filename, exiting...", True)
sys.exit(0)
# When running in atlantis, use repo and workflow name
return f"/tmp/{path_parts[-3]}_{path_parts[-2]}.cache.json"
cache_path = get_cache_filename()
status(f"Setting {cache_path} as cache path")
def get_credentials_cache():
"""Get credentials from cache file"""
if not args.cache:
return None
try:
if not os.path.exists(cache_path):
return None
with open(cache_path, 'r') as f:
cache_data = json.load(f)
cache_time = datetime.fromisoformat(cache_data['timestamp'])
if datetime.now() - cache_time < timedelta(minutes=45):
status("Using cached credentials...")
return cache_data['env_vars']
except Exception as e:
status(f"Error reading cache: {str(e)}", True)
return None
def set_credentials_cache(env_vars=None):
"""Save credentials to cache file"""
if not args.cache:
return None
if env_vars is not None:
try:
# Write file with restricted permissions (only owner can read/write)
with open(cache_path, 'w') as f:
os.chmod(cache_path, 0o600) # Set permissions to 600
json.dump({
'timestamp': datetime.now().isoformat(),
'env_vars': env_vars
}, f)
return None
except Exception as e:
status(f"Error saving cache: {str(e)}", True)
return None
def output_env_vars(env_vars):
"""Output environment variables in the specified format"""
if args.eval:
if github_actions:
status("Outputting environment variables for GitHub Actions...")
for k, v in env_vars:
status(f"{k} is available as output")
print(f'echo "::add-mask::{v}"')
print(f'echo "{k}={v}" >> $GITHUB_OUTPUT')
print(f'echo "{k}={v}" >> $GITHUB_ENV')
else:
status("Outputting environment variables for eval...")
print(*[f"{'export ' if args.eval else ''}{k}='{v}'" for k, v in env_vars], sep='\n' if args.new_line else ' ')
else:
status("Outputting environment variables...")
print(*[f"{k}='{v}'" for k, v in env_vars], sep='\n' if args.new_line else ',')
sys.exit(0)
def get_my_ip():
"""Get current IP address from specified URL"""
try:
with urllib.request.urlopen(args.myip_url) as response:
ip = response.read().decode().strip()
return ip
except Exception as e:
status(f"Error getting IP address from {args.myip_url}: {str(e)}", True)
sys.exit(1)
if args.vault_secret_id_cidr and args.vault_secret_id_cidr != "":
my_ip = args.vault_secret_id_cidr
status(f"Using --vault-secret-id-cidr {my_ip} as CIDR for secret ID", False)
else:
my_ip = f"{get_my_ip()}/32"
status(f"Using {my_ip} as CIDR for secret ID", False)
cached_env_vars = get_credentials_cache()
if cached_env_vars:
output_env_vars(cached_env_vars)
VAULT_ADDR = os.getenv('VAULT_ADDR', 'http://127.0.0.1:8200')
status(f"Using vault address: {VAULT_ADDR}", False)
def vault_request(path, method='GET', data=None):
req = urllib.request.Request(
f'{VAULT_ADDR}/v1/{path}',
data=json.dumps(data).encode() if data else None,
headers={
'X-Vault-Token': vault_token,
'Content-Type': 'application/json'
},
method=method
)
response = urllib.request.urlopen(req)
return json.loads(response.read())
def _azure_get_token(tenant_id: str, client_id: str, client_secret: str) -> str:
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
data = urllib.parse.urlencode(
{
"client_id": client_id,
"client_secret": client_secret,
"scope": "https://management.azure.com/.default",
"grant_type": "client_credentials",
}
).encode("utf-8")
req = urllib.request.Request(
token_url,
data=data,
headers={"Content-Type": "application/x-www-form-urlencoded"},
method="POST",
)
with urllib.request.urlopen(req, timeout=10) as resp:
payload = json.load(resp)
return payload["access_token"]
def azure_check_resource_group(tenant_id: str, subscription_id: str, resource_group: str, token: str) -> bool:
url = (
f"https://management.azure.com/subscriptions/{subscription_id}"
f"/providers/Microsoft.Storage/storageAccounts?api-version=2025-06-01"
)
req = urllib.request.Request(url, headers={"Authorization": f"Bearer {token}"})
try:
with urllib.request.urlopen(req, timeout=10) as resp:
payload = json.load(resp)
# status(f"Azure resource group access check response: {json.dumps(payload)}", args.debug, True)
if "value" in payload:
if isinstance(payload["value"], list):
for i, result in enumerate(payload["value"]):
status(f"Checking resource group in id: {payload['value'][i]['id']}", args.debug, True)
if resource_group.lower() in payload["value"][i]["id"].lower():
status(f"Azure resource group '{resource_group}' found.", args.debug, True)
return True
else:
status(f"Azure resource group '{resource_group}' not found in id: {payload['value'][i]['id']}", args.debug, True)
return False
else:
status(f"Azure resource group '{resource_group}' not found.", args.debug, True)
return False
else:
status(f"no values in response", args.debug, True)
return False
except urllib.error.URLError as exc:
status(f"Azure resource group access check URL error: {exc.reason}", args.debug, True)
if isinstance(exc.reason, TimeoutError):
status("Azure resource group access check timed out", True)
return False
return False
env_vars = []
vault_token = ""
# Log in using approle
try:
status(f"Getting wrapped secret ID for role {vault_role}...")
wrapped_token = json.loads(urllib.request.urlopen(urllib.request.Request(
f'{VAULT_ADDR}/v1/auth/approle/role/{vault_role}/secret-id',
data=json.dumps({"cidr_list": f"{my_ip}"}).encode(),
headers={'Content-Type': 'application/json', 'X-Vault-Wrap-TTL': '5m', 'X-Vault-Request': 'true', 'X-Vault-Token': VAULT_TOKEN},
method='PUT'
)).read())['wrap_info']['token']
status("Getting role ID...")
role_id = json.loads(urllib.request.urlopen(urllib.request.Request(
f'{VAULT_ADDR}/v1/auth/approle/role/{vault_role}/role-id',
headers={'X-Vault-Request': 'true', 'X-Vault-Token': VAULT_TOKEN}
)).read())['data']['role_id']
env_vars.append((args.vault_role_id_name, role_id))
status("Unwrapping secret ID...")
secret_id = json.loads(urllib.request.urlopen(urllib.request.Request(
f'{VAULT_ADDR}/v1/sys/wrapping/unwrap',
data='null'.encode(),
headers={'X-Vault-Request': 'true', 'X-Vault-Token': wrapped_token},
method='PUT'
)).read())['data']['secret_id']
env_vars.append((args.vault_secret_id_name, secret_id))
status("Logging in with AppRole...")
login_req = urllib.request.Request(
f'{VAULT_ADDR}/v1/auth/approle/login',
data=json.dumps({"role_id": role_id, "secret_id": secret_id}).encode(),
headers={'Content-Type': 'application/json'},
method='POST'
)
login_response = urllib.request.urlopen(login_req)
vault_token = json.loads(login_response.read())['auth']['client_token']
except urllib.error.HTTPError as e:
status(f"HTTP Error: {e.code} - {e.reason}", True)
status(f"Error response body: {e.read().decode()}", True)
sys.exit(1)
if args.cluster:
kubeconfig = {"apiVersion": "v1", "kind": "Config", "clusters": [], "contexts": [], "users": []}
kubeconfig["current-context"] = args.cluster[0].split(':')[0]
clusters = 0
for cluster_str in args.cluster:
if cluster_str == "None" or cluster_str == "":
continue
clusters += 1
status(f"Getting Kubernetes credentials for {cluster_str}...")
cluster, role, namespace = cluster_str.split(':')
cluster_info = vault_request(f'secret/applications/shared/kubernetes-config/{cluster}')['data']
creds = vault_request(
f'kubernetes-{cluster}/creds/{vault_role}-{role}',
method='PUT',
data={'kubernetes_namespace': namespace}
)['data']
kubeconfig["clusters"].append({
"name": cluster,
"cluster": {
"certificate-authority-data": base64.b64encode(cluster_info['ca_cert'].encode()).decode(),
"server": cluster_info['host']
}
})
kubeconfig["contexts"].append({
"name": cluster,
"context": {"cluster": cluster, "namespace": namespace, "user": f"{cluster}-{role}"}
})
kubeconfig["users"].append({
"name": f"{cluster}-{role}",
"user": {"token": creds['service_account_token']}
})
if clusters > 0:
kubeconfig_path = os.path.expanduser('~/.kube/atlantis/config')
os.makedirs(os.path.dirname(kubeconfig_path), exist_ok=True)
with open(kubeconfig_path, 'w') as f:
json.dump(kubeconfig, f, indent=2)
env_vars.append(("KUBE_CONFIG_PATH", kubeconfig_path))
if args.azure:
try:
status("Getting Azure credentials...")
azure_creds = vault_request(f'azure/creds/{vault_role}')['data']
env_vars.extend([
("TF_VAR_azure_client_id", azure_creds['client_id']),
("TF_VAR_azure_client_secret", azure_creds['client_secret'])
])
if not args.azure_no_arm:
env_vars.extend([
("ARM_CLIENT_ID", azure_creds['client_id']),
("ARM_CLIENT_SECRET", azure_creds['client_secret'])
])
tries=0
status("Validating Azure credentials", True, False)
while True:
token = None
try:
token = _azure_get_token(
tenant_id=args.azure_tenant_id,
client_id=azure_creds['client_id'],
client_secret=azure_creds['client_secret']
)
except urllib.error.HTTPError as token_err:
# Freshly created Azure SP credentials take time to propagate
# through Azure AD. A 401 here is expected during that window;
# retry until the credentials become usable.
body = ''
try:
body = token_err.read().decode()
except Exception:
pass
status(f"Azure token request returned {token_err.code} - {token_err.reason}: {body}", args.debug, True)
except urllib.error.URLError as token_err:
status(f"Azure token request URL error: {token_err.reason}", args.debug, True)
if token and azure_check_resource_group(
tenant_id=args.azure_tenant_id,
subscription_id=args.azure_subscription_id,
resource_group=args.azure_resource_group,
token=token
):
status("Azure credentials are valid and resource group is accessible.", args.debug, True)
break
else:
status(f".", False, False)
tries += 1
if tries >= 60:
status("\nAzure credentials did not propagate in time, exiting...", args.debug, True)
break
time.sleep(5)
status(f"sleeping for {wait_time} seconds", args.debug, True)
time.sleep(wait_time)
except urllib.error.HTTPError as e:
status(f"HTTP Error: {e.code} - {e.reason}", True)
status(f"Error response body: {e.read().decode()}", True)
sys.exit(1)
if args.gcp:
for project in args.gcp:
if project == "None" or project == "":
continue
status(f"Getting GCP credentials for project {project}...")
gcp_creds = vault_request(f'gcp/roleset/{vault_role}-{project}/key')
gcp_creds = gcp_creds['data']['private_key_data']
env_vars.append((f"TF_VAR_gcp_{project.replace('-', '_')}", gcp_creds))
if args.terraform_registry:
status("Getting Terraform registry token...")
registry_data = vault_request(f'secret/applications/{args.name}/{args.env}/terraform-registry')
registry_token = registry_data['data']['token']
env_vars.append(("TF_TOKEN_terraform__registry_nrk_cloud", registry_token))
if args.secret:
status("Getting secrets from 'setenv'")
secret_data = vault_request(f'secret/applications/{args.name}/{args.env}/setenv')
for k, v in secret_data['data'].items():
env_vars.append((k, v))
if args.vault_secret:
status("Getting secrets from vault...")
for vault_secret in args.vault_secret:
if vault_secret == "None" or vault_secret == "":
continue
path, key, var_name = vault_secret.split(':')
if key == '*':
status(f"Fetching all keys from secret at {path}...")
var_name = var_name or 'VAULT_SECRET_'
secret_data = vault_request(path)
for k, v in secret_data['data'].items():
status(f"Adding key '{var_name}{k}' to environment variables...")
env_vars.append((f"{var_name}{k}", v))
else:
status(f"Fetching key '{key}' from secret at {path}...")
var_name = var_name or 'VAULT_SECRET'
secret_data = vault_request(path)
if key not in secret_data['data']:
status(f"Key '{key}' not found in secret at {path}", True)
continue
env_vars.append((var_name, secret_data['data'][key]))
set_credentials_cache(env_vars)
output_env_vars(env_vars)