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

Commit

Permalink
Adding in external hostname to allow for events to come from other named
Browse files Browse the repository at this point in the history
services.

This introduces an 'Advanced' options section for adding and editing registries,
in which an external hostname can be defined.

Based on code written by Josh Mahowald <mahowald@gmail.com>
#888
  • Loading branch information
NickHu committed Aug 26, 2016
1 parent f14542e commit 0d58ed1
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 30 deletions.
7 changes: 4 additions & 3 deletions app/controllers/admin/registries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def create
unless msg.empty?
logger.info "\nRegistry not reachable:\n#{@registry.inspect}\n#{msg}\n"
msg = "#{msg} You can skip this check by clicking on the \"Skip remote checks\" checkbox."
hsh = { name: @registry.name, hostname: @registry.hostname, use_ssl: @registry.use_ssl }
hsh = { name: @registry.name, hostname: @registry.hostname,
use_ssl: @registry.use_ssl, external_hostname: @registry.external_hostname }
redirect_to new_admin_registry_path(hsh), alert: msg
return
end
Expand Down Expand Up @@ -73,13 +74,13 @@ def registry_created

# The required/permitted parameters on the create method.
def create_params
params.require(:registry).permit(:name, :hostname, :use_ssl)
params.require(:registry).permit(:name, :hostname, :use_ssl, :external_hostname)
end

# The required/permitted parameters on update. The hostname parameter will be
# allowed depending whether there are repositories already created or not.
def update_params
permitted = [:name, :use_ssl, (:hostname unless Repository.any?)].compact
permitted = [:name, :use_ssl, (:hostname unless Repository.any?), :external_hostname].compact
params.require(:registry).permit(permitted)
end
end
24 changes: 15 additions & 9 deletions app/models/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#
# Table name: registries
#
# id :integer not null, primary key
# name :string(255) not null
# hostname :string(255) not null
# created_at :datetime not null
# updated_at :datetime not null
# use_ssl :boolean
# id :integer not null, primary key
# name :string(255) not null
# hostname :string(255) not null
# created_at :datetime not null
# updated_at :datetime not null
# use_ssl :boolean
# external_hostname :string(255)
#
# Indexes
#
Expand All @@ -25,6 +26,7 @@ class Registry < ActiveRecord::Base

validates :name, presence: true, uniqueness: true
validates :hostname, presence: true, uniqueness: true
validates :external_hostname, presence: false
validates :use_ssl, inclusion: [true, false]

# On create, make sure that all the needed namespaces are in place.
Expand All @@ -51,10 +53,14 @@ def client

