Skip to content

Commit 4938612

Browse files
committed
Enhanced logs
1 parent e582433 commit 4938612

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

cmd/sshproxy/sshproxy.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,16 @@ func mainExitCode() int {
311311
log.Debugf("config.log_stats_interval = %s", config.LogStatsInterval.Duration())
312312
log.Debugf("config.etcd = %+v", config.Etcd)
313313
log.Debugf("config.bg_command = %s", config.BgCommand)
314-
log.Debugf("config.translate_commands = %v", config.TranslateCommands)
314+
for k, v := range config.TranslateCommands {
315+
log.Debugf("config.TranslateCommands.%s = %+v", k, v)
316+
}
315317
log.Debugf("config.environment = %v", config.Environment)
316-
log.Debugf("config.routes = %v", config.Routes)
318+
for k, v := range config.Routes {
319+
log.Debugf("config.routes.%s = %+v", k, v)
320+
}
317321
log.Debugf("config.ssh.exe = %s", config.SSH.Exe)
318322
log.Debugf("config.ssh.args = %v", config.SSH.Args)
319323

320-
setEnvironment(config.Environment)
321-
322324
log.Infof("%s connected from %s to sshd listening on %s", username, sshInfos.Src(), sshInfos.Dst())
323325
defer log.Info("disconnected")
324326

@@ -346,7 +348,13 @@ func mainExitCode() int {
346348
log.Fatalf("Invalid destination '%s': %s", hostport, err)
347349
}
348350

349-
setEnvironment(environment)
351+
log.Debugf("service = %s", service)
352+
353+
// merge service environment with global environment
354+
for k, v := range environment {
355+
config.Environment[k] = v
356+
}
357+
setEnvironment(config.Environment)
350358

351359
// waitgroup and channel to stop our background command when exiting.
352360
var wg sync.WaitGroup
@@ -496,7 +504,7 @@ func mainExitCode() int {
496504
}()
497505
}
498506

499-
log.Infof("proxied to %s", hostport)
507+
log.Infof("proxied to %s (service: %s)", hostport, service)
500508

