-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
161 lines (135 loc) · 3.26 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
package main
import (
"crypto/tls"
"crypto/x509"
"flag"
"io/ioutil"
"os"
"os/user"
"path"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"astuart.co/vpki"
"net/http"
_ "net/http/pprof"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/rest"
)
var (
inCluster = flag.Bool("incluster", false, "the client is running inside a kuberenetes cluster")
host = flag.String("host", "http://localhost:8001", "The host API server to connect to")
ttl = flag.String("ttl", "240h", "the time to live for certificates")
forceTLS = flag.Bool("forcetls", false, "force all ingresses to use TLS if certs can be obtained")
role = flag.String("vault-role", "vault", "the vault role to use when obtaining certs")
selfSigned = flag.Bool("self-signed", false, "self-sign all certificates")
certNS = flag.String("cert-namespace", "", "the namespace in which certificates should be created")
)
func init() {
flag.Parse()
log.SetFormatter(&log.JSONFormatter{})
}
type certer struct {
c vpki.Certifier
api *kubernetes.Clientset
namespace string
}
func main() {
var config *rest.Config
log.Printf("TTL: %s, inCluster: %t", *ttl, *inCluster)
if *inCluster {
var err error
config, err = rest.InClusterConfig()
if err != nil {
log.Fatal(err)
}
} else {
config = &rest.Config{
Host: *host,
}
}
ttl, err := time.ParseDuration(*ttl)
if err != nil {
flag.Usage()
log.Fatal("Error parsing certificate TTL", err)
}
vc := &vpki.Client{
Addr: os.Getenv("VAULT_ADDR"),
Email: "andrew.stuart2@gmail.com",
Mount: "pki",
Role: *role,
Strength: 2048,
TTL: ttl,
}
log.Println(os.Getenv("ROOT_CA"))
if os.Getenv("ROOT_CA") != "" {
cp := x509.NewCertPool()
cp.AppendCertsFromPEM([]byte(os.Getenv("ROOT_CA")))
vc.HTTPClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{RootCAs: cp},
},
}
}
var ctr *certer
if !*selfSigned {
switch {
case os.Getenv("VAULT_TOKEN") != "":
log.Debug("Token:", os.Getenv("VAULT_TOKEN"))
vc.SetToken(strings.TrimSpace(os.Getenv("VAULT_TOKEN")))
default:
u, err := user.Current()
if err != nil {
log.Fatal(err)
}
bs, err := ioutil.ReadFile(path.Join(u.HomeDir, ".vault-token"))
if err != nil {
log.Fatal(err)
}
vc.SetToken(string(bs))
}
}
cli, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
}
if !*selfSigned {
ctr = &certer{
c: vc,
api: cli,
}
} else {
ctr = &certer{
c: &vpki.RawMarshaler{RawCertifier: &SelfSigner{ttl}},
api: cli,
}
}
ctr.namespace = *certNS
go ctr.watchIng()
for {
// Initial watch receives all current ingresses; we only need to renew
// Sleep for 90% of the TTL before reissue
time.Sleep(time.Duration(0.9 * float64(ttl)))
ns, err := cli.Namespaces().List(v1.ListOptions{})
if err != nil {
log.Fatal(err)
}
for _, n := range ns.Items {
i := cli.Ingresses(n.Name)
li, err := i.List(v1.ListOptions{})
if err != nil {
log.Fatal(err)
}
for _, ing := range li.Items {
_, err := ctr.addTLSSecrets(&ing)
if err != nil {
log.WithFields(log.Fields{
"ingress": ing.Name,
"namespace": ing.Namespace,
}).Println(err)
}
}
}
}
}