Skip to content

Commit

Permalink
fix: solve error in default credentials provider
Browse files Browse the repository at this point in the history
  • Loading branch information
PanPanZou authored and yndu13 committed Sep 25, 2024
1 parent cd5f9eb commit e58e1df
Show file tree
Hide file tree
Showing 11 changed files with 293 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ public void GetAlibabaCloudClientCredentialWithException()
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,6 +73,14 @@ Use credential chains
[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
Expand All @@ -71,6 +93,9 @@ public void GetCredentialFileAlibabaCloudCredentialWithAKType()
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,6 +106,10 @@ Use credential chains
[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);
Expand All @@ -97,12 +126,23 @@ public void GetCredentialFileAlibabaCloudCredentialWithAKTypeButAKIsEmpty()

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();
Expand All @@ -115,6 +155,37 @@ public void GetCredentialFileAlibabaCloudCredentialWithDefaultSection()
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 @@ Use credential chains
[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 @@ public void GetCredentialFileAlibabaCloudCredentialWithEcsRamRole()

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 @@ -161,11 +244,9 @@ public void GetCredentialFileAlibabaCloudCredentialWithFileAndAkExist()

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 @@ -182,10 +263,10 @@ public void GetCredentialFileAlibabaCloudCredentialWithFileAndAkExistNotDefault(

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 @@ -328,15 +409,15 @@ public void GetInstanceRamRoleAlibabaCloudCredential()
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 @@ public void GetInstanceRamRoleAlibabaCloudCredential3()
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 @@ public void GetRsaKeyPairAlibabaCloudCredential()
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 @@ public void GetRsaKeyPairAlibabaCloudCredential2()
[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 @@ public void GetRsaKeyPairAlibabaCloudCredentialWithException()
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 @@ public void GetOIDCAlibabaCloudCredential()
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

0 comments on commit e58e1df

Please sign in to comment.