Skip to content

Commit a9a2e80

Browse files
authored
Merge pull request #92 from Venafi/py3-migration
Project migration to Python3
2 parents 1aee88b + 44f66a3 commit a9a2e80

39 files changed

Lines changed: 704 additions & 889 deletions

examples/get_cert.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17-
18-
from __future__ import print_function
19-
from vcert import (CertificateRequest, Connection, CloudConnection,
20-
FakeConnection, TPPConnection, RevocationRequest, KeyType)
17+
from vcert import (CertificateRequest, Connection, CloudConnection, FakeConnection, TPPConnection, RevocationRequest,
18+
KeyType)
2119
import string
2220
import random
2321
import logging
@@ -34,7 +32,7 @@ def main():
3432
user = environ.get('TPP_USER')
3533
password = environ.get('TPP_PASSWORD')
3634
url = environ.get('TPP_URL')
37-
zone = environ.get("ZONE")
35+
zone = environ.get('ZONE')
3836
fake = environ.get('FAKE')
3937

4038
if fake:
@@ -45,28 +43,28 @@ def main():
4543
# If token is passed Venafi Cloud connection will be used.
4644
# If user, password, and URL Venafi Platform (TPP) will be used.
4745
conn = Connection(url=url, token=token, user=user, password=password,
48-
http_request_kwargs={"verify": False})
46+
http_request_kwargs={'verify': False})
4947
# If your TPP server certificate signed with your own CA, or available only via proxy, you can specify
5048
# a trust bundle using requests vars:
51-
#conn = Connection(url=url, token=token, user=user, password=password,
49+
# conn = Connection(url=url, token=token, user=user, password=password,
5250
# http_request_kwargs={"verify": "/path-to/bundle.pem"})
5351

54-
request = CertificateRequest(common_name=randomword(10) + ".venafi.example.com")
52+
request = CertificateRequest(common_name=f"{randomword(10)}.venafi.example.com")
5553
request.san_dns = ["www.client.venafi.example.com", "ww1.client.venafi.example.com"]
5654
if not isinstance(conn, CloudConnection):
5755
# Venafi Cloud doesn't support email or IP SANs in CSR
5856
request.email_addresses = ["e1@venafi.example.com", "e2@venafi.example.com"]
5957
request.ip_addresses = ["127.0.0.1", "192.168.1.1"]
60-
request.uniform_resource_identifiers = ["http://wgtest.com","https://ragnartest.com"]
58+
request.uniform_resource_identifiers = ["http://wgtest.com", "https://ragnartest.com"]
6159
request.user_principal_names = ["e1@venafi.example.com", "e2@venafi.example.com"]
6260
# Specify ordering certificates in chain. Root can be "first" or "last". By default it last. You also can
6361
# specify "ignore" to ignore chain (supported only for Platform).
6462
# To set Custom Fields for the certificate, specify an array of CustomField objects as name-value pairs
65-
#request.custom_fields = [
63+
# request.custom_fields = [
6664
# CustomField(name="Cost Center", value="ABC123"),
6765
# CustomField(name="Environment", value="Production"),
6866
# CustomField(name="Environment", value="Staging")
69-
#]
67+
# ]
7068

7169
# configure key type, RSA example
7270
request.key_type = KeyType(KeyType.RSA, 2048)
@@ -96,11 +94,9 @@ def main():
9694
f.close()
9795

9896
if not isinstance(conn, FakeConnection):
99-
# fake connection doesn`t support certificate renewing
97+
# fake connection doesn't support certificate renewing
10098
print("Trying to renew certificate")
101-
new_request = CertificateRequest(
102-
cert_id=request.id,
103-
)
99+
new_request = CertificateRequest(cert_id=request.id)
104100
conn.renew_cert(new_request)
105101
while True:
106102
new_cert = conn.retrieve_cert(new_request)
@@ -115,8 +111,7 @@ def main():
115111
fn.write(new_request.private_key_pem)
116112
fn.close()
117113
if isinstance(conn, TPPConnection):
118-
revocation_req = RevocationRequest(req_id=request.id,
119-
comments="Just for test")
114+
revocation_req = RevocationRequest(req_id=request.id, comments="Just for test")
120115
print("Revoke", conn.revoke_cert(revocation_req))
121116

