-
Notifications
You must be signed in to change notification settings - Fork 5
/
hardcodecert.go
57 lines (49 loc) · 1.25 KB
/
hardcodecert.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
package main
import (
"context"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/BTrDB/mr-plotter/keys"
etcd "github.com/coreos/etcd/clientv3"
)
func main() {
if len(os.Args) != 3 {
fmt.Printf("Usage: %s cert.pem key.pem\n", os.Args[0])
return
}
httpscert, err := ioutil.ReadFile(os.Args[1])
if err != nil {
log.Fatalf("Could not read HTTPS certificate file: %v", err)
}
httpskey, err := ioutil.ReadFile(os.Args[2])
if err != nil {
log.Fatalf("Could not read HTTPS certificate file: %v", err)
}
hardcoded := &keys.HardcodedTLSCertificate{
Cert: httpscert,
Key: httpskey,
}
var etcdEndpoint = os.Getenv("ETCD_ENDPOINT")
if len(etcdEndpoint) == 0 {
etcdEndpoint = "localhost:2379"
log.Printf("ETCD_ENDPOINT is not set; using %s", etcdEndpoint)
}
var etcdConfig = etcd.Config{Endpoints: []string{etcdEndpoint}}
log.Println("Connecting to etcd...")
etcdConn, err := etcd.New(etcdConfig)
if err != nil {
log.Fatalf("Error: %v", err)
}
defer etcdConn.Close()
success, err := keys.UpsertHardcodedTLSCertificateAtomically(context.Background(), etcdConn, hardcoded)
if err != nil {
log.Fatalf("Could not update hardcoded TLS certificate: %v", err)
}
if success {
log.Println("Success")
} else {
log.Println("Already exists")
}
}