diff options
Diffstat (limited to 'internal/db/ssh_key.go')
-rw-r--r-- | internal/db/ssh_key.go | 34 |
1 files changed, 17 insertions, 17 deletions
diff --git a/internal/db/ssh_key.go b/internal/db/ssh_key.go index d4189cff..b31c0da1 100644 --- a/internal/db/ssh_key.go +++ b/internal/db/ssh_key.go @@ -23,8 +23,8 @@ import ( log "unknwon.dev/clog/v2" "xorm.io/xorm" + "gogs.io/gogs/internal/conf" "gogs.io/gogs/internal/process" - "gogs.io/gogs/internal/setting" ) const ( @@ -84,7 +84,7 @@ func (k *PublicKey) OmitEmail() string { // AuthorizedString returns formatted public key string for authorized_keys file. func (k *PublicKey) AuthorizedString() string { - return fmt.Sprintf(_TPL_PUBLICK_KEY, setting.AppPath, k.ID, setting.CustomConf, k.Content) + return fmt.Sprintf(_TPL_PUBLICK_KEY, conf.AppPath(), k.ID, conf.CustomConf, k.Content) } // IsDeployKey returns true if the public key is used as deploy key. @@ -179,7 +179,7 @@ func parseKeyString(content string) (string, error) { // writeTmpKeyFile writes key content to a temporary file // and returns the name of that file, along with any possible errors. func writeTmpKeyFile(content string) (string, error) { - tmpFile, err := ioutil.TempFile(setting.SSH.KeyTestPath, "gogs_keytest") + tmpFile, err := ioutil.TempFile(conf.SSH.KeyTestPath, "gogs_keytest") if err != nil { return "", fmt.Errorf("TempFile: %v", err) } @@ -199,7 +199,7 @@ func SSHKeyGenParsePublicKey(key string) (string, int, error) { } defer os.Remove(tmpName) - stdout, stderr, err := process.Exec("SSHKeyGenParsePublicKey", setting.SSH.KeygenPath, "-lf", tmpName) + stdout, stderr, err := process.Exec("SSHKeyGenParsePublicKey", conf.SSH.KeygenPath, "-lf", tmpName) if err != nil { return "", 0, fmt.Errorf("fail to parse public key: %s - %s", err, stderr) } @@ -274,7 +274,7 @@ func SSHNativeParsePublicKey(keyLine string) (string, int, error) { // CheckPublicKeyString checks if the given public key string is recognized by SSH. // It returns the actual public key line on success. func CheckPublicKeyString(content string) (_ string, err error) { - if setting.SSH.Disabled { + if conf.SSH.Disabled { return "", errors.New("SSH is disabled") } @@ -291,7 +291,7 @@ func CheckPublicKeyString(content string) (_ string, err error) { // Remove any unnecessary whitespace content = strings.TrimSpace(content) - if !setting.SSH.MinimumKeySizeCheck { + if !conf.SSH.MinimumKeySizeCheck { return content, nil } @@ -300,7 +300,7 @@ func CheckPublicKeyString(content string) (_ string, err error) { keyType string length int ) - if setting.SSH.StartBuiltinServer { + if conf.SSH.StartBuiltinServer { fnName = "SSHNativeParsePublicKey" keyType, length, err = SSHNativeParsePublicKey(content) } else { @@ -310,9 +310,9 @@ func CheckPublicKeyString(content string) (_ string, err error) { if err != nil { return "", fmt.Errorf("%s: %v", fnName, err) } - log.Trace("Key info [native: %v]: %s-%d", setting.SSH.StartBuiltinServer, keyType, length) + log.Trace("Key info [native: %v]: %s-%d", conf.SSH.StartBuiltinServer, keyType, length) - if minLen, found := setting.SSH.MinimumKeySizes[keyType]; found && length >= minLen { + if minLen, found := conf.SSH.MinimumKeySizes[keyType]; found && length >= minLen { return content, nil } else if found && length < minLen { return "", fmt.Errorf("key length is not enough: got %d, needs %d", length, minLen) @@ -325,7 +325,7 @@ func appendAuthorizedKeysToFile(keys ...*PublicKey) error { sshOpLocker.Lock() defer sshOpLocker.Unlock() - fpath := filepath.Join(setting.SSH.RootPath, "authorized_keys") + fpath := filepath.Join(conf.SSH.RootPath, "authorized_keys") f, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600) if err != nil { return err @@ -333,7 +333,7 @@ func appendAuthorizedKeysToFile(keys ...*PublicKey) error { defer f.Close() // Note: chmod command does not support in Windows. - if !setting.IsWindows { + if !conf.IsWindowsRuntime() { fi, err := f.Stat() if err != nil { return err @@ -375,12 +375,12 @@ func addKey(e Engine, key *PublicKey) (err error) { // Calculate fingerprint. tmpPath := strings.Replace(path.Join(os.TempDir(), fmt.Sprintf("%d", time.Now().Nanosecond()), "id_rsa.pub"), "\\", "/", -1) - os.MkdirAll(path.Dir(tmpPath), os.ModePerm) + _ = os.MkdirAll(path.Dir(tmpPath), os.ModePerm) if err = ioutil.WriteFile(tmpPath, []byte(key.Content), 0644); err != nil { return err } - stdout, stderr, err := process.Exec("AddPublicKey", setting.SSH.KeygenPath, "-lf", tmpPath) + stdout, stderr, err := process.Exec("AddPublicKey", conf.SSH.KeygenPath, "-lf", tmpPath) if err != nil { return fmt.Errorf("fail to parse public key: %s - %s", err, stderr) } else if len(stdout) < 2 { @@ -394,7 +394,7 @@ func addKey(e Engine, key *PublicKey) (err error) { } // Don't need to rewrite this file if builtin SSH server is enabled. - if setting.SSH.StartBuiltinServer { + if conf.SSH.StartBuiltinServer { return nil } return appendAuthorizedKeysToFile(key) @@ -523,8 +523,8 @@ func RewriteAuthorizedKeys() error { log.Trace("Doing: RewriteAuthorizedKeys") - os.MkdirAll(setting.SSH.RootPath, os.ModePerm) - fpath := filepath.Join(setting.SSH.RootPath, "authorized_keys") + _ = os.MkdirAll(conf.SSH.RootPath, os.ModePerm) + fpath := filepath.Join(conf.SSH.RootPath, "authorized_keys") tmpPath := fpath + ".tmp" f, err := os.OpenFile(tmpPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { @@ -536,7 +536,7 @@ func RewriteAuthorizedKeys() error { _, err = f.WriteString((bean.(*PublicKey)).AuthorizedString()) return err }) - f.Close() + _ = f.Close() if err != nil { return err } |