void Plugins_LicensingChange(object sender, ILicenseConfig c)
 {
     foreach (var licenseString in c.GetLicenses())
     {
         GetOrAdd(licenseString, c.LicenseScope);
     }
     Heartbeat();
 }
Esempio n. 2
0
        public Computation(ILicenseConfig c, IReadOnlyCollection <RSADecryptPublic> trustedKeys,
                           IIssueReceiver permanentIssueSink,
                           ILicenseManager mgr, ILicenseClock clock, bool enforcementEnabled) : base("Computation")
        {
            permanentIssues    = permanentIssueSink;
            EnforcementEnabled = enforcementEnabled;
            this.clock         = clock;
            LicenseConfig      = c;
            Scope        = c.LicenseScope;
            LicenseError = c.LicenseEnforcement;
            this.mgr     = mgr;
            if (mgr.FirstHeartbeat == null)
            {
                throw new ArgumentException("ILicenseManager.Heartbeat() must be called before Computation.new");
            }

            // What features are installed on this instance?
            // For a license to be OK, it must have one of each of this nested list;
            IEnumerable <IEnumerable <string> > pluginFeaturesUsed = c.GetFeaturesUsed();

            // Create or fetch all relevant license chains; ignore the empty/invalid ones, they're logged to the manager instance
            chains = c.GetLicenses()
                     .Select(str => mgr.GetOrAdd(str, c.LicenseScope))
                     .Where(x => x != null && x.Licenses().Any())
                     .Concat(c.LicenseScope.HasFlag(LicenseAccess.ProcessReadonly)
                          ? mgr.GetSharedLicenses()
                          : Enumerable.Empty <ILicenseChain>())
                     .Distinct()
                     .ToList();


            // Set up our domain map/normalize/search manager
            domainLookup = new DomainLookup(c, permanentIssueSink, chains);


            // Check for tampering via interfaces
            if (chains.Any(chain => chain.Licenses().Any(b => !b.Revalidate(trustedKeys))))
            {
                EverythingDenied = true;
                permanentIssueSink.AcceptIssue(new Issue(
                                                   "Licenses failed to revalidate; please contact [email protected]", IssueSeverity.Error));
            }

            // Look for grace periods
            var gracePeriods = chains.Where(IsPendingLicense).Select(GetGracePeriodFor).ToList();

            // Look for fetched and valid licenses
            var validLicenses = chains.Where(chain => !IsPendingLicense(chain))
                                .SelectMany(chain => chain.Licenses())
                                .Where(b => !b.Fields.IsRemotePlaceholder() && IsLicenseValid(b))
                                .ToList();

            // This computation expires when we cross an expires, issued date, or NetworkGracePeriod expiration
            ComputationExpires = chains.SelectMany(chain => chain.Licenses())
                                 .SelectMany(b => new[] { b.Fields.Expires, b.Fields.ImageflowExpires, b.Fields.Issued })
                                 .Concat(gracePeriods)
                                 .Where(date => date != null)
                                 .OrderBy(d => d)
                                 .FirstOrDefault(d => d > clock.GetUtcNow());

            AllDomainsLicensed = gracePeriods.Any(t => t != null) ||
                                 validLicenses
                                 .Any(license => !license.Fields.GetAllDomains().Any() && AreFeaturesLicensed(license, pluginFeaturesUsed, false));

            KnownDomainStatus = validLicenses.SelectMany(
                b => b.Fields.GetAllDomains()
                .SelectMany(domain => b.Fields.GetFeatures()
                            .Select(
                                feature => new
                                KeyValuePair <string, string>(
                                    domain, feature))))
                                .GroupBy(pair => pair.Key, pair => pair.Value,
                                         (k, v) => new KeyValuePair <string, IEnumerable <string> >(k, v))
                                .Select(pair => new KeyValuePair <string, bool>(pair.Key,
                                                                                pluginFeaturesUsed.All(
                                                                                    set => set.Intersect(pair.Value, StringComparer.OrdinalIgnoreCase)
                                                                                    .Any())))
                                .ToDictionary(pair => pair.Key, pair => pair.Value,
                                              StringComparer.Ordinal);

            if (UpgradeNeeded())
            {
                foreach (var b in validLicenses)
                {
                    AreFeaturesLicensed(b, pluginFeaturesUsed, true);
                }
            }
        }