2016-04-07 01:37:53 +00:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
import os
|
|
|
|
|
import shutil
|
|
|
|
|
import subprocess
|
|
|
|
|
|
2016-05-19 00:46:30 +00:00
|
|
|
import icon_lib
|
|
|
|
|
|
2016-04-07 01:37:53 +00:00
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='iconograph autoimage')
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--chroot-path',
|
|
|
|
|
dest='chroot_path',
|
|
|
|
|
action='store',
|
|
|
|
|
required=True)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--ca-cert',
|
|
|
|
|
dest='ca_cert',
|
|
|
|
|
action='store',
|
|
|
|
|
required=True)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--client-cert',
|
|
|
|
|
dest='client_cert',
|
|
|
|
|
action='store',
|
|
|
|
|
required=True)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--client-key',
|
|
|
|
|
dest='client_key',
|
|
|
|
|
action='store',
|
|
|
|
|
required=True)
|
2016-05-19 22:58:27 +00:00
|
|
|
parser.add_argument(
|
|
|
|
|
'--generate-dh',
|
|
|
|
|
dest='generate_dh',
|
|
|
|
|
action='store_true',
|
|
|
|
|
default=False)
|
2016-04-07 01:37:53 +00:00
|
|
|
parser.add_argument(
|
|
|
|
|
'--subject',
|
|
|
|
|
dest='subject',
|
|
|
|
|
action='store',
|
|
|
|
|
required=True)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--tag',
|
|
|
|
|
dest='tag',
|
|
|
|
|
action='store',
|
|
|
|
|
required=True)
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--server',
|
|
|
|
|
dest='server',
|
|
|
|
|
action='store',
|
|
|
|
|
required=True)
|
|
|
|
|
FLAGS = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2016-05-19 00:46:30 +00:00
|
|
|
module = icon_lib.IconModule(FLAGS.chroot_path)
|
|
|
|
|
module.InstallPackages('git', 'python3-requests', 'openssl')
|
2016-04-07 01:37:53 +00:00
|
|
|
|
2016-04-06 22:38:57 -07:00
|
|
|
os.makedirs(os.path.join(FLAGS.chroot_path, 'icon', 'config'), exist_ok=True)
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(os.path.join(FLAGS.chroot_path, 'icon', 'iconograph')):
|
2016-05-19 00:46:30 +00:00
|
|
|
module.ExecChroot(
|
2016-04-06 22:38:57 -07:00
|
|
|
'git',
|
|
|
|
|
'clone',
|
|
|
|
|
'https://github.com/robot-tools/iconograph.git',
|
|
|
|
|
'icon/iconograph')
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(os.path.join(FLAGS.chroot_path, 'icon', 'certserver')):
|
2016-05-19 00:46:30 +00:00
|
|
|
module.ExecChroot(
|
2016-04-06 22:38:57 -07:00
|
|
|
'git',
|
|
|
|
|
'clone',
|
|
|
|
|
'https://github.com/robot-tools/certserver.git',
|
|
|
|
|
'icon/certserver')
|
|
|
|
|
|
|
|
|
|
ca_cert_path = os.path.join('icon', 'config', 'ca.%s.certserver.cert.pem' % FLAGS.tag)
|
2016-04-07 01:37:53 +00:00
|
|
|
shutil.copyfile(
|
|
|
|
|
FLAGS.ca_cert,
|
|
|
|
|
os.path.join(FLAGS.chroot_path, ca_cert_path))
|
|
|
|
|
|
2016-04-06 22:38:57 -07:00
|
|
|
client_cert_path = os.path.join('icon', 'config', 'client.%s.certserver.cert.pem' % FLAGS.tag)
|
2016-04-07 01:37:53 +00:00
|
|
|
shutil.copyfile(
|
|
|
|
|
FLAGS.client_cert,
|
|
|
|
|
os.path.join(FLAGS.chroot_path, client_cert_path))
|
2016-04-06 22:38:57 -07:00
|
|
|
client_key_path = os.path.join('icon', 'config', 'client.%s.certserver.key.pem' % FLAGS.tag)
|
2016-04-07 01:37:53 +00:00
|
|
|
shutil.copyfile(
|
|
|
|
|
FLAGS.client_key,
|
|
|
|
|
os.path.join(FLAGS.chroot_path, client_key_path))
|
|
|
|
|
os.chmod(os.path.join(FLAGS.chroot_path, client_key_path), 0o400)
|
|
|
|
|
|
|
|
|
|
init = os.path.join(FLAGS.chroot_path, 'etc', 'init', 'certclient.%s.conf' % FLAGS.tag)
|
|
|
|
|
with open(init, 'w') as fh:
|
|
|
|
|
fh.write("""
|
|
|
|
|
description "CertClient %(tag)s"
|
|
|
|
|
|
|
|
|
|
start on systemid-ready
|
|
|
|
|
|
|
|
|
|
script
|
2016-04-07 20:48:32 +00:00
|
|
|
exec </dev/tty9 >/dev/tty9 2>&1
|
|
|
|
|
chvt 9
|
2016-04-06 22:40:43 -07:00
|
|
|
|
2016-05-19 23:01:37 +00:00
|
|
|
DH="/systemid/$(hostname).%(tag)s.dh.pem"
|
|
|
|
|
DH_LINK="/systemid/%(tag)s.dh.pem"
|
2016-04-07 01:37:53 +00:00
|
|
|
KEY="/systemid/$(hostname).%(tag)s.key.pem"
|
2016-05-19 21:46:25 +00:00
|
|
|
KEY_LINK="/systemid/%(tag)s.key.pem"
|
2016-04-07 01:37:53 +00:00
|
|
|
CERT="/systemid/$(hostname).%(tag)s.cert.pem"
|
2016-05-19 21:46:25 +00:00
|
|
|
CERT_LINK="/systemid/%(tag)s.cert.pem"
|
2016-04-07 01:37:53 +00:00
|
|
|
SUBJECT="$(echo '%(subject)s' | sed s/SYSTEMID/$(hostname)/g)"
|
|
|
|
|
|
2016-04-07 20:29:48 +00:00
|
|
|
if test ! -s "${KEY}"; then
|
2016-04-06 22:40:43 -07:00
|
|
|
openssl ecparam -name secp384r1 -genkey | openssl ec -out "${KEY}"
|
|
|
|
|
chmod 0400 "${KEY}"
|
|
|
|
|
fi
|
|
|
|
|
|
2016-04-07 20:48:32 +00:00
|
|
|
chvt 9
|
2016-05-18 00:15:25 +00:00
|
|
|
/icon/iconograph/client/wait_for_service.py --host=%(server)s --service=https
|
2016-04-07 20:48:32 +00:00
|
|
|
chvt 9
|
2016-04-06 22:40:43 -07:00
|
|
|
|
2016-04-07 20:29:48 +00:00
|
|
|
if test ! -s "${CERT}"; then
|
2016-04-06 22:40:43 -07:00
|
|
|
openssl req -new -key "${KEY}" -subj "${SUBJECT}" | /icon/certserver/certclient.py --ca-cert=/icon/config/ca.%(tag)s.certserver.cert.pem --client-cert=/icon/config/client.%(tag)s.certserver.cert.pem --client-key=/icon/config/client.%(tag)s.certserver.key.pem --server=%(server)s > "${CERT}"
|
|
|
|
|
chmod 0444 "${CERT}"
|
|
|
|
|
fi
|
|
|
|
|
|
2016-05-19 22:58:27 +00:00
|
|
|
if test "%(dh)s" = "y"; then
|
|
|
|
|
if test ! -s "${DH}"; then
|
|
|
|
|
openssl dhparam -out "${DH}" 2048
|
|
|
|
|
fi
|
|
|
|
|
ln --symbolic --force $(basename "${DH}") "${DH_LINK}"
|
|
|
|
|
fi
|
|
|
|
|
|
2016-05-19 22:07:44 +00:00
|
|
|
ln --symbolic --force $(basename "${KEY}") "${KEY_LINK}"
|
|
|
|
|
ln --symbolic --force $(basename "${CERT}") "${CERT_LINK}"
|
2016-05-19 21:46:25 +00:00
|
|
|
|
2016-04-07 20:48:32 +00:00
|
|
|
chvt 9
|
2016-04-07 01:37:53 +00:00
|
|
|
|
|
|
|
|
echo
|
|
|
|
|
echo "=================="
|
|
|
|
|
echo "certclient %(tag)s complete"
|
|
|
|
|
echo "=================="
|
|
|
|
|
end script
|
|
|
|
|
""" % {
|
2016-05-19 22:58:27 +00:00
|
|
|
'dh': 'y' if FLAGS.generate_dh else 'n',
|
2016-05-18 00:15:25 +00:00
|
|
|
'server': FLAGS.server,
|
2016-04-07 01:37:53 +00:00
|
|
|
'subject': FLAGS.subject,
|
|
|
|
|
'tag': FLAGS.tag,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|