4
0

ssh.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package config
  2. import (
  3. "github.com/pkg/errors"
  4. "github.com/smallstep/certificates/authority/provisioner"
  5. "go.step.sm/crypto/jose"
  6. "golang.org/x/crypto/ssh"
  7. )
  8. // SSHConfig contains the user and host keys.
  9. type SSHConfig struct {
  10. HostKey string `json:"hostKey"`
  11. UserKey string `json:"userKey"`
  12. Keys []*SSHPublicKey `json:"keys,omitempty"`
  13. AddUserPrincipal string `json:"addUserPrincipal,omitempty"`
  14. AddUserCommand string `json:"addUserCommand,omitempty"`
  15. Bastion *Bastion `json:"bastion,omitempty"`
  16. }
  17. // Bastion contains the custom properties used on bastion.
  18. type Bastion struct {
  19. Hostname string `json:"hostname"`
  20. User string `json:"user,omitempty"`
  21. Port string `json:"port,omitempty"`
  22. Command string `json:"cmd,omitempty"`
  23. Flags string `json:"flags,omitempty"`
  24. }
  25. // HostTag are tagged with k,v pairs. These tags are how a user is ultimately
  26. // associated with a host.
  27. type HostTag struct {
  28. ID string
  29. Name string
  30. Value string
  31. }
  32. // Host defines expected attributes for an ssh host.
  33. type Host struct {
  34. HostID string `json:"hid"`
  35. HostTags []HostTag `json:"host_tags"`
  36. Hostname string `json:"hostname"`
  37. }
  38. // Validate checks the fields in SSHConfig.
  39. func (c *SSHConfig) Validate() error {
  40. if c == nil {
  41. return nil
  42. }
  43. for _, k := range c.Keys {
  44. if err := k.Validate(); err != nil {
  45. return err
  46. }
  47. }
  48. return nil
  49. }
  50. // SSHPublicKey contains a public key used by federated CAs to keep old signing
  51. // keys for this ca.
  52. type SSHPublicKey struct {
  53. Type string `json:"type"`
  54. Federated bool `json:"federated"`
  55. Key jose.JSONWebKey `json:"key"`
  56. publicKey ssh.PublicKey
  57. }
  58. // Validate checks the fields in SSHPublicKey.
  59. func (k *SSHPublicKey) Validate() error {
  60. switch {
  61. case k.Type == "":
  62. return errors.New("type cannot be empty")
  63. case k.Type != provisioner.SSHHostCert && k.Type != provisioner.SSHUserCert:
  64. return errors.Errorf("invalid type %s, it must be user or host", k.Type)
  65. case !k.Key.IsPublic():
  66. return errors.New("invalid key type, it must be a public key")
  67. }
  68. key, err := ssh.NewPublicKey(k.Key.Key)
  69. if err != nil {
  70. return errors.Wrap(err, "error creating ssh key")
  71. }
  72. k.publicKey = key
  73. return nil
  74. }
  75. // PublicKey returns the ssh public key.
  76. func (k *SSHPublicKey) PublicKey() ssh.PublicKey {
  77. return k.publicKey
  78. }
  79. // SSHKeys represents the SSH User and Host public keys.
  80. type SSHKeys struct {
  81. UserKeys []ssh.PublicKey
  82. HostKeys []ssh.PublicKey
  83. }