-
Notifications
You must be signed in to change notification settings - Fork 13
/
helpers.go
115 lines (91 loc) · 2.38 KB
/
helpers.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
package support
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"time"
)
const AssetsDir string = "assets"
const ServerStartTimeout int = 10
const StartScript string = "./start_server.sh"
const StopScript string = "./stop_server.sh"
const DbSetupScript string = "./setup_db.sh"
var HTTPSClient = createHTTPSClient()
func UnmarshalJSONString(requestBody io.ReadCloser) map[string]interface{} {
var f interface{}
if err := json.NewDecoder(requestBody).Decode(&f); err != nil {
panic("String provided cannot be decoded as JSON")
}
return f.(map[string]interface{})
}
func ParseCertString(certString string) (*x509.Certificate, error) {
block, _ := pem.Decode([]byte(certString))
crt, err := x509.ParseCertificate(block.Bytes)
return crt, err
}
func ValidToken() string {
tokenPath := pathForAsset("uaa.token")
dat, err := os.ReadFile(tokenPath)
if err != nil {
panic(err.Error())
}
return string(dat)
}
func SetupDB() {
db := os.Getenv("DB")
err := exec.Command(DbSetupScript, db).Run()
if err != nil {
panic("Failed to setup DB: " + err.Error())
}
}
func WaitForServerToStart() {
for i := 0; i < ServerStartTimeout; i++ {
resp, err := SendGetRequestByID("1")
if err == nil && resp.StatusCode == 404 {
return
}
time.Sleep(time.Second)
}
panic(fmt.Sprintf("Could not start config server in %d seconds", ServerStartTimeout))
}
func pathForAsset(fileName string) string {
var path, rootDir string
rootDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
panic(err.Error())
}
path = filepath.Join(rootDir, AssetsDir, fileName)
return path
}
func createHTTPSClient() *http.Client {
sslCertPath := pathForAsset("ssl.crt")
sslKeyPath := pathForAsset("ssl.key")
rootCAPath := pathForAsset("ssl_root_ca.crt")
cert, err := tls.LoadX509KeyPair(sslCertPath, sslKeyPath)
if err != nil {
panic(err.Error())
}
// Load CA cert
caCert, err := os.ReadFile(rootCAPath)
if err != nil {
panic(err.Error())
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Setup HTTPS client
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
}
tlsConfig.BuildNameToCertificate() //nolint:staticcheck
transport := &http.Transport{TLSClientConfig: tlsConfig}
client := &http.Client{Transport: transport}
return client
}