encrypt master key

This commit is contained in:
Lunny Xiao 2022-10-20 23:56:42 +08:00 committed by Jason Song
parent b8c7ea782c
commit 34a1bb2c20

View File

@ -6,6 +6,7 @@
package setting package setting
import ( import (
"crypto/sha1"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"math" "math"
@ -28,6 +29,7 @@ import (
"code.gitea.io/gitea/modules/user" "code.gitea.io/gitea/modules/user"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
"golang.org/x/crypto/pbkdf2"
gossh "golang.org/x/crypto/ssh" gossh "golang.org/x/crypto/ssh"
ini "gopkg.in/ini.v1" ini "gopkg.in/ini.v1"
) )
@ -970,10 +972,9 @@ func loadFromConf(allowEmpty bool, extraConfig string) {
MasterKeyProvider = sec.Key("MASTER_KEY_PROVIDER").MustString("plain") MasterKeyProvider = sec.Key("MASTER_KEY_PROVIDER").MustString("plain")
switch MasterKeyProvider { switch MasterKeyProvider {
case "plain": case "plain":
tempSalt := []byte{'g', 'i', 't', 'e', 'a'}
MasterKey = []byte(sec.Key("MASTER_KEY").MustString(SecretKey)) MasterKey = []byte(sec.Key("MASTER_KEY").MustString(SecretKey))
if len(MasterKey) > 32 { MasterKey = pbkdf2.Key(MasterKey, tempSalt, 4096, 32, sha1.New)
MasterKey = MasterKey[:32]
}
case "none": case "none":
default: default:
log.Fatal("invalid master key provider type: %v", MasterKeyProvider) log.Fatal("invalid master key provider type: %v", MasterKeyProvider)