Ejemplo n.º 1
0
        public void GetRsaKeyPairAlibabaCloudCredential()
        {
            Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", "region_id");

            var response = new GetSessionAccessKeyResponse();

            response.SessionAccesskey = new GetSessionAccessKeyResponse.GetSessionAccessKey_SessionAccesskey();
            response.SessionAccesskey.SessionAccessKeyId     = "ak";
            response.SessionAccesskey.SessionAccessKeySecert = "aks";

            var mockClient = new Mock <IAcsClient>();

            mockClient.Setup(x => x.GetAcsResponse(It.IsAny <AcsRequest <GetSessionAccessKeyResponse> >())).Returns(response);
            var client = mockClient.Object;

            var basicSessionCredential = new BasicSessionCredentials("accessKey", "aks", "sessionToken");
            var rsaKeyCredential       = new RsaKeyPairCredential("publicKey", "privateKey");
            var profile = DefaultProfile.GetProfile();

            var rsaProvider = new Core.Auth.Provider.RsaKeyPairCredentialProvider(rsaKeyCredential, client);

            var defaultCredentialProvider = new Core.Auth.Provider.DefaultCredentialProvider(profile, rsaProvider, "publicKeyId", "privateKeyFile");

            var actualCredentil = defaultCredentialProvider.GetRsaKeyPairAlibabaCloudCredential();

            Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", null);
            Assert.NotNull(actualCredentil);
            Assert.Equal("aks", actualCredentil.GetAccessKeySecret());
        }
Ejemplo n.º 2
0
        public void GetEnvironmentClientCredentialWithoutAKTest()
        {
            Core.Auth.Provider.DefaultCredentialProvider defaultCredentialProvider = new Core.Auth.Provider.DefaultCredentialProvider();
            var credential = (AccessKeyCredential)defaultCredentialProvider.GetEnvironmentAlibabaCloudCredential();

            Assert.Null(credential);
        }
Ejemplo n.º 3
0
        public void GetRamRoleArnAlibabaCloudCredential()
        {
            TestHelper.InitializeEnvironmentValue();

            var ramRoleCredential = new RamRoleArnCredential("accessKey", "aks", "roleArn", "roleSession", "roletoken", 3000);
            var profile           = DefaultProfile.GetProfile();

            var response = new AssumeRoleResponse();

            response.Credentials                 = new AssumeRoleResponse.AssumeRole_Credentials();
            response.Credentials.AccessKeyId     = "ak";
            response.Credentials.AccessKeySecret = "aks";
            response.Credentials.SecurityToken   = "token";

            var mockClient = new Mock <IAcsClient>();

            mockClient.Setup(x => x.GetAcsResponse(It.IsAny <AcsRequest <AssumeRoleResponse> >())).Returns(response);
            var client = mockClient.Object;

            var stsProvider = new STSAssumeRoleSessionCredentialsProvider(ramRoleCredential, "roleArn", client);
            var defaultCredentialProvider = new Core.Auth.Provider.DefaultCredentialProvider(profile, stsProvider);

            var actualCredentil = defaultCredentialProvider.GetRamRoleArnAlibabaCloudCredential();

            TestHelper.RemoveEnvironmentValue();
            Assert.NotNull(actualCredentil);
            Assert.Equal("aks", actualCredentil.GetAccessKeySecret());
        }
Ejemplo n.º 4
0
        public void GetRsaKeyPairAlibabaCloudCredentialWithException()
        {
            var profile = DefaultProfile.GetProfile();
            var defaultCredentialProvider = new Core.Auth.Provider.DefaultCredentialProvider(profile, null);

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

            Assert.Equal("Missing required variable option for 'default Client'", exception.Message);
        }
