1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
import os
import sys
import lxml.etree
from mock import Mock, MagicMock, patch
from Bcfg2.Server.Plugins.Cfg.CfgAuthorizedKeysGenerator import *
from Bcfg2.Server.Plugin import PluginExecutionError
# add all parent testsuite directories to sys.path to allow (most)
# relative imports in python 2.4
path = os.path.dirname(__file__)
while path != "/":
if os.path.basename(path).lower().startswith("test"):
sys.path.append(path)
if os.path.basename(path) == "testsuite":
break
path = os.path.dirname(path)
from common import *
from TestServer.TestPlugins.TestCfg.Test_init import TestCfgGenerator
from TestServer.TestPlugin.Testhelpers import TestStructFile
class TestCfgAuthorizedKeysGenerator(TestCfgGenerator, TestStructFile):
test_obj = CfgAuthorizedKeysGenerator
should_monitor = False
def get_obj(self, name=None, core=None, fam=None):
if name is None:
name = self.path
Bcfg2.Server.Plugins.Cfg.CfgAuthorizedKeysGenerator.CFG = Mock()
if core is not None:
Bcfg2.Server.Plugins.Cfg.CfgAuthorizedKeysGenerator.CFG.core = core
return self.test_obj(name)
@patch("Bcfg2.Server.Plugins.Cfg.CfgGenerator.handle_event")
@patch("Bcfg2.Server.Plugin.helpers.StructFile.HandleEvent")
def test_handle_event(self, mock_HandleEvent, mock_handle_event):
akg = self.get_obj()
evt = Mock()
akg.handle_event(evt)
mock_HandleEvent.assert_called_with(akg, evt)
mock_handle_event.assert_called_with(akg, evt)
def test_category(self):
akg = self.get_obj()
cfp = Mock()
cfp.has_section.return_value = False
cfp.has_option.return_value = False
Bcfg2.Server.Plugins.Cfg.CfgAuthorizedKeysGenerator.SETUP = Mock()
Bcfg2.Server.Plugins.Cfg.CfgAuthorizedKeysGenerator.SETUP.cfp = cfp
self.assertIsNone(akg.category)
cfp.has_section.assert_called_with("sshkeys")
cfp.reset_mock()
cfp.has_section.return_value = True
self.assertIsNone(akg.category)
cfp.has_section.assert_called_with("sshkeys")
cfp.has_option.assert_called_with("sshkeys", "category")
cfp.reset_mock()
cfp.has_option.return_value = True
self.assertEqual(akg.category, cfp.get.return_value)
cfp.has_section.assert_called_with("sshkeys")
cfp.has_option.assert_called_with("sshkeys", "category")
cfp.get.assert_called_with("sshkeys", "category")
@patch("Bcfg2.Server.Plugins.Cfg.CfgAuthorizedKeysGenerator.ClientMetadata")
@patch("Bcfg2.Server.Plugins.Cfg.CfgAuthorizedKeysGenerator.CfgAuthorizedKeysGenerator.category", "category")
def test_get_data(self, mock_ClientMetadata):
akg = self.get_obj()
akg.XMLMatch = Mock()
def ClientMetadata(host, profile, groups, *args):
rv = Mock()
rv.hostname = host
rv.profile = profile
rv.groups = groups
return rv
mock_ClientMetadata.side_effect = ClientMetadata
def build_metadata(host):
rv = Mock()
rv.hostname = host
rv.profile = host
return rv
akg.core.build_metadata = Mock()
akg.core.build_metadata.side_effect = build_metadata
def Bind(ent, md):
ent.text = "%s %s" % (md.profile, ent.get("name"))
return ent
akg.core.Bind = Mock()
akg.core.Bind.side_effect = Bind
metadata = Mock()
metadata.profile = "profile"
metadata.group_in_category.return_value = "profile"
entry = lxml.etree.Element("Path", name="/root/.ssh/authorized_keys")
def reset():
mock_ClientMetadata.reset_mock()
akg.XMLMatch.reset_mock()
akg.core.build_metadata.reset_mock()
akg.core.Bind.reset_mock()
metadata.reset_mock()
pubkey = "/home/foo/.ssh/id_rsa.pub"
spec = lxml.etree.Element("AuthorizedKeys")
lxml.etree.SubElement(spec, "Allow", attrib={"from": pubkey})
akg.XMLMatch.return_value = spec
self.assertEqual(akg.get_data(entry, metadata), "profile %s" % pubkey)
akg.XMLMatch.assert_called_with(metadata)
self.assertEqual(akg.core.Bind.call_args[0][0].get("name"), pubkey)
self.assertEqual(akg.core.Bind.call_args[0][1], metadata)
reset()
group = "somegroup"
spec = lxml.etree.Element("AuthorizedKeys")
lxml.etree.SubElement(spec, "Allow",
attrib={"from": pubkey, "group": group})
akg.XMLMatch.return_value = spec
self.assertEqual(akg.get_data(entry, metadata),
"%s %s" % (group, pubkey))
akg.XMLMatch.assert_called_with(metadata)
self.assertItemsEqual(mock_ClientMetadata.call_args[0][2], [group])
self.assertEqual(akg.core.Bind.call_args[0][0].get("name"), pubkey)
self.assertIn(group, akg.core.Bind.call_args[0][1].groups)
reset()
host = "baz.example.com"
spec = lxml.etree.Element("AuthorizedKeys")
lxml.etree.SubElement(
lxml.etree.SubElement(spec,
"Allow",
attrib={"from": pubkey, "host": host}),
"Params", foo="foo", bar="bar=bar")
akg.XMLMatch.return_value = spec
self.assertEqual(akg.get_data(entry, metadata),
"foo=foo,bar=bar=bar %s %s" % (host, pubkey))
akg.XMLMatch.assert_called_with(metadata)
akg.core.build_metadata.assert_called_with(host)
self.assertEqual(akg.core.Bind.call_args[0][0].get("name"), pubkey)
self.assertEqual(akg.core.Bind.call_args[0][1].hostname, host)
reset()
spec = lxml.etree.Element("AuthorizedKeys")
text = lxml.etree.SubElement(spec, "Allow")
text.text = "ssh-rsa publickey /foo/bar\n"
lxml.etree.SubElement(text, "Params", foo="foo")
akg.XMLMatch.return_value = spec
self.assertEqual(akg.get_data(entry, metadata),
"foo=foo %s" % text.text.strip())
akg.XMLMatch.assert_called_with(metadata)
self.assertFalse(akg.core.build_metadata.called)
self.assertFalse(akg.core.Bind.called)
reset()
lxml.etree.SubElement(spec, "Allow", attrib={"from": pubkey})
akg.XMLMatch.return_value = spec
self.assertItemsEqual(akg.get_data(entry, metadata).splitlines(),
["foo=foo %s" % text.text.strip(),
"profile %s" % pubkey])
akg.XMLMatch.assert_called_with(metadata)
reset()
metadata.group_in_category.return_value = ''
spec = lxml.etree.Element("AuthorizedKeys")
lxml.etree.SubElement(spec, "Allow", attrib={"from": pubkey})
akg.XMLMatch.return_value = spec
self.assertEqual(akg.get_data(entry, metadata), '')
akg.XMLMatch.assert_called_with(metadata)
self.assertFalse(akg.core.build_metadata.called)
self.assertFalse(akg.core.Bind.called)
self.assertFalse(mock_ClientMetadata.called)
|