Commit 8b97739f authored by Mahendra Vishwakarma's avatar Mahendra Vishwakarma
Browse files

Added config builder

Added config builder
Config Client
Config Maker
Run-time Config change support
File-base as TOML
Secure File support
Secure Key Support
parent 1a1de11f
Branches
Tags
2 merge requests!23Devbranch to Master,!15Added config builder
Showing with 115 additions and 0 deletions
package configmdl
import (
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/filemdl"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/securitymdl"
"github.com/BurntSushi/toml"
pellgotoml "github.com/pelletier/go-toml"
)
// InitConfig initConfig
func InitConfig(fpath string, config interface{}) (toml.MetaData, error) {
return toml.DecodeFile(fpath, config)
}
// var config tomlConfig
// InitConfig("../testingdata/testData/config/config.toml", &config)
// fmt.Println(config.Name)
//******* SECURE *******//
// InitConfigSecure - Init Config Secure
func InitConfigSecure(fpath string, config interface{}, key []byte) (toml.MetaData, error) {
fileBytes, fileReadErr := filemdl.ReadFile(fpath)
if fileReadErr != nil {
return toml.MetaData{}, fileReadErr
}
fileContent, decryptErr := securitymdl.AESDecrypt(fileBytes, key)
if decryptErr != nil {
return toml.MetaData{}, decryptErr
}
return toml.Decode(string(fileContent), config)
}
// SaveConfig - SaveConfig
func SaveConfig(fpath string, config interface{}) error {
bytes, tomlMarshalError := pellgotoml.Marshal(config)
if tomlMarshalError != nil {
return tomlMarshalError
}
writeError := filemdl.WriteFile(fpath, bytes, false, false)
if writeError != nil {
return writeError
}
return nil
}
// SaveConfigSecure - SaveConfigSecure
func SaveConfigSecure(fpath string, config interface{}, key []byte) error {
configBytes, tomlMarshalError := pellgotoml.Marshal(config)
if tomlMarshalError != nil {
return tomlMarshalError
}
encryptedBytes, encryptError := securitymdl.AESEncrypt(configBytes, key)
if encryptError != nil {
return encryptError
}
writeError := filemdl.WriteFile(fpath, encryptedBytes, false, false)
if writeError != nil {
return writeError
}
return nil
}
package configmdl
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type tomlConfig struct {
Title string
Name string
AdditionalField string
Version string
}
func TestInitConfig(t *testing.T) {
var config tomlConfig
InitConfig("../testingdata/testData/config/config.toml", &config)
assert.True(t, config.Name == "github.com/OneOfOne/xxhash", "this should not throw error")
}
func TestSaveConfig(t *testing.T) {
var config tomlConfig
InitConfig("../testingdata/testData/config/config.toml", &config)
err := SaveConfig("../testingdata/testData/config/config-save.toml", config)
assert.NoError(t, err, "This should not throw error")
}
func TestSaveConfigSecure(t *testing.T) {
var config tomlConfig
InitConfig("../testingdata/testData/config/config.toml", &config)
key := "1234567891234567"
err := SaveConfigSecure("../testingdata/testData/config/config-save-secure.toml", config, []byte(key))
assert.NoError(t, err, "This should not throw error")
}
func TestInitConfigSecure(t *testing.T) {
var config tomlConfig
InitConfig("../testingdata/testData/config/config.toml", &config)
key := "1234567891234567"
SaveConfigSecure("../testingdata/testData/config/config-save-secure.toml", config, []byte(key))
_, err := InitConfigSecure("../testingdata/testData/config/config-save-secure.toml", &config, []byte(key))
fmt.Println("config: ", config)
assert.NoError(t, err, "This should not throw error")
}
# sample.toml example
name = "github.com/OneOfOne/xxhash"
version = "1.2.2"
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment