Skip to content

Commit a420283

Browse files
committed
- Added argument "platform" on the venafi_connection method to instantiate a given Connector (TPP, VaaS) with no need to pass other values.
- Added example for retrieving a TPP CA public key and principals.
1 parent a201439 commit a420283

4 files changed

Lines changed: 121 additions & 14 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright 2021 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+
import logging
18+
import random
19+
import string
20+
from os import environ
21+
22+
from vcert import venafi_connection, Authentication, SCOPE_SSH, SSHKeyPair, SSHCertRequest, write_ssh_files, \
23+
VenafiPlatform, SSHCATemplateRequest
24+
25+
logging.basicConfig(level=logging.INFO)
26+
logging.getLogger("urllib3").setLevel(logging.ERROR)
27+
28+
29+
def main():
30+
# Get credentials from environment variables.
31+
url = environ.get('TPP_URL')
32+
ca_dn = environ.get('TPP_SSH_CADN')
33+
ca_guid = environ.get('TPP_SSH_CA_GUID')
34+
# Authentication is required for retrieving the CA principals only.
35+
user = environ.get("TPP_USER")
36+
password = environ.get("TPP_PASSWORD")
37+
38+
# A Connector can be instantiated with no values by using the platform argument.
39+
# url argument is always required for TPP.
40+
connector = venafi_connection(platform=VenafiPlatform.TPP, url=url, http_request_kwargs={"verify": False})
41+
# Optionally, the connector can be instantiated passing the specific arguments:
42+
# connector = venafi_connection(url=url, user=user, password=password, http_request_kwargs={"verify": False})
43+
44+
# If your TPP server certificate is signed with your own CA, or available only via proxy,
45+
# you can specify a trust bundle using requests vars:
46+
# connector = venafi_connection(url=url, api_key=api_key, access_token=access_token,
47+
# http_request_kwargs={"verify": "/path-to/bundle.pem"})
48+
49+
# Create an SSHCATemplateRequest to pass the identifier of the SSH Certificate Authority to retrieve.
50+
# Either CADN or Guid can be used as identifiers.
51+
request = SSHCATemplateRequest(ca_template=ca_dn)
52+
# request = SSHCATemplateRequest(ca_guid=ca_guid)
53+
54+
# Retrieve the public key.
55+
# No Authentication is provided to the Connector so, only the public key is available.
56+
ssh_config = connector.retrieve_ssh_config(ca_request=request)
57+
pub_key_data = ssh_config.ca_public_key
58+
with open("./ca-pub.key", 'w') as ca_file:
59+
ca_file.write(pub_key_data)
60+
61+
# To retrieve the CA principals create an Authentication object with the proper scope to manage SSH certificates.
62+
auth = Authentication(user=user, password=password, scope=SCOPE_SSH)
63+
# Additionally, you may change the default client id for a custom one.
64+
# Make sure this id has been registered on the TPP instance beforehand.
65+
# Also, the user (TTP_USER) should be allowed to use this application
66+
# and the application should have the ssh permissions enabled.
67+
auth.client_id = 'vcert-ssh-ca-pubkey-demo'
68+
# Request an access token.
69+
# After the request is successful, subsequent api calls will use the same token
70+
connector.get_access_token(auth)
71+
# Retrieve SSH Certificate Authority public key and principals
72+
ssh_config = connector.retrieve_ssh_config(ca_request=request)
73+
with open("./ca2-pub.key", 'w') as ca_file:
74+
ca_file.write(pub_key_data)
75+
76+
print("CA principals:\n")
77+
print(ssh_config.ca_principals)
78+
79+
80+
if __name__ == '__main__':
81+
main()

