forked from zeromicro/go-zero
-
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.
- Loading branch information
Showing
4 changed files
with
107 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package conf | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"reflect" | ||
|
||
"github.com/WqyJh/confcrypt" | ||
) | ||
|
||
type SecurityConf struct { | ||
Enable bool `json:",default=true"` | ||
Env string `json:",default=CONFIG_KEY"` // environment variable name stores the encryption key | ||
} | ||
|
||
func findSecurityConfInStruct(v interface{}) (SecurityConf, bool) { | ||
if reflect.ValueOf(v).Kind() == reflect.Ptr { | ||
v = reflect.ValueOf(v).Elem().Interface() | ||
} | ||
t := reflect.TypeOf(v) | ||
for i := 0; i < t.NumField(); i++ { | ||
field := t.Field(i) | ||
if field.Type == reflect.TypeOf(SecurityConf{}) { | ||
return reflect.ValueOf(v).FieldByIndex(field.Index).Interface().(SecurityConf), true | ||
} | ||
} | ||
return SecurityConf{}, false | ||
} | ||
|
||
func SecurityLoad(path string, v interface{}, opts ...Option) error { | ||
if err := Load(path, v, opts...); err != nil { | ||
return err | ||
} | ||
c, ok := findSecurityConfInStruct(v) | ||
if ok && c.Enable { | ||
key := os.Getenv(c.Env) | ||
decoded, err := confcrypt.Decode(v, key) | ||
if err != nil { | ||
return err | ||
} | ||
if reflect.TypeOf(v).Kind() == reflect.Ptr { | ||
reflect.ValueOf(v).Elem().Set(reflect.ValueOf(decoded).Elem()) | ||
return nil | ||
} | ||
reflect.ValueOf(v).Set(reflect.ValueOf(decoded)) | ||
} | ||
return nil | ||
} | ||
|
||
func SecurityMustLoad(path string, v interface{}, opts ...Option) { | ||
if err := SecurityLoad(path, v, opts...); err != nil { | ||
log.Fatalf("error: config file %s, %s", path, err.Error()) | ||
} | ||
} |
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,50 @@ | ||
package conf | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/WqyJh/confcrypt" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestSecurityLoad(t *testing.T) { | ||
key := "testkey" | ||
type testConfig struct { | ||
SecurityConf | ||
|
||
User string `json:"user"` | ||
Pass string `json:"pass"` | ||
Secret string `json:"secret"` | ||
} | ||
expected := testConfig{ | ||
SecurityConf: SecurityConf{ | ||
Enable: true, | ||
Env: "CONFIG_KEY", | ||
}, | ||
User: "testuser", | ||
Pass: "testpass", | ||
Secret: "testsecret", | ||
} | ||
encryptedPass, err := confcrypt.EncryptString(expected.Pass, key) | ||
assert.Nil(t, err) | ||
encryptedSecret, err := confcrypt.EncryptString(expected.Secret, key) | ||
assert.Nil(t, err) | ||
text := `{ | ||
"user": "testuser", | ||
"pass": "` + encryptedPass + `", | ||
"secret": "` + encryptedSecret + `" | ||
}` | ||
tmpfile, err := createTempFile(".json", text) | ||
assert.Nil(t, err) | ||
defer os.Remove(tmpfile) | ||
|
||
os.Setenv("CONFIG_KEY", key) | ||
var config testConfig | ||
err = SecurityLoad(tmpfile, &config) | ||
assert.Nil(t, err) | ||
assert.NotEqual(t, encryptedPass, config.Pass) | ||
assert.NotEqual(t, encryptedPass, config.Secret) | ||
assert.Equal(t, expected.Pass, config.Pass) | ||
assert.Equal(t, expected.Secret, config.Secret) | ||
} |
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