public AlibabaCloudCredentials GetCredentialFileAlibabaCloudCredential()

in aliyun-net-sdk-core/Auth/Provider/DefaultCredentialProvider.cs [120:208]


        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");
            }

            var iniReader = new IniReader(credentialFileLocation);
            var sectionNameList = iniReader.GetSections();
            string userDefineSectionNode;
            if (defaultProfile != null && defaultProfile.DefaultClientName != null)
            {
                userDefineSectionNode = defaultProfile.DefaultClientName;
            }
            else
            {
                userDefineSectionNode = AuthUtils.GetClientType();
            }

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

            if (string.IsNullOrEmpty(iniKeyTypeValue))
            {
                throw new ClientException("The configured client type is empty");
            }

            if (iniKeyTypeValue.Equals("access_key"))
            {
                accessKeyId = iniReader.GetValue("access_key_id", userDefineSectionNode);
                accessKeySecret = iniReader.GetValue("access_key_secret", userDefineSectionNode);
                return GetAccessKeyCredential();
            }

            if (iniKeyTypeValue.Equals("ecs_ram_role"))
            {
                roleName = iniReader.GetValue("role_name", userDefineSectionNode);
                if (string.IsNullOrEmpty(roleName))
                {
                    throw new ClientException("The configured role_name is empty");
                }
                return GetInstanceRamRoleAlibabaCloudCredential();
            }

            if (iniKeyTypeValue.Equals("ram_role_arn"))
            {
                accessKeyId = iniReader.GetValue("access_key_id", userDefineSectionNode);
                accessKeySecret = iniReader.GetValue("access_key_secret", userDefineSectionNode);
                if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(accessKeySecret))
                {
                    throw new ClientException("The configured access_key_id or access_key_secret is empty");
                }

                roleArn = iniReader.GetValue("role_arn", userDefineSectionNode);
                var roleSessionName = iniReader.GetValue("role_session_name", userDefineSectionNode);
                if (string.IsNullOrEmpty(roleArn) || string.IsNullOrEmpty(roleSessionName))
                {
                    throw new ClientException("The configured role_session_name or role_arn is empty");
                }

                return GetRamRoleArnAlibabaCloudCredential();
            }

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

                return GetRsaKeyPairAlibabaCloudCredential();
            }

            return null;
        }