Skip to content

Commit 7525893

Browse files
authored
Merge pull request #848 from teluq-pbrideau/feat/auth-with-cert
2 parents 273e380 + 326a027 commit 7525893

File tree

5 files changed

+165
-6
lines changed

5 files changed

+165
-6
lines changed

lib/puppet/provider/zabbix_host/ruby.rb

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def self.instances
1313
selectInterfaces: %w[interfaceid type main ip port useip details],
1414
selectGroups: ['name'],
1515
selectMacros: %w[macro value],
16-
output: %w[host proxy_hostid]
16+
output: %w[host proxy_hostid tls_accept tls_connect tls_issuer tls_subject]
1717
}
1818
)
1919

@@ -38,7 +38,11 @@ def self.instances
3838
macros: h['macros'].map { |macro| { macro['macro'] => macro['value'] } },
3939
proxy: proxy_select,
4040
interfacetype: interface['type'].to_i,
41-
interfacedetails: interface['details']
41+
interfacedetails: interface['details'],
42+
tls_accept: h['tls_accept'].to_i,
43+
tls_connect: h['tls_connect'].to_i,
44+
tls_issuer: h['tls_issuer'].to_s,
45+
tls_subject: h['tls_subject'].to_s
4246
)
4347
end
4448
end
@@ -60,6 +64,9 @@ def create
6064

6165
proxy_hostid = @resource[:proxy].nil? || @resource[:proxy].empty? ? nil : zbx.proxies.get_id(host: @resource[:proxy])
6266

67+
tls_accept = @resource[:tls_accept].nil? ? 1 : @resource[:tls_accept]
68+
tls_connect = @resource[:tls_connect].nil? ? 1 : @resource[:tls_connect]
69+
6370
# Now we create the host
6471
zbx.hosts.create(
6572
host: @resource[:hostname],
@@ -76,7 +83,11 @@ def create
7683
}
7784
],
7885
templates: templates,
79-
groups: groups
86+
groups: groups,
87+
tls_connect: tls_connect,
88+
tls_accept: tls_accept,
89+
tls_issuer: @resource[:tls_issuer].nil? ? '' : @resource[:tls_issuer],
90+
tls_subject: @resource[:tls_subject].nil? ? '' : @resource[:tls_subject]
8091
)
8192
end
8293

@@ -224,4 +235,37 @@ def proxy=(string)
224235
proxy_hostid: zbx.proxies.get_id(host: string)
225236
)
226237
end
238+
239+
def tls_connect=(int)
240+
@property_hash[:tls_connect] = int
241+
zbx.hosts.create_or_update(
242+
host: @resource[:hostname],
243+
tls_connect: @property_hash[:tls_connect].nil? ? 1 : @property_hash[:tls_connect]
244+
)
245+
end
246+
247+
def tls_accept=(int)
248+
@property_hash[:tls_accept] = int
249+
250+
zbx.hosts.create_or_update(
251+
host: @resource[:hostname],
252+
tls_accept: @property_hash[:tls_accept].nil? ? 1 : @property_hash[:tls_accept]
253+
)
254+
end
255+
256+
def tls_issuer=(string)
257+
@property_hash[:tls_issuer] = string
258+
zbx.hosts.create_or_update(
259+
host: @resource[:hostname],
260+
tls_issuer: @property_hash[:tls_issuer].nil? ? '' : @property_hash[:tls_issuer]
261+
)
262+
end
263+
264+
def tls_subject=(string)
265+
@property_hash[:tls_subject] = string
266+
zbx.hosts.create_or_update(
267+
host: @resource[:hostname],
268+
tls_subject: @property_hash[:tls_subject].nil? ? '' : @property_hash[:tls_subject]
269+
)
270+
end
227271
end

lib/puppet/type/zabbix_host.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ def munge_boolean(value)
2727
end
2828
end
2929

