def test_generate_certificate(self, get_local_client,
                               configure_pki_backend, is_ca_ready,
                               sort_sans):
     client_mock = mock.MagicMock()
     client_mock.write.return_value = {'data': 'data'}
     get_local_client.return_value = client_mock
     is_ca_ready.return_value = True
     sort_sans.side_effect = lambda l: (l[0], l[1])
     write_calls = [
         mock.call(
             'charm-pki-local/issue/local',
             common_name='example.com',
         ),
         mock.call(
             'charm-pki-local/issue/local',
             common_name='example.com',
             ip_sans='ip1',
             alt_names='alt1',
         ),
         mock.call(
             'charm-pki-local/issue/local-client',
             common_name='example.com',
             ip_sans='ip1,ip2',
             alt_names='alt1,alt2',
         ),
     ]
     vault_pki.generate_certificate('server', 'example.com', ([], []))
     vault_pki.generate_certificate('server', 'example.com',
                                    (['ip1'], ['alt1']))
     vault_pki.generate_certificate('client', 'example.com',
                                    (['ip1', 'ip2'], ['alt1', 'alt2']))
     client_mock.write.assert_has_calls(write_calls)
Esempio n. 2
0
def create_certs():
    reissue_requested = is_flag_set('certificates.reissue.requested')
    tls = endpoint_from_flag('certificates.available')
    requests = tls.all_requests if reissue_requested else tls.new_requests
    if reissue_requested:
        log('Reissuing all certs')
    for request in requests:
        log('Processing certificate request from {} for {}'.format(
            request.unit_name, request.common_name))
        try:
            bundle = vault_pki.generate_certificate(request.cert_type,
                                                    request.common_name,
                                                    request.sans)
            request.set_cert(bundle['certificate'], bundle['private_key'])
        except vault.VaultInvalidRequest as e:
            log(str(e), level=ERROR)
            continue  # TODO: report failure back to client
    clear_flag('certificates.reissue.requested')
Esempio n. 3
0
def publish_global_client_cert():
    """
    This is for backwards compatibility with older tls-certificate clients
    only.  Obviously, it's not good security / design to have clients sharing
    a certificate, but it seems that there are clients that depend on this
    (though some, like etcd, only block on the flag that it triggers but don't
    actually use the cert), so we have to set it for now.
    """
    cert_created = is_flag_set('charm.vault.global-client-cert.created')
    reissue_requested = is_flag_set('certificates.reissue.global.requested')
    tls = endpoint_from_flag('certificates.available')
    if not cert_created or reissue_requested:
        bundle = vault_pki.generate_certificate('client', 'global-client', [])
        unitdata.kv().set('charm.vault.global-client-cert', bundle)
        set_flag('charm.vault.global-client-cert.created')
        clear_flag('certificates.reissue.global.requested')
    else:
        bundle = unitdata.kv().get('charm.vault.global-client-cert')
    tls.set_client_cert(bundle['certificate'], bundle['private_key'])