|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright 2019 Venafi, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +from __future__ import print_function |
| 19 | + |
| 20 | +from vcert import (CertificateRequest, Connection, RevocationRequest, CSR_ORIGIN_SERVICE) |
| 21 | +import string |
| 22 | +import random |
| 23 | +import logging |
| 24 | +import time |
| 25 | +from os import environ |
| 26 | + |
| 27 | +logging.basicConfig(level=logging.INFO) |
| 28 | +logging.getLogger("urllib3").setLevel(logging.ERROR) |
| 29 | + |
| 30 | + |
| 31 | +def main(): |
| 32 | + # Get credentials from environment variables |
| 33 | + url = environ.get('TPP_TOKEN_URL') |
| 34 | + user = environ.get('TPP_USER') |
| 35 | + password = environ.get('TPP_PASSWORD') |
| 36 | + zone = environ.get("TPP_ZONE") |
| 37 | + server_trust_bundle = environ.get('TPP_TRUST_BUNDLE') |
| 38 | + |
| 39 | + |
| 40 | + # Connection will be chosen automatically based on which arguments are passed. |
| 41 | + # If token is passed Venafi Cloud connection will be used. |
| 42 | + # If user, password, and URL Venafi Platform (TPP) will be used. |
| 43 | + conn = Connection(url=url, user=user, password=password, |
| 44 | + http_request_kwargs={"verify": server_trust_bundle}) |
| 45 | + # If your TPP server certificate signed with your own CA, or available only via proxy, you can specify |
| 46 | + # a trust bundle using requests vars: |
| 47 | + # conn = Connection(url=url, token=token, user=user, password=password, |
| 48 | + # http_request_kwargs={"verify": "/path-to/bundle.pem"}) |
| 49 | + |
| 50 | + request = CertificateRequest(common_name=random_word(10) + ".venafi.example.com") |
| 51 | + request.csr_origin = CSR_ORIGIN_SERVICE |
| 52 | + request.san_dns = ["www.client.venafi.example.com", "ww1.client.venafi.example.com"] |
| 53 | + request.email_addresses = ["e1@venafi.example.com", "e2@venafi.example.com"] |
| 54 | + request.ip_addresses = ["127.0.0.1", "192.168.1.1"] |
| 55 | + request.uniform_resource_identifiers = ["http://wgtest.com","https://ragnartest.com"] |
| 56 | + request.user_principal_names = ["e1@venafi.example.com", "e2@venafi.example.com"] |
| 57 | + # Specify ordering certificates in chain. Root can be "first" or "last". By default it last. You also can |
| 58 | + # specify "ignore" to ignore chain (supported only for Platform). |
| 59 | + # To set Custom Fields for the certificate, specify an array of CustomField objects as name-value pairs |
| 60 | + # request.custom_fields = [ |
| 61 | + # CustomField(name="Cost Center", value="ABC123"), |
| 62 | + # CustomField(name="Environment", value="Production"), |
| 63 | + # CustomField(name="Environment", value="Staging") |
| 64 | + # ] |
| 65 | + # Update certificate request from zone |
| 66 | + zone_config = conn.read_zone_conf(zone) |
| 67 | + request.update_from_zone_config(zone_config) |
| 68 | + conn.request_cert(request, zone) |
| 69 | + |
| 70 | + # and wait for signing |
| 71 | + cert = conn.retrieve_cert(request) |
| 72 | + |
| 73 | + # after that print cert and key |
| 74 | + print(cert.full_chain, request.private_key_pem, sep="\n") |
| 75 | + # and save into file |
| 76 | + f = open("/tmp/cert.pem", "w") |
| 77 | + f.write(cert.full_chain) |
| 78 | + f = open("/tmp/cert.key", "w") |
| 79 | + f.write(request.private_key_pem) |
| 80 | + f.close() |
| 81 | + |
| 82 | + print("Trying to renew certificate") |
| 83 | + new_request = CertificateRequest(cert_id=request.id) |
| 84 | + conn.renew_cert(new_request) |
| 85 | + new_cert = conn.retrieve_cert(new_request) |
| 86 | + print(new_cert.cert, new_request.private_key_pem, sep="\n") |
| 87 | + fn = open("/tmp/new_cert.pem", "w") |
| 88 | + fn.write(new_cert.cert) |
| 89 | + fn = open("/tmp/new_cert.key", "w") |
| 90 | + fn.write(new_request.private_key_pem) |
| 91 | + fn.close() |
| 92 | + |
| 93 | + revocation_req = RevocationRequest(req_id=request.id, comments="Just for test") |
| 94 | + print("Revoke", conn.revoke_cert(revocation_req)) |
| 95 | + |
| 96 | + print("Trying to sign CSR") |
| 97 | + csr_pem = open("example-csr.pem", "rb").read() |
| 98 | + csr_request = CertificateRequest(csr=csr_pem.decode()) |
| 99 | + # zone_config = conn.read_zone_conf(zone) |
| 100 | + # request.update_from_zone_config(zone_config) |
| 101 | + conn.request_cert(csr_request, zone) |
| 102 | + |
| 103 | + # and wait for signing |
| 104 | + while True: |
| 105 | + cert = conn.retrieve_cert(csr_request) |
| 106 | + if cert: |
| 107 | + break |
| 108 | + else: |
| 109 | + time.sleep(5) |
| 110 | + |
| 111 | + # after that print cert and key |
| 112 | + print(cert.full_chain) |
| 113 | + # and save into file |
| 114 | + f = open("/tmp/signed-cert.pem", "w") |
| 115 | + f.write(cert.full_chain) |
| 116 | + f.close() |
| 117 | + |
| 118 | + |
| 119 | +def random_word(length): |
| 120 | + letters = string.ascii_lowercase |
| 121 | + return ''.join(random.choice(letters) for i in range(length)) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == '__main__': |
| 125 | + main() |
0 commit comments