Skip to content

Commit

Permalink
Merge pull request #4 from wshihadeh/parse_ldap_configs_with_erb
Browse files Browse the repository at this point in the history
Parse ldap configs with erb
  • Loading branch information
lecid authored Nov 25, 2019
2 parents 26c81bf + 3bd7063 commit 42040ff
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.sw?
.DS_Store
Gemfile.lock
coverage
rdoc
pkg
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rack-auth-ldap
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.6.4
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: ruby

branches:
only:
- master

rvm:
- 2.6.4

cache: bundler
bundler_args: "--jobs=3 --retry=3 --without production"

matrix:
fast_finish: true

script:
- bundle exec rspec
45 changes: 0 additions & 45 deletions Gemfile.lock

This file was deleted.

18 changes: 14 additions & 4 deletions lib/rack/auth/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module Rack
# the auth module from Rack Sources
module Auth


# class Config provide Yaml config mapping for Rack::Auth::Module
# the class map ldap configurations values
# @note this class is not provide to be used standalone
Expand All @@ -23,7 +22,7 @@ class Config
def initialize(options = { :file => './ldap.yml'})
@values = defaults
target = (ENV['RACK_ENV'])? ENV['RACK_ENV'] : 'test'
config_values = ::YAML.load_file(::File.expand_path(options[:file], Dir.pwd))[target]
config_values = load_yaml(::File.expand_path(options[:file], Dir.pwd))[target]
debug = ::File.open("/tmp/test.txt",'a+')
debug.puts ENV['RACK_ENV']
debug.close
Expand All @@ -38,6 +37,19 @@ def initialize(options = { :file => './ldap.yml'})
end

private

def load_yaml(file)
if ::File.exist?(file)
::YAML.load ::ERB.new(IO.read(file)).result
else
raise "Could not load ldap configuration. No such file - #{file}"
end
rescue ::Psych::SyntaxError => e
raise "YAML syntax error occurred while parsing #{file}. " \
"Please note that YAML must be consistently indented using spaces. Tabs are not allowed. " \
"Error: #{e.message}"
end

# private method with default configuration values for LDAP
# @return [Hash<Symbol>] the default values of LDAP configuration
def defaults
Expand All @@ -56,8 +68,6 @@ def defaults
:debug => false
}
end


end

# class Ldap, the main authentication component for Rack
Expand Down
2 changes: 1 addition & 1 deletion rack-auth-ldap.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ Gem::Specification.new do |s|
s.date = "2014-04-29"
s.rubyforge_project = 'nowarning'
s.description = %q{rack-auth-ldap : provide LDAP authentication for Rack middelware}
s.has_rdoc = true
s.add_development_dependency('rspec')
s.add_development_dependency('yard')
s.add_development_dependency('rdoc')
s.add_development_dependency('roodi')
s.add_development_dependency('code_statistics')
s.add_development_dependency('yard-rspec')
s.add_development_dependency('ladle')
s.add_dependency('net-ldap')
s.add_dependency('rack')
s.required_ruby_version = '>= 1.9.0'
Expand Down
4 changes: 2 additions & 2 deletions spec/config/ldap.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
production: &ldap_defaults
hostname: localhost
hostname: <%= ENV['HOSTNAME'] || 'localhost' %>
basedn: ou=users,dc=test
auth: false
port: 3897
port: <%= ENV['PORT'] || '3897' %>
username_ldap_attribut: uid


Expand Down
14 changes: 12 additions & 2 deletions spec/rack-auth-ldap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
describe Rack::Auth::Ldap do

before :all do
@ldap_server = Ladle::Server.new({
@ldap_server = Ladle::Server.new({
:quiet => true, :port => 3897,
:ldif => "./spec/config/users.ldif",
:domain => "dc=test",
:tmpdir => '/tmp'
:tmpdir => '/tmp'
}).start
end

Expand Down Expand Up @@ -55,6 +55,16 @@ def assert_basic_auth_challenge(response)
response.body.should be_empty
end

it 'should render ldap.yaml with erb and use env vars' do
allow(ENV).to receive(:[]).with('RACK_ENV')
allow(ENV).to receive(:[]).with('HOSTNAME').and_return('localhost.local')
allow(ENV).to receive(:[]).with('PORT').and_return('9090')

app = Rack::Auth::Ldap.new(unprotected_app,{:file => './spec/config/ldap.yml'})
expect(app.config.hostname).to eq('localhost.local')
expect(app.config.port).to eq(9090)
end

it 'should challenge correctly when no credentials are specified' do
request do |response|
assert_basic_auth_challenge response
Expand Down

0 comments on commit 42040ff

Please sign in to comment.