示例#1
0
        public void GetCredentialFileAlibabaCloudCredentialWithRsaKey()
        {
            var basicSessionCredential = new BasicSessionCredentials("fakeak", "fakeaks", "fakesessiontoken", 4000);
            var homePath = EnvironmentUtil.GetHomePath();

            TestHelper.CreateIniFileWithRsaKey(homePath);

            var slash         = EnvironmentUtil.GetOSSlash();
            var finalLocation = EnvironmentUtil.GetComposedPath(homePath, slash);
            var configurtion  = Configuration.LoadFromFile(finalLocation);

            DefaultProfile profile = DefaultProfile.GetProfile();

            profile.DefaultClientName = "default";

            var mockDefaultCredentialProvider = new Mock <DefaultCredentialProvider>(profile, null);

            mockDefaultCredentialProvider.Setup(x => x.GetRsaKeyPairAlibabaCloudCredential()).Returns(basicSessionCredential);
            mockDefaultCredentialProvider.Setup(x => x.GetHomePath()).Returns(homePath);
            mockDefaultCredentialProvider.Setup(x => x.LoadFileFromIni(It.IsAny <string>())).Returns(configurtion);
            var defaultCredentialProvider = mockDefaultCredentialProvider.Object;

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

            TestHelper.DeleteIniFile(homePath);
            Assert.NotNull(credential);
        }
示例#2
0
        public void GetCredentialFileAlibabaCloudCredentialWithEcsRamRole()
        {
            var ecsRamRoleCredential = new InstanceProfileCredentials("fakeak", "fakeaks", "fakesession", DateTime.Now.ToString(), 4000);
            var homePath             = EnvironmentUtil.GetHomePath();

            TestHelper.CreateIniFileWithEcs(homePath);

            var slash         = EnvironmentUtil.GetOSSlash();
            var finalLocation = EnvironmentUtil.GetComposedPath(homePath, slash);
            var configuration = Configuration.LoadFromFile(finalLocation);

            DefaultProfile profile = DefaultProfile.GetProfile();

            profile.DefaultClientName = "default";

            var mockDefaultCredentialProvider = new Mock <DefaultCredentialProvider>(profile, null);

            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 = (InstanceProfileCredentials)defaultCredentialProvider.GetAlibabaCloudClientCredential();

            TestHelper.DeleteIniFile(homePath);
            Assert.NotNull(credential);
        }
示例#3
0
        public async Task <AlibabaCloudCredentials> GetCredentialFileAlibabaCloudCredentialAsync(CancellationToken cancellationToken)
        {
            if (null == credentialFileLocation)
            {
                credentialFileLocation = GetHomePath();
                var slash        = EnvironmentUtil.GetOSSlash();
                var fileLocation = EnvironmentUtil.GetComposedPath(credentialFileLocation, slash);

                if (File.Exists(fileLocation))
                {
                    credentialFileLocation = fileLocation;
                }
                else
                {
                    return(null);
                }
            }

            if (credentialFileLocation.Equals(""))
            {
                throw new ClientException(
                          "Credentials file environment variable 'ALIBABA_CLOUD_CREDENTIALS_FILE' cannot be empty");
            }

            var iniReader       = new IniReader(credentialFileLocation);
            var sectionNameList = iniReader.GetSections();

            if (null != defaultProfile.DefaultClientName)
            {
                var userDefineSectionNode = defaultProfile.DefaultClientName;

                var iniKeyTypeValue = iniReader.GetValue("type", userDefineSectionNode);

                if (iniKeyTypeValue.Equals("access_key"))
                {
                    accessKeyId     = iniReader.GetValue("access_key_id", userDefineSectionNode);
                    accessKeySecret = iniReader.GetValue("access_key_secret", userDefineSectionNode);
                    regionId        = iniReader.GetValue("region_id", userDefineSectionNode);

                    return(await GetAccessKeyCredentialAsync(cancellationToken).ConfigureAwait(false));
                }

                if (iniKeyTypeValue.Equals("ecs_ram_role"))
                {
                    roleName = iniReader.GetValue("role_name", userDefineSectionNode);
                    regionId = iniReader.GetValue("region_id", userDefineSectionNode);

                    return(await GetInstanceRamRoleAlibabaCloudCredentialAsync(cancellationToken).ConfigureAwait(false));
                }

                if (iniKeyTypeValue.Equals("ram_role_arn"))
                {
                    accessKeyId     = iniReader.GetValue("access_key_id", userDefineSectionNode);
                    accessKeySecret = iniReader.GetValue("access_key_secret", userDefineSectionNode);
                    roleArn         = iniReader.GetValue("role_arn", userDefineSectionNode);

                    return(await GetRamRoleArnAlibabaCloudCredentialAsync(cancellationToken).ConfigureAwait(false));
                }

                if (iniKeyTypeValue.Equals("rsa_key_pair"))
                {
                    publicKeyId    = iniReader.GetValue("public_key_id", userDefineSectionNode);
                    privateKeyFile = iniReader.GetValue("private_key_file", userDefineSectionNode);

                    return(await GetRsaKeyPairAlibabaCloudCredentialAsync(cancellationToken).ConfigureAwait(false));
                }
            }
            else
            {
                foreach (var sectionItem in sectionNameList)
                {
                    if (!sectionItem.Equals("default"))
                    {
                        continue;
                    }

                    accessKeyId     = iniReader.GetValue("access_key_id", "default");
                    accessKeySecret = iniReader.GetValue("access_key_secret", "default");
                    regionId        = iniReader.GetValue("region_id", "default");

                    return(await GetAccessKeyCredentialAsync(cancellationToken).ConfigureAwait(false));
                }
            }

            return(null);
        }
