Esempio n. 1
0
        public void GetCredentials2()
        {
            // When Credentials is Expired

            // Mock Response
            HttpResponse response = new HttpResponse();

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

            // Mock Fetcher
            var mockFetcher = new Mock <ECSMetadataServiceCredentialsFetcher>()
            {
                CallBase = true
            };

            mockFetcher.Setup(foo => foo.GetResponse(
                                  It.IsAny <HttpRequest>()
                                  )).Returns(response);
            ECSMetadataServiceCredentialsFetcher fetcher = mockFetcher.Object;

            string roleName = ACKMock.GetRoleName(true);
            InstanceProfileCredentialsProvider instance = new InstanceProfileCredentialsProvider(roleName);
            AlibabaCloudCredentialsProvider    provider = instance;

            instance.withFetcher(fetcher);

            Assert.Throws <ClientException>(() =>
            {
                AlibabaCloudCredentials credentials = provider.GetCredentials(); // 进行有效期判断,已失效则抛出异常
            });
        }
        public void Instance()
        {
            IClientProfile   profile;
            DefaultAcsClient instance;

            instance = new DefaultAcsClient();

            profile  = DefaultProfile.GetProfile("cn-hangzhou", AKID, AKSE);
            instance = new DefaultAcsClient(profile);
            Assert.Equal("cn-hangzhou", profile.GetRegionId());
            Assert.Equal(AKID, profile.GetCredential().AccessKeyId);
            Assert.Equal(AKSE, profile.GetCredential().AccessSecret);

            var mock = new Mock <AlibabaCloudCredentials>();
            AlibabaCloudCredentials alibabaCloudCredentials = mock.Object;

            instance = new DefaultAcsClient(profile, mock.Object);
            Assert.Equal("cn-hangzhou", profile.GetRegionId());
            Assert.Equal(AKID, profile.GetCredential().AccessKeyId);
            Assert.Equal(AKSE, profile.GetCredential().AccessSecret);

            var mockProvider = new Mock <AlibabaCloudCredentialsProvider>();
            AlibabaCloudCredentialsProvider alibabaCloudCredentialsProvider = mockProvider.Object;

            instance = new DefaultAcsClient(profile, mock.Object);
            Assert.Equal("cn-hangzhou", profile.GetRegionId());
            Assert.Equal(AKID, profile.GetCredential().AccessKeyId);
            Assert.Equal(AKSE, profile.GetCredential().AccessSecret);
        }
Esempio n. 3
0
        public void GetCredentials4()
        {
            // When Credentials will Expired

            // Mock Response
            HttpResponse response        = new HttpResponse();
            string       ExpiredDatetime = DateTime.Now.AddMilliseconds(800).ToString();

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

            // Mock Credentials
            var mockCredentials = new Mock <InstanceProfileCredentials>("MockAccessKeyId", "", "", ExpiredDatetime, 100000)
            {
                CallBase = true
            };

            mockCredentials.Setup(foo => foo.RemainTicks()).Returns(15 * 1000 * 1000 * 10);
            InstanceProfileCredentials instanceProfileCredentials = mockCredentials.Object;

            instanceProfileCredentials.SetLastFailedRefreshTime();

            // Mock Fetcher
            var mockFetcher = new Mock <ECSMetadataServiceCredentialsFetcher>()
            {
                CallBase = true
            };

            mockFetcher.Setup(foo => foo.GetResponse(
                                  It.IsAny <HttpRequest>()
                                  )).Returns(response);
            mockFetcher.Setup(foo => foo.Fetch()).Returns(instanceProfileCredentials);
            ECSMetadataServiceCredentialsFetcher fetcher = mockFetcher.Object;

            string roleName = ACKMock.GetRoleName(true);
            InstanceProfileCredentialsProvider instance = new InstanceProfileCredentialsProvider(roleName);
            AlibabaCloudCredentialsProvider    provider = instance;

            instance.withFetcher(fetcher);

            //  Credentials will Expired at first. No Throw Exception (15000 >= 10000)
            AlibabaCloudCredentials credentials = provider.GetCredentials();

            Assert.Equal("MockAccessKeyId", credentials.GetAccessKeyId());

            //  Credentials will Expired at Second. Throws Exception  (5000 < 10000)
            mockCredentials.Setup(foo => foo.RemainTicks()).Returns(5000);
            instanceProfileCredentials = mockCredentials.Object;
            instanceProfileCredentials.SetLastFailedRefreshTime();
            mockFetcher.Setup(foo => foo.Fetch()).Returns(instanceProfileCredentials);
            fetcher = mockFetcher.Object;
            instance.withFetcher(fetcher);

            Assert.Throws <ClientException>(() =>
            {
                credentials = provider.GetCredentials();
            });
        }
