Accept '/r/foo' everywhere: part 2
[reddit.git] / scripts / read_secrets
blobc6d0cffe2cfdbf44e2847b9ee49dd3d5f79f1d73
1 #!/usr/bin/env python
2 # The contents of this file are subject to the Common Public Attribution
3 # License Version 1.0. (the "License"); you may not use this file except in
4 # compliance with the License. You may obtain a copy of the License at
5 # http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
6 # License Version 1.1, but Sections 14 and 15 have been added to cover use of
7 # software over a computer network and provide for limited attribution for the
8 # Original Developer. In addition, Exhibit A has been modified to be consistent
9 # with Exhibit B.
11 # Software distributed under the License is distributed on an "AS IS" basis,
12 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
13 # the specific language governing rights and limitations under the License.
15 # The Original Code is reddit.
17 # The Original Developer is the Initial Developer. The Initial Developer of
18 # the Original Code is reddit Inc.
20 # All portions of the code written by reddit are Copyright (c) 2006-2015 reddit
21 # Inc. All Rights Reserved.
22 ###############################################################################
24 import ConfigParser
25 import base64
26 import cStringIO
27 import os
28 import sys
30 from r2.lib.utils import parse_ini_file
31 from r2.lib.zookeeper import connect_to_zookeeper
32 from r2.lib.app_globals import fetch_secrets
35 def read_secrets_from_zookeeper(config):
36 zk_hostlist = config.get("DEFAULT", "zookeeper_connection_string")
37 username = config.get("DEFAULT", "zookeeper_username")
38 password = config.get("DEFAULT", "zookeeper_password")
40 client = connect_to_zookeeper(zk_hostlist, (username, password))
41 secrets = fetch_secrets(client)
43 ini = ConfigParser.RawConfigParser()
44 ini.optionxform = str
45 ini.add_section("secrets")
46 for name, secret in secrets.iteritems():
47 ini.set("secrets", name, base64.b64encode(secret))
49 output = cStringIO.StringIO()
50 ini.write(output)
51 return output.getvalue()
54 def main():
55 progname = os.path.basename(sys.argv[0])
57 try:
58 ini_file_name = sys.argv[1]
59 except IndexError:
60 print >> sys.stderr, "USAGE: %s INI" % progname
61 return 1
63 try:
64 with open(ini_file_name) as ini_file:
65 config = parse_ini_file(ini_file)
66 except (IOError, ConfigParser.Error), e:
67 print >> sys.stderr, "%s: %s: %s" % (progname, ini_file_name, e)
68 return 1
70 print read_secrets_from_zookeeper(config)
73 if __name__ == "__main__":
74 sys.exit(main())