-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhandler.go
159 lines (131 loc) · 4.46 KB
/
handler.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
package main
import (
"context"
"fmt"
"time"
"github.com/hamstah/awstools/common"
"github.com/hashicorp/go-uuid"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
)
type SignSSHKeyResponse struct {
Certificate string `json:"certificate"`
Duration int `json:"duration"`
ValidBefore time.Time `json:"valid_before"`
}
type SignSSHKeyEvent struct {
IdentityURL string `json:"identity_url"`
Environment string `json:"environment"`
SSHPublicKey string `json:"ssh_public_key"`
Duration *int `json:"duration"`
SourceAddresses []string `json:"source_addresses"`
}
func (event SignSSHKeyEvent) Validate() error {
if event.IdentityURL == "" {
return errors.New("'identity_url' not found in Event.")
}
if event.Environment == "" {
return errors.New("'environment' not found in Event.")
}
if event.SSHPublicKey == "" {
return errors.New("'ssh_public_key' not found in Event.")
}
if event.Duration != nil && *event.Duration < 1 {
return errors.New("'duration' must be >= 1.")
}
// source address is validated later
return nil
}
type HandlerFunc func(ctx context.Context, event SignSSHKeyEvent) (*SignSSHKeyResponse, error)
func Handler(sessionFlags *common.SessionFlags, configFilenameTemplate string, identityURLMaxAge time.Duration) HandlerFunc {
return func(ctx context.Context, event SignSSHKeyEvent) (*SignSSHKeyResponse, error) {
// check event
err := event.Validate()
if err != nil {
return nil, errors.Wrap(err, "Invalid event")
}
// get config
environment, err := LoadEnvironment(sessionFlags, configFilenameTemplate, event.Environment)
if err != nil {
return nil, errors.Wrap(err, "Failed to load the environment config")
}
// check source addresses
sourceAddresses, err := ValidateIPRanges(event.SourceAddresses, environment.SourceAddresses)
if err != nil {
return nil, errors.Wrap(err, "Invalid source_addresses requested")
}
// check duration
duration := environment.ValidityMaxDuration
if event.Duration != nil {
duration = event.Duration
}
if *duration > *environment.ValidityMaxDuration {
return nil, errors.New("Requested duration exceeds the maximum duration for the environment.")
}
// check caller
identity, err := common.STSFetchIdentityURL(event.IdentityURL, identityURLMaxAge)
if err != nil {
return nil, errors.Wrap(err, "Failed to verify caller identity")
}
userARN, err := common.ParseARN(*identity.Arn)
if err != nil {
return nil, errors.Wrap(err, "Failed to parse the identity ARN")
}
if userARN.ResourceType != "user" {
return nil, errors.New("Caller identity should be an IAM user.")
}
signer := Signer{}
err = signer.Init(
[]byte(environment.CA.PrivateKey),
[]byte(environment.CA.PrivateKeyPassphrase),
[]byte(environment.CA.PublicKey),
time.Duration(*environment.ValidityStartOffset)*time.Second,
time.Duration(*duration)*time.Second,
)
if err != nil {
return nil, errors.Wrap(err, "Failed to initialise signer")
}
keyUUID, err := uuid.GenerateUUID()
if err != nil {
return nil, errors.Wrap(err, "Failed to generate key UUID")
}
principals := []string{userARN.Resource}
keyID := fmt.Sprintf("%s/%s", *identity.Arn, keyUUID)
certificate, err := signer.Sign([]byte(event.SSHPublicKey), keyID, principals, sourceAddresses)
if err != nil {
return nil, errors.Wrap(err, "Failed to generate certificate")
}
marshaledCertificate := ssh.MarshalAuthorizedKey(certificate)
if len(marshaledCertificate) == 0 {
return nil, errors.New("failed to marshal signed certificate, empty result")
}
userIdentity := map[string]string{
"type": "IAMUser",
"principalId": "",
"arn": *identity.Arn,
"accountId": userARN.AccountID,
"accessKeyId": "",
"userName": userARN.Resource,
}
validBefore := time.Unix(int64(certificate.ValidBefore), 0).UTC()
validAfter := time.Unix(int64(certificate.ValidAfter), 0).UTC()
certificateLog := map[string]interface{}{
"key_id": keyID,
"valid_before": validBefore,
"valid_after": validAfter,
"principals": principals,
}
log.WithFields(log.Fields{
"userIdentity": userIdentity,
"ssh_public_key": event.SSHPublicKey,
"environment": event.Environment,
"certificate": certificateLog,
}).Info("Generated SSH Key certificate")
return &SignSSHKeyResponse{
Certificate: string(marshaledCertificate),
Duration: *duration,
ValidBefore: validBefore,
}, nil
}
}