1
1
// Copyright 2014 The Gogs Authors. All rights reserved.
2
+ // Copyright 2019 The Gitea Authors. All rights reserved.
2
3
// Use of this source code is governed by a MIT-style
3
4
// license that can be found in the LICENSE file.
4
5
5
6
package admin
6
7
7
8
import (
8
9
"fmt"
10
+ "net/url"
9
11
"os"
10
12
"runtime"
11
13
"strings"
@@ -19,6 +21,7 @@ import (
19
21
"code.gitea.io/gitea/modules/context"
20
22
"code.gitea.io/gitea/modules/cron"
21
23
"code.gitea.io/gitea/modules/git"
24
+ "code.gitea.io/gitea/modules/log"
22
25
"code.gitea.io/gitea/modules/process"
23
26
"code.gitea.io/gitea/modules/setting"
24
27
)
@@ -202,6 +205,63 @@ func SendTestMail(ctx *context.Context) {
202
205
ctx .Redirect (setting .AppSubURL + "/admin/config" )
203
206
}
204
207
208
+ func shadownPasswordKV (cfgItem , splitter string ) string {
209
+ fields := strings .Split (cfgItem , splitter )
210
+ for i := 0 ; i < len (fields ); i ++ {
211
+ if strings .HasPrefix (fields [i ], "password=" ) {
212
+ fields [i ] = "password=******"
213
+ break
214
+ }
215
+ }
216
+ return strings .Join (fields , splitter )
217
+ }
218
+
219
+ func shadownURL (provider , cfgItem string ) string {
220
+ u , err := url .Parse (cfgItem )
221
+ if err != nil {
222
+ log .Error ("shodowPassword %v failed: %v" , provider , err )
223
+ return cfgItem
224
+ }
225
+ if u .User != nil {
226
+ atIdx := strings .Index (cfgItem , "@" )
227
+ if atIdx > 0 {
228
+ colonIdx := strings .LastIndex (cfgItem [:atIdx ], ":" )
229
+ if colonIdx > 0 {
230
+ return cfgItem [:colonIdx + 1 ] + "******" + cfgItem [atIdx :]
231
+ }
232
+ }
233
+ }
234
+ return cfgItem
235
+ }
236
+
237
+ func shadowPassword (provider , cfgItem string ) string {
238
+ switch provider {
239
+ case "redis" :
240
+ return shadownPasswordKV (cfgItem , "," )
241
+ case "mysql" :
242
+ //root:@tcp(localhost:3306)/macaron?charset=utf8
243
+ atIdx := strings .Index (cfgItem , "@" )
244
+ if atIdx > 0 {
245
+ colonIdx := strings .Index (cfgItem [:atIdx ], ":" )
246
+ if colonIdx > 0 {
247
+ return cfgItem [:colonIdx + 1 ] + "******" + cfgItem [atIdx :]
248
+ }
249
+ }
250
+ return cfgItem
251
+ case "postgres" :
252
+ // user=jiahuachen dbname=macaron port=5432 sslmode=disable
253
+ if ! strings .HasPrefix (cfgItem , "postgres://" ) {
254
+ return shadownPasswordKV (cfgItem , " " )
255
+ }
256
+
257
+ // postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full
258
+ // Notice: use shadwonURL
259
+ }
260
+
261
+ // "couchbase"
262
+ return shadownURL (provider , cfgItem )
263
+ }
264
+
205
265
// Config show admin config page
206
266
func Config (ctx * context.Context ) {
207
267
ctx .Data ["Title" ] = ctx .Tr ("admin.config" )
@@ -239,10 +299,14 @@ func Config(ctx *context.Context) {
239
299
240
300
ctx .Data ["CacheAdapter" ] = setting .CacheService .Adapter
241
301
ctx .Data ["CacheInterval" ] = setting .CacheService .Interval
242
- ctx .Data ["CacheConn" ] = setting .CacheService .Conn
302
+
303
+ ctx .Data ["CacheConn" ] = shadowPassword (setting .CacheService .Adapter , setting .CacheService .Conn )
243
304
ctx .Data ["CacheItemTTL" ] = setting .CacheService .TTL
244
305
245
- ctx .Data ["SessionConfig" ] = setting .SessionConfig
306
+ sessionCfg := setting .SessionConfig
307
+ sessionCfg .ProviderConfig = shadowPassword (sessionCfg .Provider , sessionCfg .ProviderConfig )
308
+
309
+ ctx .Data ["SessionConfig" ] = sessionCfg
246
310
247
311
ctx .Data ["DisableGravatar" ] = setting .DisableGravatar
248
312
ctx .Data ["EnableFederatedAvatar" ] = setting .EnableFederatedAvatar
0 commit comments