Skip to content

Commit

Permalink
Merge pull request #15 from NREL/wildcard-subdomains
Browse files Browse the repository at this point in the history
Wildcard subdomain support
  • Loading branch information
GUI committed May 19, 2015
2 parents e4e2b68 + aa00229 commit a6c534f
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 8 deletions.
9 changes: 8 additions & 1 deletion app/assets/javascripts/admin/models/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@ Admin.Api = Ember.Model.extend(Ember.Validations.Mixin, {
},
},
backendHost: {
presence: true,
presence: {
unless: function(object) {
return (object.get('frontendHost') && object.get('frontendHost')[0] === '*');
},
},
format: {
with: CommonValidations.host_format,
message: polyglot.t('errors.messages.invalid_host_format'),
if: function(object) {
return !!object.get('backendHost');
},
},
},
},
Expand Down
5 changes: 4 additions & 1 deletion app/models/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ class Api
}
validates :backend_host,
:presence => true,
:unless => proc { |record| record.frontend_host.start_with?("*") }
validates :backend_host,
:format => {
:with => CommonValidations::HOST_FORMAT,
:message => :invalid_host_format,
}
},
:if => proc { |record| record.backend_host.present? }
validates :balance_algorithm,
:inclusion => { :in => %w(round_robin least_conn ip_hash) }
validates_each :servers, :url_matches do |record, attr, value|
Expand Down
5 changes: 3 additions & 2 deletions lib/common_validations.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module CommonValidations
HOST_FORMAT = %r{^[a-zA-Z0-9:][a-zA-Z0-9\-\.:]*$}
FRONTEND_HOST_FORMAT = %r{(#{HOST_FORMAT.source}|^\*$)}
BASE_HOST_FORMAT = %r{[a-zA-Z0-9:][a-zA-Z0-9\-\.:]*}
HOST_FORMAT = %r{^#{BASE_HOST_FORMAT.source}$}
FRONTEND_HOST_FORMAT = %r{^(\*|\*?#{BASE_HOST_FORMAT.source}|\*\.#{BASE_HOST_FORMAT.source})$}
URL_PREFIX_FORMAT = %r{^/}
end
8 changes: 4 additions & 4 deletions spec/cassettes/elasticsearch_templates.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions spec/models/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,129 @@
api.errors_on(:backend_host).should include('must be in the format of "example.com"')
api.errors_on(:"servers[0].host").should include('must be in the format of "example.com"')
end

it "allows an empty string backend host when the frontend host is a wildcard" do
api = FactoryGirl.build(:api, {
:frontend_host => "*",
:backend_host => "",
:servers => [
FactoryGirl.attributes_for(:api_server, :host => "127.0.0.1"),
]
})

api.valid?.should eql(true)
end

it "allows an null backend host when the frontend host is a wildcard" do
api = FactoryGirl.build(:api, {
:frontend_host => "*",
:backend_host => nil,
:servers => [
FactoryGirl.attributes_for(:api_server, :host => "127.0.0.1"),
]
})

api.valid?.should eql(true)
end

it "allows an empty backend host when the frontend host contains a wildcard with dot" do
api = FactoryGirl.build(:api, {
:frontend_host => "*.example.com",
:backend_host => nil,
:servers => [
FactoryGirl.attributes_for(:api_server, :host => "127.0.0.1"),
]
})

api.valid?.should eql(true)
end

it "allows an empty backend host when the frontend host contains a wildcard without dot" do
api = FactoryGirl.build(:api, {
:frontend_host => "*example.com",
:backend_host => nil,
:servers => [
FactoryGirl.attributes_for(:api_server, :host => "127.0.0.1"),
]
})

api.valid?.should eql(true)
end

it "does not allow a frontend host starting with a dot" do
api = FactoryGirl.build(:api, {
:frontend_host => ".example.com",
:backend_host => "example.com",
:servers => [
FactoryGirl.attributes_for(:api_server, :host => "127.0.0.1"),
]
})

api.valid?.should eql(false)
api.errors.messages.keys.sort.should eql([
:frontend_host,
])
end

it "does not allow a frontend host equal to '.'" do
api = FactoryGirl.build(:api, {
:frontend_host => ".",
:backend_host => "example.com",
:servers => [
FactoryGirl.attributes_for(:api_server, :host => "127.0.0.1"),
]
})

api.valid?.should eql(false)
api.errors.messages.keys.sort.should eql([
:frontend_host,
])
end

it "does not allow a frontend host equal to '*.'" do
api = FactoryGirl.build(:api, {
:frontend_host => "*.",
:backend_host => "example.com",
:servers => [
FactoryGirl.attributes_for(:api_server, :host => "127.0.0.1"),
]
})

api.valid?.should eql(false)
api.errors.messages.keys.sort.should eql([
:frontend_host,
])
end

it "does not allow an empty backend host when the frontend host does not contain a wildcard" do
api = FactoryGirl.build(:api, {
:frontend_host => "example.com",
:backend_host => nil,
:servers => [
FactoryGirl.attributes_for(:api_server, :host => "127.0.0.1"),
]
})

api.valid?.should eql(false)
api.errors.messages.keys.sort.should eql([
:backend_host,
])
end

it "does not allow an empty backend host when the frontend host contains a wildcard in the middle" do
api = FactoryGirl.build(:api, {
:frontend_host => "exam*ple.com",
:backend_host => nil,
:servers => [
FactoryGirl.attributes_for(:api_server, :host => "127.0.0.1"),
]
})

api.valid?.should eql(false)
api.errors.messages.keys.sort.should eql([
:backend_host,
:frontend_host,
])
end
end
end

0 comments on commit a6c534f

Please sign in to comment.