Esempio n. 4
0
        public void GetCredentials3()
        {
            // When Credentials will Expired

            // Mock Response
            var response        = new HttpResponse();
            var ExpiredDatetime = DateTime.Now.AddMilliseconds(800).ToString();
            var content         = Encoding.GetEncoding("UTF-8").GetBytes(
                "{\"Code\":\"Success\",\"Message\":\"ThisIsMessage\",\"RequestId\":\"ThisIsRequestId\",\"AccessKeyId\":\"MockAccessKeyId\",\"AccessKeySecret\":\"\",\"SecurityToken\":\"\",\"Expiration\":\"" +
                ExpiredDatetime + "\"}");

            response.ContentType = FormatType.JSON;
            response.Content     = content;
            response.Status      = 200;

            // Mock Credentials
            var mockCredentials =
                new Mock <InstanceProfileCredentials>("MockAccessKeyId", "", "", ExpiredDatetime, 100000)
            {
                CallBase = true
            };

            mockCredentials.Setup(foo => foo.RemainTicks()).Returns(15 * 1000 * 1000 * 10);
            var instanceProfileCredentials = mockCredentials.Object;

            // Mock Fetcher
            var mockFetcher = new Mock <ECSMetadataServiceCredentialsFetcher>
            {
                CallBase = true
            };

            mockFetcher.Setup(foo => foo.GetResponse(
                                  It.IsAny <HttpRequest>()
                                  )).Returns(response);
            mockFetcher.Setup(foo => foo.Fetch()).Returns(instanceProfileCredentials);
            var fetcher = mockFetcher.Object;

            var roleName = ACKMock.GetRoleName(true);
            var instance = new InstanceProfileCredentialsProvider(roleName);
            AlibabaCloudCredentialsProvider provider = instance;

            instance.withFetcher(fetcher);

            // Throw exception if the date is invalid
            var credentials = provider.GetCredentials();

            // When Fetcher throw ClientException
            mockFetcher = new Mock <ECSMetadataServiceCredentialsFetcher>
            {
                CallBase = true
            };
            var ex = new ClientException("MockClientExceptionCode", "MockClinetExceptionMessage");

            mockFetcher.Setup(foo => foo.Fetch()).Throws(ex);
            fetcher = mockFetcher.Object;
            instance.withFetcher(fetcher);

            credentials = provider.GetCredentials();
            Assert.Equal("MockAccessKeyId", credentials.GetAccessKeyId());
        }
Esempio n. 5
0
 public STSAssumeRoleSessionCredentialsProvider(AlibabaCloudCredentialsProvider longLivedCredentialsProvider,
                                                String roleArn, IClientProfile clientProfile)
 {
     this.roleArn                    = roleArn;
     this.roleSessionName            = GetNewRoleSessionName();
     this.stsClient                  = new DefaultAcsClient(clientProfile, longLivedCredentialsProvider);
     this.roleSessionDurationSeconds = DEFAULT_DURATION_SECONDS;
 }
 public void SetCredentialsProvider(AlibabaCloudCredentialsProvider credentialsProvider)
 {
     if (credential != null)
     {
         return;
     }
     credential = new CredentialsBackupCompatibilityAdaptor(credentialsProvider);
 }
 public STSAssumeRoleSessionCredentialsProvider(
     AlibabaCloudCredentialsProvider longLivedCredentialsProvider,
     string roleArn,
     IClientProfile clientProfile)
 {
     this.roleArn    = roleArn;
     roleSessionName = GetNewRoleSessionName();
     stsClient       = new DefaultAcsClient(clientProfile, longLivedCredentialsProvider);
 }
Esempio n. 8
0
        public void InstanceProvider()
        {
            var mock = new Mock <AlibabaCloudCredentialsProvider>();
            AlibabaCloudCredentialsProvider longLivedCredentialsProvider = mock.Object;
            string         roleArn = "roleArn";
            IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", "accessKeyId", "accessKeySecret");

            STSAssumeRoleSessionCredentialsProvider instance = new STSAssumeRoleSessionCredentialsProvider(longLivedCredentialsProvider, roleArn, profile);

            Assert.NotNull(instance);
        }
Esempio n. 9
0
 public DefaultCredentialProvider(IClientProfile profile,
                                  AlibabaCloudCredentialsProvider alibabaCloudCredentialsProvider,
                                  string publicKeyId,
                                  string privateKeyFile)
 {
     defaultProfile      = profile;
     this.privateKeyFile = privateKeyFile;
     this.publicKeyId    = publicKeyId;
     this.alibabaCloudCredentialProvider = alibabaCloudCredentialsProvider;
     this.regionId = GetEnvironmentRegionId();
 }
