Exemple #1
0
        public void GetCredentialFileAlibabaCloudCredentialWithEcsRamRole()
        {
            var ecsRamRoleCredential = new EcsRamRoleCredential("fakeak", "fakeaks", "fakesession", DateTime.Now.ToString(), 4000);
            var homePath             = (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.Unix) ?
                                       Environment.GetEnvironmentVariable("HOME") :
                                       Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%");

            TestHelper.CreateIniFileWithEcs(homePath);

            var slash         = (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.Unix) ? "/" : "\\";
            var finalLocation = homePath + slash + ".alibabacloud" + slash + "credentials.ini";
            var configuration = Configuration.LoadFromFile(finalLocation);

            DefaultProfile profile = DefaultProfile.GetProfile();

            profile.DefaultClientName = "default";

            Mock <Core.Auth.Provider.DefaultCredentialProvider> mockDefaultCredentialProvider = new Mock <Core.Auth.Provider.DefaultCredentialProvider>(profile);

            mockDefaultCredentialProvider.Setup(x => x.GetInstanceRamRoleAlibabaCloudCredential()).Returns(ecsRamRoleCredential);
            mockDefaultCredentialProvider.Setup(x => x.GetHomePath()).Returns(homePath);
            mockDefaultCredentialProvider.Setup(x => x.LoadFileFromIni(It.IsAny <string>())).Returns(configuration);
            var defaultCredentialProvider = mockDefaultCredentialProvider.Object;
            var credential = (EcsRamRoleCredential)defaultCredentialProvider.GetAlibabaCloudClientCredential();

            TestHelper.DeleteIniFile(homePath);
            Assert.NotNull(credential);
        }
        public void FetchRetry()
        {
            var mock = new Mock <ECSMetadataServiceCredentialsFetcher>()
            {
                CallBase = true
            };

            Exception e = new Exception();

            mock.Setup(foo => foo.GetResponse(
                           It.IsAny <HttpRequest>()
                           )).Throws(e);

            ECSMetadataServiceCredentialsFetcher instance = mock.Object;

            Assert.Throws <ClientException>(
                () =>
            {
                EcsRamRoleCredential credentials = instance.Fetch(3);
            }
                );

            Action retryAction = () =>
            {
                EcsRamRoleCredential credentials = instance.Fetch(-1);;
            };
            var exception = Record.Exception(retryAction);

            Assert.Equal("Failed to connect ECS Metadata Service: Max retry times exceeded.", exception.Message);
        }
        public void Fetch4()
        {
            // When Code != "Success"
            var mock = new Mock <ECSMetadataServiceCredentialsFetcher>()
            {
                CallBase = true
            };

            HttpResponse response = new HttpResponse();

            byte[] content = Encoding.GetEncoding("UTF-8").GetBytes("{\"Code\":\"NotSuccess\",\"Message\":\"ThisIsMessage\",\"RequestId\":\"ThisIsRequestId\",\"AccessKeyId\":\"MockAccessKeyId\",\"AccessKeySecret\":\"\",\"SecurityToken\":\"\",\"Expiration\":\"" + DateTimeMock.getNowDateTimeString() + "\"}");
            response.ContentType = FormatType.JSON;
            response.Content     = content;
            response.Status      = 200;

            Exception e = new Exception();

            mock.Setup(foo => foo.GetResponse(
                           It.IsAny <HttpRequest>()
                           )).Returns(response);

            ECSMetadataServiceCredentialsFetcher instance = mock.Object;

            Assert.Throws <ClientException>(
                () =>
            {
                EcsRamRoleCredential credentials = instance.Fetch();;
            }
                );
        }
