Skip to content

Commit 66e00ff

Browse files
committed
check if port is well formatted, and add the 'host forget' action
1 parent c3f7596 commit 66e00ff

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

cmd/sshproxyctl/sshproxyctl.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func showUsers(configFile string, csvFlag bool, jsonFlag bool, allFlag bool) {
282282
i++
283283
}
284284

285-
sort.Slice(rows, func(i,j int) bool {
285+
sort.Slice(rows, func(i, j int) bool {
286286
return rows[i][0] < rows[j][0]
287287
})
288288

@@ -320,7 +320,7 @@ func showGroups(configFile string, csvFlag bool, jsonFlag bool, allFlag bool) {
320320
i++
321321
}
322322

323-
sort.Slice(rows, func(i,j int) bool {
323+
sort.Slice(rows, func(i, j int) bool {
324324
return rows[i][0] < rows[j][0]
325325
})
326326

@@ -374,6 +374,14 @@ func enableHost(host, port, configFile string) error {
374374
return cli.SetHost(key, utils.Up, time.Now())
375375
}
376376

377+
func forgetHost(host, port, configFile string) error {
378+
cli := mustInitEtcdClient(configFile)
379+
defer cli.Close()
380+
381+
key := fmt.Sprintf("%s:%s", host, port)
382+
return cli.DelHost(key)
383+
}
384+
377385
func disableHost(host, port, configFile string) error {
378386
cli := mustInitEtcdClient(configFile)
379387
defer cli.Close()
@@ -394,6 +402,7 @@ The commands are:
394402
version show version number and exit
395403
show show states present in etcd
396404
enable enable a host in etcd
405+
forget forget a host in etcd
397406
disable disable a host in etcd
398407
399408
The common options are:
@@ -461,6 +470,19 @@ Enable a previously disabled host in etcd. The default port is %s.
461470
return fs
462471
}
463472

473+
func newForgetParser() *flag.FlagSet {
474+
fs := flag.NewFlagSet("forget", flag.ExitOnError)
475+
fs.Usage = func() {
476+
fmt.Fprintf(flag.CommandLine.Output(), `Usage: %s forget HOST [PORT]
477+
478+
Forget a host in etcd. The default port is %s. Remember that if this host is
479+
used, it will appear back in the list.
480+
`, os.Args[0], defaultHostPort)
481+
os.Exit(2)
482+
}
483+
return fs
484+
}
485+
464486
func newDisableParser() *flag.FlagSet {
465487
fs := flag.NewFlagSet("disable", flag.ExitOnError)
466488
fs.Usage = func() {
@@ -483,6 +505,11 @@ func getHostPortFromCommandLine(args []string) (string, string, error) {
483505
default:
484506
return "", "", fmt.Errorf("wrong number of arguments")
485507
}
508+
if iport, err := strconv.Atoi(port); err != nil {
509+
return "", "", fmt.Errorf("port must be an integer")
510+
} else if iport < 0 || iport > 65535 {
511+
return "", "", fmt.Errorf("port must be in the 0-65535 range")
512+
}
486513
return host, port, nil
487514
}
488515

@@ -521,6 +548,7 @@ func main() {
521548
"version": newVersionParser(),
522549
"show": newShowParser(&csvFlag, &jsonFlag, &allFlag),
523550
"enable": newEnableParser(),
551+
"forget": newForgetParser(),
524552
"disable": newDisableParser(),
525553
}
526554

@@ -577,6 +605,15 @@ func main() {
577605
usage()
578606
}
579607
enableHost(host, port, *configFile)
608+
case "forget":
609+
p := parsers[cmd]
610+
p.Parse(args)
611+
host, port, err := getHostPortFromCommandLine(p.Args())
612+
if err != nil {
613+
fmt.Fprintf(os.Stderr, "ERROR: %s\n\n", err)
614+
usage()
615+
}
616+
forgetHost(host, port, *configFile)
580617
case "disable":
581618
p := parsers[cmd]
582619
p.Parse(args)

pkg/utils/etcd.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,18 @@ func (c *Client) GetHost(hostport string) (*Host, error) {
298298
// not reached
299299
}
300300

301+
// DelHost deletes a host (passed as "host:port") in etcd.
302+
func (c *Client) DelHost(hostport string) error {
303+
key := toHostKey(hostport)
304+
ctx, cancel := context.WithTimeout(context.Background(), c.requestTimeout)
305+
_, err := c.cli.Delete(ctx, key)
306+
cancel()
307+
if err != nil {
308+
return err
309+
}
310+
return nil
311+
}
312+
301313
// SetHost sets a host (passed as "host:port") state and last checked time (ts)
302314
// in etcd.
303315
func (c *Client) SetHost(hostport string, state State, ts time.Time) error {

0 commit comments

Comments
 (0)