Skip to content

Commit 919619d

Browse files
DEFERME BertValantin
authored andcommitted
Zabbix authconfig provider
1 parent dad93e4 commit 919619d

File tree

2 files changed

+250
-0
lines changed

2 files changed

+250
-0
lines changed
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../zabbix'
4+
Puppet::Type.type(:zabbix_authcfg).provide(:ruby, parent: Puppet::Provider::Zabbix) do
5+
confine feature: :zabbixapi
6+
7+
def initialize(value = {})
8+
super(value)
9+
@property_flush = {}
10+
end
11+
12+
def get_auth_by_id(id)
13+
api_auth = zbx.query(
14+
method: 'authentication.get',
15+
params: {
16+
output: 'extend',
17+
},
18+
id: id
19+
)
20+
if api_auth.empty?
21+
nil
22+
else
23+
{
24+
authentication_type: api_auth['authentication_type'],
25+
ldap_configured: api_auth['ldap_configured'],
26+
ldap_host: api_auth['ldap_host'],
27+
ldap_port: api_auth['ldap_port'],
28+
ldap_base_dn: api_auth['ldap_base_dn'],
29+
ldap_search_attribute: api_auth['ldap_search_attribute'],
30+
ldap_bind_dn: api_auth['ldap_bind_dn'],
31+
ldap_case_sensitive: api_auth['ldap_case_sensitive'],
32+
ldap_bind_password: api_auth['ldap_bind_password'],
33+
}
34+
end
35+
end
36+
37+
def auth
38+
@auth ||= get_auth_by_id(resource[:id])
39+
end
40+
41+
attr_writer :auth
42+
43+
def authentication_type
44+
auth[:authentication_type]
45+
end
46+
47+
def authentication_type=(int)
48+
@property_flush[:authentication_type] = int
49+
end
50+
51+
def ldap_configured
52+
auth[:ldap_configured]
53+
end
54+
55+
def ldap_configured=(int)
56+
@property_flush[:ldap_configured] = int
57+
end
58+
59+
def ldap_host
60+
auth[:ldap_host]
61+
end
62+
63+
def ldap_host=(value)
64+
@property_flush[:ldap_host] = value
65+
end
66+
67+
def ldap_port
68+
auth[:ldap_port]
69+
end
70+
71+
def ldap_port=(value)
72+
@property_flush[:ldap_port] = value
73+
end
74+
75+
def ldap_base_dn
76+
auth[:ldap_base_dn]
77+
end
78+
79+
def ldap_base_dn=(value)
80+
@property_flush[:ldap_base_dn] = value
81+
end
82+
83+
def ldap_search_attribute
84+
auth[:ldap_search_attribute]
85+
end
86+
87+
def ldap_search_attribute=(value)
88+
@property_flush[:ldap_search_attribute] = value
89+
end
90+
91+
def ldap_bind_dn
92+
auth[:ldap_bind_dn]
93+
end
94+
95+
def ldap_bind_dn=(value)
96+
@property_flush[:ldap_bind_dn] = value
97+
end
98+
99+
def ldap_case_sensitive
100+
auth[:ldap_case_sensitive]
101+
end
102+
103+
def ldap_case_sensitive=(int)
104+
@property_flush[:ldap_case_sensitive] = int
105+
end
106+
107+
def ldap_bind_password
108+
auth[:ldap_bind_password]
109+
end
110+
111+
def ldap_bind_password=(value)
112+
@property_flush[:ldap_bind_password] = value
113+
end
114+
115+
def flush
116+
# ensure => absent is unsupported
117+
return if @property_flush[:ensure] == :absent
118+
119+
return if @property_flush.empty?
120+
121+
update_auth
122+
end
123+
124+
def update_auth
125+
zbx.query(
126+
method: 'authentication.update',
127+
id: @resource[:id],
128+
params: @property_flush
129+
)
130+
end
131+
132+
# Unsupported/not needed since authentication with id: 1 is created by installing zabbix
133+
def create
134+
nil
135+
end
136+
137+
def exists?
138+
auth
139+
end
140+
141+
# Unused/absent is unsupported
142+
def destroy
143+
nil
144+
end
145+
end

