From 6d093d84b1406d2f4a9cb7a580c42c6bc1fbe4b1 Mon Sep 17 00:00:00 2001
From: Douwe Maan <douwe@gitlab.com>
Date: Tue, 5 May 2015 11:49:28 +0200
Subject: [PATCH] Show prettier error message when license file is invalid.

---
 Gemfile                                |  2 +-
 Gemfile.lock                           |  4 ++--
 app/helpers/license_helper.rb          | 11 ++++++-----
 app/models/license.rb                  | 14 ++++++++++----
 app/views/layouts/_broadcast.html.haml |  4 ++--
 config/initializers/license.rb         | 15 +++++++--------
 6 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/Gemfile b/Gemfile
index d0a434c8bb2..3460105ab5d 100644
--- a/Gemfile
+++ b/Gemfile
@@ -203,7 +203,7 @@ gem 'request_store'
 gem "virtus"
 gem 'addressable'
 
-gem "gitlab-license"
+gem "gitlab-license", "~> 0.0.2"
 
 group :development do
   gem 'brakeman', require: false
diff --git a/Gemfile.lock b/Gemfile.lock
index 3f3837e48da..423f4e19644 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -209,7 +209,7 @@ GEM
       diff-lcs (~> 1.1)
       mime-types (~> 1.15)
       posix-spawn (~> 0.3)
-    gitlab-license (0.0.1)
+    gitlab-license (0.0.2)
     gitlab-linguist (3.0.1)
       charlock_holmes (~> 0.6.6)
       escape_utils (~> 0.2.4)
@@ -707,7 +707,7 @@ DEPENDENCIES
   github-markup
   gitlab-flowdock-git-hook (~> 0.4.2)
   gitlab-grack (~> 2.0.2)
-  gitlab-license
+  gitlab-license (~> 0.0.2)
   gitlab-linguist (~> 3.0.1)
   gitlab_emoji (~> 0.1)
   gitlab_git (~> 7.1.10)
diff --git a/app/helpers/license_helper.rb b/app/helpers/license_helper.rb
index 00de8e72ab0..a9ed65e5d50 100644
--- a/app/helpers/license_helper.rb
+++ b/app/helpers/license_helper.rb
@@ -1,10 +1,11 @@
 module LicenseHelper
   def license_message(signed_in: signed_in?, is_admin: (current_user && current_user.is_admin?))
-    if License.current
-      yes_license_message(signed_in, is_admin)
-    else
-      no_license_message(signed_in, is_admin)
-    end
+    @license_message ||=
+      if License.current
+        yes_license_message(signed_in, is_admin)
+      else
+        no_license_message(signed_in, is_admin)
+      end
   end
 
   private
diff --git a/app/models/license.rb b/app/models/license.rb
index c6f4b64bf95..e36224cabb7 100644
--- a/app/models/license.rb
+++ b/app/models/license.rb
@@ -48,7 +48,12 @@ class License < ActiveRecord::Base
   def license
     return nil unless self.data
 
-    @license ||= Gitlab::License.import(self.data)
+    @license ||= 
+      begin
+        Gitlab::License.import(self.data)
+      rescue Gitlab::License::ImportError
+        nil
+      end
   end
 
   def license?
@@ -95,8 +100,7 @@ class License < ActiveRecord::Base
   def valid_license
     return if license?
 
-    # TODO: Clearer message
-    self.errors.add(:license, "is invalid.")
+    self.errors.add(:base, "The license file is invalid. Make sure it is exactly as you received it from GitLab B.V.")
   end
 
   def active_user_count
@@ -107,6 +111,8 @@ class License < ActiveRecord::Base
 
     return if active_user_count <= restricted_user_count
 
-    self.errors.add(:base, "This license allows #{restricted_user_count} active users. This GitLab installation currently has #{active_user_count}, i.e. #{active_user_count - restricted_user_count} too many.")
+    message = "This license allows #{restricted_user_count} active users. "
+    message << "This GitLab installation currently has #{active_user_count}, i.e. #{active_user_count - restricted_user_count} too many."
+    self.errors.add(:base, message)
   end
 end
diff --git a/app/views/layouts/_broadcast.html.haml b/app/views/layouts/_broadcast.html.haml
index 7683c15cd54..e01663e0592 100644
--- a/app/views/layouts/_broadcast.html.haml
+++ b/app/views/layouts/_broadcast.html.haml
@@ -3,7 +3,7 @@
     = icon('bullhorn')
     = broadcast_message.message
 
-- if (message = license_message) && message.present?
+- if license_message.present?
   .broadcast-message
     = icon('bullhorn')
-    = message
+    = license_message
diff --git a/config/initializers/license.rb b/config/initializers/license.rb
index eda165b62e5..4f4199ae56b 100644
--- a/config/initializers/license.rb
+++ b/config/initializers/license.rb
@@ -1,16 +1,15 @@
-public_key_file = File.read(Rails.root.join(".license_encryption_key.pub"))
-public_key = OpenSSL::PKey::RSA.new(public_key_file)
-Gitlab::License.encryption_key = public_key
-
-# TODO: Validate encryptionkey
+begin
+  public_key_file = File.read(Rails.root.join(".license_encryption_key.pub"))
+  public_key = OpenSSL::PKey::RSA.new(public_key_file)
+  Gitlab::License.encryption_key = public_key
+rescue
+  warn "WARNING: No valid license encryption key provided."
+end
 
 # Needed to run migration
 if ActiveRecord::Base.connected? && ActiveRecord::Base.connection.table_exists?('licenses')
   message = LicenseHelper.license_message(signed_in: true, is_admin: true)
   if message.present?
-    # TODO: Change
     warn "WARNING: #{message}"
   end
-
-  # TODO: Warn about too many users
 end
-- 
2.30.9