2019-09-23 11:42:36 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2019-01-30 10:28:12 +00:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
2019-10-18 15:20:44 +00:00
|
|
|
from __future__ import absolute_import, print_function, unicode_literals
|
|
|
|
|
2019-01-30 10:28:12 +00:00
|
|
|
import argparse
|
|
|
|
import base64
|
2019-10-18 15:20:44 +00:00
|
|
|
import errno
|
2019-07-26 15:08:01 +00:00
|
|
|
import json
|
2019-01-30 10:28:12 +00:00
|
|
|
import os
|
|
|
|
import taskcluster
|
|
|
|
|
2019-10-18 15:20:44 +00:00
|
|
|
|
2019-07-26 15:08:01 +00:00
|
|
|
def write_secret_to_file(path, data, key, base64decode=False, json_secret=False, append=False, prefix=''):
|
2019-10-18 15:20:44 +00:00
|
|
|
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../' + path))
|
2019-07-30 18:10:57 +00:00
|
|
|
try:
|
|
|
|
os.makedirs(os.path.dirname(path))
|
|
|
|
except OSError as error:
|
|
|
|
if error.errno != errno.EEXIST:
|
|
|
|
raise
|
2019-10-18 15:20:44 +00:00
|
|
|
print("Outputting secret to: {}".format(path))
|
2019-07-30 18:10:57 +00:00
|
|
|
|
2019-01-30 10:28:12 +00:00
|
|
|
with open(path, 'a' if append else 'w') as f:
|
|
|
|
value = data['secret'][key]
|
|
|
|
if base64decode:
|
|
|
|
value = base64.b64decode(value)
|
2019-07-26 15:08:01 +00:00
|
|
|
if json_secret:
|
|
|
|
value = json.dumps(value)
|
2019-01-30 10:28:12 +00:00
|
|
|
f.write(prefix + value)
|
|
|
|
|
|
|
|
|
|
|
|
def fetch_secret_from_taskcluster(name):
|
2019-09-23 11:42:36 +00:00
|
|
|
try:
|
|
|
|
secrets = taskcluster.Secrets({
|
|
|
|
# BaseUrl is still needed for tasks that haven't migrated to taskgraph yet.
|
|
|
|
'baseUrl': 'http://taskcluster/secrets/v1',
|
|
|
|
})
|
|
|
|
except taskcluster.exceptions.TaskclusterFailure:
|
|
|
|
# taskcluster library >=5 errors out when `baseUrl` is used
|
|
|
|
secrets = taskcluster.Secrets({
|
|
|
|
'rootUrl': os.environ.get('TASKCLUSTER_PROXY_URL', 'https://taskcluster.net'),
|
|
|
|
})
|
|
|
|
|
2019-01-30 10:28:12 +00:00
|
|
|
return secrets.get(name)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Fetch a taskcluster secret value and save it to a file.')
|
|
|
|
|
|
|
|
parser.add_argument('-s', dest="secret", action="store", help="name of the secret")
|
|
|
|
parser.add_argument('-k', dest='key', action="store", help='key of the secret')
|
|
|
|
parser.add_argument('-f', dest="path", action="store", help='file to save secret to')
|
|
|
|
parser.add_argument('--decode', dest="decode", action="store_true", default=False, help='base64 decode secret before saving to file')
|
2019-07-26 15:08:01 +00:00
|
|
|
parser.add_argument('--json', dest="json", action="store_true", default=False, help='serializes the secret to JSON format')
|
2019-01-30 10:28:12 +00:00
|
|
|
parser.add_argument('--append', dest="append", action="store_true", default=False, help='append secret to existing file')
|
|
|
|
parser.add_argument('--prefix', dest="prefix", action="store", default="", help='add prefix when writing secret to file')
|
|
|
|
|
|
|
|
result = parser.parse_args()
|
|
|
|
|
|
|
|
secret = fetch_secret_from_taskcluster(result.secret)
|
2019-07-26 15:08:01 +00:00
|
|
|
write_secret_to_file(result.path, secret, result.key, result.decode, result.json, result.append, result.prefix)
|
2019-01-30 10:28:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|