1
1
/*
2
- * Copyright 2022 MONAI Consortium
2
+ * Copyright 2022-2024 MONAI Consortium
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
15
15
*/
16
16
17
17
using System . Diagnostics ;
18
+ using System . Globalization ;
18
19
using System . IO . Abstractions ;
19
20
using Amazon . SecurityToken . Model ;
20
21
using Ardalis . GuardClauses ;
@@ -36,9 +37,12 @@ public class StorageAdminService : IStorageAdminService
36
37
private readonly string _accessKey ;
37
38
private readonly string _secretKey ;
38
39
private readonly IFileSystem _fileSystem ;
39
- private readonly string _set_connection_cmd ;
40
- private readonly string _get_connections_cmd ;
41
- private readonly string _get_users_cmd ;
40
+ private string _set_connection_cmd ;
41
+ private string _get_connections_cmd ;
42
+ private string _get_users_cmd ;
43
+ private string _set_policy_cmd ;
44
+ private string _create_policy_cmd ;
45
+ private string _remove_user_cmd ;
42
46
43
47
public StorageAdminService ( IOptions < StorageServiceConfiguration > options , ILogger < StorageAdminService > logger , IFileSystem fileSystem )
44
48
{
@@ -56,9 +60,18 @@ public StorageAdminService(IOptions<StorageServiceConfiguration> options, ILogge
56
60
_endpoint = options . Value . Settings [ ConfigurationKeys . EndPoint ] ;
57
61
_accessKey = options . Value . Settings [ ConfigurationKeys . AccessKey ] ;
58
62
_secretKey = options . Value . Settings [ ConfigurationKeys . AccessToken ] ;
63
+
64
+ SetCommandTemplates ( options ) ;
65
+ }
66
+
67
+ private void SetCommandTemplates ( IOptions < StorageServiceConfiguration > options )
68
+ {
59
69
_set_connection_cmd = $ "alias set { _serviceName } http://{ _endpoint } { _accessKey } { _secretKey } ";
60
70
_get_connections_cmd = "alias list" ;
61
71
_get_users_cmd = $ "admin user list { _serviceName } ";
72
+ _set_policy_cmd = "admin policy attach {0} {1} --{2} {3}" ;
73
+ _remove_user_cmd = "admin user remove {0} {1}" ;
74
+ _create_policy_cmd = "admin policy create {0} pol_{1} {2}" ;
62
75
}
63
76
64
77
private static void ValidateConfiguration ( StorageServiceConfiguration configuration )
@@ -89,7 +102,7 @@ public async Task<bool> SetPolicyAsync(IdentityType policyType, List<string> pol
89
102
Guard . Against . NullOrWhiteSpace ( itemName , nameof ( itemName ) ) ;
90
103
91
104
var policiesStr = string . Join ( ',' , policies ) ;
92
- var setPolicyCmd = $ "admin policy set { _serviceName } { policiesStr } { policyType . ToString ( ) . ToLower ( ) } = { itemName } " ;
105
+ var setPolicyCmd = string . Format ( CultureInfo . InvariantCulture , _set_policy_cmd , _serviceName , policiesStr , policyType . ToString ( ) . ToLowerInvariant ( ) , itemName ) ;
93
106
var result = await ExecuteAsync ( setPolicyCmd ) . ConfigureAwait ( false ) ;
94
107
95
108
var expectedResult = $ "Policy `{ policiesStr } ` is set on { policyType . ToString ( ) . ToLower ( ) } `{ itemName } `";
@@ -197,7 +210,7 @@ public async Task RemoveUserAsync(string username)
197
210
{
198
211
Guard . Against . NullOrWhiteSpace ( username , nameof ( username ) ) ;
199
212
200
- var result = await ExecuteAsync ( $ "admin user remove { _serviceName } { username } " ) . ConfigureAwait ( false ) ;
213
+ var result = await ExecuteAsync ( string . Format ( CultureInfo . InvariantCulture , _remove_user_cmd , _serviceName , username ) ) . ConfigureAwait ( false ) ;
201
214
202
215
if ( ! result . Any ( r => r . Contains ( $ "Removed user `{ username } ` successfully.") ) )
203
216
{
@@ -260,7 +273,7 @@ private async Task<string> CreatePolicyAsync(PolicyRequest[] policyRequests, str
260
273
Guard . Against . NullOrWhiteSpace ( username , nameof ( username ) ) ;
261
274
262
275
var policyFileName = await CreatePolicyFile ( policyRequests , username ) . ConfigureAwait ( false ) ;
263
- var result = await ExecuteAsync ( $ "admin policy add { _serviceName } pol_ { username } { policyFileName } " ) . ConfigureAwait ( false ) ;
276
+ var result = await ExecuteAsync ( string . Format ( CultureInfo . InvariantCulture , _create_policy_cmd , _serviceName , username , policyFileName ) ) . ConfigureAwait ( false ) ;
264
277
if ( result . Any ( r => r . Contains ( $ "Added policy `pol_{ username } ` successfully.") ) is false )
265
278
{
266
279
await RemoveUserAsync ( username ) . ConfigureAwait ( false ) ;
0 commit comments