Skip to content

Commit e2ba72b

Browse files
committed
Update mc commands
Signed-off-by: Victor Chang <[email protected]>
1 parent 2361edd commit e2ba72b

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

.licenserc.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ header:
22
license:
33
spdx-id: Apache-2.0
44
copyright-owner: MONAI Consortium
5+
copyright-year: '2021-2024'
6+
57

68
paths:
79
- 'src'

src/Plugins/MinIO/StorageAdminService.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 MONAI Consortium
2+
* Copyright 2022-2024 MONAI Consortium
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,7 @@
1515
*/
1616

1717
using System.Diagnostics;
18+
using System.Globalization;
1819
using System.IO.Abstractions;
1920
using Amazon.SecurityToken.Model;
2021
using Ardalis.GuardClauses;
@@ -36,9 +37,12 @@ public class StorageAdminService : IStorageAdminService
3637
private readonly string _accessKey;
3738
private readonly string _secretKey;
3839
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;
4246

4347
public StorageAdminService(IOptions<StorageServiceConfiguration> options, ILogger<StorageAdminService> logger, IFileSystem fileSystem)
4448
{
@@ -56,9 +60,18 @@ public StorageAdminService(IOptions<StorageServiceConfiguration> options, ILogge
5660
_endpoint = options.Value.Settings[ConfigurationKeys.EndPoint];
5761
_accessKey = options.Value.Settings[ConfigurationKeys.AccessKey];
5862
_secretKey = options.Value.Settings[ConfigurationKeys.AccessToken];
63+
64+
SetCommandTemplates(options);
65+
}
66+
67+
private void SetCommandTemplates(IOptions<StorageServiceConfiguration> options)
68+
{
5969
_set_connection_cmd = $"alias set {_serviceName} http://{_endpoint} {_accessKey} {_secretKey}";
6070
_get_connections_cmd = "alias list";
6171
_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}";
6275
}
6376

6477
private static void ValidateConfiguration(StorageServiceConfiguration configuration)
@@ -89,7 +102,7 @@ public async Task<bool> SetPolicyAsync(IdentityType policyType, List<string> pol
89102
Guard.Against.NullOrWhiteSpace(itemName, nameof(itemName));
90103

91104
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);
93106
var result = await ExecuteAsync(setPolicyCmd).ConfigureAwait(false);
94107

95108
var expectedResult = $"Policy `{policiesStr}` is set on {policyType.ToString().ToLower()} `{itemName}`";
@@ -197,7 +210,7 @@ public async Task RemoveUserAsync(string username)
197210
{
198211
Guard.Against.NullOrWhiteSpace(username, nameof(username));
199212

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);
201214

202215
if (!result.Any(r => r.Contains($"Removed user `{username}` successfully.")))
203216
{
@@ -260,7 +273,7 @@ private async Task<string> CreatePolicyAsync(PolicyRequest[] policyRequests, str
260273
Guard.Against.NullOrWhiteSpace(username, nameof(username));
261274

262275
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);
264277
if (result.Any(r => r.Contains($"Added policy `pol_{username}` successfully.")) is false)
265278
{
266279
await RemoveUserAsync(username).ConfigureAwait(false);

0 commit comments

Comments
 (0)