-
Notifications
You must be signed in to change notification settings - Fork 3
/
onepassword_secrets.py
executable file
·627 lines (474 loc) · 18.4 KB
/
onepassword_secrets.py
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
import argparse
import json
import logging
import os
import re
import subprocess
import sys
from datetime import datetime, timezone
from io import StringIO
from tempfile import NamedTemporaryFile
from dotenv import dotenv_values
from sgqlc.endpoint.http import HTTPEndpoint
FLY_GRAPHQL_ENDPOINT = "https://api.fly.io/graphql"
DATE_FORMAT = "%Y/%m/%d %H:%M:%S"
DEFAULT_ENV_FILE_NAME = ".env"
ONE_PASSWORD_FILE_PATH_FIELD_NAME = "file_name" # noqa: S105
ONE_PASSWORD_NOTES_CONTENT_FIELD_NAME = "notesPlain" # noqa: S105
ONE_PASSWORD_SECURE_NOTE_CATEGORY = "Secure Note" # noqa: S105
DEFAULT_REMOTE_NAME = "origin"
class UserError(RuntimeError):
pass
def _setup_logger():
class Formatter(logging.Formatter):
def format(self, record):
if record.levelno == logging.INFO:
self._style._fmt = "%(message)s"
else:
self._style._fmt = "%(levelname)s: %(message)s"
return super().format(record)
logger = logging.getLogger()
logger.setLevel(logging.INFO)
if logger.hasHandlers():
logger.handlers.clear()
stdout_handler = logging.StreamHandler()
stdout_handler.setFormatter(Formatter())
logger.addHandler(stdout_handler)
return logger
logger = _setup_logger()
def get_1password_env_file_item_id(title_substring, vault=None):
secure_notes = json.loads(
_run_1password_command(
"item",
"list",
"--categories",
ONE_PASSWORD_SECURE_NOTE_CATEGORY,
vault=vault,
)
)
item_ids = [item["id"] for item in secure_notes if title_substring in item["title"].split(" ")]
if len(item_ids) == 0:
raise_error(
f"No 1password secure note found with a name containing {title_substring!r} "
f"in vault {vault}"
if vault
else "across all vaults"
)
if len(item_ids) > 1:
raise_error(
f"Found {len(item_ids)} 1password secure notes with a name containing "
f"{title_substring!r}, expected one. "
"Rename or use different 1password vaults in combination with the `--vault` option."
)
return item_ids[0]
def get_item_from_1password(item_id, vault=None):
return json.loads(_run_1password_command("item", "get", item_id, vault=vault))
def get_envs_from_1password(item_id, vault=None) -> str:
item = get_item_from_1password(item_id, vault=vault)
result = first(
field.get("value")
for field in item["fields"]
if field["id"] == ONE_PASSWORD_NOTES_CONTENT_FIELD_NAME
)
if result is None or result == "":
raise_error("Empty secrets, aborting")
return result or ""
def get_filename_from_1password(item_id, vault=None):
item = get_item_from_1password(item_id, vault=vault)
return first(
field.get("value")
for field in item["fields"]
if field["label"] == ONE_PASSWORD_FILE_PATH_FIELD_NAME
)
def get_fly_auth_token():
return json.loads(
subprocess.check_output(["fly", "auth", "token", "--json"]) # noqa: S603, S607
)["token"]
def _get_file_contents(filepath, raise_if_not_found=True):
try:
with open(filepath) as file:
return file.read()
except FileNotFoundError:
if raise_if_not_found:
raise_error(f"Env file {filepath!r} not found!")
return None
def _boolean_prompt(prompt: str):
user_input = ""
while user_input not in ["y", "n"]:
user_input = input(f"{prompt} (y/n): ").lower()
return user_input == "y"
def _make_fly_graphql_request(graphql_query, variables):
headers = {"Authorization": f"Bearer {get_fly_auth_token()}"}
endpoint = HTTPEndpoint(FLY_GRAPHQL_ENDPOINT, headers)
response = endpoint(query=graphql_query, variables=variables)
logger.debug(
f"Fly request:\n{graphql_query}\n{json.dumps(variables, indent=2)}\n\n"
f"Fly response:\n{json.dumps(response, indent=2)}\n"
)
if response.get("errors") is not None:
raise_error(json.dumps(response["errors"][0]))
return response["data"]
def update_fly_secrets(app_id, secrets):
secrets_input = [{"key": key, "value": value} for key, value in secrets.items()]
last_update_secrets_response = _make_fly_graphql_request(
"""
mutation(
$appId: ID!
$secrets: [SecretInput!]!
$replaceAll: Boolean!
) {
setSecrets(
input: {
appId: $appId
replaceAll: $replaceAll
secrets: $secrets
}
) {
app {
name
}
release {
version
}
}
}
""",
{"appId": app_id, "secrets": secrets_input, "replaceAll": True},
)
get_secrets_response = _make_fly_graphql_request(
"""
query(
$appName: String
) {
app(name: $appName){
secrets{
name
}
}
}
""",
{
"appName": app_id,
},
)
secrets_names_in_env_file = set(secrets.keys())
secret_names_in_fly = {secret["name"] for secret in get_secrets_response["app"]["secrets"]}
secrets_names_in_fly_only = secret_names_in_fly.difference(secrets_names_in_env_file)
if len(secrets_names_in_fly_only) > 0 and _boolean_prompt(
"The following secrets will be deleted from Fly: {}, Are you sure?".format(
", ".join(secrets_names_in_fly_only)
)
):
last_update_secrets_response = _make_fly_graphql_request(
"""
mutation(
$appId: ID!
$secretNames: [String!]!
) {
unsetSecrets(
input: {
appId: $appId
keys: $secretNames
}
){
release{
id
}
}
}
""",
{
"appId": app_id,
"secretNames": list(secrets_names_in_fly_only),
},
)
release = last_update_secrets_response.get("setSecrets", {}).get("release", None)
if release:
release_version = release.get("version", "unknown")
print(f"Releasing fly app {app_id} version {release_version}")
else:
print(
"Fly secrets updated, no release created, "
"make sure to trigger a re-deploy for the changes to apply."
)
def _prompt_secret_diff(previous_raw_secrets, new_raw_secrets):
previous_parsed_secrets = get_secrets_from_envs(previous_raw_secrets)
new_parsed_secrets = get_secrets_from_envs(new_raw_secrets)
previous_keys = set(previous_parsed_secrets.keys())
new_keys = set(new_parsed_secrets.keys())
deleted_keys = previous_keys.difference(new_keys)
added_keys = new_keys.difference(previous_keys)
keys_whos_value_changed = [
key
for key in previous_keys.intersection(new_keys)
if previous_parsed_secrets[key] != new_parsed_secrets[key]
]
if len(deleted_keys) == 0 and len(added_keys) == 0 and len(keys_whos_value_changed) == 0:
if not _boolean_prompt("No changes detected, proceed?"):
raise_error("Aborted by user")
return
if not _boolean_prompt(
"Change summary\n{}\nProceed?".format(
"\n".join(
" {}: {}".format(label, ", ".join(items))
for label, items in {
"Deleted": deleted_keys,
"Added": added_keys,
"Modified": keys_whos_value_changed,
}.items()
if len(items) != 0
)
)
):
raise_error("Aborted by user")
def _run_1password_command(*args, vault=None, json_output=True):
command_args = ["op", *args]
if vault is not None:
command_args.extend(["--vault", vault])
if json_output:
command_args.extend(["--format", "json"])
logger.debug(
"Running command: {}".format(
" ".join((f'"{arg}"' if " " in arg else arg) for arg in command_args)
)
)
return subprocess.check_output(command_args) # noqa: S603
def create_1password_secrets(file_path, raw_secrets, title, vault=None):
logger.debug("Creating 1password secret note")
return json.loads(
_run_1password_command(
"item",
"create",
"--category",
ONE_PASSWORD_SECURE_NOTE_CATEGORY,
"--title",
title,
f"{ONE_PASSWORD_NOTES_CONTENT_FIELD_NAME}={raw_secrets}",
f"{ONE_PASSWORD_FILE_PATH_FIELD_NAME}[text]={file_path}",
_make_last_edited_1password_custom_field_cli_argument(),
vault=vault,
)
)
def update_1password_secrets(item_id, new_raw_secrets, previous_raw_secrets=None, vault=None):
if previous_raw_secrets is None:
previous_raw_secrets = get_envs_from_1password(item_id)
_prompt_secret_diff(
previous_raw_secrets=previous_raw_secrets,
new_raw_secrets=new_raw_secrets,
)
logger.debug(f"Updating 1password secret note content for item {item_id!r}")
_run_1password_command(
"item",
"edit",
item_id,
f"notesPlain={new_raw_secrets}",
_make_last_edited_1password_custom_field_cli_argument(),
vault=vault,
)
def update_1password_custom_field(item_id, field, value, vault=None):
logger.debug(f"Updating 1password custom field for item {item_id!r}")
_run_1password_command(
"item",
"edit",
item_id,
_make_1password_custom_field_cli_argument(field, value),
vault=vault,
)
def _make_1password_custom_field_cli_argument(field_name, value):
prefix = "Generated by 1password-secrets"
return f"{prefix}.{field_name}[text]={value}"
def _make_last_edited_1password_custom_field_cli_argument():
now_formatted = datetime.now(tz=timezone.utc).strftime(DATE_FORMAT)
return _make_1password_custom_field_cli_argument(
field_name="last edited at",
value=now_formatted,
)
def get_secrets_from_envs(input: str):
secrets = dotenv_values(stream=StringIO(input))
keys_with_values_null_values = [key for key, value in secrets.items() if value is None]
if len(keys_with_values_null_values) > 0:
raise_error(
"Failed to parse env file, values for the following keys are null: {}".format(
", ".join(keys_with_values_null_values)
)
)
return secrets
def import_1password_secrets_to_fly(app_id, vault=None):
item_id = get_1password_env_file_item_id(f"fly:{app_id}", vault=vault)
secrets = get_secrets_from_envs(get_envs_from_1password(item_id, vault=vault))
logger.debug(f"Secrets loaded from env: {json.dumps(secrets, indent=2)}\n")
update_fly_secrets(app_id, secrets)
now_formatted = datetime.now(tz=timezone.utc).strftime(DATE_FORMAT)
update_1password_custom_field(item_id, "last imported at", now_formatted, vault=vault)
def edit_1password_fly_secrets(app_id, vault=None):
item_id = get_1password_env_file_item_id(f"fly:{app_id}", vault=vault)
current_raw_secrets = get_envs_from_1password(item_id, vault=vault)
with NamedTemporaryFile("w+") as file:
file.writelines(current_raw_secrets)
file.flush()
subprocess.check_output(["code", "--wait", file.name]) # noqa: S603, S607
file.seek(0)
new_raw_secrets = file.read()
update_1password_secrets(
item_id,
new_raw_secrets=new_raw_secrets,
previous_raw_secrets=current_raw_secrets,
vault=vault,
)
if _boolean_prompt(
"Secrets updated in 1password, " f"do you wish to import secrets to the fly app {app_id}?"
):
import_1password_secrets_to_fly(app_id, vault=vault)
def pull_local_secrets(remote=DEFAULT_REMOTE_NAME, vault=None):
secret_note_label = get_secret_name_label_from_current_directory(remote=remote)
item_id = get_1password_env_file_item_id(secret_note_label, vault=vault)
secrets = get_envs_from_1password(item_id, vault=vault)
env_file_name = get_filename_from_1password(item_id, vault=vault) or DEFAULT_ENV_FILE_NAME
previous_raw_secrets = _get_file_contents(env_file_name, raise_if_not_found=False)
if previous_raw_secrets:
_prompt_secret_diff(
previous_raw_secrets=previous_raw_secrets,
new_raw_secrets=secrets,
)
with open(env_file_name, "w") as file:
file.writelines(secrets)
print(f"Successfully updated {env_file_name} from 1password")
def push_local_secrets(remote=DEFAULT_REMOTE_NAME, vault=None):
secret_note_label = get_secret_name_label_from_current_directory(remote=remote)
item_id = get_1password_env_file_item_id(secret_note_label, vault=vault)
env_file_name = get_filename_from_1password(item_id) or DEFAULT_ENV_FILE_NAME
secrets = _get_file_contents(env_file_name, raise_if_not_found=True)
update_1password_secrets(item_id, secrets, vault=vault)
print(f"Successfully pushed secrets from {env_file_name} to 1password")
def create_local_secrets(secrets_file_path, vault=None, remote=DEFAULT_REMOTE_NAME):
secret_note_label = get_secret_name_label_from_current_directory(remote=remote)
raw_secrets = _get_file_contents(secrets_file_path, raise_if_not_found=True)
title = f"{secrets_file_path} local development {secret_note_label}"
item = create_1password_secrets(
file_path=secrets_file_path, raw_secrets=raw_secrets, title=title, vault=vault
)
item_url = (
_run_1password_command(
"item",
"get",
item["id"],
"--share-link",
vault=vault,
json_output=False,
).decode("utf-8")
).strip()
print(f"Item {title!r} created in 1password!\n")
print(item_url)
print(item_url.replace("https://start.1password.com/", "onepassword://"))
def _get_git_remote_name(remote=DEFAULT_REMOTE_NAME) -> tuple[str | None, str | None]:
git_repository_regex = r"^(\w+)(:\/\/|@)([^\/:]+)[\/:]([^\/:]+)\/(.+).git$"
git_remote_url = None
try:
git_remote_url = (
subprocess.check_output( # noqa: S603
[ # noqa: S607
"git",
"config",
"--get",
f"remote.{remote}.url",
]
)
.decode("utf-8")
.strip()
)
except FileNotFoundError:
return ("git not in the PATH", None)
except subprocess.CalledProcessError as error:
exit_code, _command = error.args
if exit_code == 1:
return (f"Either not in a git repository or remote {remote!r} is not set", None)
return (f"Failed to retrieve the git remote {remote!r} url", None)
regex_match = re.match(git_repository_regex, git_remote_url)
if regex_match is None:
return (f'Failed to parse git remote "{remote}"', None)
return (
None,
f"{regex_match.group(4)}/{regex_match.group(5)}",
)
def get_secret_name_label_from_current_directory(remote=DEFAULT_REMOTE_NAME) -> str:
"""
Returns a predictable label for identifying the secrets based on the current directory.
If within a git repository with a remote named "origin" (and git is installed), it will output
something like: `repo:my-org/my-repo` or `repo:my-org/my-team/my-repo`.
Otherwise it will return the name of the directory as `local-dir:my-directory-name`.
"""
error_message, git_remote_name = _get_git_remote_name(remote=remote)
if not error_message:
return f"repo:{git_remote_name}"
directory_name = os.path.basename(os.getcwd())
label = f"local-dir:{directory_name}"
print(f"{error_message}, using the label based on the current directory: {label!r}")
return label
def raise_error(message):
print(message)
raise UserError(message)
def first(iterable):
try:
return next(iterable)
except StopIteration:
return None
def main():
parser = argparse.ArgumentParser(
description="1password-secrets is a set of utilities to sync 1Password secrets."
)
parser.add_argument(
"--debug",
action=argparse.BooleanOptionalAction,
type=bool,
default=False,
help="run in debug mode",
)
parser.add_argument(
"--vault",
type=str,
default=None,
help=(
"Specify a vault name or id to operate on. "
"Defaults to all vaults across the logged in account."
),
)
parser.add_argument(
"--remote",
type=str,
default=DEFAULT_REMOTE_NAME,
help='Construct secret name based on this git remote. Defaults to "origin"',
)
subparsers = parser.add_subparsers(dest="subcommand", required=True)
fly_parser = subparsers.add_parser("fly", help="manage fly secrets")
fly_parser.add_argument("action", type=str, choices=["import", "edit"])
fly_parser.add_argument("app_name", type=str, help="fly application name")
local_parser = subparsers.add_parser("local", help="manage local secrets")
local_subparsers = local_parser.add_subparsers(dest="action", required=True)
local_subparsers.add_parser("pull")
local_subparsers.add_parser("push")
create_parser = local_subparsers.add_parser("create")
create_parser.add_argument(
"secrets_file_path",
type=str,
help="secrets file path",
)
args = parser.parse_args()
if args.debug:
logger.setLevel(logging.DEBUG)
try:
if args.subcommand == "fly":
if args.action == "import":
import_1password_secrets_to_fly(args.app_name, vault=args.vault)
elif args.action == "edit":
edit_1password_fly_secrets(args.app_name, vault=args.vault)
elif args.subcommand == "local":
if args.action == "pull":
pull_local_secrets(remote=args.remote, vault=args.vault)
elif args.action == "push":
push_local_secrets(remote=args.remote, vault=args.vault)
elif args.action == "create":
create_local_secrets(args.secrets_file_path, vault=args.vault, remote=args.remote)
except UserError:
sys.exit(1)
if __name__ == "__main__":
main()