Skip to content

Latest commit

 

History

History
211 lines (180 loc) · 9.42 KB

README.md

File metadata and controls

211 lines (180 loc) · 9.42 KB

English | 简体中文

Alibaba Cloud Credentials for Go

Latest Stable Version Go Report Card codecov License Travis Build Status Appveyor Build Status Scrutinizer Code Quality

Alibaba Cloud Credentials for Go is a tool for Go developers to manage credentials.

This document introduces how to obtain and use Alibaba Cloud Credentials for Go.

Requirements

  • It's necessary for you to make sure your system have installed a Go environment which is new than 1.10.x.

Installation

Use go get to install SDK:

$ go get -u github.com/aliyun/credentials-go

If you use dep to manage your dependence, you can use the following command:

$ dep ensure -add  github.com/aliyun/credentials-go

Quick Examples

Before you begin, you need to sign up for an Alibaba Cloud account and retrieve your Credentials.

Credential Type

AccessKey

Setup access_key credential through User Information Management, it have full authority over the account, please keep it safe. Sometimes for security reasons, you cannot hand over a primary account AccessKey with full access to the developer of a project. You may create a sub-account RAM Sub-account , grant its authorization,and use the AccessKey of RAM Sub-account.

import (
	"github.com/aliyun/credentials-go/credentials"
)

func main(){
	config := &credentials.Config{
		Type:                  "access_key",       // Which type of credential you want
		AccessKeyId:           "AccessKeyId",      // AccessKeyId of your account
		AccessKeySecret:       "AccessKeySecret",  // AccessKeySecret of your account
    }
	akCredential, err := credentials.NewCredential(config)
	if err != nil {
		return err
	}
	accessKeyId, err := akCredential.GetAccessKeyId()
	accessSecret, err := akCredential.GetAccessSecret()
	credentialType := akCredential.GetType()
}

STS

Create a temporary security credential by applying Temporary Security Credentials (TSC) through the Security Token Service (STS).

import (
	"github.com/aliyun/credentials-go/credentials"
)

func main(){
	config := &credentials.Config{
		Type:                  "sts",              // Which type of credential you want
		AccessKeyId:           "AccessKeyId",      // AccessKeyId of your account
		AccessKeySecret:       "AccessKeySecret",  // AccessKeySecret of your account
		SecurityToken:         "SecurityToken",    // Temporary Security Token
    }
	stsCredential, err := credentials.NewCredential(config)
	if err != nil {
		return err
	}
	accessKeyId, err := stsCredential.GetAccessKeyId()
	accessSecret, err := stsCredential.GetAccessSecret()
	securityToken, err := stsCredential.GetSecurityToken()
	credentialType := stsCredential.GetType()
}

RamRoleArn

By specifying RAM Role, the credential will be able to automatically request maintenance of STS Token. If you want to limit the permissions(How to make a policy) of STS Token, you can assign value for Policy.

import (
	"github.com/aliyun/credentials-go/credentials"
)

func main(){
	config := &credentials.Config{
		Type:                   "ram_role_arn",     // Which type of credential you want
		AccessKeyId:            "AccessKeyId",      // AccessKeyId of your account
		AccessKeySecret:        "AccessKeySecret",  // AccessKeySecret of your account
		RoleArn:                "RoleArn",          // Format: acs:ram::USER_Id:role/ROLE_NAME
		RoleSessionName:        "RoleSessionName",  // Role Session Name
		Policy:                 "Policy",           // Not required, limit the permissions of STS Token
		RoleSessionExpiration:  3600,               // Not required, limit the Valid time of STS Token
    }
	arnCredential, err := credentials.NewCredential(config)
	if err != nil {
		return err
	}
	accessKeyId, err := arnCredential.GetAccessKeyId()
	accessSecret, err := arnCredential.GetAccessSecret()
	securityToken, err := arnCredential.GetSecurityToken()
	credentialType := arnCredential.GetType()
}

EcsRamRole

By specifying the role name, the credential will be able to automatically request maintenance of STS Token.

import (
	"github.com/aliyun/credentials-go/credentials"
)

func main(){
	config := &credentials.Config{
		Type:                   "ecs_ram_role",     // Which type of credential you want
		RoleName:               "RoleName",         // `roleName` is optional. It will be retrieved automatically if not set. It is highly recommended to set it up to reduce requests
    }
	ecsCredential, err := credentials.NewCredential(config)
	if err != nil {
		return err
	}
	accessKeyId, err := ecsCredential.GetAccessKeyId()
	accessSecret, err := ecsCredential.GetAccessSecret()
	securityToken, err := ecsCredential.GetSecurityToken()
	credentialType := ecsCredential.GetType()
}

RsaKeyPair

By specifying the public key Id and the private key file, the credential will be able to automatically request maintenance of the AccessKey before sending the request. Only Japan station is supported.

import (
	"github.com/aliyun/credentials-go/credentials"
)

func main(){
	config := &credentials.Config{
		Type:                   "rsa_key_pair",       // Which type of credential you want
		PrivateKeyFile:         "PrivateKeyFile",     // The file path to store the PrivateKey
		PublicKeyId:            "PublicKeyId",        // PublicKeyId of your account
    }
	rsaCredential, err := credentials.NewCredential(config)
	if err != nil {
		return err
	}
	accessKeyId, err := rsaCredential.GetAccessKeyId()
	accessSecret, err := rsaCredential.GetAccessSecret()
	securityToken, err := rsaCredential.GetSecurityToken()
	credentialType := rsaCredential.GetType()
}

Bearer Token

If credential is required by the Cloud Call Centre (CCC), please apply for Bearer Token maintenance by yourself.

import (
	"github.com/aliyun/credentials-go/credentials"
)

func main(){
	config := &credentials.Config{
		Type:                 "bearer",       // Which type of credential you want
		BearerToken:          "BearerToken",  // BearerToken of your account
    }
	bearerCredential, err := credentials.NewCredential(config)
	if err != nil {
		return err
	}
	bearerToken := bearerCredential.GetBearerToken()
	credentialType := bearerCredential.GetType()
}

Provider

If you call NewCredential() with nil, it will use provider chain to get credential for you.

1. Environment Credentials

The program first looks for environment credentials in the environment variable. If the ALICLOUD_ACCESS_KEY and ALICLOUD_SECRET_KEY environment variables are defined and are not empty, the program will use them to create the default credential. If not, the program loads and looks for the client in the configuration file.

2. Config File

If there is ~/.alibabacloud/credentials default file (Windows shows C:\Users\USER_NAME\.alibabacloud\credentials), the program will automatically create credential with the name of 'default'. The default file may not exist, but a parse error throws an exception. The specified files can also be loaded indefinitely: AlibabaCloud::load('/data/credentials', 'vfs://AlibabaCloud/credentials', ...); This configuration file can be shared between different projects and between different tools. Because it is outside the project and will not be accidentally committed to the version control. Environment variables can be used on Windows to refer to the home directory %UserProfile%. Unix-like systems can use the environment variable $HOME or ~ (tilde). The path to the default file can be modified by defining the ALIBABA_CLOUD_CREDENTIALS_FILE environment variable.

[default]                          # Default credential
type = access_key                  # Certification type: access_key
access_key_id = foo                # access key id
access_key_secret = bar            # access key secret

3. Instance RAM Role

If the environment variable ALIBABA_CLOUD_ECS_METADATA is defined and not empty, the program will take the value of the environment variable as the role name and request http://100.100.100.200/latest/meta-data/ram/security-credentials/ to get the temporary Security credential.

License

Apache-2.0

Copyright (c) 2009-present, Alibaba Cloud All rights reserved.