501509
var rc int
502510
if interactiveCommand {

pkg/utils/route.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var (
6262

6363
// CanConnect tests if a connection to host:port can be made (with a 1s timeout).
6464
func CanConnect(hostport string) bool {
65-
c, err := net.DialTimeout("tcp", hostport, 1*time.Second)
65+
c, err := net.DialTimeout("tcp", hostport, time.Second)
6666
if err != nil {
6767
mylog.Infof("cannot connect to %s: %s", hostport, err)
6868
return false

test/centos-image/sshproxy_test.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ var simpleConnectTests = []struct {
224224

225225
func TestSimpleConnect(t *testing.T) {
226226
for _, tt := range simpleConnectTests {
227-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
227+
ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
228228
defer cancel()
229229
args, cmd := prepareCommand(tt.user+"gateway1", tt.port, "hostname")
230230
_, stdout, stderr, err := runCommand(ctx, "ssh", args, nil, nil)
@@ -250,7 +250,7 @@ var environmentTests = []struct {
250250

251251
func TestEnvironment(t *testing.T) {
252252
for _, tt := range environmentTests {
253-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
253+
ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
254254
defer cancel()
255255
args, cmd := prepareCommand(tt.user+"gateway1", tt.port, "echo $XMODIFIERS")
256256
_, stdout, stderr, err := runCommand(ctx, "ssh", args, nil, nil)
@@ -265,7 +265,7 @@ func TestEnvironment(t *testing.T) {
265265

266266
func TestReturnCode(t *testing.T) {
267267
for _, exitCode := range []int{0, 3} {
268-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
268+
ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
269269
defer cancel()
270270
args, cmd := prepareCommand("gateway1", 2023, fmt.Sprintf("exit %d", exitCode))
271271
rc, _, _, _ := runCommand(ctx, "ssh", args, nil, nil)
@@ -276,7 +276,7 @@ func TestReturnCode(t *testing.T) {
276276
}
277277

278278
func TestMainSSHDied(t *testing.T) {
279-
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
279+
ctx, cancel := context.WithTimeout(context.Background(), 120 * time.Second)
280280
defer cancel()
281281
args, _ := prepareCommand("gateway1", 2023, "sleep 60")
282282
ch := make(chan *os.Process, 1)
@@ -295,7 +295,7 @@ func TestEtcdConnections(t *testing.T) {
295295
// remove old connections stored in etcd
296296
time.Sleep(4 * time.Second)
297297

298-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
298+
ctx, cancel := context.WithTimeout(context.Background(), 30 * time.Second)
299299
defer cancel()
300300
args, _ := prepareCommand("gateway1", 2023, "sleep 20")
301301
ch := make(chan *os.Process)
@@ -349,7 +349,7 @@ func TestStickyConnections(t *testing.T) {
349349
disableHost("server1")
350350
checkHostState(t, "server1", "disabled")
351351

352-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
352+
ctx, cancel := context.WithTimeout(context.Background(), 30 * time.Second)
353353
defer cancel()
354354
args, _ := prepareCommand("gateway1", 2022, "sleep 20")
355355
ch := make(chan *os.Process)
@@ -381,7 +381,7 @@ func TestNotLongStickyConnections(t *testing.T) {
381381
disableHost("server1")
382382
checkHostState(t, "server1", "disabled")
383383

384-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
384+
ctx, cancel := context.WithTimeout(context.Background(), 30 * time.Second)
385385
defer cancel()
386386
args, _ := prepareCommand("gateway1", 2022, "hostname")
387387
_, _, _, err := runCommand(ctx, "ssh", args, nil, nil)
@@ -412,7 +412,7 @@ func TestLongStickyConnections(t *testing.T) {
412412
disableHost("server1")
413413
checkHostState(t, "server1", "disabled")
414414

415-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
415+
ctx, cancel := context.WithTimeout(context.Background(), 30 * time.Second)
416416
defer cancel()
417417
args, _ := prepareCommand("gateway1", 2022, "hostname")
418418
_, _, _, err := runCommand(ctx, "ssh", args, nil, nil)
@@ -440,7 +440,7 @@ func TestBalancedConnections(t *testing.T) {
440440
// remove old connections stored in etcd
441441
time.Sleep(4 * time.Second)
442442

443-
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
443+
ctx, cancel := context.WithTimeout(context.Background(), 30 * time.Second)
444444
defer cancel()
445445
args, _ := prepareCommand("gateway1", 2022, "sleep 20")
446446
ch := make(chan *os.Process)
@@ -468,7 +468,7 @@ func TestBalancedConnections(t *testing.T) {
468468
}
469469

470470
func checkHostCheck(t *testing.T, host string, check time.Time) time.Time {
471-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
471+
ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
472472
defer cancel()
473473
args, _ := prepareCommand("gateway1", 2023, "hostname")
474474
want := time.Now()
@@ -482,7 +482,7 @@ func checkHostCheck(t *testing.T, host string, check time.Time) time.Time {
482482
found = true
483483
lastCheck = h.Ts
484484
if check.IsZero() {
485-
if lastCheck.Sub(want) > 1*time.Second {
485+
if lastCheck.Sub(want) > 1 * time.Second {
486486
t.Errorf("%s %s check at %s, want near %s", jsonStr, host, lastCheck, want)
487487
}
488488
} else {
@@ -534,7 +534,7 @@ func checkHostState(t *testing.T, host, state string) {
534534

535535
func TestEnableDisableHost(t *testing.T) {
536536
args, cmdStr := prepareCommand("gateway1", 2022, "hostname")
537-
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
537+
ctx, cancel := context.WithTimeout(context.Background(), 60 * time.Second)
538538
defer cancel()
539539

540540
_, stdout, _, err := runCommand(ctx, "ssh", args, nil, nil)
@@ -611,7 +611,7 @@ func TestEtcdUsers(t *testing.T) {
611611
// remove old connections stored in etcd
612612
time.Sleep(4 * time.Second)
613613

614-
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
614+
ctx, cancel := context.WithTimeout(context.Background(), 60 * time.Second)
615615
defer cancel()
616616

617617
ch := make(chan *os.Process)
@@ -654,7 +654,7 @@ func TestEtcdGroups(t *testing.T) {
654654
// remove old connections stored in etcd
655655
time.Sleep(4 * time.Second)
656656

657-
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
657+
ctx, cancel := context.WithTimeout(context.Background(), 60 * time.Second)
658658
defer cancel()
659659

660660
ch := make(chan *os.Process)
@@ -734,7 +734,7 @@ func TestSFTP(t *testing.T) {
734734
downloadFile := fmt.Sprintf("/tmp/passwd.%d", i)
735735
prepareSFTPBatchCommands(batchFile, downloadFile)
736736

737-
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
737+
ctx, cancel := context.WithTimeout(context.Background(), 20 * time.Second)
738738
defer cancel()
739739
ch := make(chan *os.Process, 1)
740740
go func() {
@@ -765,7 +765,7 @@ func TestOnlySFTP(t *testing.T) {
765765
downloadFile := "/tmp/passwd.onlySFTP"
766766
prepareSFTPBatchCommands(batchFile, downloadFile)
767767

768-
ctx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
768+
ctx, cancel := context.WithTimeout(context.Background(), 25 * time.Second)
769769
defer cancel()
770770
ch := make(chan *os.Process, 1)
771771
go func() {
@@ -819,7 +819,7 @@ func TestSCP(t *testing.T) {
819819
defer removeLineSSHProxyConf(line)
820820
}
821821
for _, tt := range scpTests {
822-
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
822+
ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
823823
defer cancel()
824824
_, _, _, err := runCommand(ctx, "scp", []string{"-P", strconv.Itoa(tt.port), tt.source, tt.dest}, nil, nil)
825825
if err != nil {

0 commit comments

Comments
 (0)