1313 * permissions and limitations under the License.
1414 */
1515
16- using System . Collections . Generic ;
17- using System . Reflection ;
18- using System . Threading . Tasks ;
19- using Amazon . Extensions . NETCore . Setup ;
2016using Amazon . Runtime ;
2117using Amazon . SimpleSystemsManagement ;
2218using Amazon . SimpleSystemsManagement . Model ;
19+ using System ;
20+ using System . Collections . Generic ;
21+ using System . Linq ;
22+ using System . Reflection ;
23+ using System . Threading . Tasks ;
24+ using Microsoft . Extensions . Configuration ;
2325
2426namespace Amazon . Extensions . Configuration . SystemsManager . Internal
2527{
2628 public interface ISystemsManagerProcessor
2729 {
28- Task < IEnumerable < Parameter > > GetParametersByPathAsync ( AWSOptions awsOptions , string path ) ;
30+ Task < IDictionary < string , string > > GetDataAsync ( ) ;
2931 }
3032
3133 public class SystemsManagerProcessor : ISystemsManagerProcessor
3234 {
33- public async Task < IEnumerable < Parameter > > GetParametersByPathAsync ( AWSOptions awsOptions , string path )
35+ private const string SecretsManagerPath = "/aws/reference/secretsmanager/" ;
36+
37+ private SystemsManagerConfigurationSource Source { get ; }
38+ private IParameterProcessor ParameterProcessor { get ; }
39+
40+ public SystemsManagerProcessor ( SystemsManagerConfigurationSource source )
3441 {
35- using ( var client = awsOptions . CreateServiceClient < IAmazonSimpleSystemsManagement > ( ) )
42+ Source = source ;
43+ ParameterProcessor = Source . ParameterProcessor ?? new DefaultParameterProcessor ( ) ;
44+ }
45+
46+ public async Task < IDictionary < string , string > > GetDataAsync ( )
47+ {
48+ return IsSecretsManagerPath ( Source . Path )
49+ ? await GetParameterAsync ( ) . ConfigureAwait ( false )
50+ : await GetParametersByPathAsync ( ) . ConfigureAwait ( false ) ;
51+ }
52+
53+ private async Task < IDictionary < string , string > > GetParametersByPathAsync ( )
54+ {
55+ using ( var client = Source . AwsOptions . CreateServiceClient < IAmazonSimpleSystemsManagement > ( ) )
3656 {
37- if ( client is AmazonSimpleSystemsManagementClient impl )
57+ if ( client is AmazonSimpleSystemsManagementClient impl )
3858 {
3959 impl . BeforeRequestEvent += ServiceClientBeforeRequestEvent ;
4060 }
@@ -43,26 +63,64 @@ public async Task<IEnumerable<Parameter>> GetParametersByPathAsync(AWSOptions aw
4363 string nextToken = null ;
4464 do
4565 {
46- var response = await client . GetParametersByPathAsync ( new GetParametersByPathRequest { Path = path , Recursive = true , WithDecryption = true , NextToken = nextToken } ) . ConfigureAwait ( false ) ;
66+ var response = await client . GetParametersByPathAsync ( new GetParametersByPathRequest { Path = Source . Path , Recursive = true , WithDecryption = true , NextToken = nextToken } ) . ConfigureAwait ( false ) ;
4767 nextToken = response . NextToken ;
4868 parameters . AddRange ( response . Parameters ) ;
4969 } while ( ! string . IsNullOrEmpty ( nextToken ) ) ;
5070
51- return parameters ;
71+ return AddPrefix ( ProcessParameters ( parameters , Source . Path , ParameterProcessor ) , Source . Prefix ) ;
72+ }
73+ }
74+
75+ private async Task < IDictionary < string , string > > GetParameterAsync ( )
76+ {
77+ using ( var client = Source . AwsOptions . CreateServiceClient < IAmazonSimpleSystemsManagement > ( ) )
78+ {
79+ if ( client is AmazonSimpleSystemsManagementClient impl )
80+ {
81+ impl . BeforeRequestEvent += ServiceClientBeforeRequestEvent ;
82+ }
83+
84+ var response = await client . GetParameterAsync ( new GetParameterRequest { Name = Source . Path , WithDecryption = true } ) . ConfigureAwait ( false ) ;
85+
86+ if ( ! ParameterProcessor . IncludeParameter ( response . Parameter , SecretsManagerPath ) ) return new Dictionary < string , string > ( ) ;
87+
88+ var prefix = Source . Prefix ?? ParameterProcessor . GetKey ( response . Parameter , SecretsManagerPath ) ;
89+ return AddPrefix ( JsonConfigurationParser . Parse ( ParameterProcessor . GetValue ( response . Parameter , SecretsManagerPath ) ) , prefix ) ;
90+
5291 }
5392 }
5493
55- const string UserAgentHeader = "User-Agent" ;
56- static readonly string _assemblyVersion = typeof ( SystemsManagerProcessor ) . GetTypeInfo ( ) . Assembly . GetName ( ) . Version . ToString ( ) ;
94+ public static bool IsSecretsManagerPath ( string path ) => path . StartsWith ( SecretsManagerPath , StringComparison . OrdinalIgnoreCase ) ;
5795
58- void ServiceClientBeforeRequestEvent ( object sender , RequestEventArgs e )
96+ public static IDictionary < string , string > AddPrefix ( IDictionary < string , string > input , string prefix )
5997 {
60- var args = e as Amazon . Runtime . WebServiceRequestEventArgs ;
61- if ( args == null || ! args . Headers . ContainsKey ( UserAgentHeader ) )
62- return ;
98+ return string . IsNullOrEmpty ( prefix )
99+ ? input
100+ : input . ToDictionary ( pair => $ "{ prefix } { ConfigurationPath . KeyDelimiter } { pair . Key } ", pair => pair . Value , StringComparer . OrdinalIgnoreCase ) ;
101+ }
102+
103+
104+ public static IDictionary < string , string > ProcessParameters ( IEnumerable < Parameter > parameters , string path , IParameterProcessor parameterProcessor )
105+ {
106+ return parameters
107+ . Where ( parameter => parameterProcessor . IncludeParameter ( parameter , path ) )
108+ . Select ( parameter => new
109+ {
110+ Key = parameterProcessor . GetKey ( parameter , path ) ,
111+ Value = parameterProcessor . GetValue ( parameter , path )
112+ } )
113+ . ToDictionary ( parameter => parameter . Key , parameter => parameter . Value , StringComparer . OrdinalIgnoreCase ) ;
114+ }
63115
116+ private const string UserAgentHeader = "User-Agent" ;
117+ private static readonly string AssemblyVersion = typeof ( SystemsManagerProcessor ) . GetTypeInfo ( ) . Assembly . GetName ( ) . Version . ToString ( ) ;
118+
119+ private static void ServiceClientBeforeRequestEvent ( object sender , RequestEventArgs e )
120+ {
121+ if ( ! ( e is WebServiceRequestEventArgs args ) || ! args . Headers . ContainsKey ( UserAgentHeader ) ) return ;
64122
65- args . Headers [ UserAgentHeader ] = args . Headers [ UserAgentHeader ] + " SSMConfigProvider/" + _assemblyVersion ;
123+ args . Headers [ UserAgentHeader ] = args . Headers [ UserAgentHeader ] + " SSMConfigProvider/" + AssemblyVersion ;
66124 }
67125 }
68126}
0 commit comments