Пример #1
0
def main():
    token = environ.get('TOKEN')
    user = environ.get('TPPUSER')
    password = environ.get('TPPPASSWORD')
    url = environ.get('TPPURL')
    zone = environ.get("ZONE")
    conn = Connection(url=url, token=token, user=user, password=password)

    print("Tring to ping url %s" % conn._base_url)
    status = conn.ping()
    print("Server online:", status)
    if not status:
        print('Server offline - exit')
        exit(1)

    request = CertificateRequest(common_name=randomword(10) +
                                 ".venafi.example.com")
    if not isinstance(conn, CloudConnection):
        # cloud connection doesn`t support dns, email and ip in CSR
        request.dns_names = [
            "www.client.venafi.example.com", "ww1.client.venafi.example.com"
        ]
        request.email_addresses = "[email protected], [email protected]"
        request.ip_addresses = ["127.0.0.1", "192.168.1.1"]
        request.chain_option = "last"

    # make certificate request
    conn.request_cert(request, zone)

    # and wait for signing
    while True:
        cert = conn.retrieve_cert(request)
        if cert:
            break
        else:
            time.sleep(5)

    # after that print cert and key
    print(cert, request.private_key_pem, sep="\n")
    # and save into file
    f = open("/tmp/cert.pem", "w")
    f.write(cert)
    f = open("/tmp/cert.key", "w")
    f.write(request.private_key_pem)
    f.close()

    if not isinstance(conn, FakeConnection):
        # fake connection doesn`t support certificate renewing
        print("Trying to renew certificate")
        new_request = CertificateRequest(id=request.id, )
        conn.renew_cert(new_request)
        while True:
            new_cert = conn.retrieve_cert(new_request)
            if new_cert:
                break
            else:
                time.sleep(5)
        print(new_cert)
        fn = open("/tmp/new_cert.pem", "w")
        fn.write(new_cert)
Пример #2
0
    def enroll(self):
        request = CertificateRequest(common_name=self.common_name,
                                     key_password=self.privatekey_passphrase,
                                     origin="Red Hat Ansible")
        zone_config = self.conn.read_zone_conf(self.zone)
        request.update_from_zone_config(zone_config)

        use_existed_key = False
        if self._check_private_key_correct() and not self.privatekey_reuse:
            private_key = to_text(open(self.privatekey_filename, "rb").read())
            request.private_key = private_key
            use_existed_key = True
        elif self.privatekey_type:
            key_type = {"RSA": "rsa", "ECDSA": "ec", "EC": "ec"}. \
                get(self.privatekey_type)
            if not key_type:
                self.module.fail_json(msg=("Failed to determine key type: %s."
                                           "Must be RSA or ECDSA" %
                                           self.privatekey_type))
            if key_type == "rsa":
                request.key_type = KeyType(KeyType.RSA, self.privatekey_size)
            elif key_type == "ecdsa" or "ec":
                request.key_type = KeyType(KeyType.ECDSA,
                                           self.privatekey_curve)
            else:
                self.module.fail_json(msg=("Failed to determine key type: %s."
                                           "Must be RSA or ECDSA" %
                                           self.privatekey_type))

        request.ip_addresses = self.ip_addresses
        request.san_dns = self.san_dns
        request.email_addresses = self.email_addresses

        request.chain_option = self.module.params['chain_option']
        try:
            csr = open(self.csr_path, "rb").read()
            request.csr = csr
        except Exception as e:
            self.module.log(msg=str(e))
            pass

        self.conn.request_cert(request, self.zone)
        print(request.csr)
        while True:
            cert = self.conn.retrieve_cert(request)  # vcert.Certificate
            if cert:
                break
            else:
                time.sleep(5)
        if self.chain_filename:
            self._atomic_write(self.chain_filename, "\n".join(cert.chain))
            self._atomic_write(self.certificate_filename, cert.cert)
        else:
            self._atomic_write(self.certificate_filename, cert.full_chain)
        if not use_existed_key:
            self._atomic_write(self.privatekey_filename,
                               request.private_key_pem)
Пример #3
0
    def enroll(self):
        request = CertificateRequest(
            common_name=self.common_name,
            key_password=self.privatekey_passphrase,
        )
        use_existed_key = False
        if self._check_private_key_correct():  # May be None
            private_key = to_text(open(self.privatekey_filename, "rb").read())
            request.private_key = private_key
            use_existed_key = True
        elif self.privatekey_type:
            key_type = {
                "RSA": "rsa",
                "ECDSA": "ec",
                "EC": "ec"
            }.get(self.privatekey_type)
            if not key_type:
                self.module.fail_json(
                    msg="Failed to determine key type: {0}. Must be RSA or ECDSA"
                    .format(self.privatekey_type))
            request.key_type = key_type
            request.key_curve = self.privatekey_curve
            request.key_length = self.privatekey_size

        request.ip_addresses = self.ip_addresses
        request.san_dns = self.san_dns
        request.email_addresses = self.email_addresses

        request.chain_option = self.module.params['chain_option']
        try:
            csr = open(self.csr_path, "rb").read()
            request.csr = csr
        except Exception as e:
            self.module.log(msg=str(e))
            pass

        self.conn.request_cert(request, self.zone)
        print(request.csr)
        while True:
            cert = self.conn.retrieve_cert(request)  # vcert.Certificate
            if cert:
                break
            else:
                time.sleep(5)
        if self.chain_filename:
            self._atomic_write(self.chain_filename, "\n".join(cert.chain))
            self._atomic_write(self.certificate_filename, cert.cert)
        else:
            self._atomic_write(self.certificate_filename, cert.full_chain)
        if not use_existed_key:
            self._atomic_write(self.privatekey_filename,
                               request.private_key_pem)