/
kms.go
83 lines (68 loc) · 2.03 KB
/
kms.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
package kms
import (
"fmt"
"log"
"os"
)
var KmsCrypto CryptoProvider
// Start - Will set up and start the server
func Start() {
var err error
// Get and check config
InitConfig()
KmsCrypto, err = NewKMSCryptoProvider()
/*switch Config["GOKMS_CRYPTO_PROVIDER"] {
case "softhsm":
// Create crypto provider
//KmsCrypto, err = NewSoftHSMCryptoProvider()
default:
KmsCrypto, err = NewKMSCryptoProvider()
}*/
if err != nil {
Exit(fmt.Sprintf("Problem creating crypto provider: %v", err), 2)
}
// Start REST endpoint
StartListener()
}
var Config = map[string]string{
"GOKMS_AUTH_KEY": "../files/auth.key",
"GOKMS_CRYPTO_PROVIDER": "goksm",
"GOKMS_HOST": "localhost",
"GOKMS_PORT": "8011",
"GOKMS_SSL_CERT": "../files/auth.key", // This is just done to allow the tests to pass
"GOKMS_SSL_KEY": "../files/auth.key", // This is just done to allow the tests to pass
}
// InitConfig read several Environment variables and based on them initialise the configuration
func InitConfig() {
envFiles := []string{"GOKMS_SSL_CERT", "GOKMS_SSL_KEY"}
// Load all Environments variables
for k, _ := range Config {
if os.Getenv(k) != "" {
Config[k] = os.Getenv(k)
}
}
// All variable MUST have a value but we can not verify the variable content
for k, _ := range Config {
if Config[k] == "" {
Exit(fmt.Sprintf("Problem with %s", k), 2)
}
}
// Check file exists
for _, v := range envFiles {
_, err := os.Stat(Config[v])
if err != nil {
Exit(fmt.Sprintf("%s %s", v, err.Error()), 2)
}
}
}
// exit will return an error code and the reason to the os
func Exit(messages string, errorCode int) {
// Exit code and messages based on Nagios plugin return codes (https://nagios-plugins.org/doc/guidelines.html#AEN78)
var prefix = map[int]string{0: "OK", 1: "Warning", 2: "Critical", 3: "Unknown"}
// Catch all unknown errorCode and convert them to Unknown
if errorCode < 0 || errorCode > 3 {
errorCode = 3
}
log.Printf("%s %s\n", prefix[errorCode], messages)
os.Exit(errorCode)
}