3 # Bootstraps the PLC database with a default administrator account and
4 # a default site. Also generates the MA/SA API certificate.
6 # Mark Huang <mlhuang@cs.princeton.edu>
7 # Copyright (C) 2006 The Trustees of Princeton University
9 # $Id: api-config,v 1.13 2006/06/23 20:33:28 mlhuang Exp $
12 from plc_config import PLCConfiguration
16 import CertOps, Certificate
22 cfg = PLCConfiguration()
24 variables = cfg.variables()
26 # Load variables into dictionaries
27 for category_id, (category, variablelist) in variables.iteritems():
28 globals()[category_id] = dict(zip(variablelist.keys(),
29 [variable['value'] for variable in variablelist.values()]))
31 # Get the issuer e-mail address and public key from the root CA certificate
32 root_ca_email = commands.getoutput("openssl x509 -in %s -noout -email" % \
33 plc_ma_sa['ca_ssl_crt'])
34 root_ca_key_pub = commands.getoutput("openssl x509 -in %s -noout -pubkey" % \
35 plc_ma_sa['ca_ssl_crt'])
37 # Verify API certificate
38 if os.path.exists(plc_ma_sa['api_crt']):
39 print "Verifying API certificate '%s'" % plc_ma_sa['api_crt']
41 cert_xml = file(plc_ma_sa['api_crt']).read().strip()
42 # Verify root CA signature
43 CertOps.authenticate_cert(cert_xml, {root_ca_email: root_ca_key_pub})
44 # Check if MA/SA e-mail address has changed
45 dom = xml.dom.minidom.parseString(cert_xml)
46 for subject in dom.getElementsByTagName('subject'):
47 if subject.getAttribute('email') != plc_mail['support_address']:
48 raise Exception, "E-mail address '%s' in certificate '%s' does not match support address '%s'" % \
49 (subject.getAttribute('email'), plc_ma_sa['api_crt'], plc_mail['support_address'])
51 # Delete invalid API certificate
53 os.unlink(plc_ma_sa['api_crt'])
55 # Generate self-signed API certificate
56 if not os.path.exists(plc_ma_sa['api_crt']):
57 print "Generating new API certificate"
59 cert = Certificate.Certificate('ticket-cert-0')
60 ma_sa_ssl_key_pub = commands.getoutput("openssl x509 -in %s -noout -pubkey" % \
62 cert.add_subject_pubkey(pubkey = ma_sa_ssl_key_pub, email = plc_mail['support_address'])
63 root_ca_subject = commands.getoutput("openssl x509 -in %s -noout -subject" % \
65 m = re.search('/CN=([^/]*).*', root_ca_subject)
67 root_ca_cn = plc['name'] + " Management and Slice Authority"
69 root_ca_cn = m.group(1)
70 cert.set_issuer(email = root_ca_email, cn = root_ca_cn)
71 cert_xml = cert.sign(plc_ma_sa['ssl_key'])
72 ma_sa_api_crt = file(plc_ma_sa['api_crt'], "w")
73 ma_sa_api_crt.write(cert_xml)
76 print "Warning: Could not generate API certificate: ", e
78 # For backward compatibility, until we can convert all code to use
79 # the now standardized variable names.
81 # API expects root SSH public key to be at /etc/planetlab/node_root_key
82 if not os.path.exists("/etc/planetlab/node_root_key"):
83 os.symlink(plc['root_ssh_key_pub'], "/etc/planetlab/node_root_key")
85 # Old variable names littered throughout the API
86 old_variables = {'PL_API_SERVER': plc_api['host'],
87 'PL_API_PATH': plc_api['path'],
88 'PL_API_PORT': plc_api['port'],
89 'PL_API_CAPABILITY_AUTH_METHOD': "capability",
90 'PL_API_CAPABILITY_PASS': plc_api['maintenance_password'],
91 'PL_API_CAPABILITY_USERNAME': plc_api['maintenance_user'],
92 'PLANETLAB_SUPPORT_EMAIL': plc_mail['support_address'],
93 'BOOT_MESSAGES_EMAIL': plc_mail['boot_address'],
94 'WWW_BASE': plc_www['host'],
95 'BOOT_BASE': plc_boot['host'],
97 'MA_SA_NAMESPACE': plc_ma_sa['namespace'],
98 'SESSION_LENGTH_HOURS': "24",
99 'ROOT_CA_EMAIL': root_ca_email,
100 'ROOT_CA_PUB_KEY': plc_ma_sa['ca_ssl_key_pub'],
101 'API_CERT_PATH': plc_ma_sa['api_crt'],
102 'MA_SA_PRIVATE_KEY': plc_ma_sa['ssl_key'],
103 'PL_API_TICKET_KEY_FILE': plc_ma_sa['ssl_key']}
105 # The format of an "allowed maintenance source" specification is
107 # ip:max_role_id:organization_id:password
109 # It is unlikely that we will let federated sites use the
110 # maintenance account to access each others' APIs, so we always
111 # set organization_id to -1.
112 old_variables['PL_API_CAPABILITY_SOURCES'] = " ".join(
113 ["%s:-1:-1:%s" % (ip, plc_api['maintenance_password']) \
114 for ip in plc_api['maintenance_sources'].split()])
116 old_config = open("/etc/planetlab/plc_api", "w")
117 for name, value in old_variables.iteritems():
118 old_config.write("%s='%s'\n" % (name, value))
122 if __name__ == '__main__':