Class: Puppet::Provider::KeycloakAPI

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/keycloak_api.rb

Overview

Shared provider class

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.install_dirObject

Returns the value of attribute install_dir.



21
22
23
# File 'lib/puppet/provider/keycloak_api.rb', line 21

def install_dir
  @install_dir
end

.passwordObject

Returns the value of attribute password.



21
22
23
# File 'lib/puppet/provider/keycloak_api.rb', line 21

def password
  @password
end

.realmObject

Returns the value of attribute realm.



21
22
23
# File 'lib/puppet/provider/keycloak_api.rb', line 21

def realm
  @realm
end

.serverObject

Returns the value of attribute server.



21
22
23
# File 'lib/puppet/provider/keycloak_api.rb', line 21

def server
  @server
end

.use_wrapperObject

Returns the value of attribute use_wrapper.



21
22
23
# File 'lib/puppet/provider/keycloak_api.rb', line 21

def use_wrapper
  @use_wrapper
end

.userObject

Returns the value of attribute user.



21
22
23
# File 'lib/puppet/provider/keycloak_api.rb', line 21

def user
  @user
end

Class Method Details

.camelize(value) ⇒ Object



32
33
34
35
# File 'lib/puppet/provider/keycloak_api.rb', line 32

def self.camelize(value)
  str = value.to_s.split('_').map(&:capitalize).join
  str[0].downcase + str[1..-1]
end

.escape(str) ⇒ Object



41
42
43
# File 'lib/puppet/provider/keycloak_api.rb', line 41

def self.escape(str)
  str.gsub(' ', '%20')
end

.kcadm(action, resource, realm = nil, file = nil, fields = nil, print_id = false, params = nil) ⇒ Object



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
# File 'lib/puppet/provider/keycloak_api.rb', line 56

def self.kcadm(action, resource, realm = nil, file = nil, fields = nil, print_id = false, params = nil)
  kcadm_wrapper = '/opt/keycloak/bin/kcadm-wrapper.sh'

  arguments = [action]

  # get-roles does not accept a resource as its parameter
  arguments << escape(resource) if resource

  if ['create', 'update'].include?(action) && !print_id
    arguments << '-o'
  end

  if realm
    arguments << '-r'
    arguments << escape(realm)
  end
  if file
    arguments << '-f'
    arguments << file
  end
  if fields
    arguments << '--fields'
    arguments << fields.join(',')
  end
  params&.each do |param, value|
    case value
    when String
      arguments << "--#{param}"
      arguments << value
    when Array
      value.each do |val|
        arguments << "--#{param}"
        arguments << val
      end
    end
  end
  if action == 'create' && print_id
    arguments << '--id'
  end
  if use_wrapper == false || use_wrapper == :false
    auth_arguments = [
      '--no-config',
      '--server', server,
      '--realm', escape(self.realm),
      '--user', user,
      '--password', password
    ]
    cmd = [File.join(install_dir, 'bin/kcadm.sh')] + arguments + auth_arguments
  else
    cmd = [kcadm_wrapper] + arguments
  end

  cmd.reject! { |c| c.empty? }

  execute(cmd, combine: false, failonfail: true)
end

.name_uuid(name) ⇒ Object



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
# File 'lib/puppet/provider/keycloak_api.rb', line 131

def self.name_uuid(name)
  # Code lovingly taken from
  # https://github.com/puppetlabs/marionette-collective/blob/master/lib/mcollective/ssl.rb

  # This is the UUID version 5 type DNS name space which is as follows:
  #
  #  6ba7b810-9dad-11d1-80b4-00c04fd430c8
  #
  uuid_name_space_dns = [0x6b,
                         0xa7,
                         0xb8,
                         0x10,
                         0x9d,
                         0xad,
                         0x11,
                         0xd1,
                         0x80,
                         0xb4,
                         0x00,
                         0xc0,
                         0x4f,
                         0xd4,
                         0x30,
                         0xc8].map { |b| b.chr }.join

  sha1 = Digest::SHA1.new
  sha1.update(uuid_name_space_dns)
  sha1.update(name)

  # first 16 bytes..
  bytes = sha1.digest[0, 16].bytes.to_a

  # version 5 adjustments
  bytes[6] &= 0x0f
  bytes[6] |= 0x50

  # variant is DCE 1.1
  bytes[8] &= 0x3f
  bytes[8] |= 0x80

  bytes = [4, 2, 2, 2, 6].map do |i|
    bytes.slice!(0, i).pack('C*').unpack('H*')
  end

  bytes.join('-')
end

.realmsObject



117
118
119
120
121
122
123
124
125
# File 'lib/puppet/provider/keycloak_api.rb', line 117

def self.realms
  output = kcadm('get', 'realms', nil, nil, ['realm'])
rescue Puppet::ExecutionFailure => e
  Puppet.notice("Failed to get realms: #{e}")
  []
else
  data = JSON.parse(output)
  data.map { |r| r['realm'] }
end

.type_propertiesObject



24
25
26
# File 'lib/puppet/provider/keycloak_api.rb', line 24

def self.type_properties
  resource_type.validproperties.reject { |p| [:ensure, :custom_properties].include? p.to_sym }
end

Instance Method Details

#camelize(*args) ⇒ Object



37
38
39
# File 'lib/puppet/provider/keycloak_api.rb', line 37

def camelize(*args)
  self.class.camelize(*args)
end

#check_theme_exists(theme, res) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'lib/puppet/provider/keycloak_api.rb', line 182

def check_theme_exists(theme, res)
  return true if theme == 'keycloak'
  return true if theme == 'keycloak.v2'

  install_dir = self.class.install_dir || '/opt/keycloak'
  path = File.join(install_dir, 'themes', theme)
  return if File.exist?(path)

  Puppet.warning("#{res}: Theme #{theme} not found at path #{path}.")
end

#convert_property_value(value) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/puppet/provider/keycloak_api.rb', line 45

def convert_property_value(value)
  case value
  when :true
    true
  when :false
    false
  else
    value
  end
end

#kcadm(*args) ⇒ Object



113
114
115
# File 'lib/puppet/provider/keycloak_api.rb', line 113

def kcadm(*args)
  self.class.kcadm(*args)
end

#name_uuid(*args) ⇒ Object



178
179
180
# File 'lib/puppet/provider/keycloak_api.rb', line 178

def name_uuid(*args)
  self.class.name_uuid(*args)
end

#realmsObject



127
128
129
# File 'lib/puppet/provider/keycloak_api.rb', line 127

def realms
  self.class.realms
end

#type_propertiesObject



28
29
30
# File 'lib/puppet/provider/keycloak_api.rb', line 28

def type_properties
  self.class.type_properties
end