Browse Source

Replace '\r\n' with '\n' in cert pem before looks for existing ones

Since the NSX fix the certificate pem when creating it, we should
do the same when comparing a new one to existing ones.

Change-Id: I7da39447869c7ec2a99820676b6fb75b0a098acf
tags/16.1.0
asarfaty 3 weeks ago
committed by Adit Sarfaty
parent
commit
dab604475a
3 changed files with 31 additions and 4 deletions
  1. +23
    -0
      vmware_nsxlib/tests/unit/v3/test_trust_management.py
  2. +4
    -2
      vmware_nsxlib/v3/policy/core_resources.py
  3. +4
    -2
      vmware_nsxlib/v3/trust_management.py

+ 23
- 0
vmware_nsxlib/tests/unit/v3/test_trust_management.py View File

@@ -41,3 +41,26 @@ class TestNsxLibTrustManagement(nsxlib_testcase.NsxClientTestCase):
create.assert_called_with(
'trust-management/certificates?action=import',
body)

def test_find_cert_with_pem_empty(self):
pem = 'abc'
with mock.patch.object(self.nsxlib.client, 'get',
return_value={'results': []}):
results = self.nsxlib.trust_management.find_cert_with_pem(pem)
self.assertEqual(0, len(results))

def test_find_cert_with_pem_found(self):
pem = consts.FAKE_CERT_PEM
with mock.patch.object(
self.nsxlib.client, 'get',
return_value={'results': consts.FAKE_CERT_LIST}):
results = self.nsxlib.trust_management.find_cert_with_pem(pem)
self.assertEqual(1, len(results))

def test_find_cert_with_pem_rn_found(self):
pem = consts.FAKE_CERT_PEM.replace('\n', '\r\n')
with mock.patch.object(
self.nsxlib.client, 'get',
return_value={'results': consts.FAKE_CERT_LIST}):
results = self.nsxlib.trust_management.find_cert_with_pem(pem)
self.assertEqual(1, len(results))

+ 4
- 2
vmware_nsxlib/v3/policy/core_resources.py View File

@@ -4567,10 +4567,12 @@ class NsxPolicyCertApi(NsxPolicyResourceBase):

def find_cert_with_pem(self, cert_pem,
tenant=constants.POLICY_INFRA_TENANT):
# Find certificate with cert_pem
"""Find NSX certificates with specific pem and return their IDs"""
# First fix Dos to unix possible issues, as the NSX backed also does
nsx_style_pem = cert_pem.replace('\r\n', '\n')
certs = self.list(tenant=tenant)
cert_ids = [cert['id'] for cert in certs
if cert['pem_encoded'] == cert_pem]
if cert['pem_encoded'] == nsx_style_pem]
return cert_ids

def update(self, certificate_id, name=IGNORE,


+ 4
- 2
vmware_nsxlib/v3/trust_management.py View File

@@ -68,10 +68,12 @@ class NsxLibTrustManagement(utils.NsxLibApiBase):
self._delete_by_path_with_retry(resource)

def find_cert_with_pem(self, cert_pem):
# Find certificate with cert_pem
"""Find NSX certificates with specific pem and return their IDs"""
# First fix Dos to unix possible issues, as the NSX backed also does
nsx_style_pem = cert_pem.replace('\r\n', '\n')
certs = self.get_certs()
cert_ids = [cert['id'] for cert in certs
if cert['pem_encoded'] == cert_pem]
if cert['pem_encoded'] == nsx_style_pem]
return cert_ids

def create_identity(self, name, cert_id,


Loading…
Cancel
Save