122117
print("Trying to sign CSR")
@@ -141,6 +136,7 @@ def main():
141136
f.write(cert.full_chain)
142137
f.close()
143138

139+
144140
def randomword(length):
145141
letters = string.ascii_lowercase
146142
return ''.join(random.choice(letters) for i in range(length))

examples/get_cert27.py

Lines changed: 0 additions & 108 deletions
This file was deleted.

examples/ssh_certificates/get_cert_ssh.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
def main():
2929
# Get credentials from environment variables
3030
url = environ.get('TPP_URL')
31-
user = environ.get("TPP_USER")
32-
password = environ.get("TPP_PASSWORD")
31+
user = environ.get('TPP_USER')
32+
password = environ.get('TPP_PASSWORD')
3333

34-
connector = venafi_connection(url=url, user=user, password=password, http_request_kwargs={"verify": False})
34+
connector = venafi_connection(url=url, user=user, password=password, http_request_kwargs={'verify': False})
3535
# If your TPP server certificate is signed with your own CA, or available only via proxy,
3636
# you can specify a trust bundle using requests vars:
3737
# connector = venafi_connection(url=url, api_key=api_key, access_token=access_token,
@@ -58,15 +58,15 @@ def main():
5858
# This is a placeholder. Make sure an SSH CA already exists on your TPP instance
5959
cadn = "\\VED\\Certificate Authority\\SSH\\Templates\\my-ca"
6060
# The id of the SSH certificate
61-
key_id = "vcert-python-%s" % random_word(12)
61+
key_id = f"vcert-python-{random_word(12)}"
6262

6363
# Create the request object
6464
request = SSHCertRequest(cadn=cadn, key_id=key_id)
6565
# Add any additional info for the certificate, like:
6666
request.validity_period = "4h"
6767
request.source_addresses = ["test.com"]
6868
request.extensions = {
69-
"permit-pty": ""
69+
'permit-pty': ""
7070
}
7171
# Include the locally-generated public key. If not set, the server will generate one for the certificate
7272
request.set_public_key_data(ssh_kp.public_key())

examples/ssh_certificates/get_cert_ssh_service.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
def main():
2929
# Get credentials from environment variables
3030
url = environ.get('TPP_URL')
31-
user = environ.get("TPP_USER")
32-
password = environ.get("TPP_PASSWORD")
31+
user = environ.get('TPP_USER')
32+
password = environ.get('TPP_PASSWORD')
3333

34-
connector = venafi_connection(url=url, user=user, password=password, http_request_kwargs={"verify": False})
34+
connector = venafi_connection(url=url, user=user, password=password, http_request_kwargs={'verify': False})
3535
# If your TPP server certificate signed with your own CA, or available only via proxy,
3636
# you can specify a trust bundle using requests vars:
3737
# connector = venafi_connection(url=url, api_key=api_key, access_token=access_token,
@@ -51,15 +51,15 @@ def main():
5151
# The path to the SSH CA in the TPP instance
5252
cadn = "\\VED\\Certificate Authority\\SSH\\Templates\\my-ca"
5353
# The id of the SSH certificate
54-
key_id = "vcert-python-%s" % random_word(12)
54+
key_id = f"vcert-python-{random_word(12)}"
5555

5656
# Create the request object
5757
request = SSHCertRequest(cadn=cadn, key_id=key_id)
5858
# Add any additional info for the certificate, like:
5959
request.validity_period = "4h"
6060
request.source_addresses = ["test.com"]
6161
request.extensions = {
62-
"permit-pty": ""
62+
'permit-pty': ""
6363
}
6464

6565
# Request the certificate from TPP instance

