Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update AWS_S3_USE_ARN_REGION to take precedence over value set i… #3513

Open
wants to merge 3 commits into
base: v4-development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions generator/.DevConfigs/83d75756-74fc-4a10-98a6-96c5c71bda8b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"services": [
{
"serviceName": "S3",
"type": "patch",
"changeLogMessages": [
"Update `AWS_S3_USE_ARN_REGION` environment variable to take precedence over `s3_use_arn_region` option in the config file."
]
}
]
}
10 changes: 7 additions & 3 deletions sdk/src/Services/S3/Custom/AmazonS3Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public partial class AmazonS3Config : ClientConfig
private const string DefaultProfileName = "default";
private const string AwsS3UsEast1RegionalEndpointsEnvironmentVariable = "AWS_S3_US_EAST_1_REGIONAL_ENDPOINT";
private const string DisableMRAPEnvName = "AWS_S3_DISABLE_MULTIREGION_ACCESS_POINTS";
private const string AwsConfigFileEnvName = "AWS_CONFIG_FILE";
dscpinheiro marked this conversation as resolved.
Show resolved Hide resolved

private bool forcePathStyle = false;
private bool useAccelerateEndpoint = false;
Expand Down Expand Up @@ -114,16 +115,19 @@ public bool UseArnRegion
return _useArnRegion.Value;
}

_useArnRegion = _profile?.S3UseArnRegion;

if (!_useArnRegion.HasValue && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable(UseArnRegionEnvName)))
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(UseArnRegionEnvName)))
{
if (bool.TryParse(Environment.GetEnvironmentVariable(UseArnRegionEnvName), out var value))
{
_useArnRegion = value;
}
}

if (!_useArnRegion.HasValue)
{
_useArnRegion = _profile?.S3UseArnRegion;
}

if (!_useArnRegion.HasValue)
{
// To maintain consistency with buckets default UseArnRegion to true when client configured for us-east-1.
Expand Down
126 changes: 92 additions & 34 deletions sdk/test/Services/S3/UnitTests/Custom/UseArnRegionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,49 @@ namespace AWSSDK.UnitTests
[TestClass]
public class UseArnRegionTests
{
/* Used by commented out tests
private static readonly string ProfileText = @"[default]
region=us-west-2
aws_access_key_id=default_aws_access_key_id
aws_secret_access_key=default_aws_secret_access_key
s3_use_arn_region=true
[other]
region=us-west-2
aws_access_key_id=other_aws_access_key_id
aws_secret_access_key=other_aws_secret_access_key
";
*/
private const string AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE = "AWS_S3_USE_ARN_REGION";
private const string AWS_PROFILE_ENVIRONMENT_VARIABLE = "AWS_PROFILE";
private const string AWS_CONFIG_ENVIRONMENT_VARIABLE = "AWS_CONFIG_FILE";

private string _beginningS3UseArnRegionEnvironmentValue;
private string _beginningAWSProfileEnvironmentValue;
private string _tempCredentialsFilePath;

private static readonly string ProfileText = new StringBuilder()
.AppendLine("[default]")
.AppendLine("region=us-west-2")
.AppendLine("aws_access_key_id=default_aws_access_key_id")
.AppendLine("aws_secret_access_key=default_aws_secret_access_key")
.AppendLine("[profile use-arn-region-enabled]")
.AppendLine("s3_use_arn_region=true")
.AppendLine("[profile use-arn-region-disabled]")
.AppendLine("s3_use_arn_region=false")
.ToString();

[TestInitialize]
public void Initialize()
{
// Save off current environment variable value to restore later
_beginningS3UseArnRegionEnvironmentValue = Environment.GetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE);
_beginningAWSProfileEnvironmentValue = Environment.GetEnvironmentVariable(AWS_PROFILE_ENVIRONMENT_VARIABLE);
// Then clear the current value so every test is starting from a clean slate
Environment.SetEnvironmentVariable(AWS_PROFILE_ENVIRONMENT_VARIABLE, "");
// set credentials file and use it to load CredentialProfileStoreChain

// set credentials file and use it to load CredentialProfileStoreChain
_tempCredentialsFilePath = Path.GetTempFileName();
File.WriteAllText(_tempCredentialsFilePath, ProfileText);
ReflectionHelpers.Invoke(typeof(AmazonS3Config), "credentialProfileChain", new CredentialProfileStoreChain(_tempCredentialsFilePath));
ReflectionHelpers.Invoke(typeof(AmazonS3Config), "_triedToResolveProfile", false);
dscpinheiro marked this conversation as resolved.
Show resolved Hide resolved
}

[TestCleanup]
public void RestoreOriginalSettings()
{
Environment.SetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE, _beginningS3UseArnRegionEnvironmentValue);
Environment.SetEnvironmentVariable(AWS_PROFILE_ENVIRONMENT_VARIABLE, _beginningAWSProfileEnvironmentValue);
File.Delete(_tempCredentialsFilePath);
}

