/
awsRole.go
198 lines (185 loc) · 5.32 KB
/
awsRole.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"context"
"crypto/rand"
"crypto/x509"
"fmt"
"net/http"
"strconv"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/organizations"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/Cloud-Foundations/keymaster/lib/certgen"
"github.com/Cloud-Foundations/keymaster/lib/instrumentedwriter"
)
const (
awsAccountListInterval = time.Minute * 5
)
type assumeRoleCredentialsProvider struct {
credentials aws.Credentials
roleArn *string
stsClient *sts.Client
}
func awsListAccounts(ctx context.Context, orgClient *organizations.Client) (
map[string]struct{}, error) {
list := make(map[string]struct{})
var nextToken *string
for {
output, err := orgClient.ListAccounts(ctx,
&organizations.ListAccountsInput{NextToken: nextToken})
if err != nil {
return nil, err
}
for _, account := range output.Accounts {
list[*account.Id] = struct{}{}
}
if output.NextToken == nil {
break
}
nextToken = output.NextToken
}
return list, nil
}
func (p *assumeRoleCredentialsProvider) Retrieve(ctx context.Context) (
aws.Credentials, error) {
if time.Until(p.credentials.Expires) > time.Minute {
return p.credentials, nil
}
output, err := p.stsClient.AssumeRole(ctx, &sts.AssumeRoleInput{
RoleArn: p.roleArn,
RoleSessionName: aws.String("keymaster"),
})
if err != nil {
return aws.Credentials{}, err
}
p.credentials = aws.Credentials{
AccessKeyID: *output.Credentials.AccessKeyId,
CanExpire: true,
Expires: *output.Credentials.Expiration,
SecretAccessKey: *output.Credentials.SecretAccessKey,
SessionToken: *output.Credentials.SessionToken,
}
return p.credentials, nil
}
func (state *RuntimeState) checkAwsRolesEnabled() bool {
if len(state.Config.AwsCerts.AllowedAccounts) > 0 {
return true
}
if state.Config.AwsCerts.ListAccountsRole != "" {
return true
}
return false
}
func (state *RuntimeState) configureAwsRoles() error {
if len(state.Config.AwsCerts.AllowedAccounts) > 0 {
state.Config.AwsCerts.allowedAccounts =
make(map[string]struct{})
for _, id := range state.Config.AwsCerts.AllowedAccounts {
if id != "*" {
if _, err := strconv.ParseUint(id, 10, 64); err != nil {
return fmt.Errorf("accountID: %s is not a number", id)
}
}
state.Config.AwsCerts.allowedAccounts[id] = struct{}{}
}
}
if state.Config.AwsCerts.ListAccountsRole != "" {
ctx := context.TODO()
awsConfig, err := awsconfig.LoadDefaultConfig(ctx,
awsconfig.WithEC2IMDSRegion())
if err != nil {
return err
}
credsProvider := &assumeRoleCredentialsProvider{
roleArn: aws.String(state.Config.AwsCerts.ListAccountsRole),
stsClient: sts.NewFromConfig(awsConfig),
}
awsConfig, err = awsconfig.LoadDefaultConfig(ctx,
awsconfig.WithEC2IMDSRegion(),
awsconfig.WithCredentialsProvider(credsProvider))
if err != nil {
return err
}
orgClient := organizations.NewFromConfig(awsConfig)
state.Config.AwsCerts.organisationAccounts, err =
awsListAccounts(ctx, orgClient)
if err != nil {
return err
}
state.logger.Printf("Discovered %d accounts in AWS Organisation\n",
len(state.Config.AwsCerts.organisationAccounts))
go state.refreshAwsAccounts(ctx, orgClient)
}
return nil
}
func (state *RuntimeState) checkAwsAccountAllowed(accountId string) bool {
if _, ok := state.Config.AwsCerts.allowedAccounts[accountId]; ok {
return true
}
if _, ok := state.Config.AwsCerts.organisationAccounts[accountId]; ok {
return true
}
if _, ok := state.Config.AwsCerts.allowedAccounts["*"]; ok {
return true
}
return false
}
func (state *RuntimeState) refreshAwsAccounts(ctx context.Context,
orgClient *organizations.Client) {
for {
time.Sleep(awsAccountListInterval)
if list, err := awsListAccounts(ctx, orgClient); err != nil {
state.logger.Println(err)
} else {
oldLength := len(state.Config.AwsCerts.organisationAccounts)
state.Config.AwsCerts.organisationAccounts = list
if len(list) != oldLength {
state.logger.Printf(
"Discovered %d accounts in AWS Organisation, was %d\n",
len(list), oldLength)
}
}
}
}
func (state *RuntimeState) requestAwsRoleCertificateHandler(
w http.ResponseWriter, r *http.Request) {
state.logger.Debugln(1, "Entered requestAwsRoleCertificateHandler()")
if state.sendFailureToClientIfLocked(w, r) {
return
}
cert := state.awsCertIssuer.RequestHandler(w, r)
if cert != nil {
w.(*instrumentedwriter.LoggingWriter).SetUsername(
cert.Subject.CommonName)
}
}
// Returns signed certificate DER.
func (state *RuntimeState) generateRoleCert(template *x509.Certificate,
publicKey interface{}) ([]byte, error) {
strong, err := certgen.ValidatePublicKeyStrength(publicKey)
if err != nil {
return nil, err
}
if !strong {
return nil, fmt.Errorf("key too weak")
}
caCert, err := x509.ParseCertificate(state.caCertDer)
if err != nil {
return nil, err
}
certDER, err := x509.CreateCertificate(rand.Reader, template, caCert,
publicKey, state.Signer)
if err != nil {
return nil, err
}
metricLogCertDuration("x509", "granted",
float64(time.Until(template.NotAfter).Seconds()))
go func(username string, certType string) {
metricsMutex.Lock()
defer metricsMutex.Unlock()
certGenCounter.WithLabelValues(username, certType).Inc()
}(template.Subject.CommonName, "x509")
return certDER, nil
}