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: solve error in default credentials provider #423

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@
Assert.Equal("There is no credential chain can use.", exception.Message);
}

[Fact]
public void GetCredentialWithException()
{
var provider = new Mock<DefaultCredentialProvider>();
var defaultProvider = provider.Object;

var exception = Assert.Throws<ClientException>(() =>
{
var credential = defaultProvider.GetCredentials();
});

Assert.Equal("There is no credential chain can use.", exception.Message);
}

/*
Case: Test Credential File With default Client Name and file exist with ak value and type
Result: should return AccessKeyCredential
Expand All @@ -59,9 +73,17 @@
[Fact]
public void GetCredentialFileAlibabaCloudCredentialWithAKType()
{
var cacheRoleArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN");
var cacheProviderArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN");
var cacheFile = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE");

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", null);

var profile = DefaultProfile.GetProfile();
profile.DefaultClientName = "default";
var defaultCredentialProvider = new DefaultCredentialProvider(profile, null);

Check warning on line 86 in aliyun-net-sdk-core.Tests/Units/Auth/Provider/DefaultCredentialProviderTest.cs

View workflow job for this annotation

GitHub Actions / build

'DefaultCredentialProvider.DefaultCredentialProvider(IClientProfile, AlibabaCloudCredentialsProvider)' is obsolete
TestHelper.CreateIniFileWithAkType();

var credential = (BasicCredentials)defaultCredentialProvider.GetAlibabaCloudClientCredential();
Expand All @@ -71,6 +93,9 @@
Assert.NotNull(credential);
Assert.NotNull(credential.GetAccessKeyId());
Assert.NotNull(credential.GetAccessKeySecret());
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", cacheRoleArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", cacheProviderArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", cacheFile);
}

/*
Expand All @@ -81,13 +106,17 @@
[Fact]
public void GetCredentialFileAlibabaCloudCredentialWithAKTypeButAKIsEmpty()
{
var cacheRoleArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN");
var cacheProviderArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN");
var cacheFile = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE");

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", null);

var profile = DefaultProfile.GetProfile();
profile.DefaultClientName = "default";
var defaultCredentialProvider = new DefaultCredentialProvider(profile, null);

Check warning on line 119 in aliyun-net-sdk-core.Tests/Units/Auth/Provider/DefaultCredentialProviderTest.cs

View workflow job for this annotation

GitHub Actions / build

'DefaultCredentialProvider.DefaultCredentialProvider(IClientProfile, AlibabaCloudCredentialsProvider)' is obsolete
TestHelper.CreateIniFileWithAkTypeWithEmptyAK();

var exception = Assert.Throws<ClientException>(() =>
Expand All @@ -97,24 +126,66 @@

TestHelper.DeleteIniFile();

Assert.Equal("Missing required variable option for 'default Client'", exception.Message);
Assert.Equal("Access key ID cannot be null.", exception.Message);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", cacheRoleArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", cacheProviderArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", cacheFile);
}

[Fact]
public void GetCredentialFileAlibabaCloudCredentialWithDefaultSection()
{
var cacheRoleArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN");
var cacheProviderArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN");
var cacheFile = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE");

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", null);

TestHelper.CreateIniFileWithDefaultSection();

var profile = DefaultProfile.GetProfile();
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_CREDENTIALS_FILE", TestHelper.GetIniFilePath());

var provider = new DefaultCredentialProvider(profile, null);

Check warning on line 151 in aliyun-net-sdk-core.Tests/Units/Auth/Provider/DefaultCredentialProviderTest.cs

View workflow job for this annotation

GitHub Actions / build

'DefaultCredentialProvider.DefaultCredentialProvider(IClientProfile, AlibabaCloudCredentialsProvider)' is obsolete

var credential = provider.GetAlibabaCloudClientCredential();

TestHelper.DeleteIniFile();
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_CREDENTIALS_FILE", null);
Assert.NotNull(credential);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", cacheRoleArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", cacheProviderArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", cacheFile);

}

[Fact]
public void GetOIDCAlibabaCloudCredentialTest()
{
var cacheRoleArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN");
var cacheProviderArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN");
var cacheFile = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE");
var cacheRegionId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID");

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", "test");
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", "test");
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", TestHelper.GetOIDCTokenFilePath());
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", "us-west-1");
var profile = DefaultProfile.GetProfile();
profile.DefaultClientName = "default";
var defaultCredentialProvider = new DefaultCredentialProvider(profile, null);

Check warning on line 178 in aliyun-net-sdk-core.Tests/Units/Auth/Provider/DefaultCredentialProviderTest.cs

View workflow job for this annotation

GitHub Actions / build

'DefaultCredentialProvider.DefaultCredentialProvider(IClientProfile, AlibabaCloudCredentialsProvider)' is obsolete
var exception = Assert.Throws<ClientException>(() =>
{
var credential = defaultCredentialProvider.GetAlibabaCloudClientCredential();
});
Assert.Contains("Parameter OIDCProviderArn is not valid", exception.Message);

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", cacheRoleArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", cacheProviderArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", cacheFile);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", cacheRegionId);
}

/*
Expand All @@ -125,6 +196,14 @@
[Fact]
public void GetCredentialFileAlibabaCloudCredentialWithEcsRamRole()
{
var cacheRoleArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN");
var cacheProviderArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN");
var cacheFile = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE");

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", null);

var ecsRamRoleCredential =
new InstanceProfileCredentials("fakeak", "fakeaks", "fakesession", DateTime.UtcNow.ToString(), 4000);
var mockHomePath = EnvironmentUtil.GetHomePath();
Expand All @@ -144,6 +223,10 @@

TestHelper.DeleteIniFile();
Assert.NotNull(credential);

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", cacheRoleArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", cacheProviderArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", cacheFile);
}

/*
Expand All @@ -159,13 +242,11 @@
var profile = DefaultProfile.GetProfile();
profile.DefaultClientName = null;

var defaultCredentialProvider = new DefaultCredentialProvider(profile, null);

Check warning on line 245 in aliyun-net-sdk-core.Tests/Units/Auth/Provider/DefaultCredentialProviderTest.cs

View workflow job for this annotation

GitHub Actions / build

'DefaultCredentialProvider.DefaultCredentialProvider(IClientProfile, AlibabaCloudCredentialsProvider)' is obsolete

var credential = (BasicCredentials)defaultCredentialProvider.GetCredentialFileAlibabaCloudCredential();

var ex = Assert.Throws<ClientException>(() => (BasicCredentials)defaultCredentialProvider.GetCredentialFileAlibabaCloudCredential());
Assert.Equal("The configured client type is empty", ex.Message);
TestHelper.DeleteIniFile();
Assert.NotNull(credential);
Assert.Equal("foo", credential.GetAccessKeyId());
}

/*
Expand All @@ -180,12 +261,12 @@
var profile = DefaultProfile.GetProfile();
profile.DefaultClientName = null;

var defaultCredentialProvider = new DefaultCredentialProvider(profile, null);

Check warning on line 264 in aliyun-net-sdk-core.Tests/Units/Auth/Provider/DefaultCredentialProviderTest.cs

View workflow job for this annotation

GitHub Actions / build

'DefaultCredentialProvider.DefaultCredentialProvider(IClientProfile, AlibabaCloudCredentialsProvider)' is obsolete

var credential = defaultCredentialProvider.GetCredentialFileAlibabaCloudCredential();
var ex = Assert.Throws<ClientException>(() => defaultCredentialProvider.GetCredentialFileAlibabaCloudCredential());
Assert.Equal("The configured client type is empty", ex.Message);

TestHelper.DeleteIniFile();
Assert.Null(credential);
}

/*
Expand Down Expand Up @@ -275,7 +356,7 @@

TestHelper.InitializeEnvironmentValue();

var defaultCredentialProvider = new DefaultCredentialProvider(profile, null);

Check warning on line 359 in aliyun-net-sdk-core.Tests/Units/Auth/Provider/DefaultCredentialProviderTest.cs

View workflow job for this annotation

GitHub Actions / build

'DefaultCredentialProvider.DefaultCredentialProvider(IClientProfile, AlibabaCloudCredentialsProvider)' is obsolete
var credential = (BasicCredentials)defaultCredentialProvider.GetAlibabaCloudClientCredential();

Assert.NotNull(credential);
Expand All @@ -296,7 +377,7 @@

TestHelper.InitializeEnvironmentValue();

var defaultCredentialProvider = new DefaultCredentialProvider(profile, null);

Check warning on line 380 in aliyun-net-sdk-core.Tests/Units/Auth/Provider/DefaultCredentialProviderTest.cs

View workflow job for this annotation

GitHub Actions / build

'DefaultCredentialProvider.DefaultCredentialProvider(IClientProfile, AlibabaCloudCredentialsProvider)' is obsolete
var credential = (BasicCredentials)defaultCredentialProvider.GetEnvironmentAlibabaCloudCredential();

Assert.NotNull(credential);
Expand All @@ -321,22 +402,22 @@
public void GetInstanceRamRoleAlibabaCloudCredential()
{
var profile = DefaultProfile.GetProfile();
var defaultCredentialProvider = new DefaultCredentialProvider(profile, null);

Check warning on line 405 in aliyun-net-sdk-core.Tests/Units/Auth/Provider/DefaultCredentialProviderTest.cs

View workflow job for this annotation

GitHub Actions / build

'DefaultCredentialProvider.DefaultCredentialProvider(IClientProfile, AlibabaCloudCredentialsProvider)' is obsolete

var exception = Assert.Throws<ClientException>(() =>
{
defaultCredentialProvider.GetInstanceRamRoleAlibabaCloudCredential();
});

Assert.Equal("RegionID cannot be null or empty.", exception.Message);
Assert.Equal("Environment variable roleName('ALIBABA_CLOUD_ECS_METADATA') cannot be empty", exception.Message);

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", "region_id");
profile.DefaultClientName = "test";
defaultCredentialProvider = new DefaultCredentialProvider(profile, null);

Check warning on line 416 in aliyun-net-sdk-core.Tests/Units/Auth/Provider/DefaultCredentialProviderTest.cs

View workflow job for this annotation

GitHub Actions / build

'DefaultCredentialProvider.DefaultCredentialProvider(IClientProfile, AlibabaCloudCredentialsProvider)' is obsolete
var credentialProvider = defaultCredentialProvider.GetInstanceRamRoleAlibabaCloudCredential();
var ex = Assert.Throws<ClientException>(() => defaultCredentialProvider.GetInstanceRamRoleAlibabaCloudCredential());

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", null);
Assert.Null(credentialProvider);
Assert.Equal("Environment variable roleName('ALIBABA_CLOUD_ECS_METADATA') cannot be empty", exception.Message);
}

[Fact]
Expand Down Expand Up @@ -369,13 +450,13 @@
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", "region_id");
profile.DefaultClientName = "default";
var defaultCredentialProvider = new DefaultCredentialProvider(profile, null);
var exception = Assert.Throws<ArgumentNullException>(() =>
var exception = Assert.Throws<ClientException>(() =>
{
var credentialProvider = defaultCredentialProvider.GetInstanceRamRoleAlibabaCloudCredential();
});

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", null);
Assert.Equal("You must specifiy a valid role name.", exception.ParamName);
Assert.StartsWith("Environment variable roleName('ALIBABA_CLOUD_ECS_METADATA') cannot be empty", exception.Message);
}

[Fact]
Expand Down Expand Up @@ -465,11 +546,10 @@
var defaultCredentialProvider =
new DefaultCredentialProvider(profile, "publicKeyId", "privateKeyFile", rsaProvider);

var actualCredentil = defaultCredentialProvider.GetRsaKeyPairAlibabaCloudCredential();
var ex = Assert.Throws<ClientException>(() => defaultCredentialProvider.GetRsaKeyPairAlibabaCloudCredential());

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", null);
Assert.NotNull(actualCredentil);
Assert.Equal("aks", actualCredentil.GetAccessKeySecret());
Assert.Equal("The configured public_key_id or private_key_file is empty", ex.Message);
}

[Fact]
Expand All @@ -496,6 +576,14 @@
[Fact]
public void GetRsaKeyPairAlibabaCloudCredentialWithException()
{
var cacheRoleArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN");
var cacheProviderArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN");
var cacheFile = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE");

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", null);

var profile = DefaultProfile.GetProfile();
var defaultCredentialProvider = new DefaultCredentialProvider(profile, null);

Expand All @@ -504,12 +592,23 @@
var credential = defaultCredentialProvider.GetRsaKeyPairAlibabaCloudCredential();
});

Assert.Equal("Missing required variable option for 'default Client'", exception.Message);
Assert.Equal("The configured private_key_file is empty", exception.Message);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", cacheRoleArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", cacheProviderArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", cacheFile);
}

[Fact]
public void GetOIDCAlibabaCloudCredential()
{
var cacheRoleArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN");
var cacheProviderArn = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN");
var cacheFile = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE");

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", null);

var profile = DefaultProfile.GetProfile();
var defaultCredentialProvider = new DefaultCredentialProvider(profile, null);
Assert.Null(defaultCredentialProvider.GetOIDCAlibabaCloudCredential());
Expand All @@ -529,9 +628,9 @@
Assert.Equal("fakesessiontoken", credential.GetSessionToken());
Assert.False(credential.WillSoonExpire());

Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", null);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ROLE_ARN", cacheRoleArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_PROVIDER_ARN", cacheProviderArn);
Environment.SetEnvironmentVariable("ALIBABA_CLOUD_OIDC_TOKEN_FILE", cacheFile);
}
}
}
2 changes: 1 addition & 1 deletion aliyun-net-sdk-core.Tests/Units/Http/HttpResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,4 @@ public void SetContentWithEmptyHeaders()
Assert.Equal(FormatType.JSON, instance.ContentType);
}
}
}
}
5 changes: 5 additions & 0 deletions aliyun-net-sdk-core.Tests/Units/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public static string GetIniFilePath()
return Directory.GetCurrentDirectory() + slash + "credentials.ini";
}

public static string GetTestIniFilePath()
{
return HomePath + Slash + "configTest.ini";
}

public static void DeleteIniFile()
{
File.Delete(homePath + slash + ".alibabacloud" + slash + "credentials.ini");
Expand Down
11 changes: 11 additions & 0 deletions aliyun-net-sdk-core.Tests/Units/Utils/IniFileHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,16 @@ public void TestGetValue()
TestHelper.DeleteIniFile();
Assert.Empty(value);
}

[Fact]
public void TestIniReader()
{
var path = TestHelper.GetTestIniFilePath();
var iniReader = new IniReader(path);
var accessKeyId = iniReader.GetValue("access_key_id", "default");
var accessKeySecret = iniReader.GetValue("access_key_secret", "default");
Assert.Equal("foo", accessKeyId);
Assert.Equal("bar", accessKeySecret);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
<None Update="OIDCToken.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="configTest.ini">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand Down
33 changes: 33 additions & 0 deletions aliyun-net-sdk-core.Tests/configTest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[default]
enable = true
type = access_key
access_key_id = foo
access_key_secret = bar
region_id = cn-hangzhou

[client1]
enable = false
type = ecs_ram_role
role_name = EcsRamRoleTest


[client2]
enable = false
type = ram_role_arn
access_key_id = foo
access_key_secret = bar
role_arn = role_arn
role_session_name = session_name


[client3]
enable = false
type = bearer_token
bearer_token = bearer_token


[client4]
enable = false
type = rsa_key_pair
public_key_id = publicKeyId
private_key_file = /your/pk.pem
Loading
Loading