diff --git a/.gitignore b/.gitignore index 7ea1419a..aa1e733c 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ package-lock.json *.swo .tern-port .tern-project.js -*_test.go \ No newline at end of file +*_test.go +cover.* \ No newline at end of file diff --git a/server/common/config_state.go b/server/common/config_state.go index 6385be73..2974e640 100644 --- a/server/common/config_state.go +++ b/server/common/config_state.go @@ -15,13 +15,19 @@ package common import ( "fmt" + "github.com/tidwall/gjson" + "github.com/tidwall/sjson" "io/ioutil" "os" "path/filepath" ) var ( - configPath string = filepath.Join(GetCurrentDir(), CONFIG_PATH+"config.json") + configPath string = filepath.Join(GetCurrentDir(), CONFIG_PATH+"config.json") + configKeysToEncrypt []string = []string{ + "middleware.identity_provider.params", + "middleware.attribute_mapping.params", + } ) func LoadConfig() ([]byte, error) { @@ -34,14 +40,28 @@ func LoadConfig() ([]byte, error) { if err != nil { return nil, err } - if s := os.Getenv("CONFIG_SECRET"); s != "" { - t, err := DecryptString(Hash(s, 16), string(cFile)) - if err != nil { - return cFile, nil + configStr := string(cFile) + for _, jsonPathWithEncryptedData := range configKeysToEncrypt { + p := gjson.Get(configStr, jsonPathWithEncryptedData).String() + if p == "" { + continue } - return []byte(t), err + key := os.Getenv("CONFIG_SECRET") + if key == "" { + InitSecretDerivate(gjson.Get(configStr, "general.secret_key").String()) + key = SECRET_KEY_DERIVATE_FOR_PROOF + } + t, err := DecryptString(Hash(key, 16), p) + if err != nil { + continue + } + val, err := sjson.Set(configStr, jsonPathWithEncryptedData, t) + if err != nil { + continue + } + configStr = val } - return cFile, nil + return []byte(configStr), nil } func SaveConfig(v []byte) error { @@ -53,17 +73,29 @@ func SaveConfig(v []byte) error { configPath, ) } - cFile := PrettyPrint([]byte(v)) - if s := os.Getenv("CONFIG_SECRET"); s != "" { - t, err := EncryptString(Hash(s, 16), string(cFile)) - if err != nil { - Log.Error("common::config_state SaveConfig '%s'", err.Error()) - file.Close() - return err - } - cFile = []byte(t) - } - file.Write(cFile) + configStr := string(v) + for _, jsonPath := range configKeysToEncrypt { + key := os.Getenv("CONFIG_SECRET") + if key == "" { + key = SECRET_KEY_DERIVATE_FOR_PROOF + } + p := gjson.Get(configStr, jsonPath).String() + if p == "" { + continue + } + t, err := EncryptString(Hash(key, 16), p) + if err != nil { + Log.Warning("common::config_state cannot encrypt config path '%s'", jsonPath) + continue + } + val, err := sjson.Set(configStr, jsonPath, t) + if err != nil { + Log.Warning("common::config_state cannot put json value in config '%s'", jsonPath) + continue + } + configStr = val + } + file.Write(PrettyPrint([]byte(configStr))) return file.Close() } diff --git a/server/plugin/plg_authenticate_htpasswd/index.go b/server/plugin/plg_authenticate_htpasswd/index.go index 509ee374..12f84011 100644 --- a/server/plugin/plg_authenticate_htpasswd/index.go +++ b/server/plugin/plg_authenticate_htpasswd/index.go @@ -12,7 +12,6 @@ import ( "github.com/tredoe/osutil/user/crypt/sha256_crypt" "github.com/tredoe/osutil/user/crypt/sha512_crypt" "net/http" - "os" "strings" ) @@ -97,6 +96,7 @@ func (this Htpasswd) Callback(formData map[string]string, idpParams map[string]s } else if verifyPassword( formData["password"], strings.SplitN(pair[1], ":", 2)[0], // filter out unwanted fields from hash + formData["user"], ) == false { continue } @@ -114,11 +114,9 @@ func (this Htpasswd) Callback(formData map[string]string, idpParams map[string]s return nil, ErrAuthenticationFailed } -func verifyPassword(password string, hash string) bool { +func verifyPassword(password string, hash string, _user string) bool { if password == hash { - if s := os.Getenv("CONFIG_SECRET"); s == "" { - Log.Warning("plg_authenticate_htpasswd your password shouldn't be stored in clear text!") - } + Log.Warning("plg_authenticate_htpasswd password for user '%s' isn't stored in a secure way, you should hash your password using something like 'openssl passwd -6'", _user) return true } else if strings.HasPrefix(hash, "{SHA}") { d := sha1.New()