From 24555adbe42c0d8987fc0a8c1ad8a328574304a0 Mon Sep 17 00:00:00 2001
From: Gabriel Mazetto <brodock@gmail.com>
Date: Thu, 6 Jul 2017 08:28:00 +0200
Subject: [PATCH] Added NTP check to make sure machine clock is in sync

---
 Gemfile                                         |  3 +++
 Gemfile.lock                                    |  2 ++
 .../geo/clocks_synchronization_check.rb         | 17 +++++++++++++++++
 lib/tasks/gitlab/check.rake                     |  3 ++-
 4 files changed, 24 insertions(+), 1 deletion(-)
 create mode 100644 lib/system_check/geo/clocks_synchronization_check.rb

diff --git a/Gemfile b/Gemfile
index ffa4618f504d5..9150eacc59da7 100644
--- a/Gemfile
+++ b/Gemfile
@@ -394,6 +394,9 @@ gem 'health_check', '~> 2.6.0'
 gem 'vmstat', '~> 2.3.0'
 gem 'sys-filesystem', '~> 1.1.6'
 
+# NTP client
+gem 'net-ntp'
+
 # Gitaly GRPC client
 gem 'gitaly', '~> 0.9.0'
 
diff --git a/Gemfile.lock b/Gemfile.lock
index e2ba55d01836c..b28d4bf64c3d6 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -503,6 +503,7 @@ GEM
       mustermann (= 0.4.0)
     mysql2 (0.4.5)
     net-ldap (0.12.1)
+    net-ntp (2.1.3)
     net-ssh (3.0.1)
     netrc (0.11.0)
     nokogiri (1.6.8.1)
@@ -1053,6 +1054,7 @@ DEPENDENCIES
   mousetrap-rails (~> 1.4.6)
   mysql2 (~> 0.4.5)
   net-ldap
+  net-ntp
   net-ssh (~> 3.0.1)
   nokogiri (~> 1.6.7, >= 1.6.7.2)
   oauth2 (~> 1.4)
diff --git a/lib/system_check/geo/clocks_synchronization_check.rb b/lib/system_check/geo/clocks_synchronization_check.rb
new file mode 100644
index 0000000000000..2f2ebf729446a
--- /dev/null
+++ b/lib/system_check/geo/clocks_synchronization_check.rb
@@ -0,0 +1,17 @@
+module SystemCheck
+  module Geo
+    class ClocksSynchronizationCheck < SystemCheck::BaseCheck
+      set_name 'Machine clock is synchronized'
+
+      def check?
+        Net::NTP.get.offset.abs < Gitlab::Geo::JwtRequestDecoder::IAT_LEEWAY
+      end
+
+      def show_error
+        try_fixing_it(
+          'Enable a NTP service on this machine to keep clocks synchronized'
+        )
+      end
+    end
+  end
+end
diff --git a/lib/tasks/gitlab/check.rake b/lib/tasks/gitlab/check.rake
index 95b1f0a1c72fb..1708bc7a647f2 100644
--- a/lib/tasks/gitlab/check.rake
+++ b/lib/tasks/gitlab/check.rake
@@ -551,7 +551,8 @@ namespace :gitlab do
         SystemCheck::Geo::EnabledCheck,
         SystemCheck::Geo::GeoDatabaseConfiguredCheck,
         SystemCheck::Geo::DatabaseReplicationCheck,
-        SystemCheck::Geo::HttpConnectionCheck
+        SystemCheck::Geo::HttpConnectionCheck,
+        SystemCheck::Geo::ClocksSynchronizationCheck
       ]
 
       SystemCheck.run('Geo', checks)
-- 
GitLab