示例#4
0
        public AlibabaCloudCredentials GetCredentialFileAlibabaCloudCredential()
        {
            if (null == credentialFileLocation)
            {
                credentialFileLocation = GetHomePath();
                var slash        = EnvironmentUtil.GetOSSlash();
                var fileLocation = EnvironmentUtil.GetComposedPath(credentialFileLocation, slash);

                if (File.Exists(fileLocation))
                {
                    credentialFileLocation = fileLocation;
                }
                else
                {
                    return(null);
                }
            }
            if (credentialFileLocation.Equals(""))
            {
                throw new ClientException("Credentials file environment variable 'ALIBABA_CLOUD_CREDENTIALS_FILE' cannot be empty");
            }

            Configuration config;

            try
            {
                config = LoadFileFromIni(credentialFileLocation);
            }
            catch (Exception)
            {
                throw new ClientException("Invalid credentials file: " + credentialFileLocation);
            }

            ArrayList sectionNameList = new ArrayList();

            foreach (var sectionName in config)
            {
                sectionNameList.Add(sectionName.Name);
            }

            if (null != defaultProfile.DefaultClientName)
            {
                string userDefineSectionNode = defaultProfile.DefaultClientName;

                if (config[userDefineSectionNode]["type"].RawValue.Equals("access_key"))
                {
                    accessKeyId     = config[userDefineSectionNode]["access_key_id"].RawValue;
                    accessKeySecret = config[userDefineSectionNode]["access_key_secret"].RawValue;
                    regionId        = config[userDefineSectionNode]["region_id"].RawValue;

                    return(GetAccessKeyCredential());
                }

                if (config[userDefineSectionNode]["type"].RawValue.Equals("ecs_ram_role"))
                {
                    roleName = config[userDefineSectionNode]["role_name"].RawValue;
                    regionId = config[userDefineSectionNode]["region_id"].RawValue;

                    return(GetInstanceRamRoleAlibabaCloudCredential());
                }

                if (config[userDefineSectionNode]["type"].RawValue.Equals("ram_role_arn"))
                {
                    accessKeyId     = config[userDefineSectionNode]["access_key_id"].RawValue;
                    accessKeySecret = config[userDefineSectionNode]["access_key_secret"].RawValue;
                    roleArn         = config[userDefineSectionNode]["role_arn"].RawValue;

                    return(GetRamRoleArnAlibabaCloudCredential());
                }

                if (config[userDefineSectionNode]["type"].RawValue.Equals("rsa_key_pair"))
                {
                    publicKeyId    = config[userDefineSectionNode]["public_key_id"].RawValue;
                    privateKeyFile = config[userDefineSectionNode]["private_key_file"].RawValue;

                    return(GetRsaKeyPairAlibabaCloudCredential());
                }
            }
            else
            {
                foreach (string sectionItem in sectionNameList)
                {
                    if (!sectionItem.Equals("default"))
                    {
                        continue;
                    }
                    string userDefineSectionNode = "default";
                    accessKeyId     = config[userDefineSectionNode]["access_key_id"].RawValue;
                    accessKeySecret = config[userDefineSectionNode]["access_key_secret"].RawValue;
                    regionId        = config[userDefineSectionNode]["region_id"].RawValue;

                    return(GetAccessKeyCredential());
                }
            }
            return(null);
        }