Skip to content

Commit

Permalink
feat: allow logging in by using subnets (a.b.c.d/XY)
Browse files Browse the repository at this point in the history
cherry pick feature from vdombrovski@50ca01b

relaxed hostkey checking on subnet matches (otherwise hostkey get used for all hosts in subnet)
  • Loading branch information
libvoid committed Jun 6, 2023
1 parent 655f9c9 commit bcac26d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/bastion/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ func (c authContext) userType() userType {

func dynamicHostKey(db *gorm.DB, host *dbmodels.Host) gossh.HostKeyCallback {
return func(hostname string, remote net.Addr, key gossh.PublicKey) error {

if strings.Contains(host.Name, `/`) {
return nil
}

if len(host.HostKey) == 0 {
log.Println("Discovering host fingerprint...")
return db.Model(host).Update("HostKey", key.Marshal()).Error
Expand Down
22 changes: 21 additions & 1 deletion pkg/dbmodels/dbmodels.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"time"
"net"

gossh "golang.org/x/crypto/ssh"
"gorm.io/gorm"
Expand Down Expand Up @@ -191,6 +192,7 @@ func GenericNameOrID(db *gorm.DB, identifiers []string) *gorm.DB {
func (host *Host) DialAddr() string {
return fmt.Sprintf("%s:%d", host.Hostname(), host.Port())
}

func (host *Host) String() string {
if host.URL != "" {
return host.URL
Expand Down Expand Up @@ -291,9 +293,27 @@ func HostsByIdentifiers(db *gorm.DB, identifiers []string) *gorm.DB {
}
func HostByName(db *gorm.DB, name string) (*Host, error) {
var host Host
db.Preload("SSHKey").Where("name = ?", name).Find(&host)
db.Preload("SSHKey").Where("name = ? AND name NOT LIKE '%/%'", name).Find(&host)
if host.Name == "" {
// FIXME: add available hosts
ips, err := net.LookupIP(name)
if err != nil {
return nil, err
}
var subnetHosts []*Host
db.Preload("SSHKey").Where("name LIKE '%/%'", name).Find(&subnetHosts)
for _, subnet := range subnetHosts {
_, ipnet, err := net.ParseCIDR(subnet.Name)
if err != nil {
return nil, err
}
for _, ip := range ips {
if ipnet.Contains(ip) {
subnet.URL = strings.Replace(subnet.URL, "*", ip.String(), -1)
return subnet, nil
}
}
}
return nil, fmt.Errorf("no such target: %q", name)
}
return &host, nil
Expand Down

0 comments on commit bcac26d

Please sign in to comment.