|
|
f00d0e |
From a6504bd7d32fe3553b9f6f807f3d84a1b87bb77c Mon Sep 17 00:00:00 2001
|
|
|
f00d0e |
From: Antonio Torres <antorres@redhat.com>
|
|
|
f00d0e |
Date: Wed, 24 Feb 2021 17:26:08 +0100
|
|
|
f00d0e |
Subject: [PATCH] Add tests for KRA Agent validation
|
|
|
f00d0e |
|
|
|
f00d0e |
Add unit tests for KRA Agent validation.
|
|
|
f00d0e |
|
|
|
f00d0e |
Signed-off-by: Antonio Torres <antorres@redhat.com>
|
|
|
f00d0e |
---
|
|
|
f00d0e |
tests/test_ipa_agent.py | 174 +++++++++++++++++++++++++++++++++++++++-
|
|
|
f00d0e |
1 file changed, 172 insertions(+), 2 deletions(-)
|
|
|
f00d0e |
|
|
|
f00d0e |
diff --git a/tests/test_ipa_agent.py b/tests/test_ipa_agent.py
|
|
|
f00d0e |
index 6605745..9b691f7 100644
|
|
|
f00d0e |
--- a/tests/test_ipa_agent.py
|
|
|
f00d0e |
+++ b/tests/test_ipa_agent.py
|
|
|
f00d0e |
@@ -4,11 +4,11 @@
|
|
|
f00d0e |
|
|
|
f00d0e |
from base import BaseTest
|
|
|
f00d0e |
from unittest.mock import Mock, patch
|
|
|
f00d0e |
-from util import capture_results, CAInstance
|
|
|
f00d0e |
+from util import capture_results, CAInstance, KRAInstance
|
|
|
f00d0e |
|
|
|
f00d0e |
from ipahealthcheck.core import config, constants
|
|
|
f00d0e |
from ipahealthcheck.ipa.plugin import registry
|
|
|
f00d0e |
-from ipahealthcheck.ipa.certs import IPARAAgent
|
|
|
f00d0e |
+from ipahealthcheck.ipa.certs import IPARAAgent, IPAKRAAgent
|
|
|
f00d0e |
|
|
|
f00d0e |
from ipalib import errors
|
|
|
f00d0e |
from ipapython.dn import DN
|
|
|
f00d0e |
@@ -218,3 +218,173 @@ class TestNSSAgent(BaseTest):
|
|
|
f00d0e |
assert result.result == constants.SUCCESS
|
|
|
f00d0e |
assert result.source == 'ipahealthcheck.ipa.certs'
|
|
|
f00d0e |
assert result.check == 'IPARAAgent'
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+class TestKRAAgent(BaseTest):
|
|
|
f00d0e |
+ cert = IPACertificate()
|
|
|
f00d0e |
+ patches = {
|
|
|
f00d0e |
+ 'ldap.initialize':
|
|
|
f00d0e |
+ Mock(return_value=mock_ldap_conn()),
|
|
|
f00d0e |
+ 'ipaserver.install.krainstance.KRAInstance':
|
|
|
f00d0e |
+ Mock(return_value=KRAInstance()),
|
|
|
f00d0e |
+ 'ipalib.x509.load_certificate_from_file':
|
|
|
f00d0e |
+ Mock(return_value=cert),
|
|
|
f00d0e |
+ }
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ def test_kra_agent_ok(self):
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ attrs = dict(
|
|
|
f00d0e |
+ description=['2;1;CN=ISSUER;CN=RA AGENT'],
|
|
|
f00d0e |
+ usercertificate=[self.cert],
|
|
|
f00d0e |
+ )
|
|
|
f00d0e |
+ fake_conn = LDAPClient('ldap://localhost', no_schema=True)
|
|
|
f00d0e |
+ ldapentry = LDAPEntry(fake_conn,
|
|
|
f00d0e |
+ DN('uid=ipakra,ou=people,o=kra,o=ipaca'))
|
|
|
f00d0e |
+ for attr, values in attrs.items():
|
|
|
f00d0e |
+ ldapentry[attr] = values
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ framework = object()
|
|
|
f00d0e |
+ registry.initialize(framework, config.Config())
|
|
|
f00d0e |
+ f = IPAKRAAgent(registry)
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ f.conn = mock_ldap([ldapentry])
|
|
|
f00d0e |
+ self.results = capture_results(f)
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ assert len(self.results) == 1
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ result = self.results.results[0]
|
|
|
f00d0e |
+ assert result.result == constants.SUCCESS
|
|
|
f00d0e |
+ assert result.source == 'ipahealthcheck.ipa.certs'
|
|
|
f00d0e |
+ assert result.check == 'IPAKRAAgent'
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ def test_kra_agent_no_description(self):
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ attrs = dict(
|
|
|
f00d0e |
+ usercertificate=[self.cert],
|
|
|
f00d0e |
+ )
|
|
|
f00d0e |
+ fake_conn = LDAPClient('ldap://localhost', no_schema=True)
|
|
|
f00d0e |
+ ldapentry = LDAPEntry(fake_conn,
|
|
|
f00d0e |
+ DN('uid=ipakra,ou=people,o=kra,o=ipaca'))
|
|
|
f00d0e |
+ for attr, values in attrs.items():
|
|
|
f00d0e |
+ ldapentry[attr] = values
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ framework = object()
|
|
|
f00d0e |
+ registry.initialize(framework, config.Config())
|
|
|
f00d0e |
+ f = IPAKRAAgent(registry)
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ f.conn = mock_ldap([ldapentry])
|
|
|
f00d0e |
+ self.results = capture_results(f)
|
|
|
f00d0e |
+ result = self.results.results[0]
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ assert result.result == constants.ERROR
|
|
|
f00d0e |
+ assert 'description' in result.kw.get('msg')
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ @patch('ipalib.x509.load_certificate_from_file')
|
|
|
f00d0e |
+ def test_kra_agent_load_failure(self, mock_load_cert):
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ mock_load_cert.side_effect = IOError('test')
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ framework = object()
|
|
|
f00d0e |
+ registry.initialize(framework, config.Config())
|
|
|
f00d0e |
+ f = IPAKRAAgent(registry)
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ self.results = capture_results(f)
|
|
|
f00d0e |
+ result = self.results.results[0]
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ assert result.result == constants.ERROR
|
|
|
f00d0e |
+ assert result.kw.get('error') == 'test'
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ def test_kra_agent_no_entry_found(self):
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ framework = object()
|
|
|
f00d0e |
+ registry.initialize(framework, config.Config())
|
|
|
f00d0e |
+ f = IPAKRAAgent(registry)
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ f.conn = mock_ldap(None) # None == NotFound
|
|
|
f00d0e |
+ self.results = capture_results(f)
|
|
|
f00d0e |
+ result = self.results.results[0]
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ assert result.result == constants.ERROR
|
|
|
f00d0e |
+ assert result.kw.get('msg') == 'KRA agent not found in LDAP'
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ def test_kra_agent_too_many(self):
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ attrs = dict(
|
|
|
f00d0e |
+ description=['2;1;CN=ISSUER;CN=RA AGENT'],
|
|
|
f00d0e |
+ usercertificate=[self.cert],
|
|
|
f00d0e |
+ )
|
|
|
f00d0e |
+ fake_conn = LDAPClient('ldap://localhost', no_schema=True)
|
|
|
f00d0e |
+ ldapentry = LDAPEntry(fake_conn,
|
|
|
f00d0e |
+ DN('uid=ipakra,ou=people,o=kra,o=ipaca'))
|
|
|
f00d0e |
+ for attr, values in attrs.items():
|
|
|
f00d0e |
+ ldapentry[attr] = values
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ ldapentry2 = LDAPEntry(fake_conn,
|
|
|
f00d0e |
+ DN('uid=ipakra,ou=people,o=kra,o=ipaca'))
|
|
|
f00d0e |
+ for attr, values in attrs.items():
|
|
|
f00d0e |
+ ldapentry[attr] = values
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ framework = object()
|
|
|
f00d0e |
+ registry.initialize(framework, config.Config())
|
|
|
f00d0e |
+ f = IPAKRAAgent(registry)
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ f.conn = mock_ldap([ldapentry, ldapentry2])
|
|
|
f00d0e |
+ self.results = capture_results(f)
|
|
|
f00d0e |
+ result = self.results.results[0]
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ assert result.result == constants.ERROR
|
|
|
f00d0e |
+ assert result.kw.get('found') == 2
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ def test_kra_agent_nonmatching_cert(self):
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ cert2 = IPACertificate(2)
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ attrs = dict(
|
|
|
f00d0e |
+ description=['2;1;CN=ISSUER;CN=RA AGENT'],
|
|
|
f00d0e |
+ usercertificate=[cert2],
|
|
|
f00d0e |
+ )
|
|
|
f00d0e |
+ fake_conn = LDAPClient('ldap://localhost', no_schema=True)
|
|
|
f00d0e |
+ ldapentry = LDAPEntry(fake_conn,
|
|
|
f00d0e |
+ DN('uid=ipakra,ou=people,o=kra,o=ipaca'))
|
|
|
f00d0e |
+ for attr, values in attrs.items():
|
|
|
f00d0e |
+ ldapentry[attr] = values
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ framework = object()
|
|
|
f00d0e |
+ registry.initialize(framework, config.Config())
|
|
|
f00d0e |
+ f = IPAKRAAgent(registry)
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ f.conn = mock_ldap([ldapentry])
|
|
|
f00d0e |
+ self.results = capture_results(f)
|
|
|
f00d0e |
+ result = self.results.results[0]
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ assert result.result == constants.ERROR
|
|
|
f00d0e |
+ assert result.kw.get('certfile') == paths.RA_AGENT_PEM
|
|
|
f00d0e |
+ assert result.kw.get('dn') == 'uid=ipakra,ou=people,o=kra,o=ipaca'
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ def test_kra_agent_multiple_certs(self):
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ cert2 = IPACertificate(2)
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ attrs = dict(
|
|
|
f00d0e |
+ description=['2;1;CN=ISSUER;CN=RA AGENT'],
|
|
|
f00d0e |
+ usercertificate=[cert2, self.cert],
|
|
|
f00d0e |
+ )
|
|
|
f00d0e |
+ fake_conn = LDAPClient('ldap://localhost', no_schema=True)
|
|
|
f00d0e |
+ ldapentry = LDAPEntry(fake_conn,
|
|
|
f00d0e |
+ DN('uid=ipakra,ou=people,o=kra,o=ipaca'))
|
|
|
f00d0e |
+ for attr, values in attrs.items():
|
|
|
f00d0e |
+ ldapentry[attr] = values
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ framework = object()
|
|
|
f00d0e |
+ registry.initialize(framework, config.Config)
|
|
|
f00d0e |
+ f = IPAKRAAgent(registry)
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ f.conn = mock_ldap([ldapentry])
|
|
|
f00d0e |
+ self.results = capture_results(f)
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ assert len(self.results) == 1
|
|
|
f00d0e |
+
|
|
|
f00d0e |
+ result = self.results.results[0]
|
|
|
f00d0e |
+ assert result.result == constants.SUCCESS
|
|
|
f00d0e |
+ assert result.source == 'ipahealthcheck.ipa.certs'
|
|
|
f00d0e |
+ assert result.check == 'IPAKRAAgent'
|
|
|
f00d0e |
--
|
|
|
f00d0e |
2.26.2
|
|
|
f00d0e |
|