-
Notifications
You must be signed in to change notification settings - Fork 71
/
list_requests.go
105 lines (97 loc) · 2.73 KB
/
list_requests.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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/cloudtools/ssh-cert-authority/util"
"github.com/codegangsta/cli"
"golang.org/x/crypto/ssh"
"io/ioutil"
"net/http"
"os"
"time"
)
func listCertFlags() []cli.Flag {
home := os.Getenv("HOME")
if home == "" {
home = "/"
}
configPath := home + "/.ssh_ca/signer_config.json"
return []cli.Flag{
cli.StringFlag{
Name: "environment, e",
Value: "",
Usage: "An environment name (e.g. prod)",
},
cli.StringFlag{
Name: "config-file, c",
Value: configPath,
Usage: "Path to config.json",
},
cli.BoolFlag{
Name: "show-all, a",
Usage: "Show certs that have already been signed as well",
},
}
}
func listCerts(c *cli.Context) error {
configPath := c.String("config-file")
environment := c.String("environment")
showAll := c.Bool("show-all")
allConfig := make(map[string]ssh_ca_util.RequesterConfig)
err := ssh_ca_util.LoadConfig(configPath, &allConfig)
wrongTypeConfig, err := ssh_ca_util.GetConfigForEnv(environment, &allConfig)
if err != nil {
return cli.NewExitError(fmt.Sprintf("%s", err), 1)
}
config := wrongTypeConfig.(ssh_ca_util.RequesterConfig)
getResp, err := http.Get(config.SignerUrl + "cert/requests")
if err != nil {
return cli.NewExitError(fmt.Sprintf("Didn't get a valid response: %s", err), 1)
}
getRespBuf, err := ioutil.ReadAll(getResp.Body)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Error reading response body: %s", err), 1)
}
getResp.Body.Close()
if getResp.StatusCode != 200 {
return cli.NewExitError(fmt.Sprintf("Error getting listing of certs: %s", string(getRespBuf)), 1)
}
certs := make(certRequestResponse)
json.Unmarshal(getRespBuf, &certs)
for requestID, respElement := range certs {
if showAll || !respElement.Signed {
rawCert, err := base64.StdEncoding.DecodeString(respElement.CertBlob)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Trouble base64 decoding response '%s': %s", err, respElement.CertBlob), 1)
}
pubKey, err := ssh.ParsePublicKey(rawCert)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Trouble parsing response: %s", err), 1)
}
cert := *pubKey.(*ssh.Certificate)
env, ok := cert.Extensions["[email protected]"]
if !ok {
env = "unknown env"
}
expired := int64(cert.ValidBefore)-int64(time.Now().Unix()) < 1
if !expired || showAll {
expiredMsg := ""
if expired {
expiredMsg = ", \033[91mexpired\033[0m"
}
fmt.Printf("%d %s[%s, %d/%d%s]: %s - %s\n",
respElement.Serial,
requestID,
env,
respElement.NumSignatures,
respElement.SignaturesRequired,
expiredMsg,
cert.KeyId,
cert.Extensions["[email protected]"],
)
}
}
}
return nil
}