Esempio n. 10
0
 public DefaultCredentialProvider(IClientProfile profile,
                                  AlibabaCloudCredentialsProvider alibabaCloudCredentialProvider)
 {
     this.accessKeyId                    = GetEnvironmentAccessKeyId();
     this.accessKeySecret                = GetEnvironmentAccessKeySecret();
     this.regionId                       = GetEnvironmentRegionId();
     this.credentialFileLocation         = GetEnvironmentCredentialFile();
     this.roleName                       = GetEnvironmentRoleName();
     defaultProfile                      = profile;
     this.alibabaCloudCredentialProvider = alibabaCloudCredentialProvider;
 }
Esempio n. 11
0
        public void GetCredentials1()
        {
            // When Success Get Credentials

            // Mock Response
            var response = new HttpResponse();
            var content  = Encoding.GetEncoding("UTF-8").GetBytes(
                "{\"Code\":\"Success\",\"Message\":\"ThisIsMessage\",\"RequestId\":\"ThisIsRequestId\",\"AccessKeyId\":\"MockAccessKeyId\",\"AccessKeySecret\":\"\",\"SecurityToken\":\"\",\"Expiration\":\"" +
                DateTimeMock.getNotExpiredDateTimeString() + "\"}");

            response.ContentType = FormatType.JSON;
            response.Content     = content;
            response.Status      = 200;

            // Mock Credentials
            var mockCredentials = new Mock <InstanceProfileCredentials>("MockAccessKeyId", "MockAccessKeySecret",
                                                                        "MockSecurityToken", DateTimeMock.getNotExpiredDateTimeString(), 100000)
            {
                CallBase = true
            };

            mockCredentials.Setup(foo => foo.RemainTicks()).Returns(15 * 1000 * 1000 * 10);
            var instanceProfileCredentials = mockCredentials.Object;

            // Mock Fetcher
            var mockFetcher = new Mock <ECSMetadataServiceCredentialsFetcher>
            {
                CallBase = true
            };

            mockFetcher.Setup(foo => foo.GetResponse(
                                  It.IsAny <HttpRequest>()
                                  )).Returns(response);
            mockFetcher.Setup(foo => foo.Fetch()).Returns(instanceProfileCredentials);
            var fetcher = mockFetcher.Object;

            var roleName = ACKMock.GetRoleName(true);
            var instance = new InstanceProfileCredentialsProvider(roleName);

            instance.withFetcher(fetcher);
            AlibabaCloudCredentialsProvider provider = instance;

            var credentials = provider.GetCredentials();

            Assert.Equal("MockAccessKeyId", credentials.GetAccessKeyId());
        }
Esempio n. 12
0
        public void SetCredentialsProvider()
        {
            DefaultProfile.ClearDefaultProfile();
            Credential     credential;
            DefaultProfile profile = DefaultProfile.GetProfile();
            var            mock    = new Mock <AlibabaCloudCredentialsProvider>();
            AlibabaCloudCredentialsProvider provider = mock.Object;

            profile.SetCredentialsProvider(provider); // 不为null时,直接回调,即此操作无效
            credential = profile.GetCredential();     // 会执行credential初始化
            Assert.NotNull(credential);
            Assert.IsType <CredentialsBackupCompatibilityAdaptor>(credential);

            profile.SetCredentialsProvider(null); // 会执行credential初始化
            credential = profile.GetCredential(); // 不会执行credential初始化
            Assert.NotNull(credential);
            Assert.IsType <CredentialsBackupCompatibilityAdaptor>(credential);
        }
 public DefaultAcsClient(IClientProfile profile) : this()
 {
     clientProfile       = profile;
     credentialsProvider = new StaticCredentialsProvider(profile);
     clientProfile.SetCredentialsProvider(credentialsProvider);
 }
 public DefaultAcsClient(IClientProfile profile)
 {
     this.clientProfile       = profile;
     this.credentialsProvider = new StaticCredentialsProvider(profile);
     this.clientProfile.SetCredentialsProvider(this.credentialsProvider);
 }
 public CredentialsBackupCompatibilityAdaptor(AlibabaCloudCredentialsProvider provider)
 {
     this.provider = provider;
 }
 public ConsoleAcsClient(IClientProfile profile, AlibabaCloudCredentialsProvider credentialsProvider) : base(profile, credentialsProvider)
 {
 }
 public DefaultAcsClient(IClientProfile profile, AlibabaCloudCredentials credentials) : this()
 {
     clientProfile       = profile;
     credentialsProvider = new StaticCredentialsProvider(credentials);
     clientProfile.SetCredentialsProvider(credentialsProvider);
 }
 public DefaultAcsClient(IClientProfile profile, AlibabaCloudCredentialsProvider credentialsProvider) : this()
 {
     clientProfile            = profile;
     this.credentialsProvider = credentialsProvider;
     clientProfile.SetCredentialsProvider(this.credentialsProvider);
 }