vcert/__init__.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
17-
from .logger import setup_logger, get_logger, get_child
1816
from .common import CertificateRequest, CommonConnection, RevocationRequest, ZoneConfig, CertField, KeyType, \
1917
CustomField, Authentication, SCOPE_CM, SCOPE_PM, SCOPE_SSH, CSR_ORIGIN_LOCAL, CSR_ORIGIN_PROVIDED, \
20-
CSR_ORIGIN_SERVICE, CHAIN_OPTION_FIRST, CHAIN_OPTION_IGNORE, CHAIN_OPTION_LAST
18+
CSR_ORIGIN_SERVICE, CHAIN_OPTION_FIRST, CHAIN_OPTION_IGNORE, CHAIN_OPTION_LAST, VenafiPlatform
2119
from .connection_cloud import CloudConnection
2220
from .connection_tpp import TPPConnection
2321
from .connection_tpp_token import TPPTokenConnection
2422
from .connection_fake import FakeConnection
23+
from .errors import VenafiError
24+
from .logger import setup_logger, get_logger, get_child
2525
from .pem import Certificate
26-
from .ssh_utils import SSHCertRequest, SSHKeyPair, write_ssh_files
26+
from .ssh_utils import SSHCertRequest, SSHKeyPair, write_ssh_files, SSHCATemplateRequest, SSHConfig
2727
from .tpp_utils import IssuerHint
2828

2929
setup_logger()
@@ -54,7 +54,7 @@ def Connection(url=None, token=None, user=None, password=None, fake=False, http_
5454

5555

5656
def venafi_connection(url=None, api_key=None, user=None, password=None, access_token=None, refresh_token=None,
57-
fake=False, http_request_kwargs=None):
57+
fake=False, http_request_kwargs=None, platform=None):
5858
"""
5959
Return connection based on credentials list.
6060
Venafi Platform (TPP) requires URL and access_token (or user and password for getting a new access_token)
@@ -68,14 +68,26 @@ def venafi_connection(url=None, api_key=None, user=None, password=None, access_t
6868
:param str refresh_token: TPP refresh token (optional)
6969
:param bool fake: Use fake connection
7070
:param dict[str, Any] http_request_kwargs: Option for specifying trust bundle or to operate insecurely.
71+
:param VenafiPlatform platform: The platform to be used with the Connector
7172
:rtype CommonConnection:
7273
"""
73-
if fake:
74-
return FakeConnection()
75-
if url and (access_token or refresh_token or (user and password)):
76-
return TPPTokenConnection(url=url, user=user, password=password, access_token=access_token,
77-
refresh_token=refresh_token, http_request_kwargs=http_request_kwargs)
78-
if api_key:
79-
return CloudConnection(token=api_key, url=url, http_request_kwargs=http_request_kwargs)
74+
if platform:
75+
if platform == VenafiPlatform.FAKE:
76+
return FakeConnection()
77+
elif platform == VenafiPlatform.TPP:
78+
return TPPTokenConnection(url=url, user=user, password=password, access_token=access_token,
79+
refresh_token=refresh_token, http_request_kwargs=http_request_kwargs)
80+
elif platform == VenafiPlatform.VAAS:
81+
return CloudConnection(token=api_key, url=url, http_request_kwargs=http_request_kwargs)
82+
else:
83+
raise VenafiError("Invalid Platform: %s. Cannot instantiate a Connector." % platform)
8084
else:
81-
raise Exception("Bad credentials list")
85+
if fake:
86+
return FakeConnection()
87+
if url and (access_token or refresh_token or (user and password)):
88+
return TPPTokenConnection(url=url, user=user, password=password, access_token=access_token,
89+
refresh_token=refresh_token, http_request_kwargs=http_request_kwargs)
90+
if api_key:
91+
return CloudConnection(token=api_key, url=url, http_request_kwargs=http_request_kwargs)
92+
else:
93+
raise VenafiError("Bad credentials list")

vcert/common.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import socket
2323
import sys
2424

25+
import enum
2526
import ipaddress
2627
from builtins import bytes
2728
from cryptography import x509
@@ -729,3 +730,16 @@ def process_server_response(r):
729730
else:
730731
log.error("Unexpected content type: %s for request %s" % (content_type, r.request.url))
731732
raise ServerUnexptedBehavior
733+
734+
735+
class VenafiPlatform(enum.IntEnum):
736+
def __new__(cls, value, description):
737+
obj = int.__new__(cls, value)
738+
obj._value_ = value
739+
obj.description = description
740+
741+
return obj
742+
743+
FAKE = 100, "Connector for testing purposes"
744+
TPP = 200, "Trust Protection Platfom"
745+
VAAS = 400, "Venafi as a Service"

vcert/ssh_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def __init__(self, response):
176176

177177

178178
class SSHCATemplateRequest:
179-
def __init__(self, ca_template, ca_guid):
179+
def __init__(self, ca_template=None, ca_guid=None):
180180
"""
181181
182182
:param str ca_template:

0 commit comments

Comments
 (0)