-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
188 lines (154 loc) · 5.64 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"time"
kingpin "github.com/alecthomas/kingpin/v2"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/hamstah/awstools/common"
"github.com/mitchellh/go-homedir"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)
var (
lambdaARN = kingpin.Flag("lambda-arn", "ARN of the lambda function signing the SSH key.").Required().String()
sshPrivateKeyFilename = kingpin.Flag("ssh-private-key-filename", "Path to the SSH key to add to the agent.").String()
sshPublicKeyFilename = kingpin.Flag("ssh-public-key-filename", "Path to the SSH key to sign.").String()
environment = kingpin.Flag("environment", "Name of the environment to sign the key for.").Default("").String()
duration = kingpin.Flag("duration", "Duration of validity of the signature.").Default("1m").Duration()
dump = kingpin.Flag("dump", "Dump the event JSON instead of calling lambda.").Default("false").Bool()
output = kingpin.Flag("output", "Where to store the generated certificate.").Default("agent").Enum("agent", "stdout")
sourceAddresses = kingpin.Flag("source-address", "Set the IP restriction on the cert in CIDR format, can be repeated.").Strings()
proxyConfig = kingpin.Flag("proxy-config", "Configuration for the ssh ProxyCommand host:port.").String()
)
var (
defaultSSHKeyLocations = []string{"~/.ssh/id_ecdsa", "~/.ssh/id_rsa"}
)
type SignSSHKeyResponse struct {
Certificate string `json:"certificate"`
Duration int `json:"duration"`
ValidBefore time.Time `json:"valid_before"`
}
type LambdaPayload struct {
IdentityURL string `json:"identity_url"`
Environment string `json:"environment"`
SSHPublicKey string `json:"ssh_public_key"`
Duration time.Duration `json:"duration"`
SourceAddresses []string `json:"source_addresses"`
}
func HandleOptionalArgs() {
if len(*sshPrivateKeyFilename) == 0 {
found := false
for _, defaultSSHKeyLocation := range defaultSSHKeyLocations {
detected, err := homedir.Expand(defaultSSHKeyLocation)
if err != nil {
continue
}
if _, err := os.Stat(detected); err != nil {
continue
}
*sshPrivateKeyFilename = detected
found = true
break
}
if !found {
common.Fatalln("could not find the default private SSH key")
}
log.Info("Using default SSH key", *sshPrivateKeyFilename)
}
if len(*sshPublicKeyFilename) == 0 {
*sshPublicKeyFilename = fmt.Sprintf("%s.pub", *sshPrivateKeyFilename)
}
}
func ConnectIO(con net.Conn) {
c := make(chan int64)
copy := func(r io.ReadCloser, w io.WriteCloser) {
defer func() {
r.Close()
w.Close()
}()
n, _ := io.Copy(w, r)
c <- n
}
go copy(con, os.Stdout)
go copy(os.Stdin, con)
<-c
<-c
}
func main() {
kingpin.CommandLine.Name = "iam-request-ssh-key-signature"
kingpin.CommandLine.Help = "Request a signature for a SSH key from lambda-sign-ssh-key."
flags := common.HandleFlags()
HandleOptionalArgs()
sshPublicKeyBytes, err := os.ReadFile(*sshPublicKeyFilename)
common.FatalOnErrorW(err, "could not read the SSH public key")
userSession, err := common.NewSession("")
common.FatalOnErrorW(err, "could not open user IAM session")
stsClient := sts.New(userSession)
url, err := common.STSGetIdentityURL(stsClient)
common.FatalOnErrorW(err, "could not generate the STS signed URL")
session, conf := common.OpenSession(flags)
lambdaPayload := LambdaPayload{
IdentityURL: url,
Environment: *environment,
SSHPublicKey: string(sshPublicKeyBytes),
Duration: *duration / time.Second,
SourceAddresses: *sourceAddresses,
}
lambdaPayloadBytes, err := json.Marshal(lambdaPayload)
common.FatalOnErrorW(err, "could not encode the lambda payload")
if *dump {
fmt.Println(string(lambdaPayloadBytes))
os.Exit(0)
}
lambdaClient := lambda.New(session, conf)
ret, err := lambdaClient.Invoke(&lambda.InvokeInput{
FunctionName: lambdaARN,
Payload: lambdaPayloadBytes,
})
common.FatalOnErrorW(err, "could not invoke the lambda function")
response := SignSSHKeyResponse{}
err = json.Unmarshal(ret.Payload, &response)
common.FatalOnErrorW(err, "could not parse the signature response")
switch *output {
case "agent":
parsedCert, _, _, _, err := ssh.ParseAuthorizedKey([]byte(response.Certificate))
common.FatalOnErrorW(err, "could not parse the certificate from the response")
cert, ok := parsedCert.(*ssh.Certificate)
if !ok {
common.Fatalln("failed to parse response certificate")
}
privateKeyBytes, err := ioutil.ReadFile(*sshPrivateKeyFilename)
common.FatalOnErrorW(err, "could not read the private SSH key")
privateKey, err := ssh.ParseRawPrivateKey(privateKeyBytes)
common.FatalOnErrorW(err, "could not parse the private SSH key")
sock, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
common.FatalOnErrorW(err, "could not connect to the SSH agent socket, make sure SSH_AUTH_SOCK is set")
defer sock.Close()
sshAgent := agent.NewClient(sock)
pubcert := agent.AddedKey{
Comment: fmt.Sprintf("iam-request-ssh-key-signature env=\"%s\" expires=\"%s\"", *environment, response.ValidBefore),
PrivateKey: privateKey,
Certificate: cert,
LifetimeSecs: uint32(response.Duration),
}
err = sshAgent.Add(pubcert)
common.FatalOnErrorW(err, "could not add the certificate to the SSH agent")
case "stdout":
fmt.Println(string(ret.Payload))
default:
}
if *proxyConfig != "" {
conn, err := net.Dial("tcp", *proxyConfig)
if err != nil {
common.FatalOnErrorW(err, "could not connect to server")
}
ConnectIO(conn)
}
}