30+
def munge_encryption(value)
31+
case value
32+
when 1, 'unencrypted', :unencrypted
33+
1
34+
when 2, 'psk', :psk
35+
2
36+
when 4, 'cert', :cert
37+
4
38+
else
39+
raise(Puppet::Error, 'munge_encryption only takes unencrypted, psk or cert')
40+
end
41+
end
42+
3043
newparam(:hostname, namevar: true) do
3144
desc 'FQDN of the machine.'
3245
end
@@ -123,6 +136,36 @@ def insync?(is)
123136
desc 'Whether it is monitored by an proxy or not.'
124137
end
125138

139+
newproperty(:tls_connect) do
140+
desc 'How the server connect to the client (unencrypted, psk or cert)'
141+
def insync?(is)
142+
is.to_i == should.to_i
143+
end
144+
145+
munge do |value|
146+
@resource.munge_encryption(value)
147+
end
148+
end
149+
150+
newproperty(:tls_accept) do
151+
desc 'How the client connect to the server (unencrypted, psk or cert)'
152+
def insync?(is)
153+
is.to_i == should.to_i
154+
end
155+
156+
munge do |value|
157+
@resource.munge_encryption(value)
158+
end
159+
end
160+
161+
newproperty(:tls_issuer) do
162+
desc 'Certificate issuer.'
163+
end
164+
165+
newproperty(:tls_subject) do
166+
desc 'Certificate subject.'
167+
end
168+
126169
autorequire(:file) { '/etc/zabbix/api.conf' }
127170

128171
validate do

manifests/agent.pp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
# @param tlsaccept What incoming connections to accept from Zabbix server. Used for a passive proxy, ignored on an active proxy.
8080
# @param tlscafile Full pathname of a file containing the top-level CA(s) certificates for peer certificate verification.
8181
# @param tlscertfile Full pathname of a file containing the proxy certificate or certificate chain.
82+
# @param tlscertissuer Issuer of the certificate that is allowed to talk with the serve
83+
# @param tlscertsubject Subject of the certificate that is allowed to talk with the server
8284
# @param tlsconnect How the proxy should connect to Zabbix server. Used for an active proxy, ignored on a passive proxy.
8385
# @param tlscrlfile Full pathname of a file containing revoked certificates.
8486
# @param tlskeyfile Full pathname of a file containing the proxy private key.
@@ -192,16 +194,18 @@
192194
$userparameter = $zabbix::params::agent_userparameter,
193195
Optional[String[1]] $loadmodulepath = $zabbix::params::agent_loadmodulepath,
194196
$loadmodule = $zabbix::params::agent_loadmodule,
195-
$tlsaccept = $zabbix::params::agent_tlsaccept,
197+
Optional[Enum['unencrypted','psk','cert']] $tlsaccept = $zabbix::params::agent_tlsaccept,
196198
$tlscafile = $zabbix::params::agent_tlscafile,
197199
$tlscertfile = $zabbix::params::agent_tlscertfile,
200+
Optional[String[1]] $tlscertissuer = undef,
201+
Optional[String[1]] $tlscertsubject = undef,
198202
Optional[String[1]] $tlscipherall = $zabbix::params::agent_tlscipherall,
199203
Optional[String[1]] $tlscipherall13 = $zabbix::params::agent_tlscipherall13,
200204
Optional[String[1]] $tlsciphercert = $zabbix::params::agent_tlsciphercert,
201205
Optional[String[1]] $tlsciphercert13 = $zabbix::params::agent_tlsciphercert13,
202206
Optional[String[1]] $tlscipherpsk = $zabbix::params::agent_tlscipherpsk,
203207
Optional[String[1]] $tlscipherpsk13 = $zabbix::params::agent_tlscipherpsk13,
204-
$tlsconnect = $zabbix::params::agent_tlsconnect,
208+
Optional[Enum['unencrypted','psk','cert']] $tlsconnect = $zabbix::params::agent_tlsconnect,
205209
$tlscrlfile = $zabbix::params::agent_tlscrlfile,
206210
$tlskeyfile = $zabbix::params::agent_tlskeyfile,
207211
$tlspskfile = $zabbix::params::agent_tlspskfile,
@@ -273,6 +277,10 @@
273277
interfacetype => $zbx_interface_type,
274278
interfacedetails => $zbx_interface_details,
275279
proxy => $use_proxy,
280+
tls_accept => $tlsaccept,
281+
tls_connect => $tlsconnect,
282+
tls_issuer => $tlscertissuer,
283+
tls_subject => $tlscertsubject,
276284
}
277285
}
278286

