-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from goreflect/issue-34
start implemented vault configuring
- Loading branch information
Showing
8 changed files
with
492 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,11 @@ | ||
module github.com/goreflect/gostructor | ||
|
||
go 1.13 | ||
go 1.15 | ||
|
||
require ( | ||
github.com/go-restit/lzjson v0.0.0-20161206095556-efe3c53acc68 | ||
github.com/google/go-cmp v0.5.2 // indirect | ||
github.com/goreflect/go_hocon v0.0.2 | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/sirupsen/logrus v1.5.0 | ||
github.com/stretchr/testify v1.5.1 | ||
gotest.tools v2.2.0+incompatible | ||
github.com/mittwald/vaultgo v0.0.6 | ||
github.com/sirupsen/logrus v1.7.0 | ||
github.com/stretchr/testify v1.6.1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package pipeline | ||
|
||
import ( | ||
"errors" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/goreflect/gostructor/converters" | ||
"github.com/goreflect/gostructor/infra" | ||
"github.com/goreflect/gostructor/properties" | ||
"github.com/goreflect/gostructor/tags" | ||
vault "github.com/mittwald/vaultgo" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
/*VaultConfig - source vault configuring*/ | ||
type VaultConfig struct { | ||
Config *properties.VaultConfiguration | ||
connection *vault.Client | ||
} | ||
|
||
func (config *VaultConfig) configureVault() error { | ||
configured, errConfigure := Configure(&properties.VaultConfiguration{}, "", []infra.FuncType{infra.FunctionSetupEnvironment}, "", true) | ||
if errConfigure != nil { | ||
logrus.Error("Can not initialize vault properties. Please setup VAULT_ADDRESS & VAULT_TOKEN for working with cf_vault") | ||
return errConfigure | ||
} | ||
config.Config = configured.(*properties.VaultConfiguration) | ||
return nil | ||
} | ||
|
||
func (config *VaultConfig) connect() error { | ||
conn, errConnection := vault.NewClient(config.Config.VaultAddress, | ||
vault.WithCaPath(""), | ||
vault.WithAuthToken(config.Config.VaultToken)) | ||
if errConnection != nil { | ||
return errConnection | ||
} | ||
conn.SetToken(config.Config.VaultToken) | ||
config.connection = conn | ||
return nil | ||
} | ||
|
||
func (config *VaultConfig) vaultAvailable() error { | ||
if config.Config == nil { | ||
if err := config.configureVault(); err != nil { | ||
logrus.Error("Configure Vault Error: ", err) | ||
return err | ||
} | ||
} | ||
|
||
if config.connection == nil { | ||
return config.connect() | ||
} | ||
return nil | ||
} | ||
|
||
func (config *VaultConfig) prepareLayer(context *structContext) error { | ||
if err := config.configureVault(); err != nil { | ||
return err | ||
} | ||
|
||
if errConn := config.vaultAvailable(); errConn != nil { | ||
logrus.Error("Error while connect to vault: ", errConn) | ||
return errConn | ||
} | ||
return nil | ||
} | ||
|
||
func (config VaultConfig) GetBaseType(context *structContext) infra.GoStructorValue { | ||
if err := config.prepareLayer(context); err != nil { | ||
return infra.NewGoStructorNoValue(context.Value, err) | ||
} | ||
nameField := context.StructField.Tag.Get(tags.TagHashiCorpVault) | ||
path := strings.Split(nameField, "#")[0] | ||
secretName := strings.Split(nameField, "#")[1] | ||
secret, err := config.connection.Logical().Read(path) | ||
secretValue := secret.Data[secretName] | ||
logrus.Debug("Secret Vault: ", secretValue) | ||
if err != nil { | ||
logrus.Error("Error while reading config from vault: ", err) | ||
return infra.NewGoStructorNoValue(context.Value, err) | ||
} | ||
return converters.ConvertBetweenPrimitiveTypes(reflect.ValueOf(secret.Data[secretName]), reflect.Indirect(context.Value)) | ||
} | ||
|
||
func (config VaultConfig) GetComplexType(context *structContext) infra.GoStructorValue { | ||
if err := config.prepareLayer(context); err != nil { | ||
return infra.NewGoStructorNoValue(context.Value, err) | ||
} | ||
nameField := context.StructField.Tag.Get(tags.TagHashiCorpVault) | ||
path := strings.Split(nameField, "#")[0] | ||
secretName := strings.Split(nameField, "#")[1] | ||
secret, err := config.connection.Logical().Read(path) | ||
secretValue := secret.Data[secretName] | ||
logrus.Debug("Secret Vault: ", secretValue) | ||
if err != nil { | ||
logrus.Error("Error while reading config from vault: ", err) | ||
return infra.NewGoStructorNoValue(context.Value, err) | ||
} | ||
kind := reflect.Indirect(context.Value).Kind() | ||
if kind == reflect.Slice { | ||
return converters.ConvertBetweenComplexTypes(reflect.ValueOf(strings.Split(secret.Data[secretName].(string), ",")), reflect.Indirect(context.Value)) | ||
} | ||
return infra.NewGoStructorNoValue(context.Value, errors.New("not supported complex type")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package pipeline | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/goreflect/gostructor/infra" | ||
) | ||
|
||
type TestStruct struct { | ||
MyAPIKey string `cf_vault:"test/tururu#api-key"` | ||
MyIntKey int64 `cf_vault:"test/tururu#kur"` | ||
MyCustomComplexType []int32 `cf_vault:"test/tururu#kur2"` | ||
} | ||
|
||
func TestConnectionToVault(t *testing.T) { | ||
os.Setenv("VAULT_URL", "http://localhost:1234") | ||
os.Setenv("VAULT_TOKEN", "myroot") | ||
result, err := Configure(&TestStruct{}, "", []infra.FuncType{infra.FunctionSetupVault}, "", true) | ||
if err != nil { | ||
t.Fail() | ||
t.Log(err) | ||
return | ||
} | ||
t.Log(result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package properties | ||
|
||
type VaultConfiguration struct { | ||
VaultAddress string `cf_env:"VAULT_URL"` | ||
VaultToken string `cf_env:"VAULT_TOKEN"` | ||
} |