# Find the registry for the given push event.
def self.find_from_event(event)
registry = Registry.find_by(hostname: event["request"]["host"])
request_hostname = event["request"]["host"]
registry = Registry.find_by(hostname: request_hostname)
if registry.nil?
logger.info("Ignoring event coming from unknown registry
#{event["request"]["host"]}")
logger.debug("No hostname matching #{request_hostname}, testing external_hostname")
registry = Registry.find_by(external_hostname: request_hostname)
end
if registry.nil?
logger.info("Ignoring event coming from unknown registry #{request_hostname}")
end
registry
end
Expand Down
28 changes: 27 additions & 1 deletion app/views/admin/registries/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@
= f.label :use_ssl, "Use SSL", {class: 'control-label col-md-2', title: 'Set this to enable SSL in the communication between Portus and the Registry', checked: @registry.use_ssl?}
.col-md-7
= f.check_box(:use_ssl)
div.collapse#advanced
.form-group
= f.label :external_hostname, "External Registry Name", {class:'control-label col-md-2', title: 'Set this if the name that clients use to communicate with the registry is different than the name that Portus uses to connect'}
.col-md-7
- if params[:external_hostname]
= f.text_field(:external_hostname, class: 'form-control', value: params[:external_hostname], placeholder: '(Optional)', required: false)
- else
= f.text_field(:external_hostname, class: 'form-control', placeholder: '(Optional)', required: false)
span.help-block
| Portus uses the hostname field to communicate with the registry,
but this may be on an internal network and inaccessible to the
client.
Clients may connect to the registry by a name different to the
hostname above, for example if it is behind a reverse proxy.
This field must be set to prevent Portus from ignoring registry
events originating from this external hostname.
.form-group
.col-md-offset-2.col-md-7
= f.submit('Update', class: 'btn btn-primary')
div.btn-toolbar role='toolbar'
div.btn-group role='group'
= f.submit('Update', class: 'btn btn-primary')
div.btn-group role='group'
span data-toggle='collapse' data-target='#advanced'
button.btn.btn-primary#advancedButton type='button' data-toggle='button' aria-expanded='false' aria-controls='advanced' Show Advanced
javascript:
$('#advancedButton').click(function() {
$(this).text($(this).text() == 'Show Advanced' ? 'Hide Advanced' : 'Show Advanced');
});
27 changes: 26 additions & 1 deletion app/views/admin/registries/new.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,37 @@
= f.check_box(:use_ssl, checked: true)
- else
= f.check_box(:use_ssl)
div.collapse#advanced
.form-group
= f.label :external_hostname, "External Registry Name", {class:'control-label col-md-2', title: 'Set this if the name that clients use to communicate with the registry is different than the name that Portus uses to connect'}
.col-md-7
- if params[:external_hostname]
= f.text_field(:external_hostname, class: 'form-control', value: params[:external_hostname], placeholder: '(Optional)', required: false)
- else
= f.text_field(:external_hostname, class: 'form-control', placeholder: '(Optional)', required: false)
span.help-block
| Portus uses the hostname field to communicate with the registry,
but this may be on an internal network and inaccessible to the
client.
Clients may connect to the registry by a name different to the
hostname above, for example if it is behind a reverse proxy.
This field must be set to prevent Portus from ignoring registry
events originating from this external hostname.
- unless flash[:alert].nil? || flash[:alert].empty?
.form-group.has-error
= label_tag :force, "Skip remote checks", {class: 'control-label col-md-2', title: "Force the creation of the registry, even if it's not reachable."}
.col-md-7
= check_box_tag(:force)
.form-group
.col-md-offset-2.col-md-7
= f.submit('Create', class: 'btn btn-primary')
div.btn-toolbar role='toolbar'
div.btn-group role='group'
= f.submit('Create', class: 'btn btn-primary')
div.btn-group role='group'
span data-toggle='collapse' data-target='#advanced'
button.btn.btn-primary#advancedButton type='button' data-toggle='button' aria-expanded='false' aria-controls='advanced' Show Advanced
javascript:
$('#advancedButton').click(function() {
$(this).text($(this).text() == 'Show Advanced' ? 'Hide Advanced' : 'Show Advanced');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddExternalHostnameToRegistries < ActiveRecord::Migration
def change
add_column :registries, :external_hostname, :string
end
end
9 changes: 5 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@
add_index "namespaces", ["team_id"], name: "index_namespaces_on_team_id", using: :btree

create_table "registries", force: :cascade do |t|
t.string "name", limit: 255, null: false
t.string "hostname", limit: 255, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "name", limit: 255, null: false
t.string "hostname", limit: 255, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "use_ssl"
t.string "external_hostname", limit: 255
end

add_index "registries", ["hostname"], name: "index_registries_on_hostname", unique: true, using: :btree
Expand Down
13 changes: 7 additions & 6 deletions spec/factories/registries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#
# Table name: registries
#
# id :integer not null, primary key
# name :string(255) not null
# hostname :string(255) not null
# created_at :datetime not null
# updated_at :datetime not null
# use_ssl :boolean
# id :integer not null, primary key
# name :string(255) not null
# hostname :string(255) not null
# created_at :datetime not null
# updated_at :datetime not null
# use_ssl :boolean
# external_hostname :string(255)
#
# Indexes
#
Expand Down
13 changes: 7 additions & 6 deletions spec/models/registry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
#
# Table name: registries
#
# id :integer not null, primary key
# name :string(255) not null
# hostname :string(255) not null
# created_at :datetime not null
# updated_at :datetime not null
# use_ssl :boolean
# id :integer not null, primary key
# name :string(255) not null
# hostname :string(255) not null
# created_at :datetime not null
# updated_at :datetime not null
# use_ssl :boolean
# external_hostname :string(255)
#
# Indexes
#
Expand Down

0 comments on commit 0d58ed1

Please sign in to comment.