-
Notifications
You must be signed in to change notification settings - Fork 2
/
gsm.py
executable file
·148 lines (120 loc) · 4.31 KB
/
gsm.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
#!/usr/bin/env python3
#
""" A script to interact with Google Secret Manager in a manner
consistent with our usage of External Secrets Operator.
Examples:
# list the secret versions in the stage env
./gsm.py list -p moz-fx-testapp1-nonprod -e stage
# view the latest version of the stage secret [or version 5]
./gsm.py view -p moz-fx-testapp1-nonprod -e stage [-v 5]
# edit the latest version of the stage secret
# This will create a secret for you if one does not already exist
./gsm.py edit -p moz-fx-testapp1-nonprod -e stage
Tips:
- Use your preferred code editor:
Define an `EDITOR` environment variable in your local environment to override the
default `vi` value.
Examples: `nano`, `vim`, `code`.
"""
# Why am I using `subprocess` to shell out to `gcloud` instead of using the API?
# Well, it's quicker to write, for one.
# But mainly, this way doesn't require downloading and installing any additional
# python packages (like google-cloud-secret-manager) and all of its dependencies,
# which makes installing this script very much simpler.
#
# Also, I tried using the API first and got it to work once. Every time after
# that it would just time out. :(
#
import argparse
import sys
from argparse import ArgumentParser
from gsm_editor import commands
from gsm_editor.models import CommandConfig
def add_default_arguments(parser: ArgumentParser) -> None:
parser.add_argument(
"-p", "--project", type=str, required=True, help="GCP project id"
)
parser.add_argument(
"-e",
"--env",
type=str,
choices=["qa", "dev", "stage", "prod", "test"],
required=True,
help="secret env",
)
def add_select_arguments(parser: ArgumentParser) -> None:
parser.add_argument(
"-s",
"--secret",
type=str,
default="app",
required=False,
help='Custom secret identifier (default is "app")',
)
parser.add_argument(
"-v",
"--version",
type=str,
default="latest",
required=False,
help="Version of the secret to select",
)
def add_parser_args(parser: ArgumentParser) -> None:
subparsers = parser.add_subparsers(dest="action", help="Secret action:")
edit = subparsers.add_parser("edit", help="Edit a secret")
add_default_arguments(parser=edit)
add_select_arguments(parser=edit)
view = subparsers.add_parser("view", help="Display secret content in the terminal")
add_default_arguments(parser=view)
add_select_arguments(parser=view)
list_secrets = subparsers.add_parser("list", help="List all managed secrets in a project")
add_default_arguments(parser=list_secrets)
add_select_arguments(parser=list_secrets)
diff = subparsers.add_parser("diff",
help="Display differences between secret versions")
add_default_arguments(parser=diff)
diff.add_argument(
"-s",
"--secret",
type=str,
default="app",
required=False,
help='custom secret identifier (default is "app")',
)
diff.add_argument(
"version_a",
type=str,
help="Version to compare",
)
diff.add_argument(
"version_b",
type=str,
help="Other version to compare",
)
names = subparsers.add_parser("names", help="Display managed secret names")
add_default_arguments(parser=names)
def get_parser() -> ArgumentParser:
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
add_parser_args(parser=parser)
return parser
if __name__ == "__main__":
parser = get_parser()
# Print help if no arguments are given
if len(sys.argv) == 1:
parser.print_help()
else:
args = parser.parse_args()
config = CommandConfig.from_parser_args(args)
match config.action:
case "edit":
commands.edit_secret(config=config)
case "view":
commands.view_secret(config=config)
case "list":
commands.list_secrets(config=config)
case "names":
commands.name_secrets(config=config)
case "diff":
commands.diff_secrets(config=config, version_a=args.version_a, version_b=args.version_b)