Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
[feature] role auth (#33)
Browse files Browse the repository at this point in the history
* [feature] support role auth

Allow the provider to assume a specified role instead of using a profile
from the configuration.

Tested manually, both scenarios.

* increase timeout for golangci-lint
  • Loading branch information
ryanking authored May 28, 2020
1 parent 098c8ea commit 028832e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .reviewdog.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
runner:
golangci:
cmd: ./bin/golangci-lint run --out-format=line-number
cmd: ./bin/golangci-lint run --out-format=line-number -v --timeout 5m
errorformat:
- '%E%f:%l:%c: %m'
- '%E%f:%l: %m'
Expand Down
11 changes: 10 additions & 1 deletion pkg/aws/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package aws

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
Expand All @@ -27,8 +29,15 @@ func NewClient(d *schema.ResourceData) (*Client, error) {
Profile: d.Get("profile").(string),
},
))

var creds *credentials.Credentials

if r, ok := d.Get("role_arn").(string); ok {
creds = stscreds.NewCredentials(sess, r)
}

client := &Client{
KMS: NewKMS(sess),
KMS: NewKMS(sess, creds),
}

return client, nil
Expand Down
6 changes: 4 additions & 2 deletions pkg/aws/kms.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package aws
import (
"encoding/base64"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/kms"
"github.com/aws/aws-sdk-go/service/kms/kmsiface"
Expand All @@ -15,8 +17,8 @@ type KMS struct {
}

// NewKMS returns a KMS client
func NewKMS(s *session.Session) KMS {
return KMS{kms.New(s)}
func NewKMS(s *session.Session, creds *credentials.Credentials) KMS {
return KMS{kms.New(s, &aws.Config{Credentials: creds})}
}

// EncryptBytes encrypts the plaintext using the keyID key, result is base64 encoded
Expand Down
11 changes: 8 additions & 3 deletions pkg/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ func Provider() *schema.Provider {
InputDefault: "us-east-1",
},
"profile": {
Type: schema.TypeString,
Optional: true,
Default: "",
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"role_arn"},
},
"role_arn": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"profile"},
},
},
ResourcesMap: map[string]*schema.Resource{
Expand Down

0 comments on commit 028832e

Please sign in to comment.