examples/ssh_certificates/retrieve_ca_public_key.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ def main():
2929
ca_dn = environ.get('TPP_SSH_CADN')
3030
ca_guid = environ.get('TPP_SSH_CA_GUID')
3131
# Authentication is required for retrieving the CA principals only.
32-
user = environ.get("TPP_USER")
33-
password = environ.get("TPP_PASSWORD")
32+
user = environ.get('TPP_USER')
33+
password = environ.get('TPP_PASSWORD')
3434

3535
# A Connector can be instantiated with no values by using the platform argument.
3636
# url argument is always required for TPP.
3737
connector = venafi_connection(platform=VenafiPlatform.TPP, url=url,
38-
http_request_kwargs={"verify": "/tmp/chain.pem"})
38+
http_request_kwargs={'verify': "/tmp/chain.pem"})
3939
# Optionally, the connector can be instantiated passing the specific arguments:
4040
# connector = venafi_connection(url=url, user=user, password=password, http_request_kwargs={"verify": False})
4141

@@ -70,7 +70,7 @@ def main():
7070
ssh_config = connector.retrieve_ssh_config(ca_request=request)
7171
with open("./ca2-pub.key", 'w') as ca_file:
7272
ca_file.write(pub_key_data)
73-
print("Certificate Authority principals: %s" % ssh_config.ca_principals)
73+
print(f"Certificate Authority principals: {ssh_config.ca_principals}")
7474

7575

7676
if __name__ == '__main__':

examples/tpp/get_cert_tpp_token.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17-
18-
from __future__ import print_function
1917
from vcert import (CertificateRequest, Connection, FakeConnection, TPPConnection, RevocationRequest, KeyType,
2018
TPPTokenConnection, venafi_connection)
2119
import string
@@ -33,7 +31,7 @@ def main():
3331
user = environ.get('TPP_USER')
3432
password = environ.get('TPP_PASSWORD')
3533
url = environ.get('TPP_TOKEN_URL')
36-
zone = environ.get("TPP_ZONE")
34+
zone = environ.get('TPP_ZONE')
3735
fake = environ.get('FAKE')
3836

3937
if fake:
@@ -43,18 +41,18 @@ def main():
4341
# If user and password are passed, you can get a new token from them.
4442
# If access_token and refresh_token are passed, there is no need for the username and password.
4543
# If only access_token is passed, the Connection will fail when token expires, as there is no way to refresh it.
46-
conn = venafi_connection(url=url, user=user, password=password, http_request_kwargs={"verify": False})
44+
conn = venafi_connection(url=url, user=user, password=password, http_request_kwargs={'verify': False})
4745
# If your TPP server certificate signed with your own CA, or available only via proxy, you can specify
4846
# a trust bundle using requests vars:
4947
# conn = token_connection(url=url, user=user, password=password,
5048
# http_request_kwargs={"verify": "/path-to/bundle.pem"})
5149

52-
request = CertificateRequest(common_name=random_word(10) + ".venafi.example.com")
53-
request.san_dns = [u"www.client.venafi.example.com", u"ww1.client.venafi.example.com"]
54-
request.email_addresses = [u"e1@venafi.example.com", u"e2@venafi.example.com"]
55-
request.ip_addresses = [u"127.0.0.1", u"192.168.1.1"]
56-
request.uniform_resource_identifiers = [u"http://wgtest.com",u"https://ragnartest.com"]
57-
request.user_principal_names = [u"e1@venafi.example.com", u"e2@venafi.example.com"]
50+
request = CertificateRequest(common_name=f"{random_word(10)}.venafi.example.com")
51+
request.san_dns = ["www.client.venafi.example.com", "ww1.client.venafi.example.com"]
52+
request.email_addresses = ["e1@venafi.example.com", "e2@venafi.example.com"]
53+
request.ip_addresses = ["127.0.0.1", u"192.168.1.1"]
54+
request.uniform_resource_identifiers = ["http://wgtest.com", "https://ragnartest.com"]
55+
request.user_principal_names = ["e1@venafi.example.com", "e2@venafi.example.com"]
5856
# Specify ordering certificates in chain. Root can be "first" or "last". By default its last. You also can
5957
# specify "ignore" to ignore chain (supported only for Platform).
6058
# To set Custom Fields for the certificate, specify an array of CustomField objects as name-value pairs

