forked from cloudtools/ssh-cert-authority
/
sign_cert.go
148 lines (137 loc) · 3.61 KB
/
sign_cert.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
package main
import (
"bufio"
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/cloudtools/ssh-cert-authority/client"
"github.com/cloudtools/ssh-cert-authority/util"
"github.com/codegangsta/cli"
"golang.org/x/crypto/ssh"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strings"
)
func signCertFlags() []cli.Flag {
home := os.Getenv("HOME")
if home == "" {
home = "/"
}
configPath := home + "/.ssh_ca/signer_config.json"
return []cli.Flag{
cli.StringFlag{
Name: "environment",
Value: "",
Usage: "An environment name (e.g. prod)",
},
cli.StringFlag{
Name: "config-file",
Value: configPath,
Usage: "Path to config.json",
},
cli.StringFlag{
Name: "cert-request-id",
Value: "",
Usage: "The certificate request id to look at. Also works as a positional argument.",
},
}
}
func signCert(c *cli.Context) {
configPath := c.String("config-file")
allConfig := make(map[string]ssh_ca_util.SignerConfig)
err := ssh_ca_util.LoadConfig(configPath, &allConfig)
if err != nil {
fmt.Println("Load Config failed:", err)
os.Exit(1)
}
certRequestID := c.String("cert-request-id")
if certRequestID == "" {
certRequestID = c.Args().First()
if certRequestID == "" {
fmt.Println("Specify a cert-request-id")
os.Exit(1)
}
}
environment := c.String("environment")
wrongTypeConfig, err := ssh_ca_util.GetConfigForEnv(environment, &allConfig)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
config := wrongTypeConfig.(ssh_ca_util.SignerConfig)
conn, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
if err != nil {
fmt.Println("Dial failed:", err)
os.Exit(1)
}
signer, err := ssh_ca_util.GetSignerForFingerprint(config.KeyFingerprint, conn)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
requestParameters := make(url.Values)
requestParameters["certRequestId"] = make([]string, 1)
requestParameters["certRequestId"][0] = certRequestID
getResp, err := http.Get(config.SignerUrl + "cert/requests?" + requestParameters.Encode())
if err != nil {
fmt.Println("Didn't get a valid response", err)
os.Exit(1)
}
getRespBuf, err := ioutil.ReadAll(getResp.Body)
if err != nil {
fmt.Println("Error reading response body", err)
os.Exit(1)
}
getResp.Body.Close()
if getResp.StatusCode != 200 {
fmt.Println("Error getting that request id:", string(getRespBuf))
os.Exit(1)
}
getResponse := make(certRequestResponse)
err = json.Unmarshal(getRespBuf, &getResponse)
if err != nil {
fmt.Println("Unable to unmarshall response", err)
os.Exit(1)
}
if getResponse[certRequestID].Signed {
fmt.Println("Certificate already signed. Thanks for trying.")
os.Exit(0)
}
rawCert, err := base64.StdEncoding.DecodeString(getResponse[certRequestID].CertBlob)
if err != nil {
fmt.Println("Trouble base64 decoding response", err)
os.Exit(1)
}
pubKey, err := ssh.ParsePublicKey(rawCert)
if err != nil {
fmt.Println("Trouble parsing response", err)
os.Exit(1)
}
cert := *pubKey.(*ssh.Certificate)
ssh_ca_util.PrintForInspection(cert)
fmt.Printf("Type 'yes' if you'd like to sign this cert request ")
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')
text = strings.TrimSpace(text)
if text != "yes" && text != "YES" {
os.Exit(0)
}
err = cert.SignCert(rand.Reader, signer)
if err != nil {
fmt.Println("Error signing:", err)
os.Exit(1)
}
request := ssh_ca_client.MakeSigningRequest(cert, certRequestID, config)
requestWebParameters := request.BuildWebRequest()
err = request.PostToWeb(requestWebParameters)
if err != nil {
fmt.Println("Error sending in +1:", err)
os.Exit(1)
} else {
fmt.Println("Signature accepted by server.")
}
}