Skip to content

Commit

Permalink
Update AWS S3 Extension to allow for use of credential chain. (#969)
Browse files Browse the repository at this point in the history
## Motivation and Context (Why the change? What's the scenario?)

Allow the use of [instance
metadata](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html)
for authentication and the proper use of [credential
chain](https://docs.aws.amazon.com/sdkref/latest/guide/standardized-credentials.html).

## High level description (Approach, Design)

Added AWS S3 Extension CredentialChain authentication method to use
default credentials

---------

Co-authored-by: Armando Portill <aportillo@fingercheck.com>
Co-authored-by: Devis Lucato <dluc@users.noreply.github.com>
Co-authored-by: Devis Lucato <devis@microsoft.com>
  • Loading branch information
4 people authored Jan 8, 2025
1 parent 6c3f50a commit b3b285a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
18 changes: 11 additions & 7 deletions extensions/AWS/S3/AWSS3Config.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.

using System.Text.Json.Serialization;

Expand All @@ -13,6 +13,7 @@ public enum AuthTypes
{
Unknown = -1,
AccessKey,
CredentialChain,
}

public AuthTypes Auth { get; set; } = AuthTypes.Unknown;
Expand Down Expand Up @@ -45,14 +46,17 @@ public void Validate()
throw new ConfigurationException($"Authentication type '{this.Auth}' undefined or not supported");
}

if (string.IsNullOrWhiteSpace(this.AccessKey))
if (this.Auth == AuthTypes.AccessKey)
{
throw new ConfigurationException("S3 Access Key is undefined");
}
if (string.IsNullOrWhiteSpace(this.AccessKey))
{
throw new ConfigurationException("S3 Access Key is undefined");
}

if (string.IsNullOrWhiteSpace(this.SecretAccessKey))
{
throw new ConfigurationException("S3 Secret Key Access undefined");
if (string.IsNullOrWhiteSpace(this.SecretAccessKey))
{
throw new ConfigurationException("S3 Secret Key Access undefined");
}
}

if (string.IsNullOrWhiteSpace(this.BucketName))
Expand Down
11 changes: 10 additions & 1 deletion extensions/AWS/S3/AWSS3Storage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All rights reserved.
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -43,6 +43,15 @@ public AWSS3Storage(
);
break;
}
case AWSS3Config.AuthTypes.CredentialChain:
{
this._client = new AmazonS3Client(new AmazonS3Config
{
ServiceURL = config.Endpoint,
LogResponse = true
});
break;
}

default:
this._log.LogCritical("Authentication type '{0}' undefined or not supported", config.Auth);
Expand Down
1 change: 1 addition & 0 deletions service/Service/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
"HttpClientName": ""
},
"AWSS3": {
// "AccessKey" or "CredentialChain". For other options see <AWSS3Config>.
"Auth": "AccessKey",
// AccessKey ID, required when using AccessKey auth
// Note: you can use an env var 'KernelMemory__Services__AWSS3__AccessKey' to set this
Expand Down

0 comments on commit b3b285a

Please sign in to comment.