-
Notifications
You must be signed in to change notification settings - Fork 9
/
session.go
147 lines (120 loc) · 4.14 KB
/
session.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package aws
import (
"context"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/sts"
"google.golang.org/protobuf/encoding/prototext"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/std/cfg"
"namespacelabs.dev/foundation/universe/aws/configuration"
)
const identityTokenEnv = "AWS_WEB_IDENTITY_TOKEN_FILE"
var confConfigType = cfg.DefineConfigType[*configuration.Configuration]("foundation.providers.aws.Conf")
func hasWebIdentityEnvVar() bool {
// Check if we run inside an AWS cluster with a configured IAM role.
token := os.Getenv(identityTokenEnv)
return token != ""
}
func ConfiguredSession(ctx context.Context, cfg cfg.Configuration) (*Session, error) {
return configuredSession(ctx, cfg)
}
func configuredSession(ctx context.Context, cfg cfg.Configuration) (*Session, error) {
makeSession, conf, err := innerSession(ctx, cfg)
if err != nil {
return nil, err
}
if makeSession == nil {
return nil, nil
}
session, err := makeSession()
if err != nil {
return nil, err
}
if conf.AssumeRoleArn != "" {
stsclient := sts.NewFromConfig(session)
assumedSession, err := makeSession(config.WithCredentialsProvider(
aws.NewCredentialsCache(stscreds.NewAssumeRoleProvider(stsclient, conf.AssumeRoleArn))))
if err != nil {
return nil, err
}
return &Session{aws: assumedSession, conf: conf}, nil
}
return &Session{aws: session, conf: conf}, nil
}
func currentConfiguration(cfg cfg.Configuration) *configuration.Configuration {
if conf, ok := confConfigType.CheckGet(cfg); ok {
return conf
}
if hasWebIdentityEnvVar() {
return &configuration.Configuration{UseInjectedWebIdentity: true}
}
return nil
}
func innerSession(ctx context.Context, cfg cfg.Configuration) (func(...func(*config.LoadOptions) error) (aws.Config, error), *configuration.Configuration, error) {
conf := currentConfiguration(cfg)
if conf == nil {
return nil, nil, nil
}
if conf.GetUseInjectedWebIdentity() {
if !hasWebIdentityEnvVar() {
return nil, nil, fnerrors.BadInputError("aws: use_injected_web_identity was specified but no %q env var found", identityTokenEnv)
}
return func(opts ...func(*config.LoadOptions) error) (aws.Config, error) {
return config.LoadDefaultConfig(ctx, opts...)
}, conf, nil
}
if conf.Static != nil {
return func(opts ...func(*config.LoadOptions) error) (aws.Config, error) {
opts = append(opts, config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
conf.Static.AccessKeyId,
conf.Static.SecretAccessKey,
conf.Static.SessionToken,
)))
if conf.Region != "" {
opts = append(opts, config.WithRegion(conf.Region))
}
return config.LoadDefaultConfig(ctx, opts...)
}, conf, nil
}
return func(opts ...func(*config.LoadOptions) error) (aws.Config, error) {
opts = append(opts, config.WithSharedConfigProfile(conf.Profile))
if conf.Region != "" {
opts = append(opts, config.WithRegion(conf.Region))
}
return config.LoadDefaultConfig(ctx, opts...)
}, conf, nil
}
type Session struct {
aws aws.Config
conf *configuration.Configuration
}
func (s *Session) Config() aws.Config { return s.aws }
func (s *Session) CacheKey() string { return prototext.Format(s.conf) }
func (s *Session) RefreshUsage() string {
if s.conf.UseInjectedWebIdentity {
return ""
}
if s.conf.Profile == "" {
return "Try running `aws sso login`."
}
return fmt.Sprintf("Try running `aws --profile %s sso login`.", s.conf.Profile)
}
// MustConfiguredSession also returns a cache key.
func MustConfiguredSession(ctx context.Context, cfg cfg.Configuration) (*Session, error) {
session, err := configuredSession(ctx, cfg)
if err != nil {
return nil, err
}
if session == nil {
return nil, fnerrors.UsageError("Run `ns prepare`.", "Namespace has not been configured to access AWS.")
}
return session, nil
}