Skip to content

Commit a87ae6c

Browse files
committed
for enable/disable/forget, don't read the config, read etcd
1 parent 05933f7 commit a87ae6c

File tree

4 files changed

+25
-95
lines changed

4 files changed

+25
-95
lines changed

cmd/sshproxyctl/sshproxyctl.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,19 +769,25 @@ Should be used like this:
769769
func getHostPortFromCommandLine(allFlag bool, hostsNodeset string, portsNodeset string, configFile string) ([]string, error) {
770770
_, nodesetDlclose, nodesetExpand := nodesets.InitExpander()
771771
defer nodesetDlclose()
772+
cli := mustInitEtcdClient(configFile)
773+
defer cli.Close()
772774

773-
configDests, err := utils.LoadAllDestsFromConfig(configFile)
775+
etcdFlatHosts, err := cli.GetAllHosts()
774776
if err != nil {
775-
return []string{}, fmt.Errorf("%s", err)
777+
return []string{}, fmt.Errorf("ERROR: getting hosts from etcd: %v", err)
778+
}
779+
etcdHosts := make([]string, len(etcdFlatHosts))
780+
for i, h := range etcdFlatHosts {
781+
etcdHosts[i] = h.Hostname
776782
}
777783

778784
if allFlag && portsNodeset == "" {
779-
return configDests, nil
785+
return etcdHosts, nil
780786
}
781787

782788
var hosts []string
783789
var ports []string
784-
for _, configDest := range configDests {
790+
for _, configDest := range etcdHosts {
785791
host, port, err := utils.SplitHostPort(configDest)
786792
if err != nil {
787793
return []string{}, fmt.Errorf("%s", err)

doc/sshproxyctl.txt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,30 @@ COMMANDS
4242
*enable -all|-host HOST [-port PORT]*::
4343
Enable a destination host in etcd if the host was previously disabled
4444
by the 'disable' command (see below). If '-all' is specified instead
45-
of '-host', all the hosts present in the configuration are enabled. If
46-
no '-port' is specified, all the ports present in the configuration are
47-
used. 'HOST' and 'PORT' can be nodesets. If libnodeset.so (from
48-
https://github.com/fdiakh/nodeset-rs) is available, clustershell groups
49-
can also be used.
45+
of '-host', all the hosts present in etcd are enabled. If no '-port'
46+
is specified, all the ports present in etcd are used. 'HOST' and
47+
'PORT' can be nodesets. If libnodeset.so (from
48+
https://github.com/fdiakh/nodeset-rs) is available, clustershell
49+
groups can also be used.
5050

5151
*disable -all|-host HOST [-port PORT]*::
5252
Disable a destination host in etcd. A disabled host will not be
5353
proposed as a destination. The only way to enable it again is to send
5454
the 'enable' command. It could be used for host maintenance. If
55-
'-all' is specified instead of '-host', all the hosts present in the
56-
configuration are enabled. If no '-port' is specified, all the ports
57-
present in the configuration are used. 'HOST' and 'PORT' can be
58-
nodesets. If libnodeset.so (from https://github.com/fdiakh/nodeset-rs)
59-
is available, clustershell groups can also be used.
55+
'-all' is specified instead of '-host', all the hosts present in etcd
56+
are enabled. If no '-port' is specified, all the ports present in etcd
57+
are used. 'HOST' and 'PORT' can be nodesets. If libnodeset.so (from
58+
https://github.com/fdiakh/nodeset-rs) is available, clustershell
59+
groups can also be used.
6060

6161
*forget host -all|-host HOST [-port PORT]*::
6262
Forget a host in etcd. Remember that if this host is used, it will
6363
appear back in the list. If '-all' is specified instead of '-host',
64-
all the hosts present in the configuration are forgotten. If no '-port'
65-
is specified, all the ports present in the configuration are used.
66-
'HOST' and 'PORT' can be nodesets. If libnodeset.so (from
67-
https://github.com/fdiakh/nodeset-rs) is available, clustershell groups
68-
can also be used.
64+
all the hosts present in etcd are forgotten. If no '-port' is
65+
specified, all the ports present in etcd are used. 'HOST' and 'PORT'
66+
can be nodesets. If libnodeset.so (from
67+
https://github.com/fdiakh/nodeset-rs) is available, clustershell
68+
groups can also be used.
6969

7070
*forget error_banner*::
7171
Remove the error banner in etcd.

pkg/utils/config.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -335,31 +335,6 @@ func replace(src string, replacer *patternReplacer) string {
335335
return replacer.Regexp.ReplaceAllString(src, replacer.Text)
336336
}
337337

338-
// LoadAllDestsFromConfig loads configuration file and returns all defined destinations.
339-
func LoadAllDestsFromConfig(filename string) ([]string, error) {
340-
yamlFile, err := os.ReadFile(filename)
341-
if err != nil {
342-
return nil, err
343-
}
344-
var config Config
345-
if err := yaml.Unmarshal(yamlFile, &config); err != nil {
346-
return nil, err
347-
}
348-
for _, override := range config.Overrides {
349-
if override.Dest != nil {
350-
config.Dest = append(config.Dest, override.Dest...)
351-
}
352-
}
353-
// expand destination nodesets
354-
_, nodesetDlclose, nodesetExpand := nodesets.InitExpander()
355-
defer nodesetDlclose()
356-
dsts, err := nodesetExpand(strings.Join(config.Dest, ","))
357-
if err != nil {
358-
return nil, fmt.Errorf("invalid nodeset: %s", err)
359-
}
360-
return dsts, nil
361-
}
362-
363338
// LoadConfig load configuration file and adapt it according to specified user/group/sshdHostPort.
364339
func LoadConfig(filename, currentUsername, sid string, start time.Time, groups map[string]bool, sshdHostPort string) (*Config, error) {
365340
if cachedConfig.ready {

pkg/utils/config_test.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"fmt"
1515
"reflect"
1616
"regexp"
17-
"slices"
1817
"testing"
1918
"time"
2019
)
@@ -60,56 +59,6 @@ func BenchmarkReplace(b *testing.B) {
6059
}
6160
}
6261

63-
var loadAllDestsFromConfigTests = []struct {
64-
filename string
65-
want []string
66-
err string
67-
}{
68-
{"nonexistingfile.yaml", []string{}, "open nonexistingfile.yaml: no such file or directory"},
69-
{"../../test/configInvalid.yaml", []string{}, "yaml: found character that cannot start any token"},
70-
{"../../test/configDestNodesetError.yaml", []string{}, "invalid nodeset: cannont convert ending range to integer a - rangeset parse error"},
71-
{"../../test/configDefault.yaml", []string{"127.0.0.1"}, ""},
72-
{"../../test/config.yaml", []string{
73-
"192.168.0.1",
74-
"192.168.0.2",
75-
"192.168.0.3",
76-
"192.168.0.4",
77-
"192.168.0.5",
78-
"192.168.0.6",
79-
"192.168.0.7",
80-
"192.168.0.8",
81-
"192.168.0.9",
82-
"host5:4222",
83-
"server1:12345",
84-
}, ""},
85-
}
86-
87-
func TestLoadAllDestsFromConfig(t *testing.T) {
88-
for _, tt := range loadAllDestsFromConfigTests {
89-
config, err := LoadAllDestsFromConfig(tt.filename)
90-
if err == nil && tt.err != "" {
91-
t.Errorf("got no error, want %s", tt.err)
92-
} else if err != nil && err.Error() != tt.err {
93-
t.Errorf("ERROR: %s, want %s", err, tt.err)
94-
} else if err == nil {
95-
slices.Sort(config)
96-
if !reflect.DeepEqual(config, tt.want) {
97-
t.Errorf("want:\n%v\ngot:\n%v", tt.want, config)
98-
}
99-
}
100-
}
101-
}
102-
103-
func BenchmarkLoadAllDestsFromConfig(b *testing.B) {
104-
for _, tt := range loadAllDestsFromConfigTests {
105-
b.Run(tt.filename, func(b *testing.B) {
106-
for i := 0; i < b.N; i++ {
107-
LoadAllDestsFromConfig(tt.filename)
108-
}
109-
})
110-
}
111-
}
112-
11362
var loadConfigTests = []struct {
11463
filename, username string
11564
want []string

0 commit comments

Comments
 (0)