Commit 2c2bb5dd authored by Stan Hu's avatar Stan Hu

Merge branch '723-lazy-load-krb5_auth-gem' into 'master'

Lazy-load the krb5_auth gem

Closes #723.

See merge request !520
parents e3de18ab b84baef6
require "krb5_auth"
# This calls helps to authenticate to Kerberos by providing username and password # This calls helps to authenticate to Kerberos by providing username and password
module Gitlab module Gitlab
module Kerberos module Kerberos
class Authentication class Authentication
def self.kerberos_default_realm def self.kerberos_default_realm
krb5 = ::Krb5Auth::Krb5.new krb5 = krb5_class.new
default_realm = krb5.get_default_realm default_realm = krb5.get_default_realm
krb5.close # release memory allocated by the krb5 library krb5.close # release memory allocated by the krb5 library
default_realm default_realm
...@@ -19,15 +17,22 @@ module Gitlab ...@@ -19,15 +17,22 @@ module Gitlab
auth.login auth.login
end end
def self.krb5_class
@krb5_class ||= begin
require "krb5_auth"
Krb5Auth::Krb5
end
end
def initialize(login, password) def initialize(login, password)
@login = login @login = login
@password = password @password = password
@krb5 = ::Krb5Auth::Krb5.new @krb5 = self.class.krb5_class.new
end end
def valid? def valid?
@krb5.get_init_creds_password(@login, @password) @krb5.get_init_creds_password(@login, @password)
rescue ::Krb5Auth::Krb5::Exception rescue self.class.krb5_class::Exception
false false
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Kerberos::Authentication do describe Gitlab::Kerberos::Authentication do
let(:klass) { Gitlab::Kerberos::Authentication }
let(:user) { create(:omniauth_user, provider: :kerberos, extern_uid: 'gitlab@FOO.COM') } let(:user) { create(:omniauth_user, provider: :kerberos, extern_uid: 'gitlab@FOO.COM') }
let(:login) { 'john' } let(:login) { 'john' }
let(:password) { 'password' } let(:password) { 'password' }
describe :kerberos_default_realm do before do
described_class.krb5_class # eager load the krb5_auth gem
end
describe '.kerberos_default_realm' do
it "returns the default realm exposed by the Kerberos library" do it "returns the default realm exposed by the Kerberos library" do
allow_any_instance_of(::Krb5Auth::Krb5).to receive_messages(get_default_realm: "FOO.COM") allow_any_instance_of(::Krb5Auth::Krb5).to receive_messages(get_default_realm: "FOO.COM")
expect(klass.kerberos_default_realm).to eq("FOO.COM") expect(described_class.kerberos_default_realm).to eq("FOO.COM")
end end
end end
describe :login do describe '.login' do
before do before do
allow(Devise).to receive_messages(omniauth_providers: [:kerberos]) allow(Devise).to receive_messages(omniauth_providers: [:kerberos])
user # make sure user is instanciated user # make sure user is instanciated
...@@ -23,20 +26,20 @@ describe Gitlab::Kerberos::Authentication do ...@@ -23,20 +26,20 @@ describe Gitlab::Kerberos::Authentication do
it "finds the user if authentication is successful (login without kerberos realm)" do it "finds the user if authentication is successful (login without kerberos realm)" do
allow_any_instance_of(::Krb5Auth::Krb5).to receive_messages(get_init_creds_password: true, get_default_principal: 'gitlab@FOO.COM') allow_any_instance_of(::Krb5Auth::Krb5).to receive_messages(get_init_creds_password: true, get_default_principal: 'gitlab@FOO.COM')
expect(klass.login('gitlab', password)).to be_truthy expect(described_class.login('gitlab', password)).to be_truthy
end end
it "finds the user if authentication is successful (login with a kerberos realm)" do it "finds the user if authentication is successful (login with a kerberos realm)" do
allow_any_instance_of(::Krb5Auth::Krb5).to receive_messages(get_init_creds_password: true, get_default_principal: 'gitlab@FOO.COM') allow_any_instance_of(::Krb5Auth::Krb5).to receive_messages(get_init_creds_password: true, get_default_principal: 'gitlab@FOO.COM')
expect(klass.login('gitlab@FOO.COM', password)).to be_truthy expect(described_class.login('gitlab@FOO.COM', password)).to be_truthy
end end
it "returns false if there is no such user in kerberos" do it "returns false if there is no such user in kerberos" do
kerberos_login = "some-login" kerberos_login = "some-login"
allow_any_instance_of(::Krb5Auth::Krb5).to receive_messages(get_init_creds_password: true, get_default_principal: 'some-login@FOO.COM') allow_any_instance_of(::Krb5Auth::Krb5).to receive_messages(get_init_creds_password: true, get_default_principal: 'some-login@FOO.COM')
expect(klass.login(kerberos_login, password)).to be_falsy expect(described_class.login(kerberos_login, password)).to be_falsy
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment