Created
January 8, 2021 15:32
-
-
Save alanraso/1eaff44187348c83662555317f4748a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Script para adicionar secrets ao Secret Manager do GCloud usando a API. Argumentos passados por linha de comando: | |
--project: ID do projeto no GCloud | |
--input_file: caminho do arquivo JSON com o env. Formato: `{ secret_key: secret_value }` | |
--access_token: token de acesso Gcloud. Possível obter por linha de comando: `$ gcloud auth print-access-token` | |
Se necessário, edite a variável `secret_body` para mudar o location padrão e o as tags desejadas | |
""" | |
import argparse | |
import base64 | |
import json | |
import sys | |
import requests | |
BASE_URL = "https://secretmanager.googleapis.com/v1" | |
gcloud_project = "" | |
headers = { 'Content-type': 'application/json' } | |
def setup_args(argv=None): | |
global gcloud_project | |
global headers | |
parser = argparse.ArgumentParser() | |
parser.add_argument( | |
'--project', | |
dest='project', | |
required=True, | |
help='Id do projeto GCloud') | |
parser.add_argument( | |
'--input_file', | |
dest='input_file', | |
required=True, | |
help='Arquivo JSON contendo os dados dos secrets necessários') | |
parser.add_argument( | |
'--access_token', | |
dest='access_token', | |
required=True, | |
help='Token de acesso ao GCloud: `$ gcloud auth print-access-token`') | |
args, _ = parser.parse_known_args(argv) | |
gcloud_project = args.project | |
headers.update({ 'Authorization': f"Bearer {args.access_token}" }) | |
return args.input_file | |
def add_secret_with_version(secret_id, secret_data): | |
print(f"Creating {secret_id} on secret manager...") | |
secret_body = { | |
'replication': { | |
'userManaged': { | |
'replicas': [ | |
{ | |
'location': "southamerica-east1" | |
} | |
] | |
} | |
}, | |
'labels': { | |
'env': "dev" | |
} | |
} | |
secret_endpoint = f"{BASE_URL}/projects/{gcloud_project}/secrets?secretId={secret_id}" | |
res = requests.post(secret_endpoint, json=secret_body, headers=headers) | |
print(res.json()) | |
if res.status_code >= 400: | |
return | |
print(f"Creating {secret_id} first version with value...") | |
secret_version_endpoint = f"{BASE_URL}/projects/{gcloud_project}/secrets/{secret_id}:addVersion" | |
version_body = { | |
'payload': { | |
'data': base64.b64encode(secret_data.encode('ascii')).decode('ascii') | |
} | |
} | |
res = requests.post(secret_version_endpoint, json=version_body, headers=headers) | |
print(res.json()) | |
if res.status_code == 200: | |
print(f"Secret {secret_id} created sucessfully!\n") | |
def main(argv=None): | |
input_file = setup_args(argv) | |
with open(input_file) as env_file: | |
secrets = json.load(env_file) | |
for (secret_id, secret_value) in secrets.items(): | |
add_secret_with_version(secret_id, secret_value) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment