
committed by
GitHub

No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 401 additions and 2 deletions
-
56Minio.Examples/Cases/ChainedCredentialProvider.cs
-
58Minio/Credentials/AWSEnvironmentProvider.cs
-
80Minio/Credentials/ChainedProvider.cs
-
29Minio/Credentials/EnvironmentProvider.cs
-
28Minio/Credentials/IClientProvider.cs
-
40Minio/Credentials/MinioEnvironmentProvider.cs
-
60Minio/DataModel/AccessCredentials.cs
-
7Minio/DataModel/MinioClientBuilder.cs
-
6Minio/Helper/utils.cs
-
39Minio/MinioClient.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}"); |
|||
} |
|||
} |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
|
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
|||
} |
@ -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); |
|||
} |
|||
} |
|||
} |
@ -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(); |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue