/
hardcodecert.go
67 lines (60 loc) · 1.64 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
58
59
60
61
62
63
64
65
66
67
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) != 4 {
fmt.Printf("Usage: %s plotter/api cert.pem key.pem\n", os.Args[0])
return
}
httpscert, err := ioutil.ReadFile(os.Args[2])
if err != nil {
log.Fatalf("Could not read HTTPS certificate file: %v", err)
}
httpskey, err := ioutil.ReadFile(os.Args[3])
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()
switch os.Args[1] {
case "plotter":
err := keys.UpsertHardcodedTLSCertificate(context.Background(), etcdConn, hardcoded)
if err != nil {
log.Fatalf("Could not update hardcoded TLS certificate: %v", err)
}
case "api":
_, err = etcdConn.Put(context.Background(), "api/hardcoded_priv", string(httpskey))
if err != nil {
log.Fatalf("could not set API private key: %v", err)
}
_, err = etcdConn.Put(context.Background(), "api/hardcoded_pub", string(httpscert))
if err != nil {
log.Fatalf("could not set API public key: %v", err)
}
default:
fmt.Printf("second argument must be 'plotter' or 'api'\n")
os.Exit(1)
}
log.Println("DONE")
}