Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit a6f6035

Browse files
committed
health: implemented check for LDAP
In order to implement this, I needed to move things a bit on the LDAP support. So much, that I ended up refactoring quite some pieces that needed it. Fixes #1810 Signed-off-by: Miquel Sabaté Solà <[email protected]>
1 parent 52e5c8d commit a6f6035

37 files changed

+704
-469
lines changed

app/controllers/auth/sessions_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Auth::SessionsController < Devise::SessionsController
99
# or LDAP support is enabled, work as usual. Otherwise, redirect always to
1010
# the signup page.
1111
def new
12-
signup_allowed = !Portus::LDAP.enabled? && APP_CONFIG.enabled?("signup")
12+
signup_allowed = APP_CONFIG.disabled?("ldap") && APP_CONFIG.enabled?("signup")
1313

1414
if User.not_portus.any? || !signup_allowed
1515
@errors_occurred = flash[:alert].present?
@@ -28,7 +28,7 @@ def new
2828
def create
2929
super
3030

31-
if ::Portus::LDAP.enabled? && session[:first_login]
31+
if APP_CONFIG.enabled?("ldap") && session[:first_login]
3232
session[:first_login] = nil
3333
session_flash(current_user, nil)
3434
else

app/controllers/concerns/check_ldap.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ module CheckLDAP
1414

1515
# Redirect to the login page if LDAP is enabled.
1616
def check_ldap
17-
redirect_to new_user_session_path if Portus::LDAP.enabled?
17+
redirect_to new_user_session_path if APP_CONFIG.enabled?("ldap")
1818
end
1919
end

app/helpers/application_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ def activity_time_tag(ct)
4949

5050
# Returns true of signup is enabled.
5151
def signup_enabled?
52-
!Portus::LDAP.enabled? && APP_CONFIG.enabled?("signup")
52+
APP_CONFIG.disabled?("ldap") && APP_CONFIG.enabled?("signup")
5353
end
5454

5555
# Returns true if the login form should show the "first user admin" alert.
5656
def show_first_user_alert?
57-
User.not_portus.none? && APP_CONFIG.enabled?("first_user_admin") && Portus::LDAP.enabled?
57+
User.not_portus.none? && APP_CONFIG.enabled?("first_user_admin") && APP_CONFIG.enabled?("ldap")
5858
end
5959

6060
# Returns pagination limit config

app/models/user.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class User < ActiveRecord::Base
5252
],
5353
authentication_keys: [:username]]
5454

55-
enabled_devise_modules.delete(:validatable) if Portus::LDAP.enabled?
55+
enabled_devise_modules.delete(:validatable) if APP_CONFIG.enabled?("ldap")
5656
devise(*enabled_devise_modules)
5757

5858
APPLICATION_TOKENS_MAX = 5
@@ -80,7 +80,7 @@ class User < ActiveRecord::Base
8080
# Special method used by Devise to require an email on signup. This is always
8181
# true except for LDAP.
8282
def email_required?
83-
!(Portus::LDAP.enabled? && email.blank?)
83+
!(APP_CONFIG.enabled?("ldap") && email.blank?)
8484
end
8585

8686
# It adds an error if the username clashes with either a namespace or a team.

app/views/devise/registrations/edit.html.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
any affiliations with any team will be lost.
8282
= submit_tag('Disable', class: 'btn btn-primary')
8383

84-
- unless ::Portus::LDAP.enabled?
84+
- if APP_CONFIG.disabled?("ldap")
8585
- if current_user.email?
8686
.col-sm-6
8787
.panel.panel-default

app/views/devise/sessions/new.html.slim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ section.row-0
55
= render 'shared/notifications'
66
= image_tag 'layout/portus-logo-login-page.png', class: 'login-picture'
77
= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
8-
- if Portus::LDAP.enabled? || APP_CONFIG.enabled?("oauth.local_login")
8+
- if APP_CONFIG.enabled?("ldap") || APP_CONFIG.enabled?("oauth.local_login")
99
= f.text_field :username, class: 'input form-control input-lg first', placeholder: 'Username', autofocus: true, required: true
1010
= f.password_field :password, class: 'input form-control input-lg last', placeholder: 'Password', autocomplete: 'off', required: true
1111
= f.button id: "login-btn", class: 'classbutton btn btn-primary btn-block btn-lg' do
12-
- if Portus::LDAP.enabled?
12+
- if APP_CONFIG.enabled?("ldap")
1313
i.fa.fa-check
1414
' LDAP Login
1515
- else
@@ -25,7 +25,7 @@ section.row-0
2525
= link_to "Explore", explore_index_path, id: "explore", class: 'btn btn-link', title: "Explore existing images from this registry"
2626
.col-sm-4.forgot-password
2727
= link_to "I forgot my password", new_user_password_path, class: 'btn btn-link'
28-
- elsif !Portus::LDAP.enabled?
28+
- elsif APP_CONFIG.disabled?("ldap")
2929
- if APP_CONFIG.enabled?("anonymous_browsing")
3030
.col-sm-6.explore
3131
= link_to "Explore", explore_index_path, id: "explore", class: 'btn btn-link', title: "Explore existing images from this registry"

