From 4adaf83fd3b544f2eb5676bb1beb406d92a1bb5a Mon Sep 17 00:00:00 2001 From: Jonathan Lestrelin Date: Fri, 7 Jun 2019 18:15:25 +0200 Subject: [PATCH] Accept to create a host using an ip as name by relaxing unix_user constraint on name and not splitting on `.` if an ip is detected. --- pkg/bastion/shell.go | 12 +++++++++++- pkg/dbmodels/dbmodels.go | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/bastion/shell.go b/pkg/bastion/shell.go index e13dcc9..315ab0c 100644 --- a/pkg/bastion/shell.go +++ b/pkg/bastion/shell.go @@ -7,6 +7,7 @@ import ( "fmt" "net/url" "os" + "regexp" "runtime" "strings" "time" @@ -685,7 +686,16 @@ GLOBAL OPTIONS: if c.String("password") != "" { host.Password = c.String("password") } - host.Name = strings.Split(host.Hostname(), ".")[0] + matched, err := regexp.MatchString(`^([0-9]{1,3}.){3}.([0-9]{1,3})$`, host.Hostname()) + if err != nil { + return err + } + if matched { + host.Name = host.Hostname() + } else { + host.Name = strings.Split(host.Hostname(), ".")[0] + } + if c.String("hop") != "" { hop, err := dbmodels.HostByName(db, c.String("hop")) if err != nil { diff --git a/pkg/dbmodels/dbmodels.go b/pkg/dbmodels/dbmodels.go index b3f0d00..967856a 100644 --- a/pkg/dbmodels/dbmodels.go +++ b/pkg/dbmodels/dbmodels.go @@ -53,7 +53,7 @@ type SSHKey struct { type Host struct { // FIXME: use uuid for ID gorm.Model - Name string `gorm:"size:32" valid:"required,length(1|32),unix_user"` + Name string `gorm:"size:32" valid:"required,length(1|32)"` Addr string `valid:"optional"` // FIXME: to be removed in a future version in favor of URL User string `valid:"optional"` // FIXME: to be removed in a future version in favor of URL Password string `valid:"optional"` // FIXME: to be removed in a future version in favor of URL