Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions eth/signer_awskms.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/asn1"
"encoding/hex"
"fmt"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"math/big"
"os"
"strings"
Expand Down Expand Up @@ -41,7 +43,12 @@ func NewKmsSigner(region, keyAlias, awsKey, awsSec string, chainId *big.Int) (*K
cfg := &aws.Config{
Region: aws.String(region),
}
if awsKey != "" && awsSec != "" {
if awsKey == "profile" {
cfg.Credentials = credentials.NewSharedCredentials("", awsSec)
} else if awsKey == "iam" {
// force use iam role, ignore cre or env
cfg.Credentials = ec2rolecreds.NewCredentialsWithClient(ec2metadata.New(session.Must(session.NewSession())))
} else if awsKey != "" && awsSec != "" {
cfg.Credentials = credentials.NewStaticCredentials(awsKey, awsSec, "")
}
sess, err := session.NewSession(cfg)
Expand Down Expand Up @@ -183,8 +190,10 @@ func padBigInt(i *big.Int) []byte {
// passphrase will be awsKey:awsSec or if empty, will use aws auto search env variable etc
// otherwise normal ks json file based signer
const awskmsPre = "awskms:"
const awsCreProfilePre = "profile"

// return signer, address
// if use profile, passphrase should be "profile:default" or "profile:xxx"
func CreateSigner(ksfile, passphrase string, chainid *big.Int) (Signer, common.Address, error) {
if strings.HasPrefix(ksfile, awskmsPre) {
kmskeyinfo := strings.SplitN(ksfile, ":", 3)
Expand All @@ -195,7 +204,7 @@ func CreateSigner(ksfile, passphrase string, chainid *big.Int) (Signer, common.A
if passphrase != "" {
awskeysec = strings.SplitN(passphrase, ":", 2)
if len(awskeysec) != 2 {
return nil, common.Address{}, fmt.Errorf("%s has wrong format, expected '<awsKey>:<awsSecret>'", passphrase)
return nil, common.Address{}, fmt.Errorf("%s has wrong format, expected '<awsKey>:<awsSecret>' or 'profile:<profile-name>'", passphrase)
}
}
kmsSigner, err := NewKmsSigner(kmskeyinfo[1], kmskeyinfo[2], awskeysec[0], awskeysec[1], chainid)
Expand Down
Loading