Example #1
0
        protected virtual async Task LoadTocEntryStatement(
            IMetadataRepository repository,
            MetadataBLOBPayload blob,
            MetadataBLOBPayloadEntry entry,
            DateTime?cacheUntil = null)
        {
            if (entry.AaGuid != null && !_entries.ContainsKey(Guid.Parse(entry.AaGuid)))
            {
                var entryAaGuid = Guid.Parse(entry.AaGuid);

                var cacheKey = GetEntryCacheKey(repository, entryAaGuid);

                var cachedEntry = await _cache.GetStringAsync(cacheKey);

                if (cachedEntry != null)
                {
                    var statement = JsonSerializer.Deserialize <MetadataStatement>(cachedEntry);
                    if (!string.IsNullOrWhiteSpace(statement.AaGuid))
                    {
                        var aaGuid = Guid.Parse(statement.AaGuid);
                        _metadataStatements.TryAdd(aaGuid, statement);
                        _entries.TryAdd(aaGuid, entry);
                    }
                }
                else
                {
                    _log?.LogInformation("Entry for {0} {1} not cached so loading from MDS...", entry.AaGuid, entry.MetadataStatement?.Description ?? entry.StatusReports?.FirstOrDefault().CertificationDescriptor ?? "(unknown)");

                    try
                    {
                        if (!string.IsNullOrWhiteSpace(entry.AaGuid))
                        {
                            var statementJson = JsonSerializer.Serialize(entry.MetadataStatement, new JsonSerializerOptions {
                                WriteIndented = true
                            });

                            _log?.LogDebug("{0}:{1}\n{2}", entry.AaGuid, entry.MetadataStatement.Description, statementJson);

                            var aaGuid = Guid.Parse(entry.AaGuid);

                            _metadataStatements.TryAdd(aaGuid, entry.MetadataStatement);
                            _entries.TryAdd(aaGuid, entry);

                            if (cacheUntil.HasValue)
                            {
                                await _cache.SetStringAsync(cacheKey, statementJson, new DistributedCacheEntryOptions
                                {
                                    AbsoluteExpiration = cacheUntil
                                });
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _log?.LogError(ex, "Error getting MetadataStatement from {0} for AAGUID '{1}' ", repository.GetType().Name, entry.AaGuid);
                        throw;
                    }
                }
            }
        }
Example #2
0
        public async Task <MetadataBLOBPayload> GetBLOBAsync(CancellationToken cancellationToken = default)
        {
            var req = new
            {
                endpoint = _origin
            };

            var content = new StringContent(JsonSerializer.Serialize(req), Encoding.UTF8, "application/json");

            using var response = await _httpClient.PostAsync(_getEndpointsUrl, content, cancellationToken);

            await using var responseStream = await response.Content.ReadAsStreamAsync(cancellationToken);

            var result = await JsonSerializer.DeserializeAsync <MDSGetEndpointResponse>(responseStream, cancellationToken : cancellationToken);

            var conformanceEndpoints = result !.Result;

            var combinedBlob = new MetadataBLOBPayload
            {
                Number     = -1,
                NextUpdate = "2099-08-07"
            };

            var entries = new List <MetadataBLOBPayloadEntry>();

            foreach (var blobUrl in conformanceEndpoints)
            {
                var rawBlob = await DownloadStringAsync(blobUrl, cancellationToken);

                MetadataBLOBPayload blob;

                try
                {
                    blob = await DeserializeAndValidateBlob(rawBlob, cancellationToken);
                }
                catch
                {
                    continue;
                }

                if (string.Compare(blob.NextUpdate, combinedBlob.NextUpdate, StringComparison.InvariantCulture) < 0)
                {
                    combinedBlob.NextUpdate = blob.NextUpdate;
                }
                if (combinedBlob.Number < blob.Number)
                {
                    combinedBlob.Number = blob.Number;
                }

                foreach (var entry in blob.Entries)
                {
                    entries.Add(entry);
                }
                combinedBlob.JwtAlg = blob.JwtAlg;
            }

            combinedBlob.Entries = entries.ToArray();
            return(combinedBlob);
        }
Example #3
0
        public async Task <MetadataStatement?> GetMetadataStatementAsync(MetadataBLOBPayload blob, MetadataBLOBPayloadEntry entry, CancellationToken cancellationToken = default)
        {
            if (_blob is null)
            {
                await GetBLOBAsync(cancellationToken);
            }

            if (!string.IsNullOrEmpty(entry.AaGuid) && Guid.TryParse(entry.AaGuid, out Guid parsedAaGuid))
            {
                if (_entries.ContainsKey(parsedAaGuid))
                {
                    return(_entries[parsedAaGuid].MetadataStatement);
                }
            }

            return(null);
        }
Example #4
0
        private DateTime?GetCacheUntilTime(MetadataBLOBPayload blob)
        {
            if (!string.IsNullOrWhiteSpace(blob?.NextUpdate) &&
                DateTime.TryParseExact(
                    blob.NextUpdate,
                    new[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "o" }, //Sould be ISO8601 date but allow for other ISO formats too
                    System.Globalization.CultureInfo.InvariantCulture,
                    System.Globalization.DateTimeStyles.AssumeUniversal | System.Globalization.DateTimeStyles.AdjustToUniversal,
                    out var parsedDate))
            {
                //NextUpdate is in the past to default to a useful number that will result us cross the date theshold for the next update
                if (parsedDate < DateTime.UtcNow.AddMinutes(5))
                {
                    return(DateTime.UtcNow.Add(_defaultCacheInterval));
                }

                return(parsedDate);
            }

            return(null);
        }
Example #5
0
 public Task <MetadataStatement?> GetMetadataStatementAsync(MetadataBLOBPayload blob, MetadataBLOBPayloadEntry entry, CancellationToken cancellationToken = default)
 {
     return(Task.FromResult <MetadataStatement?>(entry.MetadataStatement));
 }
        protected virtual async Task LoadEntryStatementAsync(IMetadataRepository repository, MetadataBLOBPayload blob, MetadataBLOBPayloadEntry entry, CancellationToken cancellationToken)
        {
            if (entry.AaGuid != null)
            {
                var statement = await repository.GetMetadataStatementAsync(blob, entry, cancellationToken);

                if (!string.IsNullOrWhiteSpace(statement?.AaGuid))
                {
                    _metadataStatements.TryAdd(Guid.Parse(statement.AaGuid), statement);
                }
            }
        }