From 04bb8fe9942f8558124f00e8cdfb50ef7059bf9e Mon Sep 17 00:00:00 2001
From: Douwe Maan <douwe@selenight.nl>
Date: Tue, 30 May 2017 16:40:31 -0500
Subject: [PATCH] Return nil when looking up config for unknown LDAP provider

---
 changelogs/unreleased/dm-oauth-config-for.yml |  4 ++
 lib/gitlab/o_auth/provider.rb                 |  6 ++-
 spec/lib/gitlab/o_auth/provider_spec.rb       | 42 +++++++++++++++++++
 3 files changed, 51 insertions(+), 1 deletion(-)
 create mode 100644 changelogs/unreleased/dm-oauth-config-for.yml
 create mode 100644 spec/lib/gitlab/o_auth/provider_spec.rb

diff --git a/changelogs/unreleased/dm-oauth-config-for.yml b/changelogs/unreleased/dm-oauth-config-for.yml
new file mode 100644
index 00000000000..8fbbd45bb57
--- /dev/null
+++ b/changelogs/unreleased/dm-oauth-config-for.yml
@@ -0,0 +1,4 @@
+---
+title: Return nil when looking up config for unknown LDAP provider
+merge_request:
+author:
diff --git a/lib/gitlab/o_auth/provider.rb b/lib/gitlab/o_auth/provider.rb
index 9ad7a38d505..ac9d66c836d 100644
--- a/lib/gitlab/o_auth/provider.rb
+++ b/lib/gitlab/o_auth/provider.rb
@@ -22,7 +22,11 @@ module Gitlab
       def self.config_for(name)
         name = name.to_s
         if ldap_provider?(name)
-          Gitlab::LDAP::Config.new(name).options
+          if Gitlab::LDAP::Config.valid_provider?(name)
+            Gitlab::LDAP::Config.new(name).options
+          else
+            nil
+          end
         else
           Gitlab.config.omniauth.providers.find { |provider| provider.name == name }
         end
diff --git a/spec/lib/gitlab/o_auth/provider_spec.rb b/spec/lib/gitlab/o_auth/provider_spec.rb
new file mode 100644
index 00000000000..1e2a1f8c039
--- /dev/null
+++ b/spec/lib/gitlab/o_auth/provider_spec.rb
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+describe Gitlab::OAuth::Provider, lib: true do
+  describe '#config_for' do
+    context 'for an LDAP provider' do
+      context 'when the provider exists' do
+        it 'returns the config' do
+          expect(described_class.config_for('ldapmain')).to be_a(Hash)
+        end
+      end
+
+      context 'when the provider does not exist' do
+        it 'returns nil' do
+          expect(described_class.config_for('ldapfoo')).to be_nil
+        end
+      end
+    end
+
+    context 'for an OmniAuth provider' do
+      before do
+        provider = OpenStruct.new(
+          name: 'google',
+          app_id: 'asd123',
+          app_secret: 'asd123'
+        )
+        allow(Gitlab.config.omniauth).to receive(:providers).and_return([provider])
+      end
+
+      context 'when the provider exists' do
+        it 'returns the config' do
+          expect(described_class.config_for('google')).to be_a(OpenStruct)
+        end
+      end
+
+      context 'when the provider does not exist' do
+        it 'returns nil' do
+          expect(described_class.config_for('foo')).to be_nil
+        end
+      end
+    end
+  end
+end
-- 
GitLab