Exemplo n.º 1
0
        public async Task <ActionResult <Api.TrackKey> > PutTrackKeyType([FromBody] Api.TrackKey trackKey)
        {
            try
            {
                if (!await ModelState.TryValidateObjectAsync(trackKey))
                {
                    return(BadRequest(ModelState));
                }

                var mTrackKey = mapper.Map <TrackKey>(trackKey);

                var mTrack = await tenantRepository.GetTrackByNameAsync(new Track.IdKey {
                    TenantName = RouteBinding.TenantName, TrackName = RouteBinding.TrackName
                });

                if (mTrack.Key.Type != mTrackKey.Type)
                {
                    switch (mTrackKey.Type)
                    {
                    case TrackKeyType.Contained:
                        mTrack.Key.Type = mTrackKey.Type;
                        var certificate = await RouteBinding.CreateSelfSignedCertificateBySubjectAsync();

                        mTrack.Key.Keys = new List <TrackKeyItem> {
                            new TrackKeyItem {
                                Key = await certificate.ToFTJsonWebKeyAsync(true)
                            }
                        };
                        if (!mTrack.Key.ExternalName.IsNullOrWhiteSpace())
                        {
                            await externalKeyLogic.DeleteExternalKeyAsync(mTrack.Key.ExternalName);

                            mTrack.Key.ExternalName = null;
                        }
                        break;

                    case TrackKeyType.KeyVaultRenewSelfSigned:
                        mTrack.Key.Type         = mTrackKey.Type;
                        mTrack.Key.Keys         = null;
                        mTrack.Key.ExternalName = await externalKeyLogic.CreateExternalKeyAsync(mTrack);

                        break;

                    case TrackKeyType.KeyVaultUpload:
                    default:
                        throw new Exception($"Track key type not supported '{mTrackKey.Type}'.");
                    }

                    await tenantRepository.UpdateAsync(mTrack);
                }

                return(Ok(mapper.Map <Api.TrackKey>(mTrack.Key)));
            }
            catch (CosmosDataException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    logger.Warning(ex, $"NotFound, Update '{typeof(Api.TrackKey).Name}' type by track name '{RouteBinding.TrackName}'.");
                    return(NotFound(typeof(Api.TrackKey).Name, RouteBinding.TrackName));
                }
                throw;
            }
        }
        public async Task <ActionResult <Api.TrackKeyItemsContained> > PutTrackKeyContained([FromBody] Api.TrackKeyItemContainedRequest trackKeyRequest)
        {
            try
            {
                if (!await ModelState.TryValidateObjectAsync(trackKeyRequest))
                {
                    return(BadRequest(ModelState));
                }

                var mTrackKey = mapper.Map <TrackKeyItem>(trackKeyRequest);
                if (!trackKeyRequest.CreateSelfSigned)
                {
                    try
                    {
                        if (!mTrackKey.Key.HasPrivateKey())
                        {
                            throw new ValidationException("Private key is required.");
                        }
                    }
                    catch (ValidationException vex)
                    {
                        logger.Warning(vex);
                        ModelState.TryAddModelError(nameof(trackKeyRequest.Key), vex.Message);
                        return(BadRequest(ModelState));
                    }
                }

                var mTrack = await tenantRepository.GetTrackByNameAsync(new Track.IdKey {
                    TenantName = RouteBinding.TenantName, TrackName = RouteBinding.TrackName
                });

                try
                {
                    if (mTrack.Key.Type != TrackKeyType.Contained)
                    {
                        throw new ValidationException($"Only {Api.TrackKeyType.Contained} keys is supported.");
                    }
                }
                catch (ValidationException vex)
                {
                    logger.Warning(vex);
                    ModelState.TryAddModelError(string.Empty, vex.Message);
                    return(BadRequest(ModelState));
                }

                if (trackKeyRequest.CreateSelfSigned)
                {
                    var certificate = await RouteBinding.CreateSelfSignedCertificateBySubjectAsync();

                    mTrackKey.Key = await certificate.ToFTJsonWebKeyAsync(true);
                }

                if (trackKeyRequest.IsPrimary)
                {
                    mTrack.Key.Keys[0] = mTrackKey;
                }
                else
                {
                    if (mTrack.Key.Keys.Count > 1)
                    {
                        mTrack.Key.Keys[1] = mTrackKey;
                    }
                    else
                    {
                        mTrack.Key.Keys.Add(mTrackKey);
                    }
                }

                await tenantRepository.UpdateAsync(mTrack);

                return(Ok(mapper.Map <Api.TrackKeyItemsContained>(mTrack.Key)));
            }
            catch (CosmosDataException ex)
            {
                if (ex.StatusCode == HttpStatusCode.NotFound)
                {
                    logger.Warning(ex, $"NotFound, Update '{typeof(Api.TrackKeyItemContainedRequest).Name}' contained by track name '{RouteBinding.TrackName}'.");
                    return(NotFound(typeof(Api.TrackKeyItemContainedRequest).Name, RouteBinding.TrackName));
                }
                throw;
            }
        }