Skip to content

Commit 874776b

Browse files
committed
also capture logs for pre-login and check-password commands
Signed-off-by: Nicola Murino <[email protected]>
1 parent ec67b67 commit 874776b

10 files changed

+143
-130
lines changed

docs/check-password-hook.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Global environment variables are cleared, for security reasons, when the script
2222

2323
The program must write, on its standard output, the expected JSON serialized response described above.
2424

25+
Any output of the program on its standard error will be recorded in the SFTPGo logs with sender `check_password_hook` and level `warn`.
26+
2527
If the hook is an HTTP URL then it will be invoked as HTTP POST. The request body will contain a JSON serialized struct with the following fields:
2628

2729
- `username`

docs/dynamic-user-mod.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ The program must write, on its standard output:
1515
- an empty string (or no response at all) if the user should not be created/updated
1616
- or the SFTPGo user, JSON serialized, if you want to create or update the given user
1717

18+
Any output of the program on its standard error will be recorded in the SFTPGo logs with sender `pre_login_hook` and level `warn`.
19+
1820
If the hook is an HTTP URL then it will be invoked as HTTP POST. The login method, the used protocol and the ip address of the user trying to login are added to the query string, for example `<http_url>?login_method=password&ip=1.2.3.4&protocol=SSH`.
1921
The request body will contain the user trying to login serialized as JSON. If no modification is needed the HTTP response code must be 204, otherwise the response code must be 200 and the response body a valid SFTPGo user serialized as JSON.
2022

docs/external-auth.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The program must write, on its standard output:
2121
- an empty string, or no response at all, if authentication succeeds and the existing SFTPGo user does not need to be updated. This means that the credentials already stored in SFTPGo must match those used for the current authentication.
2222
- a user with an empty username if the authentication fails
2323

24-
Any output of the program on its standard error will be recorded in the sftpgo logs with sender `external_auth_hook`.
24+
Any output of the program on its standard error will be recorded in the SFTPGo logs with sender `external_auth_hook` and level `warn`.
2525