examples/tpp/get_service_gen_cert_tpp.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17-
18-
from __future__ import print_function
19-
2017
from vcert import (CertificateRequest, venafi_connection, CSR_ORIGIN_SERVICE, CHAIN_OPTION_FIRST)
2118
import string
2219
import random
@@ -32,25 +29,25 @@ def main():
3229
url = environ.get('TPP_TOKEN_URL')
3330
user = environ.get('TPP_USER')
3431
password = environ.get('TPP_PASSWORD')
35-
zone = environ.get("TPP_ZONE")
32+
zone = environ.get('TPP_ZONE')
3633
server_trust_bundle = environ.get('TPP_TRUST_BUNDLE')
3734

3835
# Connection will be chosen automatically based on which arguments are passed.
3936
# If token is passed Venafi Cloud connection will be used.
4037
# If user, password, and URL Venafi Platform (TPP) will be used.
4138
# If your TPP server certificate signed with your own CA, or available only via proxy, you can specify
4239
# a trust bundle using http_request_kwargs.
43-
conn = venafi_connection(url=url, user=user, password=password, http_request_kwargs={"verify": server_trust_bundle})
40+
conn = venafi_connection(url=url, user=user, password=password, http_request_kwargs={'verify': server_trust_bundle})
4441

4542
# Build a Certificate request
46-
request = CertificateRequest(common_name=random_word(10) + ".venafi.example.com")
43+
request = CertificateRequest(common_name=f"{random_word(10)}.venafi.example.com")
4744
# Set the request to use a service generated CSR
4845
request.csr_origin = CSR_ORIGIN_SERVICE
4946
# Include some Subject Alternative Names
5047
request.san_dns = ["www.dns.venafi.example.com", "ww1.dns.venafi.example.com"]
5148
request.email_addresses = ["email1@venafi.example.com", "email2@venafi.example.com"]
5249
request.ip_addresses = ["127.0.0.1", "192.168.1.1"]
53-
request.uniform_resource_identifiers = ["http://wgtest.uri.com","https://ragnartest.uri.com"]
50+
request.uniform_resource_identifiers = ["http://wgtest.uri.com", "https://ragnartest.uri.com"]
5451
request.user_principal_names = ["upn1@venafi.example.com", "upn2@venafi.example.com"]
5552
# Specify whether or not to return the private key. It is False by default.
5653
# A password should be defined for the private key if include_private_key is True.

examples/tpp/set_policy_tpp_token.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
from parser import json_parser, yaml_parser
2020
from parser.utils import parse_policy_spec
21-
from policy.policy_spec import PolicySpecification, Policy, Subject, KeyPair, SubjectAltNames, Defaults, \
22-
DefaultSubject, DefaultKeyPair
21+
from policy.policy_spec import (PolicySpecification, Policy, Subject, KeyPair, SubjectAltNames, Defaults,
22+
DefaultSubject, DefaultKeyPair)
2323
from vcert import venafi_connection
2424
from vcert.common import Authentication, SCOPE_PM
2525
import logging
@@ -40,7 +40,7 @@ def main():
4040
# Get connector object.
4141
# The default state of this connection only allows for certificate management.
4242
connector = venafi_connection(url=url, user=user, password=password,
43-
http_request_kwargs={"verify": server_trust_bundle})
43+
http_request_kwargs={'verify': server_trust_bundle})
4444

4545
# Create Authentication object with required scope for policy management.
4646
auth = Authentication(user=user, password=password, scope=SCOPE_PM)

0 commit comments

Comments
 (0)