Skip to content

Commit 5208d39

Browse files
authored
Merge pull request #1 from gkarthiks/develop
chore: adding functionalities
2 parents 3191087 + 3153ab3 commit 5208d39

File tree

9 files changed

+793
-0
lines changed

9 files changed

+793
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM golang:alpine3.11
2+
RUN mkdir -p /usr/local/src
3+
COPY . /usr/local/src
4+
WORKDIR /usr/local/src/
5+
RUN go build -o vault-initializer cmd/main.go
6+
CMD ./vault-initializer

cmd/main.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
log "github.com/sirupsen/logrus"
5+
"os"
6+
"vault-initializer/utility"
7+
)
8+
9+
var (
10+
avail bool
11+
secSharesStr string
12+
secThresholdStr string
13+
err error
14+
)
15+
16+
func init() {
17+
appMode, avail := os.LookupEnv("APP_MODE")
18+
if !avail {
19+
appMode = "debug"
20+
log.SetFormatter(&log.JSONFormatter{
21+
TimestampFormat: "2006-01-02 15:04:05",
22+
})
23+
log.SetLevel(log.DebugLevel)
24+
} else if appMode == "production" {
25+
log.SetFormatter(&log.JSONFormatter{
26+
TimestampFormat: "2006-01-02 15:04:05",
27+
})
28+
log.SetLevel(log.InfoLevel)
29+
} else {
30+
log.SetFormatter(&log.JSONFormatter{
31+
TimestampFormat: "2006-01-02 15:04:05",
32+
})
33+
log.SetLevel(log.DebugLevel)
34+
}
35+
36+
vaultInitConfigMap, avail := os.LookupEnv("INIT_CONFIG_MAP")
37+
if !avail {
38+
log.Panic("The initialization config map is not specified")
39+
} else {
40+
log.Debugf("The initialization config map is specified as %s", vaultInitConfigMap)
41+
}
42+
43+
utility.ParseInitConfigData(vaultInitConfigMap)
44+
45+
}
46+
47+
func main() {
48+
doneCh := make(chan bool)
49+
go func() {
50+
utility.StartRoutine()
51+
}()
52+
<-doneCh
53+
}

common/VarConstTypes.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package common
2+
3+
import "reflect"
4+
5+
var (
6+
//VaultURL string
7+
SecShares int
8+
SecThreshold int
9+
WaitTimeSeconds int
10+
ReadinessProbeInSeconds int
11+
)
12+
13+
const (
14+
WaitTime = 3
15+
ReadinessProbe = 5
16+
DefaultSecretShares = 5
17+
DefaultSecretThreshold = 3
18+
HttpMethodGET = "GET"
19+
HttpMethodPOST = "POST"
20+
HttpMethodPUT = "PUT"
21+
VaultKeysSecretName = "vault-init-keys"
22+
)
23+
24+
type VaultInitResp struct {
25+
Keys []string `json:"keys"`
26+
KeysBase64 []string `json:"keys_base64"`
27+
RootToken string `json:"root_token"`
28+
}
29+
30+
type VaultUnsealResp struct {
31+
Type string `json:"type"`
32+
Initialized bool `json:"initialized"`
33+
Sealed bool `json:"sealed"`
34+
T int `json:"t"`
35+
N int `json:"n"`
36+
Progress int `json:"progress"`
37+
Nonce string `json:"nonce"`
38+
Version string `json:"version"`
39+
Migration bool `json:"migration"`
40+
ClusterName string `json:"cluster_name"`
41+
ClusterID string `json:"cluster_id"`
42+
RecoverySeal bool `json:"recovery_seal"`
43+
StorageType string `json:"storage_type"`
44+
}
45+
46+
func (parsedKeys VaultInitResp) IsEmpty() bool {
47+
return reflect.DeepEqual(parsedKeys, VaultInitResp{})
48+
}

go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module vault-initializer
2+
3+
go 1.13
4+
5+
require (
6+
github.com/gkarthiks/k8s-discovery v0.0.0-20190821062943-753b4c007093
7+
github.com/sirupsen/logrus v1.4.2
8+
k8s.io/api v0.0.0-20190819141258-3544db3b9e44
9+
k8s.io/apimachinery v0.17.3
10+
)

go.sum

Lines changed: 168 additions & 0 deletions
Large diffs are not rendered by default.

utility/common.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package utility
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
log "github.com/sirupsen/logrus"
7+
"io/ioutil"
8+
"net/http"
9+
)
10+
11+
func parseJSONRespo(respJSON []byte, structType interface{}) {
12+
if respJSON != nil {
13+
json.Unmarshal(respJSON, &structType)
14+
}
15+
return
16+
}
17+
18+
// FireRequest fires the request based on the parameters to the provided URL
19+
func FireRequest(payloadJSON string, url string, reqHeaders map[string]string, method string) ([]byte, error) {
20+
var req *http.Request
21+
22+
if len(payloadJSON) > 0 {
23+
req, err = http.NewRequest(method, url, bytes.NewBuffer([]byte(payloadJSON)))
24+
log.Debugf("JSON String getting passed as payload: %s to the URL %s", payloadJSON, url)
25+
req.Header.Set("Content-Type", "application/json")
26+
} else {
27+
log.Debug("No payload to pass")
28+
req, err = http.NewRequest(method, url, nil)
29+
}
30+
for key, val := range reqHeaders {
31+
req.Header.Set(key, val)
32+
}
33+
client := &http.Client{}
34+
resp, err := client.Do(req)
35+
if err != nil {
36+
return nil, err
37+
}
38+
defer resp.Body.Close()
39+
body, _ := ioutil.ReadAll(resp.Body)
40+
//log.Debugf("Response body getting returned: %s", string(body))
41+
return body, nil
42+
}

0 commit comments

Comments
 (0)