2626
If the hook is an HTTP URL then it will be invoked as HTTP POST. The request body will contain a JSON serialized struct with the following fields:
2727

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ require (
2424
github.com/eikenb/pipeat v0.0.0-20210730190139-06b3e6902001
2525
github.com/fclairamb/ftpserverlib v0.21.0
2626
github.com/fclairamb/go-log v0.4.1
27-
github.com/go-acme/lego/v4 v4.10.0
27+
github.com/go-acme/lego/v4 v4.10.2
2828
github.com/go-chi/chi/v5 v5.0.8
2929
github.com/go-chi/jwtauth/v5 v5.1.0
3030
github.com/go-chi/render v1.0.2
@@ -133,7 +133,7 @@ require (
133133
github.com/mattn/go-colorable v0.1.13 // indirect
134134
github.com/mattn/go-isatty v0.0.17 // indirect
135135
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
136-
github.com/miekg/dns v1.1.50 // indirect
136+
github.com/miekg/dns v1.1.51 // indirect
137137
github.com/minio/sha256-simd v1.0.0 // indirect
138138
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
139139
github.com/mitchellh/mapstructure v1.5.0 // indirect

go.sum

+6-3
Original file line numberDiff line numberDiff line change
@@ -920,8 +920,8 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME
920920
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
921921
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
922922
github.com/gin-gonic/gin v1.7.7/go.mod h1:axIBovoeJpVj8S3BwE0uPMTeReE4+AfFtqpqaZ1qq1U=
923-
github.com/go-acme/lego/v4 v4.10.0 h1:G4Cgq4lsPxCjqsTKsqhUjRs3oKAGVMFPhvrl6kzzs44=
924-
github.com/go-acme/lego/v4 v4.10.0/go.mod h1:EMbf0Jmqwv94nJ5WL9qWnSXIBZnvsS9gNypansHGc6U=
923+
github.com/go-acme/lego/v4 v4.10.2 h1:5eW3qmda5v/LP21v1Hj70edKY1jeFZQwO617tdkwp6Q=
924+
github.com/go-acme/lego/v4 v4.10.2/go.mod h1:EMbf0Jmqwv94nJ5WL9qWnSXIBZnvsS9gNypansHGc6U=
925925
github.com/go-chi/chi/v5 v5.0.8 h1:lD+NLqFcAi1ovnVZpsnObHGW4xb4J8lNmoYVfECH1Y0=
926926
github.com/go-chi/chi/v5 v5.0.8/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
927927
github.com/go-chi/jwtauth/v5 v5.1.0 h1:wJyf2YZ/ohPvNJBwPOzZaQbyzwgMZZceE1m8FOzXLeA=
@@ -1516,8 +1516,9 @@ github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3N
15161516
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
15171517
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=
15181518
github.com/miekg/dns v1.1.48/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
1519-
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
15201519
github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
1520+
github.com/miekg/dns v1.1.51 h1:0+Xg7vObnhrz/4ZCZcZh7zPXlmU0aveS2HDBd0m0qSo=
1521+
github.com/miekg/dns v1.1.51/go.mod h1:2Z9d3CP1LQWihRZUf29mQ19yDThaI4DAYzte2CaQW5c=
15211522
github.com/miekg/pkcs11 v1.0.3/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
15221523
github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=
15231524
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
@@ -2098,6 +2099,7 @@ golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
20982099
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
20992100
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
21002101
golang.org/x/mod v0.6.0/go.mod h1:4mET923SAdbXp2ki8ey+zGs1SLqsuM2Y0uvdZR/fUNI=
2102+
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
21012103
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
21022104
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
21032105
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -2513,6 +2515,7 @@ golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
25132515
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
25142516
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
25152517
golang.org/x/tools v0.2.0/go.mod h1:y4OqIKeOV/fWJetJ8bXPU1sEVniLMIyDAZWeHdV+NTA=
2518+
golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k=
25162519
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
25172520
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
25182521
golang.org/x/xerrors v0.0.0-20190410155217-1f06c39b4373/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

internal/common/actions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ func (h *defaultActionHandler) handleCommand(event *notifier.FsEvent) error {
307307
startTime := time.Now()
308308
err := cmd.Run()
309309

310-
logger.Debug(event.Protocol, "", "executed command %#v, elapsed: %v, error: %v",
310+
logger.Debug(event.Protocol, "", "executed command %q, elapsed: %s, error: %v",
311311
Config.Actions.Hook, time.Since(startTime), err)
312312

313313
return err

internal/common/common.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ func (c *Configuration) ExecuteStartupHook() error {
662662
cmd := exec.CommandContext(ctx, c.StartupHook, args...)
663663
cmd.Env = env
664664
err := cmd.Run()
665-
logger.Debug(logSender, "", "Startup hook executed, elapsed: %v, error: %v", time.Since(startTime), err)
665+
logger.Debug(logSender, "", "Startup hook executed, elapsed: %s, error: %v", time.Since(startTime), err)
666666
return nil
667667
}
668668

@@ -708,12 +708,12 @@ func (c *Configuration) executePostDisconnectHook(remoteAddr, protocol, username
708708
startTime := time.Now()
709709
cmd := exec.CommandContext(ctx, c.PostDisconnectHook, args...)
710710
cmd.Env = append(env,
711-
fmt.Sprintf("SFTPGO_CONNECTION_IP=%v", ipAddr),
712-
fmt.Sprintf("SFTPGO_CONNECTION_USERNAME=%v", username),
713-
fmt.Sprintf("SFTPGO_CONNECTION_DURATION=%v", connDuration),
714-
fmt.Sprintf("SFTPGO_CONNECTION_PROTOCOL=%v", protocol))
711+
fmt.Sprintf("SFTPGO_CONNECTION_IP=%s", ipAddr),
712+
fmt.Sprintf("SFTPGO_CONNECTION_USERNAME=%s", username),
713+
fmt.Sprintf("SFTPGO_CONNECTION_DURATION=%d", connDuration),
714+
fmt.Sprintf("SFTPGO_CONNECTION_PROTOCOL=%s", protocol))
715715
err := cmd.Run()
716-
logger.Debug(protocol, connID, "Post disconnect hook executed, elapsed: %v error: %v", time.Since(startTime), err)
716+
logger.Debug(protocol, connID, "Post disconnect hook executed, elapsed: %s error: %v", time.Since(startTime), err)
717717
}
718718

719719
func (c *Configuration) checkPostDisconnectHook(remoteAddr, protocol, username, connID string, connectionTime time.Time) {
@@ -767,11 +767,11 @@ func (c *Configuration) ExecutePostConnectHook(ipAddr, protocol string) error {
767767

768768
cmd := exec.CommandContext(ctx, c.PostConnectHook, args...)
769769
cmd.Env = append(env,
770-
fmt.Sprintf("SFTPGO_CONNECTION_IP=%v", ipAddr),
771-
fmt.Sprintf("SFTPGO_CONNECTION_PROTOCOL=%v", protocol))
770+
fmt.Sprintf("SFTPGO_CONNECTION_IP=%s", ipAddr),
771+
fmt.Sprintf("SFTPGO_CONNECTION_PROTOCOL=%s", protocol))
772772
err := cmd.Run()
773773
if err != nil {
774-
logger.Warn(protocol, "", "Login from ip %#v denied, connect hook error: %v", ipAddr, err)
774+
logger.Warn(protocol, "", "Login from ip %q denied, connect hook error: %v", ipAddr, err)
775775
}
776776
return err
777777
}

internal/common/dataretention.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,10 @@ func (c *RetentionCheck) sendHookNotification(elapsed time.Duration, errCheck er
460460

461461
cmd := exec.CommandContext(ctx, Config.DataRetentionHook, args...)
462462
cmd.Env = append(env,
463-
fmt.Sprintf("SFTPGO_DATA_RETENTION_RESULT=%v", string(jsonData)))
463+
fmt.Sprintf("SFTPGO_DATA_RETENTION_RESULT=%s", string(jsonData)))
464464
err := cmd.Run()
465465

466-
c.conn.Log(logger.LevelDebug, "notified result using command: %v, elapsed: %v err: %v",
466+
c.conn.Log(logger.LevelDebug, "notified result using command: %q, elapsed: %s err: %v",
467467
Config.DataRetentionHook, time.Since(startTime), err)
468468
return err
469469
}

internal/dataprovider/actions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func executeNotificationCommand(operation, executor, ip, objectType, objectName,
152152

153153
startTime := time.Now()
154154
err := cmd.Run()
155-
providerLog(logger.LevelDebug, "executed command %#v, elapsed: %v, error: %v", config.Actions.Hook,
155+
providerLog(logger.LevelDebug, "executed command %q, elapsed: %s, error: %v", config.Actions.Hook,
156156
time.Since(startTime), err)
157157
return err
158158
}

0 commit comments

Comments
 (0)