From c4cc66c391323eff08881d35d8ebf9ab6190fa5d Mon Sep 17 00:00:00 2001
From: Kyle Spearrin <kyle.spearrin@gmail.com>
Date: Thu, 17 Aug 2017 17:10:34 -0400
Subject: [PATCH] job logging

---
 .../Implementations/RsaLicensingService.cs    | 22 ++++++++++++-------
 src/Jobs/Program.cs                           |  2 ++
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/Core/Services/Implementations/RsaLicensingService.cs b/src/Core/Services/Implementations/RsaLicensingService.cs
index 0a4203f36c..f6ebe6efc0 100644
--- a/src/Core/Services/Implementations/RsaLicensingService.cs
+++ b/src/Core/Services/Implementations/RsaLicensingService.cs
@@ -3,6 +3,7 @@ using Bit.Core.Models.Table;
 using Bit.Core.Repositories;
 using Bit.Core.Utilities;
 using Microsoft.AspNetCore.Hosting;
+using Microsoft.Extensions.Logging;
 using Newtonsoft.Json;
 using System;
 using System.Collections.Generic;
@@ -20,18 +21,20 @@ namespace Bit.Core.Services
         private readonly GlobalSettings _globalSettings;
         private readonly IUserRepository _userRepository;
         private readonly IOrganizationRepository _organizationRepository;
+        private readonly ILogger<RsaLicensingService> _logger;
 
         private IDictionary<Guid, DateTime> _userCheckCache = new Dictionary<Guid, DateTime>();
-        private DateTime? _organizationCheckCache = null;
 
         public RsaLicensingService(
             IUserRepository userRepository,
             IOrganizationRepository organizationRepository,
             IHostingEnvironment environment,
+            ILogger<RsaLicensingService> logger,
             GlobalSettings globalSettings)
         {
             _userRepository = userRepository;
             _organizationRepository = organizationRepository;
+            _logger = logger;
 
             var certThumbprint = "‎207e64a231e8aa32aaf68a61037c075ebebd553f";
             _globalSettings = globalSettings;
@@ -56,19 +59,17 @@ namespace Bit.Core.Services
                 return;
             }
 
-            var now = DateTime.UtcNow;
-            if(_organizationCheckCache.HasValue && now - _organizationCheckCache.Value < TimeSpan.FromDays(1))
-            {
-                return;
-            }
-            _organizationCheckCache = now;
-
             var orgs = await _organizationRepository.GetManyAsync();
+            _logger.LogInformation("Validating licenses for {0} organizations.", orgs.Count);
+
             foreach(var org in orgs.Where(o => o.Enabled))
             {
                 var license = ReadOrganiztionLicense(org);
                 if(license == null || !license.VerifyData(org, _globalSettings) || !license.VerifySignature(_certificate))
                 {
+                    _logger.LogInformation("Organization {0}({1}) has an invalid license and is being disabled.",
+                        org.Id, org.Name);
+
                     org.Enabled = false;
                     org.ExpirationDate = license.Expires;
                     org.RevisionDate = DateTime.UtcNow;
@@ -108,10 +109,15 @@ namespace Bit.Core.Services
                 _userCheckCache.Add(user.Id, now);
             }
 
+            _logger.LogInformation("Validating premium license for user {0}({1}).", user.Id, user.Email);
+
             var license = ReadUserLicense(user);
             var licensedForPremium = license != null && license.VerifyData(user) && license.VerifySignature(_certificate);
             if(!licensedForPremium)
             {
+                _logger.LogInformation("User {0}({1}) has an invalid license and premium is being disabled.",
+                    user.Id, user.Email);
+
                 user.Premium = false;
                 user.PremiumExpirationDate = license.Expires;
                 user.RevisionDate = DateTime.UtcNow;
diff --git a/src/Jobs/Program.cs b/src/Jobs/Program.cs
index 2ec1173b19..23e42fc8c3 100644
--- a/src/Jobs/Program.cs
+++ b/src/Jobs/Program.cs
@@ -71,6 +71,8 @@ namespace Bit.Jobs
                 _logger.LogError(2, e, "Error performing job.");
                 throw e;
             }
+
+            _logger.LogInformation("Finished job '{0}'.", parameters["j"]);
         }
 
         private static IDictionary<string, string> ParseParameters(string[] args)