-
Notifications
You must be signed in to change notification settings - Fork 2
/
certs.go
55 lines (41 loc) · 1002 Bytes
/
certs.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
package main
import (
"io/ioutil"
"os"
"github.com/Sirupsen/logrus"
)
func DumpCerts(certs map[string]string) {
logrus.Infof("Dumping certs to %s", certDir)
for name, content := range certs {
_, err := writeFile(certDir, name, content)
if err != nil {
logrus.Errorf("Cannot write file to %s/%s: %v", certDir, name, err)
}
}
os.Setenv("CONSUL_CACERT", certDir+"/ca.crt")
os.Setenv("CONSUL_TLSCERT", certDir+"/client.crt")
os.Setenv("CONSUL_TLSKEY", certDir+"/client.key")
}
func fileExists(name string) bool {
_, err := os.Stat(name)
if os.IsNotExist(err) {
return false
}
return err == nil
}
func writeFile(directory string, filename string, content string) (bool, error) {
file := directory + "/" + filename
exists := fileExists(file)
if exists {
return false, nil
}
err := os.MkdirAll(directory, 0755)
if err != nil {
return false, err
}
err = ioutil.WriteFile(file, []byte(content), 0644)
if err != nil {
return false, err
}
return true, nil
}