Пример #1
0
        public async Task <IActionResult> RunAsync(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "TemporaryExposureKeys")] HttpRequest req)
        {
            // Check Valid Route
            IValidationServerService.ValidateResult validateResult = ValidationServerService.Validate(req);
            if (!validateResult.IsValid)
            {
                return(validateResult.ErrorActionResult);
            }

            if (!long.TryParse(req.Query?["since"], out var sinceEpochSeconds))
            {
                sinceEpochSeconds = new DateTimeOffset(DateTime.UtcNow.AddDays(-14)).ToUnixTimeSeconds();
            }

            var keysResponse = await TekExport.GetKeysAsync((ulong)sinceEpochSeconds);

            var result = new TemporaryExposureKeysResult();

            // TODO: Url util
            result.Keys = keysResponse
                          .Select(_ => new TemporaryExposureKeysResult.Key()
            {
                Url = $"{ExportKeyUrl}/{TekExportBlobStorageContainerPrefix}/{_.Region}/{_.BatchNum}.zip"
            });
            result.Timestamp = keysResponse
                               .OrderByDescending(_ => _.TimestampSecondsSinceEpoch)
                               .FirstOrDefault()?.TimestampSecondsSinceEpoch ?? sinceEpochSeconds;
            return(new OkObjectResult(result));
        }
        public async Task RunAsync()
        {
            try
            {
                Logger.LogInformation($"start {nameof(RunAsync)}");
                var batchTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
                var items          = await TekRepository.GetNextAsync();

                foreach (var kv in items.GroupBy(_ => new
                {
                    RollingStartUnixTimeSeconds = _.GetRollingStartUnixTimeSeconds(),
                    RollingPeriodSeconds = _.GetRollingPeriodSeconds()
                }))
                {
                    var batchNum = (int)await Sequence.GetNextAsync(SequenceName, 1);

                    batchTimestamp++;
                    foreach (var region in Regions)
                    {
                        // Security considerations: Random Order TemporaryExposureKey
                        var sorted = kv
                                     .OrderBy(_ => RandomNumberGenerator.GetInt32(int.MaxValue));
                        await CreateAsync((ulong)kv.Key.RollingStartUnixTimeSeconds,
                                          (ulong)(kv.Key.RollingStartUnixTimeSeconds + kv.Key.RollingPeriodSeconds),
                                          region,
                                          batchNum,
                                          batchTimestamp,
                                          sorted.ToArray());
                    }

                    foreach (var key in kv)
                    {
                        key.Exported = true;
                        await TekRepository.UpsertAsync(key);
                    }
                }

                // Write Export Files json
                var models = await TekExportRepository.GetKeysAsync(0);

                await BlobService.WriteFilesJsonAsync(models);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, $"Error on {nameof(TemporaryExposureKeyExportBatchService)}");
                throw;
            }
        }
Пример #3
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "TemporaryExposureKeys")] HttpRequest req)
        {
            if (!long.TryParse(req.Query?["since"], out var sinceEpochSeconds))
            {
                sinceEpochSeconds = new DateTimeOffset(DateTime.UtcNow.AddDays(-14)).ToUnixTimeSeconds();
            }

            var keysResponse = await TekExport.GetKeysAsync((ulong)sinceEpochSeconds);

            var result = new TemporaryExposureKeysResult();

            // TODO: Url util
            result.Keys = keysResponse.Select(_ => new TemporaryExposureKeysResult.Key()
            {
                Url = $"{ExportKeyUrl}/{TekExportBlobStorageContainerPrefix}/{_.BatchNum}.tekexport"
            });
            result.Timestamp = keysResponse.Max(_ => _.TimestampSecondsSinceEpoch);
            return(new OkObjectResult(result));
        }