Ejemplo n.º 5
0
        public void GetEnvironmentAlibabaCloudCredentialUseChain()
        {
            DefaultProfile profile = DefaultProfile.GetProfile();

            profile.DefaultClientName = "default";

            TestHelper.InitializeEnvironmentValue();

            Core.Auth.Provider.DefaultCredentialProvider defaultCredentialProvider = new Core.Auth.Provider.DefaultCredentialProvider(profile);
            var credential = (AccessKeyCredential)defaultCredentialProvider.GetAlibabaCloudClientCredential();

            Assert.NotNull(credential);
            Assert.Equal("ACCESS_KEY_ID", credential.GetAccessKeyId());

            TestHelper.RemoveEnvironmentValue();
        }
Ejemplo n.º 6
0
        public void GetCredentialFileAlibabaCloudCredentialWithEnvDefinedButNotFoundTest()
        {
            DefaultProfile profile = DefaultProfile.GetProfile();

            Environment.SetEnvironmentVariable("ALIBABA_CLOUD_CREDENTIALS_FILE", "D://credentials1.ini");

            Core.Auth.Provider.DefaultCredentialProvider defaultCredentialProvider = new Core.Auth.Provider.DefaultCredentialProvider(profile);

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

            Assert.Equal("Invalid credentials file: D://credentials1.ini", exception.Message);
            Environment.SetEnvironmentVariable("ALIBABA_CLOUD_CREDENTIALS_FILE", null);
        }
Ejemplo n.º 7
0
        public void GetCredentialFileAlibabaCloudCredentialWithBearerToken()
        {
            DefaultProfile profile = DefaultProfile.GetProfile();

            profile.DefaultClientName = "default";
            Core.Auth.Provider.DefaultCredentialProvider defaultCredentialProvider = new Core.Auth.Provider.DefaultCredentialProvider(profile);
            var homePath = defaultCredentialProvider.GetHomePath();

            TestHelper.CreateIniFileWithBearerToken(homePath);

            var credential = (BearerTokenCredential)defaultCredentialProvider.GetAlibabaCloudClientCredential();

            TestHelper.DeleteIniFile(homePath);
            Assert.NotNull(credential);
            Assert.Null(credential.GetAccessKeyId());
            Assert.Null(credential.GetAccessKeySecret());
        }
Ejemplo n.º 8
0
        public void GetInstanceRamRoleAlibabaCloudCredential()
        {
            var profile = DefaultProfile.GetProfile();
            var defaultCredentialProvider = new Core.Auth.Provider.DefaultCredentialProvider(profile, null);

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

            Assert.Equal("RegionID cannot be null or empty.", exception.Message);

            Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", "region_id");
            profile.DefaultClientName = "test";
            defaultCredentialProvider = new Core.Auth.Provider.DefaultCredentialProvider(profile, null);
            var credentialProvider = defaultCredentialProvider.GetInstanceRamRoleAlibabaCloudCredential();

            Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", null);
            Assert.Null(credentialProvider);
        }
Ejemplo n.º 9
0
        public void GetInstanceRamRoleAlibabaCloudCredential2()
        {
            var profile = DefaultProfile.GetProfile();

            profile.DefaultClientName = "default";
            Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", "region_id");
            Environment.SetEnvironmentVariable("ALIBABA_CLOUD_ECS_METADATA", "role_name");

            var ecsRamRoleCredential = new EcsRamRoleCredential("fakeak", "fakeaks", "fakesession", DateTime.Now.ToString(), 4000);
            var mockEcsMetaService   = new Mock <ECSMetadataServiceCredentialsFetcher>();

            mockEcsMetaService.Setup(x => x.Fetch()).Returns(ecsRamRoleCredential);
            var eCSMetadataServiceCredentialsFetcher = mockEcsMetaService.Object;

            var defaultCredentialProvider = new Core.Auth.Provider.DefaultCredentialProvider(profile, eCSMetadataServiceCredentialsFetcher);
            var actualCredential          = defaultCredentialProvider.GetInstanceRamRoleAlibabaCloudCredential();

            Environment.SetEnvironmentVariable("ALIBABA_CLOUD_REGION_ID", null);
            Assert.NotNull(actualCredential);
            Assert.Equal("fakeak", actualCredential.GetAccessKeyId());
        }