/* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at http://www.apache.org/licenses/LICENSE-2.0 or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ using Microsoft.AspNetCore.DataProtection.KeyManagement; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using System; using System.Collections.Generic; using System.Text; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.AspNetCore.DataProtection; using Amazon.AspNetCore.DataProtection.SSM; using Amazon.SimpleSystemsManagement; namespace Microsoft.Extensions.DependencyInjection { /// /// Extension methods to make it easy to register SSM to persist data protection keys. /// public static class ExtensionMethods { /// /// Register AWS Systems Manager (SSM) to persist the ASP.NET Core DataProtection framework keys. Keys will be stored in SSM's /// Parameter Store using the prefix specified by the parameterNamePrefix parameter. It is expected that only DataProtection keys will be stored /// with this prefix. /// /// /// The prefix applied to the DataProtection key names. /// public static IDataProtectionBuilder PersistKeysToAWSSystemsManager(this IDataProtectionBuilder builder, string parameterNamePrefix) { return PersistKeysToAWSSystemsManager(builder, parameterNamePrefix, null); } /// /// Register AWS Systems Manager (SSM) to persist the ASP.NET Core DataProtection framework keys. Keys will be stored in SSM's /// Parameter Store using the prefix specified by the parameterNamePrefix parameter. It is expected that only DataProtection keys will be stored /// with this prefix. /// /// /// The prefix applied to the DataProtection key names. /// Delegate to specify options for persistence. For example setting a KMS Key ID. /// public static IDataProtectionBuilder PersistKeysToAWSSystemsManager(this IDataProtectionBuilder builder, string parameterNamePrefix, Action setupAction = null) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } builder.Services.TryAddAWSService(); builder.Services.AddSingleton>(services => { var ssmOptions = new PersistOptions(); setupAction?.Invoke(ssmOptions); var ssmClient = services.GetService(); var loggerFactory = services.GetService() ?? NullLoggerFactory.Instance; return new ConfigureOptions(options => { options.XmlRepository = new SSMXmlRepository(ssmClient, parameterNamePrefix, ssmOptions, loggerFactory); }); }); return builder; } } }