Skip to content

Commit

Permalink
Improve logging of connection validator (#280)
Browse files Browse the repository at this point in the history
* Improve logging of connection validator
Fix path handling for relative paths in validator
Fixes #279

* PDK update
  • Loading branch information
treydock authored Mar 20, 2023
1 parent 7328c50 commit 18b7456
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ jobs:
run: |
set -x
sudo apt-get remove mysql-server --purge
sudo apt-get update
sudo apt-get install apparmor-profiles
sudo apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld
- uses: actions/checkout@v2
Expand Down
2 changes: 2 additions & 0 deletions .sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ spec/acceptance/nodesets/ubuntu-1804.yml:
spec/acceptance/nodesets/ubuntu-2004.yml:
packages:
- iproute2
spec/acceptance/nodesets/ubuntu-2204.yml:
delete: true
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def create
# If `#create` is called, that means that `#exists?` returned false, which
# means that the connection could not be established... so we need to
# cause a failure here.
raise Puppet::Error, "Unable to connect to keycloak server! (#{@validator.keycloak_server}:#{@validator.keycloak_port})"
raise Puppet::Error, "Unable to connect to keycloak server! (#{@validator.keycloak_server}:#{@validator.keycloak_port}#{@validator.path})"
end

# Returns the existing validator, if one exists otherwise creates a new object
Expand Down
14 changes: 9 additions & 5 deletions lib/puppet/util/keycloak_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

# Validator class, for testing that Keycloak is alive
class Puppet::Util::KeycloakValidator
attr_reader :keycloak_server, :keycloak_port, :use_ssl, :test_path
attr_reader :keycloak_server, :keycloak_port, :use_ssl, :test_path, :relative_path, :path

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

# Utility method; attempts to make an http/https connection to the keycloak server.
Expand All @@ -23,21 +28,20 @@ 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.
path = "#{@relative_path}#{@test_path.sub(%r{^/}, '')}"
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 = 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}): [#{response.code}] #{response.msg}"
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}): #{e.message}"
Puppet.notice "Unable to connect to keycloak server (http#{use_ssl ? 's' : ''}://#{keycloak_server}:#{keycloak_port}#{path}): #{e.message}"
false
end
end
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,5 @@
],
"pdk-version": "2.1.0",
"template-url": "https://github.com/treydock/pdk-templates.git#master",
"template-ref": "heads/master-0-g978d356"
"template-ref": "heads/master-0-g4965280"
}
5 changes: 3 additions & 2 deletions spec/acceptance/1_class_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ class { 'keycloak':
it 'runs successfully' do
pp = <<-PUPPET_PP
class { 'keycloak':
java_opts => '-Xmx512m -Xms64m',
configs => {
http_relative_path => '/auth',
java_opts => '-Xmx512m -Xms64m',
configs => {
'metrics-enabled' => true,
},
}
Expand Down

0 comments on commit 18b7456

Please sign in to comment.