forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssumeRolev2_test.go
91 lines (70 loc) · 1.88 KB
/
AssumeRolev2_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/aws/aws-sdk-go-v2/service/sts/types"
)
type STSAssumeRoleImpl struct{}
func (dt STSAssumeRoleImpl) AssumeRole(ctx context.Context,
params *sts.AssumeRoleInput,
optFns ...func(*sts.Options)) (*sts.AssumeRoleOutput, error) {
user := types.AssumedRoleUser{
Arn: aws.String("aws-docs-example-user-arn"),
AssumedRoleId: aws.String("aws-docs-example-userID"),
}
output := &sts.AssumeRoleOutput{
AssumedRoleUser: &user,
}
return output, nil
}
type Config struct {
RoleArn string `json:"RoleArn"`
SessionName string `json:"SessionName"`
}
var configFileName = "config.json"
var globalConfig Config
func populateConfiguration(t *testing.T) error {
content, err := ioutil.ReadFile(configFileName)
if err != nil {
return err
}
text := string(content)
err = json.Unmarshal([]byte(text), &globalConfig)
if err != nil {
return err
}
if globalConfig.RoleArn == "" || globalConfig.SessionName == "" {
msg := "You must specify a value for RoleArn and SessionName in " + configFileName
return errors.New(msg)
}
return nil
}
func TestAssumeRole(t *testing.T) {
thisTime := time.Now()
nowString := thisTime.Format("2006-01-02 15:04:05 Monday")
t.Log("Starting unit test at " + nowString)
err := populateConfiguration(t)
if err != nil {
t.Fatal(err)
}
api := &STSAssumeRoleImpl{}
input := &sts.AssumeRoleInput{
RoleArn: &globalConfig.RoleArn,
RoleSessionName: &globalConfig.SessionName,
}
resp, err := TakeRole(context.Background(), api, input)
if err != nil {
fmt.Println("Got an error assuming the role:")
fmt.Println(err)
return
}
t.Log("User ARN: " + *resp.AssumedRoleUser.Arn)
t.Log("User role ID: " + *resp.AssumedRoleUser.AssumedRoleId)
}