Browse Source

Allow custom SSH user in UI for built-in SSH server (#2617) (#2678)

* Allow custom SSH user in UI for built-in SSH server (#2617)

* Some fixes

* Did make fmt

* Updated according to review

- Renamed config to BUILTIN_SSH_SERVER_USER
- Removed unnecessary default string value for config item

* Updated according to review

* Fixed some minor issues
Patrick G 2 years ago
parent
commit
e89bb7ef6a
3 changed files with 13 additions and 3 deletions
  1. 2 0
      conf/app.ini
  2. 8 3
      models/repo.go
  3. 3 0
      modules/setting/setting.go

+ 2 - 0
conf/app.ini

@@ -113,6 +113,8 @@ LOCAL_ROOT_URL = %(PROTOCOL)s://%(HTTP_ADDR)s:%(HTTP_PORT)s/
113 113
 DISABLE_SSH = false
114 114
 ; Whether use builtin SSH server or not.
115 115
 START_SSH_SERVER = false
116
+; Username to use for builtin SSH server. If blank, then it is the value of RUN_USER.
117
+BUILTIN_SSH_SERVER_USER =
116 118
 ; Domain name to be exposed in clone URL
117 119
 SSH_DOMAIN = %(DOMAIN)s
118 120
 ; Network interface builtin SSH server listens on

+ 8 - 3
models/repo.go

@@ -806,14 +806,19 @@ func (repo *Repository) cloneLink(isWiki bool) *CloneLink {
806 806
 		repoName += ".wiki"
807 807
 	}
808 808
 
809
+	sshUser := setting.RunUser
810
+	if setting.SSH.StartBuiltinServer {
811
+		sshUser = setting.SSH.BuiltinServerUser
812
+	}
813
+
809 814
 	repo.Owner = repo.MustOwner()
810 815
 	cl := new(CloneLink)
811 816
 	if setting.SSH.Port != 22 {
812
-		cl.SSH = fmt.Sprintf("ssh://%s@%s:%d/%s/%s.git", setting.RunUser, setting.SSH.Domain, setting.SSH.Port, repo.Owner.Name, repoName)
817
+		cl.SSH = fmt.Sprintf("ssh://%s@%s:%d/%s/%s.git", sshUser, setting.SSH.Domain, setting.SSH.Port, repo.Owner.Name, repoName)
813 818
 	} else if setting.Repository.UseCompatSSHURI {
814
-		cl.SSH = fmt.Sprintf("ssh://%s@%s/%s/%s.git", setting.RunUser, setting.SSH.Domain, repo.Owner.Name, repoName)
819
+		cl.SSH = fmt.Sprintf("ssh://%s@%s/%s/%s.git", sshUser, setting.SSH.Domain, repo.Owner.Name, repoName)
815 820
 	} else {
816
-		cl.SSH = fmt.Sprintf("%s@%s:%s/%s.git", setting.RunUser, setting.SSH.Domain, repo.Owner.Name, repoName)
821
+		cl.SSH = fmt.Sprintf("%s@%s:%s/%s.git", sshUser, setting.SSH.Domain, repo.Owner.Name, repoName)
817 822
 	}
818 823
 	cl.HTTPS = ComposeHTTPSCloneURL(repo.Owner.Name, repoName)
819 824
 	return cl

+ 3 - 0
modules/setting/setting.go

@@ -90,6 +90,7 @@ var (
90 90
 	SSH = struct {
91 91
 		Disabled             bool           `ini:"DISABLE_SSH"`
92 92
 		StartBuiltinServer   bool           `ini:"START_SSH_SERVER"`
93
+		BuiltinServerUser    string         `ini:"BUILTIN_SSH_SERVER_USER"`
93 94
 		Domain               string         `ini:"SSH_DOMAIN"`
94 95
 		Port                 int            `ini:"SSH_PORT"`
95 96
 		ListenHost           string         `ini:"SSH_LISTEN_HOST"`
@@ -915,6 +916,8 @@ func NewContext() {
915 916
 		}
916 917
 	}
917 918
 
919
+	SSH.BuiltinServerUser = Cfg.Section("server").Key("BUILTIN_SSH_SERVER_USER").MustString(RunUser)
920
+
918 921
 	// Determine and create root git repository path.
919 922
 	sec = Cfg.Section("repository")
920 923
 	Repository.DisableHTTPGit = sec.Key("DISABLE_HTTP_GIT").MustBool()