fix: interface_name strategy, instance hosts in sftp/scp, alias rewrites
Found by the new Docker test environment against a real NetBox 4.7: - The interface_name strategy filtered IP addresses with interface_name and vminterface_name, which NetBox ignores, so it returned the first address of the host instead of the named interface's. Use the real filters interface and vminterface; a missing interface (HTTP 400) falls through to the next strategy. - sftp and scp took instance-qualified hosts (prod/web01:/path) for local paths, because a "/" comes before the ":". Such arguments are remote when the prefix is a configured NetBox instance. - Shell aliases of instance-qualified hosts ran the shortcut rules on "instance/host", so anchored rewrite rules never matched. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3c9e54b254
commit
55e7a5106b
10 files changed
+144
-11
No files matched your search
@@ -59,7 +59,7 @@ func (c Client) Parse(args []string) *ParsedArgs {
|
||||
func (c Client) parsed(args []string, i int) *ParsedArgs {
|
||||
dest, path := args[i], ""
|
||||
if c.paths {
|
||||
if d, p, remote := splitRemote(dest); remote {
|
||||
if d, p, remote := c.splitRemote(dest); remote {
|
||||
dest, path = d, p
|
||||
}
|
||||
}
|
||||
@@ -88,7 +88,7 @@ func (c Client) replaceHost(args []string, dests []int, newHost string) []string
|
||||
prefix, original = original[:at+1], original[at+1:]
|
||||
}
|
||||
if c.paths {
|
||||
if _, path, remote := splitRemote(original); remote {
|
||||
if _, path, remote := c.splitRemote(original); remote {
|
||||
suffix = ":" + path
|
||||
}
|
||||
}
|
||||
|
||||
+28
-3
@@ -13,6 +13,18 @@ type Client struct {
|
||||
port byte // flag for the port: -p (ssh) or -P (sftp, scp)
|
||||
user byte // flag for the login user; 0 when only user@ or -o User work
|
||||
paths bool // destinations are host[:path]
|
||||
// instances are the configured NetBox instance names; "inst/host:path"
|
||||
// is remote only for these, so local paths like "dir/file:x" stay local.
|
||||
instances map[string]bool
|
||||
}
|
||||
|
||||
// WithInstances returns c aware of the NetBox instance prefixes in host:path.
|
||||
func (c Client) WithInstances(names []string) Client {
|
||||
c.instances = make(map[string]bool, len(names))
|
||||
for _, name := range names {
|
||||
c.instances[strings.ToLower(name)] = true
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -61,7 +73,7 @@ func (c Client) Operands(args []string) ([]Operand, error) {
|
||||
if strings.HasPrefix(arg, "scp://") || strings.HasPrefix(arg, "sftp://") {
|
||||
return nil, fmt.Errorf("%q: use host:path instead of a URI", arg)
|
||||
}
|
||||
dest, path, remote := splitRemote(arg)
|
||||
dest, path, remote := c.splitRemote(arg)
|
||||
if !remote {
|
||||
continue
|
||||
}
|
||||
@@ -105,9 +117,22 @@ func (c Client) insertionPoint(args []string) int {
|
||||
return len(args)
|
||||
}
|
||||
|
||||
// splitRemote mirrors scp's colon(): an argument is remote when a ':' comes
|
||||
// splitRemote is splitHostPath with netssh's [user@]instance/host:path form.
|
||||
func (c Client) splitRemote(arg string) (dest, path string, remote bool) {
|
||||
if head, rest, ok := strings.Cut(arg, "/"); ok {
|
||||
instance := head[strings.LastIndex(head, "@")+1:]
|
||||
if c.instances[strings.ToLower(instance)] {
|
||||
if d, p, r := splitHostPath(rest); r {
|
||||
return head + "/" + d, p, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return splitHostPath(arg)
|
||||
}
|
||||
|
||||
// splitHostPath mirrors scp's colon(): an argument is remote when a ':' comes
|
||||
// before any '/'. A leading "[" starts a bracketed IPv6 address.
|
||||
func splitRemote(arg string) (dest, path string, remote bool) {
|
||||
func splitHostPath(arg string) (dest, path string, remote bool) {
|
||||
if strings.HasPrefix(arg, "/") || strings.HasPrefix(arg, ".") {
|
||||
return "", "", false
|
||||
}
|
||||
|
||||
@@ -154,3 +154,27 @@ func TestApplyInsertsOptionsBeforeDoubleDash(t *testing.T) {
|
||||
t.Fatalf("got %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// Instance-qualified hosts contain "/" before the ":" of host:path; only
|
||||
// configured instances make such an argument remote.
|
||||
func TestInstanceQualifiedHostPath(t *testing.T) {
|
||||
scp := SCP.WithInstances([]string{"prod"})
|
||||
ops, err := scp.Operands([]string{"a.txt", "ops@prod/web01:/tmp/", "dir/file:x"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if want := []Operand{{Idx: 1, User: "ops", Host: "prod/web01", Path: "/tmp/"}}; !reflect.DeepEqual(ops, want) {
|
||||
t.Fatalf("got %+v", ops)
|
||||
}
|
||||
got := scp.Apply([]string{"a.txt", "ops@prod/web01:/tmp/"}, []int{1}, Host{Name: "web01", IP: "192.0.2.1"}, config.SSHOptions{}, WrapperFlags{}).Args
|
||||
if want := []string{"a.txt", "ops@192.0.2.1:/tmp/"}; !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("apply: %v", got)
|
||||
}
|
||||
sftp := SFTP.WithInstances([]string{"prod"})
|
||||
if p := sftp.Parse([]string{"prod/web01:/var/log"}); p == nil || p.Host != "prod/web01" || p.Path != "/var/log" {
|
||||
t.Fatalf("sftp: %+v", p)
|
||||
}
|
||||
if ops, _ := SCP.Operands([]string{"prod/web01:/tmp/", "."}); len(ops) != 0 {
|
||||
t.Fatalf("unknown instance must stay local: %+v", ops)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user