GetDomainMappings(ILicenseConfig c, IIssueReceiver sink,
                          IReadOnlyCollection <string> knownDomains)    //c.configurationSectionIssue
        {
            var mappings = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

            foreach (var pair in c.GetDomainMappings())
            {
                var from = pair.Key;
                var to   = pair.Value;
                if (string.IsNullOrEmpty(from) || string.IsNullOrEmpty(to))
                {
                    sink.AcceptIssue(new Issue($"Both from= and to= attributes are required on maphost, found {from} and {to}",
                                               IssueSeverity.ConfigurationError));
                }
                else if (!from.EndsWith(".local") && from.IndexOf('.') > -1)
                {
                    sink.AcceptIssue(new Issue(
                                         $"You can only map non-public hostnames to arbitrary licenses. Skipping {from}",
                                         IssueSeverity.ConfigurationError));
                }
                else if (!knownDomains.Contains(to))
                {
                    sink.AcceptIssue(new Issue(
                                         $"You have mapped {from} to {to}. {to} is not one of the known domains: {string.Join(" ", knownDomains.OrderBy(s => s))}",
                                         IssueSeverity.ConfigurationError));
                }
                else
                {
                    mappings[from] = to;
                }
            }
            return(mappings);
        }
        public DomainLookup(ILicenseConfig c, IIssueReceiver sink, IEnumerable <ILicenseChain> licenseChains)
        {
            // What domains are mentioned in which licenses?
            var chainsByDomain = GetChainsByDomain(licenseChains);

            var knownDomains = chainsByDomain.Keys.ToList();

            // What custom mappings has the user set up?
            var customMappings = GetDomainMappings(c, sink, knownDomains);

            // Start with identity mappings and the mappings for the normalized domains.
            lookupTable = new ConcurrentDictionary <string, string>(
                customMappings.Concat(
                    knownDomains.Select(v => new KeyValuePair <string, string>(v, v)))
                , StringComparer.Ordinal);

            lookupTableSize = lookupTable.Count;

            // Set up a list for suffix searching
            suffixSearchList = knownDomains.Select(known =>
            {
                var d = known.TrimStart('.');
                d     = d.StartsWith("www.") ? d.Substring(4) : d;
                return(new KeyValuePair <string, string>("." + d, known));
            })
                               .ToList();
        }
 void Plugins_LicensingChange(object sender, ILicenseConfig c)
 {
     foreach (var licenseString in c.GetLicenses())
     {
         GetOrAdd(licenseString, c.LicenseScope);
     }
     Heartbeat();
 }
        string GetInfo(ILicenseConfig c, LicenseManagerSingleton mgr)
        {
            var result = new Computation(c, mgr.TrustedKeys, mgr, mgr,
                                         mgr.Clock, true);
            var sb = new StringBuilder();

            sb.AppendLine($"Plugins.LicenseError = {c.LicenseEnforcement}");
            sb.AppendLine($"Plugins.LicenseScope = {c.LicenseScope}");
            sb.AppendLine($"Computation.");
            sb.AppendLine($"LicensedForAll() => {result.LicensedForAll()}");
            sb.AppendLine($"LicensedForSomething() => {result.LicensedForSomething()}");
            sb.AppendLine($"LicensedForRequestUrl(null) => {result.LicensedForRequestUrl(null)}");
            sb.AppendLine($"LicensedForRequestUrl(new Uri(\"http://other.com\")) => {result.LicensedForRequestUrl(new Uri("http://other.com"))}");
            sb.AppendLine($"LicensedForRequestUrl(new Uri(\"http://acme.com\")) => {result.LicensedForRequestUrl(new Uri("http://acme.com"))}");
            sb.AppendLine($"GetBuildDate() => {result.GetBuildDate()}");
            sb.AppendLine($"ProvideDiagnostics() => {result.ProvideDiagnostics()}");

            return(sb.ToString());
        }
 void Pipeline_Heartbeat(object sender, ILicenseConfig c)
 {
     Heartbeat();
 }
 public void MonitorLicenses(ILicenseConfig c)
 {
     c.LicensingChange -= Plugins_LicensingChange;
     c.LicensingChange += Plugins_LicensingChange;
     Plugins_LicensingChange(c, c);
 }
 public void MonitorHeartbeat(ILicenseConfig c)
 {
     c.Heartbeat -= Pipeline_Heartbeat;
     c.Heartbeat += Pipeline_Heartbeat;
     Pipeline_Heartbeat(c, c);
 }
Exemple #8
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);
                }
            }
        }