1414# See the License for the specific language governing permissions and
1515# limitations under the License.
1616#
17- import logging
1817import platform
1918import re
2019import unittest
2120
2221from assets import SSH_CERT_DATA , SSH_PRIVATE_KEY , SSH_PUBLIC_KEY
2322from test_env import TPP_TOKEN_URL , TPP_USER , TPP_PASSWORD , TPP_SSH_CADN
2423from test_utils import timestamp
25- from vcert import CommonConnection , SSHCertRequest , TPPTokenConnection , Authentication , \
26- SCOPE_SSH , write_ssh_files , logger , venafi_connection , VenafiPlatform
24+ from vcert import ( CommonConnection , SSHCertRequest , TPPTokenConnection , Authentication ,
25+ SCOPE_SSH , write_ssh_files , logger , venafi_connection , VenafiPlatform )
2726from vcert .ssh_utils import SSHRetrieveResponse , SSHKeyPair , SSHCATemplateRequest
2827
2928log = logger .get_child ("test-ssh" )
3029
31- SERVICE_GENERATED_NO_KEY_ERROR = "%s key data is %s empty for Certificate %s " # type: str
32- SSH_CERT_DATA_ERROR = "Certificate data is empty for Certificate %s " # type: str
30+ SERVICE_GENERATED_NO_KEY_ERROR = "{} key data is {} empty for Certificate {} " # type: str
31+ SSH_CERT_DATA_ERROR = "Certificate data is empty for Certificate {} " # type: str
3332
3433
3534class TestTPPSSHCertificate (unittest .TestCase ):
3635 def __init__ (self , * args , ** kwargs ):
37- self .tpp_conn = TPPTokenConnection (url = TPP_TOKEN_URL , http_request_kwargs = {" verify" : "/tmp/chain.pem" })
36+ self .tpp_conn = TPPTokenConnection (url = TPP_TOKEN_URL , http_request_kwargs = {' verify' : "/tmp/chain.pem" })
3837 auth = Authentication (user = TPP_USER , password = TPP_PASSWORD , scope = SCOPE_SSH )
3938 self .tpp_conn .get_access_token (auth )
4039 super (TestTPPSSHCertificate , self ).__init__ (* args , ** kwargs )
@@ -49,64 +48,65 @@ def test_enroll_local_generated_keypair(self):
4948 request .set_public_key_data (keypair .public_key ())
5049 response = _enroll_ssh_cert (self .tpp_conn , request )
5150 self .assertTrue (response .private_key_data is None ,
52- SERVICE_GENERATED_NO_KEY_ERROR % ("Private" , "not" , request .key_id ))
53- self .assertTrue (response .public_key_data , SERVICE_GENERATED_NO_KEY_ERROR % ("Public" , "" , request .key_id ))
51+ SERVICE_GENERATED_NO_KEY_ERROR . format ("Private" , "not" , request .key_id ))
52+ self .assertTrue (response .public_key_data , SERVICE_GENERATED_NO_KEY_ERROR . format ("Public" , "" , request .key_id ))
5453 self .assertTrue (response .public_key_data == request .get_public_key_data (),
55- "Public key on response does not match request.\n Expected: %s\n Got: %s"
56- % (request .get_public_key_data (), response .public_key_data ))
57- self .assertTrue (response .certificate_data , SSH_CERT_DATA_ERROR % request .key_id )
54+ f"Public key on response does not match request."
55+ f"\n Expected: { request .get_public_key_data ()} "
56+ f"\n Got: { response .public_key_data } " )
57+ self .assertTrue (response .certificate_data , SSH_CERT_DATA_ERROR .format (request .key_id ))
5858
5959 def test_enroll_service_generated_keypair (self ):
6060 request = SSHCertRequest (cadn = TPP_SSH_CADN , key_id = _random_key_id ())
6161 request .validity_period = "4h"
6262 request .source_addresses = ["test.com" ]
6363 response = _enroll_ssh_cert (self .tpp_conn , request )
64- self .assertTrue (response .private_key_data , SERVICE_GENERATED_NO_KEY_ERROR % ("Private" , "" , request .key_id ))
65- self .assertTrue (response .public_key_data , SERVICE_GENERATED_NO_KEY_ERROR % ("Public" , "" , request .key_id ))
66- self .assertTrue (response .certificate_data , SSH_CERT_DATA_ERROR % request .key_id )
64+ self .assertTrue (response .private_key_data , SERVICE_GENERATED_NO_KEY_ERROR . format ("Private" , "" , request .key_id ))
65+ self .assertTrue (response .public_key_data , SERVICE_GENERATED_NO_KEY_ERROR . format ("Public" , "" , request .key_id ))
66+ self .assertTrue (response .certificate_data , SSH_CERT_DATA_ERROR . format ( request .key_id ) )
6767
6868 def test_retrieve_ca_public_key (self ):
6969 tpp_connector = venafi_connection (platform = VenafiPlatform .TPP , url = TPP_TOKEN_URL ,
70- http_request_kwargs = {" verify" : "/tmp/chain.pem" })
70+ http_request_kwargs = {' verify' : "/tmp/chain.pem" })
7171 request = SSHCATemplateRequest (ca_template = TPP_SSH_CADN )
7272 ssh_config = tpp_connector .retrieve_ssh_config (ca_request = request )
73- self .assertIsNotNone (ssh_config .ca_public_key , "%s Public Key data is empty" % TPP_SSH_CADN )
74- self .assertIsNone (ssh_config .ca_principals , "%s default principals is not empty" % TPP_SSH_CADN )
75- log .debug ("%s Public Key data:\n %s" % ( TPP_SSH_CADN , ssh_config .ca_public_key ) )
73+ self .assertIsNotNone (ssh_config .ca_public_key , f" { TPP_SSH_CADN } Public Key data is empty" )
74+ self .assertIsNone (ssh_config .ca_principals , f" { TPP_SSH_CADN } default principals is not empty" )
75+ log .debug (f" { TPP_SSH_CADN } Public Key data:\n { ssh_config .ca_public_key } " )
7676
7777 def test_retrieve_ca_public_key_and_principals (self ):
7878 request = SSHCATemplateRequest (ca_template = TPP_SSH_CADN )
7979 ssh_config = self .tpp_conn .retrieve_ssh_config (ca_request = request )
80- self .assertIsNotNone (ssh_config .ca_public_key , "%s Public Key data is empty" % TPP_SSH_CADN )
81- self .assertIsNotNone (ssh_config .ca_principals , "%s default principals is empty" % TPP_SSH_CADN )
82- log .debug ("%s Public Key data: %s" % ( TPP_SSH_CADN , ssh_config .ca_public_key ) )
83- log .debug ("%s default principals: %s" % ( TPP_SSH_CADN , ssh_config .ca_principals ) )
80+ self .assertIsNotNone (ssh_config .ca_public_key , f" { TPP_SSH_CADN } Public Key data is empty" )
81+ self .assertIsNotNone (ssh_config .ca_principals , f" { TPP_SSH_CADN } default principals is empty" )
82+ log .debug (f" { TPP_SSH_CADN } Public Key data: { ssh_config .ca_public_key } " )
83+ log .debug (f" { TPP_SSH_CADN } default principals: { ssh_config .ca_principals } " )
8484
8585
8686class TestSSHUtils (unittest .TestCase ):
8787 def test_write_ssh_files (self ):
8888 key_id = _random_key_id ()
8989 normalized_name = re .sub (r"[^A-Za-z0-9]+" , "_" , key_id )
90- full_path = "./" + normalized_name
90+ full_path = f "./{ normalized_name } "
9191 write_ssh_files ("./" , key_id , SSH_CERT_DATA , SSH_PRIVATE_KEY , SSH_PUBLIC_KEY )
9292
93- err_msg = "%s serialization does not match expected value"
93+ err_msg = "{} serialization does not match expected value"
9494
95- with open (full_path + " -cert.pub" , "r" ) as cert_file :
95+ with open (f" { full_path } -cert.pub" , "r" ) as cert_file :
9696 s_cert = cert_file .read ()
97- self .assertTrue (SSH_CERT_DATA == s_cert , err_msg % "SSH Certificate" )
97+ self .assertTrue (SSH_CERT_DATA == s_cert , err_msg . format ( "SSH Certificate" ) )
9898
9999 with open (full_path , "r" ) as priv_key_file :
100100 s_priv_key = priv_key_file .read ()
101101 expected_priv_key = SSH_PRIVATE_KEY
102102 if platform .system () != "Windows" :
103103 expected_priv_key = expected_priv_key .replace ("\r \n " , "\n " )
104104
105- self .assertTrue (expected_priv_key == s_priv_key , err_msg % "SSH Private Key" )
105+ self .assertTrue (expected_priv_key == s_priv_key , err_msg . format ( "SSH Private Key" ) )
106106
107- with open (full_path + " .pub" , "r" ) as pub_key_file :
107+ with open (f" { full_path } .pub" , "r" ) as pub_key_file :
108108 s_pub_key = pub_key_file .read ()
109- self .assertTrue (SSH_PUBLIC_KEY == s_pub_key , err_msg % "SSH Public Key" )
109+ self .assertTrue (SSH_PUBLIC_KEY == s_pub_key , err_msg . format ( "SSH Public Key" ) )
110110
111111
112112def _enroll_ssh_cert (connector , request ):
@@ -123,4 +123,4 @@ def _enroll_ssh_cert(connector, request):
123123
124124
125125def _random_key_id ():
126- return "vcert-python-ssh-%s" % timestamp ()
126+ return f "vcert-python-ssh-{ timestamp ()} "
0 commit comments