Skip to content

Commit 656f35e

Browse files
committed
Use an actual Hosts type when returning ssh hosts
1 parent 50188fc commit 656f35e

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

api/ssh.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/pkg/errors"
1111
"github.com/smallstep/certificates/authority"
1212
"github.com/smallstep/certificates/authority/provisioner"
13+
"github.com/smallstep/certificates/sshutil"
1314
"github.com/smallstep/certificates/templates"
1415
"golang.org/x/crypto/ssh"
1516
)
@@ -24,7 +25,7 @@ type SSHAuthority interface {
2425
GetSSHFederation() (*authority.SSHKeys, error)
2526
GetSSHConfig(typ string, data map[string]string) ([]templates.Output, error)
2627
CheckSSHHost(principal string) (bool, error)
27-
GetSSHHosts(cert *x509.Certificate) ([]string, error)
28+
GetSSHHosts(cert *x509.Certificate) ([]sshutil.Host, error)
2829
GetSSHBastion(user string, hostname string) (*authority.Bastion, error)
2930
}
3031

@@ -83,7 +84,7 @@ type SSHCertificate struct {
8384
// SSHGetHostsResponse is the response object that returns the list of valid
8485
// hosts for SSH.
8586
type SSHGetHostsResponse struct {
86-
Hosts []string `json:"hosts"`
87+
Hosts []sshutil.Host `json:"hosts"`
8788
}
8889

8990
// MarshalJSON implements the json.Marshaler interface. Returns a quoted,

authority/authority.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/pkg/errors"
1212
"github.com/smallstep/certificates/authority/provisioner"
1313
"github.com/smallstep/certificates/db"
14+
"github.com/smallstep/certificates/sshutil"
1415
"github.com/smallstep/certificates/templates"
1516
"github.com/smallstep/cli/crypto/pemutil"
1617
"github.com/smallstep/cli/crypto/x509util"
@@ -40,7 +41,7 @@ type Authority struct {
4041
initOnce bool
4142
// Custom functions
4243
sshBastionFunc func(user, hostname string) (*Bastion, error)
43-
sshGetHostsFunc func(cert *x509.Certificate) ([]string, error)
44+
sshGetHostsFunc func(cert *x509.Certificate) ([]sshutil.Host, error)
4445
getIdentityFunc provisioner.GetIdentityFunc
4546
}
4647

authority/options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/smallstep/certificates/authority/provisioner"
77
"github.com/smallstep/certificates/db"
8+
"github.com/smallstep/certificates/sshutil"
89
)
910

1011
// Option sets options to the Authority.
@@ -36,7 +37,7 @@ func WithSSHBastionFunc(fn func(user, host string) (*Bastion, error)) Option {
3637

3738
// WithSSHGetHosts sets a custom function to get the bastion for a
3839
// given user-host pair.
39-
func WithSSHGetHosts(fn func(cert *x509.Certificate) ([]string, error)) Option {
40+
func WithSSHGetHosts(fn func(cert *x509.Certificate) ([]sshutil.Host, error)) Option {
4041
return func(a *Authority) {
4142
a.sshGetHostsFunc = fn
4243
}

authority/ssh.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/pkg/errors"
1313
"github.com/smallstep/certificates/authority/provisioner"
1414
"github.com/smallstep/certificates/db"
15+
"github.com/smallstep/certificates/sshutil"
1516
"github.com/smallstep/certificates/templates"
1617
"github.com/smallstep/cli/crypto/randutil"
1718
"github.com/smallstep/cli/jose"
@@ -674,17 +675,22 @@ func (a *Authority) CheckSSHHost(principal string) (bool, error) {
674675
}
675676

676677
// GetSSHHosts returns a list of valid host principals.
677-
func (a *Authority) GetSSHHosts(cert *x509.Certificate) ([]string, error) {
678+
func (a *Authority) GetSSHHosts(cert *x509.Certificate) ([]sshutil.Host, error) {
678679
if a.sshGetHostsFunc != nil {
679680
return a.sshGetHostsFunc(cert)
680681
}
681-
hosts, err := a.db.GetSSHHostPrincipals()
682+
hostnames, err := a.db.GetSSHHostPrincipals()
682683
if err != nil {
683684
return nil, &apiError{
684685
err: errors.Wrap(err, "getSSHHosts"),
685686
code: http.StatusInternalServerError,
686687
}
687688
}
689+
690+
hosts := make([]sshutil.Host, len(hostnames))
691+
for i, hn := range hostnames {
692+
hosts[i] = sshutil.Host{Hostname: hn}
693+
}
688694
return hosts, nil
689695
}
690696

sshutil/types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package sshutil
2+
3+
// HostGroup defines expected attributes for a host group that a host might belong to.
4+
type HostGroup struct {
5+
ID string
6+
Name string
7+
}
8+
9+
// Host defines expected attributes for an ssh host.
10+
type Host struct {
11+
HostID string `json:"hid"`
12+
HostGroups []HostGroup `json:"host_groups"`
13+
Hostname string `json:"hostname"`
14+
}

0 commit comments

Comments
 (0)