Browse Source

support for credential providers. (#518)

pull/525/head
Priyank Raj 4 years ago
committed by GitHub
parent
commit
54c2f66d0a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 56
      Minio.Examples/Cases/ChainedCredentialProvider.cs
  2. 58
      Minio/Credentials/AWSEnvironmentProvider.cs
  3. 80
      Minio/Credentials/ChainedProvider.cs
  4. 29
      Minio/Credentials/EnvironmentProvider.cs
  5. 28
      Minio/Credentials/IClientProvider.cs
  6. 40
      Minio/Credentials/MinioEnvironmentProvider.cs
  7. 60
      Minio/DataModel/AccessCredentials.cs
  8. 7
      Minio/DataModel/MinioClientBuilder.cs
  9. 6
      Minio/Helper/utils.cs
  10. 39
      Minio/MinioClient.cs

56
Minio.Examples/Cases/ChainedCredentialProvider.cs

@ -0,0 +1,56 @@
/*
* MinIO .NET Library for Amazon S3 Compatible Cloud Storage,
* (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 System;
using System.Threading.Tasks;
using Minio.Credentials;
using Minio.DataModel;
using Minio.Exceptions;
namespace Minio.Examples.Cases
{
public class ChainedCredentialProvider
{
// Establish Credentials with AWS Session token
public async static Task Run()
{
ChainedProvider provider = new ChainedProvider()
.AddProviders(new ClientProvider[]{new AWSEnvironmentProvider(), new MinioEnvironmentProvider()});
//Chained provider definition here.
MinioClient minioClient = new MinioClient()
.WithEndpoint("s3.amazonaws.com")
.WithSSL()
.WithCredentialsProvider(provider)
.Build();
try
{
StatObjectArgs statObjectArgs = new StatObjectArgs()
.WithBucket("my-bucket-name")
.WithObject("my-object-name");
ObjectStat result = await minioClient.StatObjectAsync(statObjectArgs);
}
catch (MinioException me)
{
Console.WriteLine($"[Bucket] ChainedCredentialProvider example case encountered Exception: {me}");
}
catch (Exception e)
{
Console.WriteLine($"[Bucket] ChainedCredentialProvider example case encountered Exception: {e}");
}
}
}
}

58
Minio/Credentials/AWSEnvironmentProvider.cs

@ -0,0 +1,58 @@
/*
* MinIO .NET Library for Amazon S3 Compatible Cloud Storage,
* (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 System;
using System.Threading.Tasks;
using Minio.DataModel;
namespace Minio.Credentials
{
public class AWSEnvironmentProvider : EnvironmentProvider
{
public override AccessCredentials GetCredentials()
{
AccessCredentials credentials = new AccessCredentials(GetAccessKey(), GetSecretKey(), GetEnvironmentVariable("AWS_SESSION_TOKEN"), default(DateTime));
return credentials;
}
public override Task<AccessCredentials> GetCredentialsAsync()
{
throw new InvalidOperationException("Please use the non-async function GetCredentials()");
}
protected string GetAccessKey()
{
string accessKey = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY_ID");
if (string.IsNullOrEmpty(accessKey) || string.IsNullOrWhiteSpace(accessKey))
{
accessKey = Environment.GetEnvironmentVariable("AWS_ACCESS_KEY");
}
return accessKey;
}
protected string GetSecretKey()
{
string secretKey = Environment.GetEnvironmentVariable("AWS_SECRET_ACCESS_KEY");
if (string.IsNullOrEmpty(secretKey) || string.IsNullOrWhiteSpace(secretKey))
{
secretKey = Environment.GetEnvironmentVariable("AWS_SECRET_KEY");
}
return secretKey;
}
}
}

80
Minio/Credentials/ChainedProvider.cs

@ -0,0 +1,80 @@
/*
* MinIO .NET Library for Amazon S3 Compatible Cloud Storage,
* (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Minio.DataModel;
namespace Minio.Credentials
{
public class ChainedProvider : ClientProvider
{
internal List<ClientProvider> Providers { get; set; }
internal ClientProvider CurrentProvider { get; set; }
internal AccessCredentials Credentials { get; set; }
public ChainedProvider()
{
this.Providers = new List<ClientProvider>();
}
public ChainedProvider AddProvider(ClientProvider provider)
{
this.Providers.Add(provider);
return this;
}
public ChainedProvider AddProviders(ClientProvider[] providers)
{
this.Providers.AddRange(providers.ToList());
return this;
}
public override AccessCredentials GetCredentials()
{
if (this.Credentials != null && !this.Credentials.AreExpired())
{
return this.Credentials;
}
if (this.CurrentProvider != null && !this.Credentials.AreExpired())
{
this.Credentials = this.CurrentProvider.GetCredentials();
return this.CurrentProvider.GetCredentials();
}
foreach (var provider in this.Providers)
{
var credentials = provider.GetCredentials();
if (credentials != null && !credentials.AreExpired())
{
this.CurrentProvider = provider;
this.Credentials = credentials;
return credentials;
}
}
throw new InvalidOperationException("None of the assigned providers were able to provide valid credentials.");
}
public override async Task<AccessCredentials> GetCredentialsAsync()
{
AccessCredentials credentials = this.GetCredentials();
await Task.Yield();
return credentials;
}
}
}

29
Minio/Credentials/EnvironmentProvider.cs

@ -0,0 +1,29 @@
/*
* MinIO .NET Library for Amazon S3 Compatible Cloud Storage,
* (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 System;
namespace Minio.Credentials
{
public abstract class EnvironmentProvider : ClientProvider
{
internal string GetEnvironmentVariable(string env)
{
return Environment.GetEnvironmentVariable(env);
}
}
}

28
Minio/Credentials/IClientProvider.cs

@ -0,0 +1,28 @@
/*
* MinIO .NET Library for Amazon S3 Compatible Cloud Storage,
* (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 System.Threading.Tasks;
using Minio.DataModel;
namespace Minio.Credentials
{
public abstract class ClientProvider
{
public abstract AccessCredentials GetCredentials();
public abstract Task<AccessCredentials> GetCredentialsAsync();
}
}

40
Minio/Credentials/MinioEnvironmentProvider.cs

@ -0,0 +1,40 @@
/*
* MinIO .NET Library for Amazon S3 Compatible Cloud Storage,
* (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 System;
using System.Threading.Tasks;
using Minio.DataModel;
namespace Minio.Credentials
{
public class MinioEnvironmentProvider : EnvironmentProvider
{
public override AccessCredentials GetCredentials()
{
AccessCredentials credentials = new AccessCredentials(GetEnvironmentVariable("MINIO_ACCESS_KEY"), GetEnvironmentVariable("MINIO_SECRET_KEY"), null, default(DateTime));
return credentials;
}
public override async Task<AccessCredentials> GetCredentialsAsync()
{
AccessCredentials credentials = this.GetCredentials();
await Task.Yield();
return credentials;
}
}
}

60
Minio/DataModel/AccessCredentials.cs

@ -0,0 +1,60 @@
/*
* MinIO .NET Library for Amazon S3 Compatible Cloud Storage,
* (C) 2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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 System;
using System.Xml.Serialization;
namespace Minio.DataModel
{
[Serializable]
[XmlRoot(ElementName = "Credentials")]
public class AccessCredentials
{
[XmlElement(ElementName = "AccessKeyId", IsNullable = true)]
public string AccessKey { get; set; }
[XmlElement(ElementName = "SecretAccessKey", IsNullable = true)]
public string SecretKey { get; set; }
[XmlElement(ElementName = "SessionToken", IsNullable = true)]
public string SessionToken { get; set; }
// Needs to be stored in ISO8601 format from Datetime
[XmlElement(ElementName = "Expiration", IsNullable = true)]
public string Expiration { get; set; }
public AccessCredentials(string accessKey, string secretKey,
string sessionToken, DateTime expiration)
{
if (string.IsNullOrEmpty(accessKey) || string.IsNullOrEmpty(secretKey) ||
string.IsNullOrWhiteSpace(accessKey) || string.IsNullOrWhiteSpace(secretKey))
{
throw new ArgumentNullException(nameof(this.AccessKey) + " and " + nameof(this.SecretKey) + " cannot be null or empty.");
}
this.AccessKey = accessKey;
this.SecretKey = secretKey;
this.SessionToken = sessionToken;
this.Expiration = (expiration.Equals(default(DateTime)))?null:utils.To8601String(expiration);
}
public bool AreExpired()
{
if (string.IsNullOrEmpty(this.Expiration))
{
return false;
}
DateTime expiry = utils.From8601String(this.Expiration);
return DateTime.Now.CompareTo(expiry) > 0;
}
}
}

7
Minio/DataModel/MinioClientBuilder.cs

@ -17,6 +17,7 @@
using System;
using System.Net;
using Minio.Credentials;
using Minio.Exceptions;
namespace Minio
@ -144,7 +145,11 @@ namespace Minio
{
throw new MinioException("Endpoint not initialized.");
}
if (string.IsNullOrEmpty(this.AccessKey) || string.IsNullOrEmpty(this.SecretKey) )
if (this.Provider != null && this.Provider.GetType() != (typeof(ChainedProvider)) && this.SessionToken == null)
{
throw new MinioException("User Access Credentials Provider not initialized correctly.");
}
if (this.Provider == null && (string.IsNullOrEmpty(this.AccessKey) || string.IsNullOrEmpty(this.SecretKey)))
{
throw new MinioException("User Access Credentials not initialized.");
}

6
Minio/Helper/utils.cs

@ -880,5 +880,11 @@ namespace Minio
{
return dt.ToString("yyyy-MM-dd'T'HH:mm:ssZ", CultureInfo.InvariantCulture);
}
public static DateTime From8601String(string dt)
{
return DateTime.Parse(dt, null, System.Globalization.DateTimeStyles.RoundtripKind);
}
}
}

39
Minio/MinioClient.cs

@ -25,7 +25,8 @@ using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Xml.Serialization;
using Minio.Credentials;
using Minio.DataModel;
using Minio.DataModel.Tracing;
using Minio.Exceptions;
using Minio.Helper;
@ -64,6 +65,8 @@ namespace Minio
private IRequestLogger logger;
internal ClientProvider Provider;
// Enables HTTP tracing if set to true
private bool trace = false;
@ -294,6 +297,19 @@ namespace Minio
}
}
if (this.Provider != null)
{
AccessCredentials creds = await this.Provider.GetCredentialsAsync();
bool isAWSProvider = (this.Provider is AWSEnvironmentProvider) ||
(this.Provider is ChainedProvider chained && chained.CurrentProvider is AWSEnvironmentProvider);
bool isAWSSessionTokenAvailable = isAWSProvider && (!string.IsNullOrEmpty(creds.SessionToken) && !string.IsNullOrWhiteSpace(creds.SessionToken));
if (isAWSSessionTokenAvailable)
{
request.AddHeader("X-Amz-Security-Token", creds.SessionToken);
}
authenticator.Authenticate(restClient, request);
}
return request;
}
@ -370,6 +386,7 @@ namespace Minio
{
this.Region = "";
this.SessionToken = "";
this.Provider = null;
}
/// <summary>
@ -449,6 +466,26 @@ namespace Minio
return this;
}
/// <summary>
/// With provider for credentials and session token if being used
/// </summary>
/// <returns></returns>
public MinioClient WithCredentialsProvider(ClientProvider provider)
{
this.Provider = provider;
AccessCredentials credentials = this.Provider.GetCredentials();
this.AccessKey = credentials.AccessKey;
this.SecretKey = credentials.SecretKey;
bool isSessionTokenAvailable = !string.IsNullOrEmpty(credentials.SessionToken);
if ((this.Provider is AWSEnvironmentProvider aWSEnvironmentProvider ||
(this.Provider is ChainedProvider chainedProvider && chainedProvider.CurrentProvider is AWSEnvironmentProvider))
&& isSessionTokenAvailable)
{
this.SessionToken = credentials.SessionToken;
}
return this;
}
/// <summary>
/// Sets endpoint URL on the client object that request will be made against
/// </summary>

Loading…
Cancel
Save