Skip to content

Commit

Permalink
support for credential providers. (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
BigUstad authored Feb 26, 2021
1 parent 97324ef commit 54c2f66
Show file tree
Hide file tree
Showing 10 changed files with 401 additions and 2 deletions.
56 changes: 56 additions & 0 deletions Minio.Examples/Cases/ChainedCredentialProvider.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 58 additions & 0 deletions Minio/Credentials/AWSEnvironmentProvider.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 80 additions & 0 deletions Minio/Credentials/ChainedProvider.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 29 additions & 0 deletions Minio/Credentials/EnvironmentProvider.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 28 additions & 0 deletions Minio/Credentials/IClientProvider.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 40 additions & 0 deletions Minio/Credentials/MinioEnvironmentProvider.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 60 additions & 0 deletions Minio/DataModel/AccessCredentials.cs
Original file line number Diff line number Diff line change
@@ -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 changes: 6 additions & 1 deletion Minio/DataModel/MinioClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

using System;
using System.Net;
using Minio.Credentials;
using Minio.Exceptions;

namespace Minio
Expand Down Expand Up @@ -144,7 +145,11 @@ public MinioClient Build()
{
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.");
}
Expand Down
6 changes: 6 additions & 0 deletions Minio/Helper/utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -880,5 +880,11 @@ public static string To8601String(DateTime dt)
{
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);
}

}
}
Loading

0 comments on commit 54c2f66

Please sign in to comment.