bin/check_services.rb

Lines changed: 0 additions & 59 deletions
This file was deleted.

bin/health.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
require "optparse"
4+
require "ostruct"
5+
require "json"
6+
7+
require "portus/health"
8+
9+
# Returns true if the given string value contains a truthy value.
10+
def truthy?(val)
11+
v = val&.downcase
12+
v == "t" || v == "y" || v == "1"
13+
end
14+
15+
##
16+
# Parse options.
17+
18+
options = OpenStruct.new(quiet: false, components: [])
19+
20+
# It can come as flags.
21+
OptionParser.new do |opt|
22+
opt.on("-q", "--quiet") { options.quiet = true }
23+
opt.on("-c COMPONENT", "--component COMPONENT") { |o| options.components << o }
24+
end.parse!
25+
26+
# It can also come as environment variables.
27+
options.quiet = true if truthy?(ENV["PORTUS_HEALTH_QUIET"])
28+
(1..5).each do |n|
29+
v = ENV["PORTUS_HEALTH_COMPONENT_#{n}"]
30+
break if v.nil?
31+
options.components << v
32+
end
33+
34+
##
35+
# Actual call.
36+
37+
response, _success = ::Portus::Health.check
38+
hsh = if options.components.any?
39+
response.select { |k, _v| options.components.include?(k) }
40+
else
41+
response
42+
end
43+
44+
puts JSON.pretty_generate(hsh) unless options.quiet
45+
46+
success = hsh.all? { |_, v| v[:success] }
47+
exit success ? 0 : 1

bin/integration/integration.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ def cn
124124
yml["services"]["portus"]["environment"] << "PORTUS_LOG_LEVEL=debug"
125125
yml["services"]["background"]["environment"] << "PORTUS_LOG_LEVEL=debug"
126126

127+
# Variables for the health command.
128+
yml["services"]["portus"]["environment"] << "PORTUS_HEALTH_QUIET=1"
129+
yml["services"]["portus"]["environment"] << "PORTUS_HEALTH_COMPONENT_1=database"
130+
if ENV["PORTUS_INTEGRATION_PROFILE"] == "ldap"
131+
yml["services"]["portus"]["environment"] << "PORTUS_HEALTH_COMPONENT_2=ldap"
132+
end
133+
127134
# Add profiles.
128135
yml["services"]["portus"]["volumes"] << "./profiles:/srv/Portus/spec/integration/profiles:ro"
129136
yml["services"]["portus"]["volumes"] << "./helpers:/srv/Portus/spec/integration/helpers:ro"

bin/test-integration.sh

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,16 @@ start_containers() {
5050
LDAP=0
5151

5252
while [ $RETRY -ne 0 ]; do
53-
msg=$(SKIP_MIGRATION=1 docker exec $CNAME portusctl exec rails r /srv/Portus/bin/check_services.rb)
54-
case $(echo "$msg" | grep DB) in
55-
"DB_READY")
56-
DB=1
57-
;;
58-
*)
59-
echo "Database is not ready yet:"
60-
echo $msg
61-
;;
62-
esac
63-
64-
case $(echo "$msg" | grep LDAP) in
65-
"LDAP_DISABLED"|"LDAP_OK")
66-
LDAP=1
67-
;;
68-
*)
69-
echo "LDAP is not ready yet"
70-
;;
71-
esac
72-
73-
if (( "$DB" == "1" )) && (( "$LDAP" == "1" )); then
74-
echo "Let's go!"
53+
set +e
54+
docker exec $CNAME portusctl exec rails r /srv/Portus/bin/health.rb
55+
if [ $? -eq "0" ]; then
56+
set -e
57+
echo "We are all set, let's go!"
7558
break
59+
else
60+
echo "Waiting for services to be ready..."
7661
fi
62+
set -e
7763

7864
if [ "$COUNT" -ge "$TIMEOUT" ]; then
7965
echo "[integration] Timeout reached, exiting with error"

0 commit comments

Comments
 (0)