Skip to content

Commit

Permalink
Merge pull request #1 from gkarthiks/develop
Browse files Browse the repository at this point in the history
chore: adding functionalities
  • Loading branch information
gkarthiks authored Apr 27, 2020
2 parents 3191087 + 3153ab3 commit 5208d39
Show file tree
Hide file tree
Showing 9 changed files with 793 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM golang:alpine3.11
RUN mkdir -p /usr/local/src
COPY . /usr/local/src
WORKDIR /usr/local/src/
RUN go build -o vault-initializer cmd/main.go
CMD ./vault-initializer
53 changes: 53 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
log "github.com/sirupsen/logrus"
"os"
"vault-initializer/utility"
)

var (
avail bool
secSharesStr string
secThresholdStr string
err error
)

func init() {
appMode, avail := os.LookupEnv("APP_MODE")
if !avail {
appMode = "debug"
log.SetFormatter(&log.JSONFormatter{
TimestampFormat: "2006-01-02 15:04:05",
})
log.SetLevel(log.DebugLevel)
} else if appMode == "production" {
log.SetFormatter(&log.JSONFormatter{
TimestampFormat: "2006-01-02 15:04:05",
})
log.SetLevel(log.InfoLevel)
} else {
log.SetFormatter(&log.JSONFormatter{
TimestampFormat: "2006-01-02 15:04:05",
})
log.SetLevel(log.DebugLevel)
}

vaultInitConfigMap, avail := os.LookupEnv("INIT_CONFIG_MAP")
if !avail {
log.Panic("The initialization config map is not specified")
} else {
log.Debugf("The initialization config map is specified as %s", vaultInitConfigMap)
}

utility.ParseInitConfigData(vaultInitConfigMap)

}

func main() {
doneCh := make(chan bool)
go func() {
utility.StartRoutine()
}()
<-doneCh
}
48 changes: 48 additions & 0 deletions common/VarConstTypes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package common

import "reflect"

var (
//VaultURL string
SecShares int
SecThreshold int
WaitTimeSeconds int
ReadinessProbeInSeconds int
)

const (
WaitTime = 3
ReadinessProbe = 5
DefaultSecretShares = 5
DefaultSecretThreshold = 3
HttpMethodGET = "GET"
HttpMethodPOST = "POST"
HttpMethodPUT = "PUT"
VaultKeysSecretName = "vault-init-keys"
)

type VaultInitResp struct {
Keys []string `json:"keys"`
KeysBase64 []string `json:"keys_base64"`
RootToken string `json:"root_token"`
}

type VaultUnsealResp struct {
Type string `json:"type"`
Initialized bool `json:"initialized"`
Sealed bool `json:"sealed"`
T int `json:"t"`
N int `json:"n"`
Progress int `json:"progress"`
Nonce string `json:"nonce"`
Version string `json:"version"`
Migration bool `json:"migration"`
ClusterName string `json:"cluster_name"`
ClusterID string `json:"cluster_id"`
RecoverySeal bool `json:"recovery_seal"`
StorageType string `json:"storage_type"`
}

func (parsedKeys VaultInitResp) IsEmpty() bool {
return reflect.DeepEqual(parsedKeys, VaultInitResp{})
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module vault-initializer

go 1.13

require (
github.com/gkarthiks/k8s-discovery v0.0.0-20190821062943-753b4c007093
github.com/sirupsen/logrus v1.4.2
k8s.io/api v0.0.0-20190819141258-3544db3b9e44
k8s.io/apimachinery v0.17.3
)
168 changes: 168 additions & 0 deletions go.sum

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions utility/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package utility

import (
"bytes"
"encoding/json"
log "github.com/sirupsen/logrus"
"io/ioutil"
"net/http"
)

func parseJSONRespo(respJSON []byte, structType interface{}) {
if respJSON != nil {
json.Unmarshal(respJSON, &structType)
}
return
}

// FireRequest fires the request based on the parameters to the provided URL
func FireRequest(payloadJSON string, url string, reqHeaders map[string]string, method string) ([]byte, error) {
var req *http.Request

if len(payloadJSON) > 0 {
req, err = http.NewRequest(method, url, bytes.NewBuffer([]byte(payloadJSON)))
log.Debugf("JSON String getting passed as payload: %s to the URL %s", payloadJSON, url)
req.Header.Set("Content-Type", "application/json")
} else {
log.Debug("No payload to pass")
req, err = http.NewRequest(method, url, nil)
}
for key, val := range reqHeaders {
req.Header.Set(key, val)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
//log.Debugf("Response body getting returned: %s", string(body))
return body, nil
}
Loading

0 comments on commit 5208d39

Please sign in to comment.