-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
95 lines (80 loc) · 2.3 KB
/
utils.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
package utils
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"
log "github.com/Sirupsen/logrus"
"github.com/samalba/dockerclient"
)
func FromUnixTimestamp(timestamp int64) (*time.Time, error) {
i, err := strconv.ParseInt("1405544146", 10, 64)
if err != nil {
return nil, err
}
t := time.Unix(i, 0)
return &t, nil
}
func GetTLSConfig(caCert, cert, key []byte, allowInsecure bool) (*tls.Config, error) {
// TLS config
var tlsConfig tls.Config
tlsConfig.InsecureSkipVerify = true
certPool := x509.NewCertPool()
certPool.AppendCertsFromPEM(caCert)
tlsConfig.RootCAs = certPool
keypair, err := tls.X509KeyPair(cert, key)
if err != nil {
return &tlsConfig, err
}
tlsConfig.Certificates = []tls.Certificate{keypair}
if allowInsecure {
tlsConfig.InsecureSkipVerify = true
}
return &tlsConfig, nil
}
func GetClient(dockerUrl, tlsCaCert, tlsCert, tlsKey string, allowInsecure bool) (*dockerclient.DockerClient, error) {
// only load env vars if no args
// check environment for docker client config
envDockerHost := os.Getenv("DOCKER_HOST")
if dockerUrl == "" && envDockerHost != "" {
dockerUrl = envDockerHost
}
// only load env vars if no args
envDockerCertPath := os.Getenv("DOCKER_CERT_PATH")
envDockerTlsVerify := os.Getenv("DOCKER_TLS_VERIFY")
if tlsCaCert == "" && envDockerCertPath != "" && envDockerTlsVerify != "" {
tlsCaCert = filepath.Join(envDockerCertPath, "ca.pem")
tlsCert = filepath.Join(envDockerCertPath, "cert.pem")
tlsKey = filepath.Join(envDockerCertPath, "key.pem")
}
// load tlsconfig
var tlsConfig *tls.Config
if tlsCaCert != "" && tlsCert != "" && tlsKey != "" {
log.Debug("using tls for communication with docker")
caCert, err := ioutil.ReadFile(tlsCaCert)
if err != nil {
log.Fatalf("error loading tls ca cert: %s", err)
}
cert, err := ioutil.ReadFile(tlsCert)
if err != nil {
log.Fatalf("error loading tls cert: %s", err)
}
key, err := ioutil.ReadFile(tlsKey)
if err != nil {
log.Fatalf("error loading tls key: %s", err)
}
cfg, err := GetTLSConfig(caCert, cert, key, allowInsecure)
if err != nil {
log.Fatalf("error configuring tls: %s", err)
}
tlsConfig = cfg
}
client, err := dockerclient.NewDockerClient(dockerUrl, tlsConfig)
if err != nil {
return nil, err
}
return client, nil
}