]>
git.proxmox.com Git - ceph.git/blob - ceph/qa/tasks/openssl_keys.py
2 Generates and installs a signed SSL certificate.
8 from teuthology
import misc
9 from teuthology
.exceptions
import ConfigError
10 from teuthology
.orchestra
import run
11 from teuthology
.task
import Task
13 log
= logging
.getLogger(__name__
)
15 class OpenSSLKeys(Task
):
18 Generates and installs a signed SSL certificate.
20 To create a self-signed certificate:
24 root: # results in root.key and root.crt
26 # [required] make the private key and certificate available in this client's test directory
29 # common name, defaults to `hostname`. chained certificates must not share a common name
32 # private key type for -newkey, defaults to rsa:2048
35 # install the certificate as trusted on these clients:
36 install: [client.0, client.1]
39 To create a certificate signed by a ca certificate:
42 root: (self-signed certificate as above)
48 # use another ssl certificate (by 'name') as the certificate authority
49 ca: root # --CAkey=root.key -CA=root.crt
51 # embed the private key in the certificate file
55 def __init__(self
, ctx
, config
):
56 super(OpenSSLKeys
, self
).__init
__(ctx
, config
)
61 # global dictionary allows other tasks to look up certificate paths
62 if not hasattr(self
.ctx
, 'ssl_certificates'):
63 self
.ctx
.ssl_certificates
= {}
65 # use testdir/ca as a working directory
66 self
.cadir
= '/'.join((misc
.get_testdir(self
.ctx
), 'ca'))
67 # make sure self-signed certs get added first, they don't have 'ca' field
68 configs
= sorted(self
.config
.items(), key
=lambda x
: 'ca' in x
[1])
69 for name
, config
in configs
:
70 # names must be unique to avoid clobbering each others files
71 if name
in self
.ctx
.ssl_certificates
:
72 raise ConfigError('ssl: duplicate certificate name {}'.format(name
))
74 # create the key and certificate
75 cert
= self
.create_cert(name
, config
)
77 self
.ctx
.ssl_certificates
[name
] = cert
78 self
.certs
.append(cert
)
80 # install as trusted on the requested clients
81 for client
in config
.get('install', []):
82 installed
= self
.install_cert(cert
, client
)
83 self
.installed
.append(installed
)
87 Clean up any created/installed certificate files.
89 for cert
in self
.certs
:
90 self
.remove_cert(cert
)
92 for installed
in self
.installed
:
93 self
.uninstall_cert(installed
)
95 def create_cert(self
, name
, config
):
97 Create a certificate with the given configuration.
99 cert
= argparse
.Namespace()
101 cert
.key_type
= config
.get('key-type', 'rsa:2048')
103 cert
.client
= config
.get('client', None)
105 raise ConfigError('ssl: missing required field "client"')
107 (cert
.remote
,) = self
.ctx
.cluster
.only(cert
.client
).remotes
.keys()
109 cert
.remote
.run(args
=['mkdir', '-p', self
.cadir
])
111 cert
.key
= '{}/{}.key'.format(self
.cadir
, cert
.name
)
112 cert
.certificate
= '{}/{}.crt'.format(self
.cadir
, cert
.name
)
114 # provide the common name in -subj to avoid the openssl command prompts
115 subject
= '/CN={}'.format(config
.get('cn', cert
.remote
.hostname
))
117 # if a ca certificate is provided, use it to sign the new certificate
118 ca
= config
.get('ca', None)
120 # the ca certificate must have been created by a prior ssl task
121 ca_cert
= self
.ctx
.ssl_certificates
.get(ca
, None)
123 raise ConfigError('ssl: ca {} not found for certificate {}'
124 .format(ca
, cert
.name
))
126 # these commands are run on the ca certificate's client because
127 # they need access to its private key and cert
129 # generate a private key and signing request
130 csr
= '{}/{}.csr'.format(self
.cadir
, cert
.name
)
131 ca_cert
.remote
.run(args
=['openssl', 'req', '-nodes',
132 '-newkey', cert
.key_type
, '-keyout', cert
.key
,
133 '-out', csr
, '-subj', subject
])
135 # create the signed certificate
136 ca_cert
.remote
.run(args
=['openssl', 'x509', '-req', '-in', csr
,
137 '-CA', ca_cert
.certificate
, '-CAkey', ca_cert
.key
, '-CAcreateserial',
138 '-out', cert
.certificate
, '-days', '365', '-sha256'])
140 srl
= '{}/{}.srl'.format(self
.cadir
, ca_cert
.name
)
141 ca_cert
.remote
.run(args
=['rm', csr
, srl
]) # clean up the signing request and serial
143 # verify the new certificate against its ca cert
144 ca_cert
.remote
.run(args
=['openssl', 'verify',
145 '-CAfile', ca_cert
.certificate
, cert
.certificate
])
147 if cert
.remote
!= ca_cert
.remote
:
148 # copy to remote client
149 self
.remote_copy_file(ca_cert
.remote
, cert
.certificate
, cert
.remote
, cert
.certificate
)
150 self
.remote_copy_file(ca_cert
.remote
, cert
.key
, cert
.remote
, cert
.key
)
151 # clean up the local copies
152 ca_cert
.remote
.run(args
=['rm', cert
.certificate
, cert
.key
])
153 # verify the remote certificate (requires ca to be in its trusted ca certificate store)
154 cert
.remote
.run(args
=['openssl', 'verify', cert
.certificate
])
156 # otherwise, generate a private key and use it to self-sign a new certificate
157 cert
.remote
.run(args
=['openssl', 'req', '-x509', '-nodes',
158 '-newkey', cert
.key_type
, '-keyout', cert
.key
,
159 '-days', '365', '-out', cert
.certificate
, '-subj', subject
])
161 if config
.get('embed-key', False):
162 # append the private key to the certificate file
163 cert
.remote
.run(args
=['cat', cert
.key
, run
.Raw('>>'), cert
.certificate
])
167 def remove_cert(self
, cert
):
169 Delete all of the files associated with the given certificate.
171 # remove the private key and certificate
172 cert
.remote
.run(args
=['rm', '-f', cert
.certificate
, cert
.key
])
174 # remove ca subdirectory if it's empty
175 cert
.remote
.run(args
=['rmdir', '--ignore-fail-on-non-empty', self
.cadir
])
177 def install_cert(self
, cert
, client
):
179 Install as a trusted ca certificate on the given client.
181 (remote
,) = self
.ctx
.cluster
.only(client
).remotes
.keys()
183 installed
= argparse
.Namespace()
184 installed
.remote
= remote
186 if remote
.os
.package_type
== 'deb':
187 installed
.path
= '/usr/local/share/ca-certificates/{}.crt'.format(cert
.name
)
188 installed
.command
= ['sudo', 'update-ca-certificates']
190 installed
.path
= '/usr/share/pki/ca-trust-source/anchors/{}.crt'.format(cert
.name
)
191 installed
.command
= ['sudo', 'update-ca-trust']
194 if remote
!= cert
.remote
:
195 # copy into remote cadir (with mkdir if necessary)
196 remote
.run(args
=['mkdir', '-p', self
.cadir
])
197 self
.remote_copy_file(cert
.remote
, cert
.certificate
, remote
, cert
.certificate
)
198 cp_or_mv
= 'mv' # move this remote copy into the certificate store
200 # install into certificate store as root
201 remote
.run(args
=['sudo', cp_or_mv
, cert
.certificate
, installed
.path
])
202 remote
.run(args
=installed
.command
)
206 def uninstall_cert(self
, installed
):
208 Uninstall a certificate from the trusted certificate store.
210 installed
.remote
.run(args
=['sudo', 'rm', installed
.path
])
211 installed
.remote
.run(args
=installed
.command
)
213 def remote_copy_file(self
, from_remote
, from_path
, to_remote
, to_path
):
215 Copies a file from one remote to another.
217 The remotes don't have public-key auth for 'scp' or misc.copy_file(),
218 so this copies through an intermediate local tmp file.
220 log
.info('copying from {}:{} to {}:{}...'.format(from_remote
, from_path
, to_remote
, to_path
))
221 local_path
= from_remote
.get_file(from_path
)
223 to_remote
.put_file(local_path
, to_path
)
225 os
.remove(local_path
)