Class: Puppet::Util::KeycloakValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/util/keycloak_validator.rb

Overview

Validator class, for testing that Keycloak is alive

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keycloak_server, keycloak_port, use_ssl = false, test_path = '/realms/master/.well-known/openid-configuration', relative_path = '/') ⇒ KeycloakValidator

Returns a new instance of KeycloakValidator.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/puppet/util/keycloak_validator.rb', line 9

def initialize(keycloak_server, keycloak_port, use_ssl = false, test_path = '/realms/master/.well-known/openid-configuration', relative_path = '/')
  @keycloak_server = keycloak_server
  @keycloak_port   = keycloak_port
  @use_ssl         = use_ssl
  @test_path       = test_path
  @relative_path   = relative_path
  @path = if @relative_path == '/'
            @test_path
          else
            "#{@relative_path}#{@test_path}"
          end
end

Instance Attribute Details

#keycloak_portObject (readonly)

Returns the value of attribute keycloak_port.



7
8
9
# File 'lib/puppet/util/keycloak_validator.rb', line 7

def keycloak_port
  @keycloak_port
end

#keycloak_serverObject (readonly)

Returns the value of attribute keycloak_server.



7
8
9
# File 'lib/puppet/util/keycloak_validator.rb', line 7

def keycloak_server
  @keycloak_server
end

#pathObject (readonly)

Returns the value of attribute path.



7
8
9
# File 'lib/puppet/util/keycloak_validator.rb', line 7

def path
  @path
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



7
8
9
# File 'lib/puppet/util/keycloak_validator.rb', line 7

def relative_path
  @relative_path
end

#test_pathObject (readonly)

Returns the value of attribute test_path.



7
8
9
# File 'lib/puppet/util/keycloak_validator.rb', line 7

def test_path
  @test_path
end

#use_sslObject (readonly)

Returns the value of attribute use_ssl.



7
8
9
# File 'lib/puppet/util/keycloak_validator.rb', line 7

def use_ssl
  @use_ssl
end

Instance Method Details

#attempt_connectionObject

Utility method; attempts to make an http/https connection to the keycloak server. This is abstracted out into a method so that it can be called multiple times for retry attempts.

Returns:

  • true if the connection is successful, false otherwise.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/puppet/util/keycloak_validator.rb', line 27

def attempt_connection
  # All that we care about is that we are able to connect successfully via
  # http(s), so here we're simpling hitting a somewhat arbitrary low-impact URL
  # on the keycloak server.
  http = Net::HTTP.new(@keycloak_server, @keycloak_port)
  http.use_ssl = @use_ssl
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  request = Net::HTTP::Get.new(@path)
  request.add_field('Accept', 'application/json')
  response = http.request(request)

  unless response.is_a?(Net::HTTPSuccess) || response.is_a?(Net::HTTPUnauthorized)
    Puppet.notice "Unable to connect to keycloak server (http#{use_ssl ? 's' : ''}://#{keycloak_server}:#{keycloak_port}#{path}): [#{response.code}] #{response.msg}"
    return false
  end
  true
rescue Exception => e # rubocop:disable Lint/RescueException
  Puppet.notice "Unable to connect to keycloak server (http#{use_ssl ? 's' : ''}://#{keycloak_server}:#{keycloak_port}#{path}): #{e.message}"
  false
end