17215: add IAM role support to arvados-dispatch-cloud on EC2.
authorWard Vandewege <ward@curii.com>
Wed, 20 Jan 2021 21:16:11 +0000 (16:16 -0500)
committerWard Vandewege <ward@curii.com>
Wed, 20 Jan 2021 21:16:11 +0000 (16:16 -0500)
Arvados-DCO-1.1-Signed-off-by: Ward Vandewege <ward@curii.com>

doc/install/crunch2-cloud/install-dispatch-cloud.html.textile.liquid
lib/cloud/ec2/ec2.go
lib/config/config.default.yml
lib/config/generated_config.go

index a2186a42fe75819533a2d207d1324fd598de8e88..51d4f8fbcff8e7be2d45ada95cffc532dffd0558 100644 (file)
@@ -82,8 +82,12 @@ The <span class="userinput">ImageID</span> value is the compute node image that
         ImageID: <span class="userinput">ami-01234567890abcdef</span>
         Driver: ec2
         DriverParameters:
+          # If you are not using an IAM role for authentication, specify access
+          # credentials here. Otherwise, omit or set AccessKeyID and
+          # SecretAccessKey to an empty value.
           AccessKeyID: XXXXXXXXXXXXXXXXXXXX
           SecretAccessKey: YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
+
           SecurityGroupIDs:
           - sg-0123abcd
           SubnetID: subnet-0123abcd
index b20dbfcc986f764e78c51dead8a4ec11d427516a..1e0de74024f52851ebe4eb08c0414617d0bdc7db 100644 (file)
@@ -19,6 +19,8 @@ import (
        "git.arvados.org/arvados.git/sdk/go/arvados"
        "github.com/aws/aws-sdk-go/aws"
        "github.com/aws/aws-sdk-go/aws/credentials"
+       "github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
+       "github.com/aws/aws-sdk-go/aws/ec2metadata"
        "github.com/aws/aws-sdk-go/aws/session"
        "github.com/aws/aws-sdk-go/service/ec2"
        "github.com/sirupsen/logrus"
@@ -65,12 +67,19 @@ func newEC2InstanceSet(config json.RawMessage, instanceSetID cloud.InstanceSetID
        if err != nil {
                return nil, err
        }
-       awsConfig := aws.NewConfig().
-               WithCredentials(credentials.NewStaticCredentials(
-                       instanceSet.ec2config.AccessKeyID,
-                       instanceSet.ec2config.SecretAccessKey,
-                       "")).
-               WithRegion(instanceSet.ec2config.Region)
+
+       sess, err := session.NewSession()
+       if err != nil {
+               return nil, err
+       }
+       // First try any static credentials, fall back to an IAM instance profile/role
+       creds := credentials.NewChainCredentials(
+               []credentials.Provider{
+                       &credentials.StaticProvider{Value: credentials.Value{AccessKeyID: instanceSet.ec2config.AccessKeyID, SecretAccessKey: instanceSet.ec2config.SecretAccessKey}},
+                       &ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(sess)},
+               })
+
+       awsConfig := aws.NewConfig().WithCredentials(creds).WithRegion(instanceSet.ec2config.Region)
        instanceSet.client = ec2.New(session.Must(session.NewSession(awsConfig)))
        instanceSet.keys = make(map[string]string)
        if instanceSet.ec2config.EBSVolumeType == "" {
index 2aa53a4329a9091ef1c5d10a91c0f134568f314a..f464c7421c9b1a754cc2cb668939098ebf227dda 100644 (file)
@@ -1042,7 +1042,7 @@ Clusters:
         # Cloud-specific driver parameters.
         DriverParameters:
 
-          # (ec2) Credentials.
+          # (ec2) Credentials. Omit or leave blank if using IAM role.
           AccessKeyID: ""
           SecretAccessKey: ""
 
index 34f0a0c92b24d0c8f28a10b4fb1da66c0717edca..514fafaded7371d7f320bca5d1feacb0c5e32497 100644 (file)
@@ -1048,7 +1048,7 @@ Clusters:
         # Cloud-specific driver parameters.
         DriverParameters:
 
-          # (ec2) Credentials.
+          # (ec2) Credentials. Omit or leave blank if using IAM role.
           AccessKeyID: ""
           SecretAccessKey: ""