2019-11-21 01:23:51 +00:00
|
|
|
package sshutil
|
|
|
|
|
2020-07-25 00:08:32 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
2020-04-02 06:02:10 +00:00
|
|
|
// Hosts are tagged with k,v pairs. These tags are how a user is ultimately
|
|
|
|
// associated with a host.
|
|
|
|
type HostTag struct {
|
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
Value string
|
2019-11-21 01:23:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Host defines expected attributes for an ssh host.
|
|
|
|
type Host struct {
|
2020-04-02 06:02:10 +00:00
|
|
|
HostID string `json:"hid"`
|
|
|
|
HostTags []HostTag `json:"host_tags"`
|
|
|
|
Hostname string `json:"hostname"`
|
2019-11-21 01:23:51 +00:00
|
|
|
}
|
2020-07-25 00:08:32 +00:00
|
|
|
|
|
|
|
// CertType defines the certificate type, it can be a user or a host
|
|
|
|
// certificate.
|
|
|
|
type CertType uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
// UserCert defines a user certificate.
|
|
|
|
UserCert CertType = ssh.UserCert
|
|
|
|
|
|
|
|
// HostCert defines a host certificate.
|
|
|
|
HostCert CertType = ssh.HostCert
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
userString = "user"
|
|
|
|
hostString = "host"
|
|
|
|
)
|
|
|
|
|
2020-07-27 18:06:51 +00:00
|
|
|
// CertTypeFromString returns the CertType for the string "user" and "host".
|
|
|
|
func CertTypeFromString(s string) (CertType, error) {
|
|
|
|
switch strings.ToLower(s) {
|
|
|
|
case userString:
|
|
|
|
return UserCert, nil
|
|
|
|
case hostString:
|
|
|
|
return HostCert, nil
|
|
|
|
default:
|
|
|
|
return 0, errors.Errorf("unknown certificate type '%s'", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 00:08:32 +00:00
|
|
|
// String returns "user" for user certificates and "host" for host certificates.
|
|
|
|
// It will return the empty string for any other value.
|
|
|
|
func (c CertType) String() string {
|
|
|
|
switch c {
|
|
|
|
case UserCert:
|
|
|
|
return userString
|
|
|
|
case HostCert:
|
|
|
|
return hostString
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface for CertType. UserCert
|
|
|
|
// will be marshaled as the string "user" and HostCert as "host".
|
|
|
|
func (c CertType) MarshalJSON() ([]byte, error) {
|
|
|
|
if s := c.String(); s != "" {
|
|
|
|
return []byte(`"` + s + `"`), nil
|
|
|
|
}
|
|
|
|
return nil, errors.Errorf("unknown certificate type %d", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface for CertType.
|
|
|
|
func (c *CertType) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return errors.Wrap(err, "error unmarshaling certificate type")
|
|
|
|
}
|
2020-07-27 18:06:51 +00:00
|
|
|
certType, err := CertTypeFromString(s)
|
|
|
|
if err != nil {
|
2020-07-25 00:08:32 +00:00
|
|
|
return errors.Errorf("error unmarshaling '%s' as a certificate type", s)
|
|
|
|
}
|
2020-07-27 18:06:51 +00:00
|
|
|
*c = certType
|
|
|
|
return nil
|
2020-07-25 00:08:32 +00:00
|
|
|
}
|