lib/puppet/type/zabbix_authcfg.rb

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# frozen_string_literal: true
2+
3+
require 'uri'
4+
5+
Puppet::Type.newtype(:zabbix_authcfg) do
6+
@doc = %q("Manage zabbix authentication configuration
7+
8+
zabbix_authcfg { '1':
9+
ensure => present,
10+
ldap_configured => true,
11+
ldap_host => 'ldap://host.name',
12+
ldap_port => '389',
13+
ldap_base_dn => 'dc=example,dc=com',
14+
ldap_search_attribute => 'uid',
15+
ldap_bind_dn => 'cn=Manager',
16+
ldap_bind_password => Sensitive('password'),
17+
ldap_case_sensitive => true,
18+
}")
19+
20+
ensurable do
21+
# We should not be able to delete the authentication settings
22+
newvalues(:present)
23+
defaultto :present
24+
end
25+
26+
newparam(:id, namevar: true) do
27+
# Zabbix 5, 6 only support updating the default authentication settings (id: 1)
28+
desc 'authentication settings id'
29+
newvalues(1)
30+
end
31+
32+
newproperty(:authentication_type) do
33+
desc 'authentication type'
34+
newvalues('internal', 'LDAP')
35+
defaultto 'internal'
36+
munge do |value|
37+
value == 'internal' ? 0 : 1
38+
end
39+
end
40+
41+
newproperty(:ldap_configured, boolean: true) do
42+
desc 'Enable LDAP authentication'
43+
newvalues(true, false)
44+
defaultto true
45+
munge do |value|
46+
value ? 1 : 0
47+
end
48+
end
49+
50+
newproperty(:ldap_host) do
51+
desc 'LDAP host'
52+
validate do |value|
53+
raise ArgumentError, "ldap_host must be a valid ldap uri, \"#{value}\" is not" unless value =~ URI::DEFAULT_PARSER.make_regexp
54+
end
55+
end
56+
57+
newproperty(:ldap_port) do
58+
desc 'LDAP port'
59+
validate do |value|
60+
raise ArgumentError, "ldap_port must be an Integer, not #{value}" unless value.is_a?(Integer)
61+
end
62+
end
63+
64+
newproperty(:ldap_base_dn) do
65+
desc 'LDAP base DN'
66+
validate do |value|
67+
raise ArgumentError, "ldap_base_dn must be a valid DN, not #{value}" unless %r{^((dc|ou|cn)=.+,*)+$}i.match?(value)
68+
end
69+
end
70+
71+
newproperty(:ldap_search_attribute) do
72+
desc 'LDAP search attribute'
73+
newvalues('uid', 'sAMAccountName')
74+
end
75+
76+
newproperty(:ldap_bind_dn) do
77+
desc 'LDAP bind DN'
78+
validate do |value|
79+
raise ArgumentError, "ldap_bind_dn must be a valid DN, not #{value}" unless %r{^((dc|ou|cn)=.+,*)+$}i.match?(value)
80+
end
81+
end
82+
83+
newproperty(:ldap_case_sensitive, boolean: true) do
84+
desc 'LDAP case sensitive login'
85+
newvalues(true, false)
86+
defaultto true
87+
munge do |value|
88+
value ? 1 : 0
89+
end
90+
end
91+
92+
newproperty(:ldap_bind_password) do
93+
desc 'LDAP bind password'
94+
validate do |value|
95+
raise ArgumentError, "ldap_bind_password must be an String, not #{value}" unless value.is_a?(String)
96+
end
97+
end
98+
99+
def set_sensitive_parameters(sensitive_parameters) # rubocop:disable Naming/AccessorMethodName
100+
parameter(:ldap_bind_password).sensitive = true if parameter(:ldap_bind_password)
101+
super(sensitive_parameters)
102+
end
103+
104+
autorequire(:file) { '/etc/zabbix/api.conf' }
105+
end

0 commit comments

Comments
 (0)