Exemple #4
0
        public void EcsRamRoleProviderClientTest()
        {
            EcsRamRoleCredentialProvider providerConfig = new EcsRamRoleCredentialProvider(
                new Config()
            {
                RoleName = "roleName"
            });

            Assert.Equal("roleName", providerConfig.RoleName);

            providerConfig = new EcsRamRoleCredentialProvider(
                new Config()
            {
                RoleName = "roleName", ConnectTimeout = 1100, Timeout = 1200
            });
            Mock <IConnClient> mock         = new Mock <IConnClient>();
            HttpResponse       httpResponse = new HttpResponse("http://www.aliyun.com");

            mock.Setup(p => p.DoAction(It.IsAny <HttpRequest>())).Returns(httpResponse);
            Assert.Throws <CredentialException>(() =>
            {
                TestHelper.RunInstanceMethod(typeof(EcsRamRoleCredentialProvider), "CreateCredential", providerConfig, new object[] { mock.Object });
            });

            httpResponse = new HttpResponse("http://www.aliyun.com")
            {
                Status = 200
            };
            mock.Setup(p => p.DoAction(It.IsAny <HttpRequest>())).Returns(httpResponse);
            Assert.Throws <CredentialException>(() =>
            {
                TestHelper.RunInstanceMethod(typeof(EcsRamRoleCredentialProvider), "CreateCredential", providerConfig, new object[] { mock.Object });
            });

            httpResponse = new HttpResponse("http://www.aliyun.com")
            {
                Status = 200, Encoding = "UTF-8", ContentType = FormatType.Json, Content = Encoding.UTF8.GetBytes("{\"Code\":\"fail\",  \"AccessKeyId\":\"test\", \"AccessKeySecret\":\"test\"}")
            };
            mock.Setup(p => p.DoAction(It.IsAny <HttpRequest>())).Returns(httpResponse);
            Assert.Throws <CredentialException>(() =>
            {
                TestHelper.RunInstanceMethod(typeof(EcsRamRoleCredentialProvider), "CreateCredential", providerConfig, new object[] { mock.Object });
            });

            httpResponse = new HttpResponse("http://www.aliyun.com")
            {
                Status = 200, Encoding = "UTF-8", ContentType = FormatType.Json, Content = Encoding.UTF8.GetBytes("{\"Code\":\"Success\",  \"AccessKeyId\":\"test\", \"AccessKeySecret\":\"test\", \"SecurityToken\":\"test\",  \"Expiration\":\"2019-08-08T1:1:1Z\"}")
            };
            mock.Setup(p => p.DoAction(It.IsAny <HttpRequest>())).Returns(httpResponse);

            EcsRamRoleCredential credential = (EcsRamRoleCredential)TestHelper.RunInstanceMethod(typeof(EcsRamRoleCredentialProvider), "CreateCredential", providerConfig, new object[] { mock.Object });

            Assert.NotNull(credential);
        }
        public async Task TestEcsRamRoleCredentialAsync()
        {
            Mock <IAlibabaCloudCredentialsProvider> mockProvider = new Mock <IAlibabaCloudCredentialsProvider>();

            mockProvider.Setup(p => p.GetCredentialsAsync()).ReturnsAsync(new EcsRamRoleCredential("accessKeyId", "accessKeySecret", "securityToken", 64090527132000L, null));
            EcsRamRoleCredential ecsRamRoleCredential = new EcsRamRoleCredential("accessKeyId", "accessKeySecret", "securityToken", 64090527132000L, mockProvider.Object);

            Assert.Equal("accessKeyId", await ecsRamRoleCredential.GetAccessKeyIdAsync());
            Assert.Equal("accessKeySecret", await ecsRamRoleCredential.GetAccessKeySecretAsync());
            Assert.Equal("securityToken", await ecsRamRoleCredential.GetSecurityTokenAsync());
            Assert.Equal(64090527132000L, await ecsRamRoleCredential.GetExpirationAsync());
            Assert.Equal(AuthConstant.EcsRamRole, await ecsRamRoleCredential.GetCredentialTypeAsync());
            Assert.NotNull(await ecsRamRoleCredential.GetNewCredentialAsync <EcsRamRoleCredential>());
        }
        public void TestEcsRamRoleCredential()
        {
            Mock <IAlibabaCloudCredentialsProvider> mockProvider = new Mock <IAlibabaCloudCredentialsProvider>();

            mockProvider.Setup(p => p.GetCredentials()).Returns(new EcsRamRoleCredential("accessKeyId", "accessKeySecret", "securityToken", 64090527132000L, null));
            EcsRamRoleCredential ecsRamRoleCredential = new EcsRamRoleCredential("accessKeyId", "accessKeySecret", "securityToken", 64090527132000L, mockProvider.Object);

            Assert.Equal("accessKeyId", ecsRamRoleCredential.GetAccessKeyId());
            Assert.Equal("accessKeySecret", ecsRamRoleCredential.GetAccessKeySecret());
            Assert.Equal("securityToken", ecsRamRoleCredential.GetSecurityToken());
            Assert.Equal(64090527132000L, ecsRamRoleCredential.GetExpiration());
            Assert.Equal(AuthConstant.EcsRamRole, ecsRamRoleCredential.GetCredentialType());
            Assert.NotNull(ecsRamRoleCredential.GetNewCredential <EcsRamRoleCredential>());
        }
Exemple #7
0
        public void TestEcsRamRoleCredentialShouldRefresh(long expiration)
        {
            Mock <IAlibabaCloudCredentialsProvider> mockProvider = new Mock <IAlibabaCloudCredentialsProvider>();

            mockProvider.Setup(p => p.GetCredentials()).Returns(new EcsRamRoleCredential("accessKeyId", "accessKeySecret", "securityToken", 64090527132000L, null));
            EcsRamRoleCredential ecsRamRoleCredential = new EcsRamRoleCredential("accessKeyId", "accessKeySecret", "securityToken", expiration, mockProvider.Object);

            if (expiration == 0)
            {
                Assert.True(ecsRamRoleCredential.WithShouldRefresh());
            }
            else
            {
                Assert.False(ecsRamRoleCredential.WithShouldRefresh());
            }
            ecsRamRoleCredential.RefreshCredential();
        }
Exemple #8
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());
        }