fix: load uid key from manager config
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const adminUIDKeyConfigGroup = "im"
|
||||
|
||||
var adminUIDKeyConfigKeys = []string{"uid_encryption_key", "UID_ENCRYPTION_KEY", "uid_key"}
|
||||
|
||||
func storeConfigWithAdminUIDKey(raw json.RawMessage, client *adminInternalClient) (json.RawMessage, bool, error) {
|
||||
if client == nil {
|
||||
return raw, false, nil
|
||||
}
|
||||
values, err := client.ThirdPartyConfig(adminUIDKeyConfigGroup, adminUIDKeyConfigKeys...)
|
||||
if err != nil {
|
||||
return raw, false, err
|
||||
}
|
||||
uidKey := firstAdminUIDKeyValue(values)
|
||||
if uidKey == "" {
|
||||
return raw, false, nil
|
||||
}
|
||||
merged, err := mergeStoreUIDKey(raw, uidKey)
|
||||
if err != nil {
|
||||
return raw, false, err
|
||||
}
|
||||
return merged, true, nil
|
||||
}
|
||||
|
||||
func firstAdminUIDKeyValue(values map[string]string) string {
|
||||
for _, key := range adminUIDKeyConfigKeys {
|
||||
if value := strings.TrimSpace(values[key]); value != "" {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func mergeStoreUIDKey(raw json.RawMessage, uidKey string) (json.RawMessage, error) {
|
||||
config := map[string]json.RawMessage{}
|
||||
if len(raw) > 0 && string(raw) != "null" {
|
||||
if err := json.Unmarshal(raw, &config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
encoded, err := json.Marshal(strings.TrimSpace(uidKey))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config["uid_key"] = encoded
|
||||
return json.Marshal(config)
|
||||
}
|
||||
Reference in New Issue
Block a user