manifests/resources/agent.pp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
# @param proxy Whether it is monitored by an proxy or not.
1212
# @param interfacetype Internally used identifier for the host interface.
1313
# @param interfacedetails Hash with interface details for SNMP when interface type is 2.
14+
# @param tls_connect How the server must connect to the agent
15+
# @param tls_accept How the agent can connect to the server
16+
# @param tls_issuer Issuer of the certificate that is allowed to talk with the serve
17+
# @param tls_subject Subject of the certificate that is allowed to talk with the server
1418
class zabbix::resources::agent (
1519
$hostname = undef,
1620
$ipaddress = undef,
@@ -24,6 +28,10 @@
2428
$proxy = undef,
2529
$interfacetype = 1,
2630
Variant[Array, Hash] $interfacedetails = [],
31+
Optional[Enum['unencrypted','psk','cert']] $tls_connect = undef,
32+
Optional[Enum['unencrypted','psk','cert']] $tls_accept = undef,
33+
Optional[String[1]] $tls_issuer = undef,
34+
Optional[String[1]] $tls_subject = undef,
2735
) {
2836
if $group and $groups {
2937
fail("Got group and groups. This isn't support! Please use groups only.")
@@ -47,5 +55,9 @@
4755
proxy => $proxy,
4856
interfacetype => $interfacetype,
4957
interfacedetails => $interfacedetails,
58+
tls_connect => $tls_connect,
59+
tls_accept => $tls_accept,
60+
tls_issuer => $tls_issuer,
61+
tls_subject => $tls_subject,
5062
}
5163
}

spec/acceptance/zabbix_host_spec.rb

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,36 @@ class { 'zabbix':
132132
end
133133
end
134134

135+
it_behaves_like 'an idempotent resource' do
136+
let(:manifest) do
137+
<<-EOS
138+
zabbix_host { 'test5.example.com':
139+
ipaddress => '127.0.0.5',
140+
use_ip => false,
141+
port => 1051,
142+
groups => ['Virtual machines'],
143+
templates => #{template},
144+
macros => [],
145+
tls_accept => 'cert',
146+
tls_connect => 'cert',
147+
tls_issuer => 'Zabbix.com',
148+
tls_subject => 'MyClientCertificate',
149+
}
150+
EOS
151+
end
152+
end
153+
135154
let(:result_hosts) do
136-
zabbixapi('localhost', 'Admin', 'zabbix', 'host.get', selectParentTemplates: ['host'], selectInterfaces: %w[dns ip main port type useip details], selectGroups: ['name'], output: ['host', '']).result
155+
zabbixapi(
156+
'localhost',
157+
'Admin',
158+
'zabbix',
159+
'host.get',
160+
selectParentTemplates: ['host'],
161+
selectInterfaces: %w[dns ip main port type useip details],
162+
selectGroups: ['name'],
163+
output: ['host', 'tls_accept', 'tls_connect', 'tls_issuer', 'tls_subject', '']
164+
).result
137165
end
138166

139167
context 'test1.example.com' do
@@ -308,6 +336,30 @@ class { 'zabbix':
308336
end
309337

310338
end
339+
340+
context 'test5.example.com' do
341+
let(:test5) { result_hosts.select { |h| h['host'] == 'test5.example.com' }.first }
342+
343+
it 'is created' do
344+
expect(test5['host']).to eq('test5.example.com')
345+
end
346+
347+
it 'has a correct tls_accept configured' do
348+
expect(test5['tls_accept']).to eq('4')
349+
end
350+
351+
it 'has a correct tls_connect configured' do
352+
expect(test5['tls_connect']).to eq('4')
353+
end
354+
355+
it 'has a correct tls_issuer configured' do
356+
expect(test5['tls_issuer']).to eq('Zabbix.com')
357+
end
358+
359+
it 'has a correct tls_subject configured' do
360+
expect(test5['tls_subject']).to eq('MyClientCertificate')
361+
end
362+
end
311363
end
312364
end
313365
end

0 commit comments

Comments
 (0)