[TestMethod]
[TestCategory("S3")]
Expand All @@ -68,9 +99,11 @@ public void ExplicitlyEnable()
[TestCategory("S3")]
public void EnvironmentVariableEnable()
{
Environment.SetEnvironmentVariable("AWS_S3_USE_ARN_REGION", "true");
var beginningS3UseArnRegionEnvValue = Environment.GetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE);
try
{
Environment.SetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE, "true");

var config = new AmazonS3Config
{
};
Expand All @@ -79,41 +112,43 @@ public void EnvironmentVariableEnable()
}
finally
{
Environment.SetEnvironmentVariable("AWS_S3_USE_ARN_REGION", "");
Environment.SetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE, beginningS3UseArnRegionEnvValue);
}
}


[TestMethod]
[TestCategory("S3")]
public void ExplicitDisableOverridesEnvironmentVariable()
{
Environment.SetEnvironmentVariable("AWS_S3_USE_ARN_REGION", "true");
var beginningS3UseArnRegionEnvValue = Environment.GetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE);
try
{
Environment.SetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE, "true");

var config = new AmazonS3Config
{
UseArnRegion = false
};

Assert.IsFalse(config.UseArnRegion);
}

finally
{
Environment.SetEnvironmentVariable("AWS_S3_USE_ARN_REGION", "");
Environment.SetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE, beginningS3UseArnRegionEnvValue);
}

}

/*[TestMethod]
[TestMethod]
[TestCategory("S3")]
public void CredentialProfileEnable()
public void ProfileValueIsUsedIfSet()
{
var tempCredentialsFilePath = Path.GetTempFileName();
File.WriteAllText(tempCredentialsFilePath, ProfileText);
var beginningS3UseArnRegionEnvValue = Environment.GetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE);
try
{
ReflectionHelpers.Invoke(typeof(AmazonS3Config), "credentialProfileChain", new CredentialProfileStoreChain(tempCredentialsFilePath));
Environment.SetEnvironmentVariable(AWS_PROFILE_ENVIRONMENT_VARIABLE, "use-arn-region-enabled");

var config = new AmazonS3Config
{
};
Expand All @@ -122,36 +157,59 @@ public void CredentialProfileEnable()
}
finally
{
File.Delete(tempCredentialsFilePath);
Environment.SetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE, beginningS3UseArnRegionEnvValue);
}

}

[TestMethod]
[TestCategory("S3")]
public void ExplicitDisableOverridesCredentialProfile()
public void EnvironmentVariableTakesPrecedenceOverProfileValue()
{
var currentAwsProfile = Environment.GetEnvironmentVariable("AWS_PROFILE");
var beginningAwsProfileEnvValue = Environment.GetEnvironmentVariable(AWS_PROFILE_ENVIRONMENT_VARIABLE);
var beginningS3UseArnRegionEnvValue = Environment.GetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE);

var tempCredentialsFilePath = Path.GetTempFileName();
File.WriteAllText(tempCredentialsFilePath, ProfileText);
try
{
Environment.SetEnvironmentVariable("AWS_PROFILE", "other");
Environment.SetEnvironmentVariable(AWS_PROFILE_ENVIRONMENT_VARIABLE, "use-arn-region-enabled");
Environment.SetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE, "false");

ReflectionHelpers.Invoke(typeof(AmazonS3Config), "credentialProfileChain", new CredentialProfileStoreChain(tempCredentialsFilePath));
var config = new AmazonS3Config
{
};

Assert.IsFalse(config.UseArnRegion);
Environment.SetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE, "");
}
finally
{

Environment.SetEnvironmentVariable(AWS_PROFILE_ENVIRONMENT_VARIABLE, beginningAwsProfileEnvValue);
Environment.SetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE, beginningS3UseArnRegionEnvValue);
}
}

[TestMethod]
[TestCategory("S3")]
public void ConfigValueTakesPrecedenceOverAllValues()
{
var beginningAwsProfileEnvValue = Environment.GetEnvironmentVariable(AWS_PROFILE_ENVIRONMENT_VARIABLE);
var beginningS3UseArnRegionEnvValue = Environment.GetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE);
try
{
Environment.SetEnvironmentVariable(AWS_PROFILE_ENVIRONMENT_VARIABLE, "use-arn-region-disabled");
Environment.SetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE, "false");
var config = new AmazonS3Config
{
UseArnRegion = true
};

Assert.IsTrue(config.UseArnRegion);
}
finally
{
Environment.SetEnvironmentVariable("AWS_PROFILE", currentAwsProfile);
File.Delete(tempCredentialsFilePath);
Environment.SetEnvironmentVariable(AWS_PROFILE_ENVIRONMENT_VARIABLE, beginningAwsProfileEnvValue);
Environment.SetEnvironmentVariable(AWS_S3_USE_ARN_REGION_ENVIRONMENT_VARIABLE, beginningS3UseArnRegionEnvValue);
}
}*/
}
}
}