2019-07-24 01:46:43 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-07-29 19:52:13 +00:00
|
|
|
"context"
|
2019-11-20 20:59:48 +00:00
|
|
|
"crypto/x509"
|
2019-07-24 01:46:43 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-11-27 01:45:32 +00:00
|
|
|
"time"
|
2019-07-24 01:46:43 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-03-18 13:25:34 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
|
|
|
|
"github.com/smallstep/certificates/api/read"
|
2022-03-30 08:22:22 +00:00
|
|
|
"github.com/smallstep/certificates/api/render"
|
2019-09-25 02:12:13 +00:00
|
|
|
"github.com/smallstep/certificates/authority"
|
2021-05-03 19:48:20 +00:00
|
|
|
"github.com/smallstep/certificates/authority/config"
|
2019-07-24 01:46:43 +00:00
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2019-12-16 07:54:25 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2019-10-04 02:03:38 +00:00
|
|
|
"github.com/smallstep/certificates/templates"
|
2019-07-24 01:46:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SSHAuthority is the interface implemented by a SSH CA authority.
|
|
|
|
type SSHAuthority interface {
|
2020-07-23 01:24:45 +00:00
|
|
|
SignSSH(ctx context.Context, key ssh.PublicKey, opts provisioner.SignSSHOptions, signOpts ...provisioner.SignOption) (*ssh.Certificate, error)
|
2020-03-11 02:01:45 +00:00
|
|
|
RenewSSH(ctx context.Context, cert *ssh.Certificate) (*ssh.Certificate, error)
|
|
|
|
RekeySSH(ctx context.Context, cert *ssh.Certificate, key ssh.PublicKey, signOpts ...provisioner.SignOption) (*ssh.Certificate, error)
|
|
|
|
SignSSHAddUser(ctx context.Context, key ssh.PublicKey, cert *ssh.Certificate) (*ssh.Certificate, error)
|
2021-05-03 19:48:20 +00:00
|
|
|
GetSSHRoots(ctx context.Context) (*config.SSHKeys, error)
|
|
|
|
GetSSHFederation(ctx context.Context) (*config.SSHKeys, error)
|
2020-03-11 02:01:45 +00:00
|
|
|
GetSSHConfig(ctx context.Context, typ string, data map[string]string) ([]templates.Output, error)
|
2019-12-10 07:14:56 +00:00
|
|
|
CheckSSHHost(ctx context.Context, principal string, token string) (bool, error)
|
2021-05-03 19:48:20 +00:00
|
|
|
GetSSHHosts(ctx context.Context, cert *x509.Certificate) ([]config.Host, error)
|
|
|
|
GetSSHBastion(ctx context.Context, user string, hostname string) (*config.Bastion, error)
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 01:35:28 +00:00
|
|
|
// SSHSignRequest is the request body of an SSH certificate request.
|
|
|
|
type SSHSignRequest struct {
|
2019-11-20 19:51:25 +00:00
|
|
|
PublicKey []byte `json:"publicKey"` // base64 encoded
|
|
|
|
OTT string `json:"ott"`
|
|
|
|
CertType string `json:"certType,omitempty"`
|
2020-07-30 21:59:17 +00:00
|
|
|
KeyID string `json:"keyID,omitempty"`
|
2019-11-20 19:51:25 +00:00
|
|
|
Principals []string `json:"principals,omitempty"`
|
|
|
|
ValidAfter TimeDuration `json:"validAfter,omitempty"`
|
|
|
|
ValidBefore TimeDuration `json:"validBefore,omitempty"`
|
|
|
|
AddUserPublicKey []byte `json:"addUserPublicKey,omitempty"`
|
|
|
|
IdentityCSR CertificateRequest `json:"identityCSR,omitempty"`
|
2020-07-30 21:59:17 +00:00
|
|
|
TemplateData json.RawMessage `json:"templateData,omitempty"`
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 01:35:28 +00:00
|
|
|
// Validate validates the SSHSignRequest.
|
|
|
|
func (s *SSHSignRequest) Validate() error {
|
2019-09-26 20:22:07 +00:00
|
|
|
switch {
|
|
|
|
case s.CertType != "" && s.CertType != provisioner.SSHUserCert && s.CertType != provisioner.SSHHostCert:
|
2021-11-19 02:17:36 +00:00
|
|
|
return errs.BadRequest("invalid certType '%s'", s.CertType)
|
2019-09-26 20:22:07 +00:00
|
|
|
case len(s.PublicKey) == 0:
|
2021-11-19 02:17:36 +00:00
|
|
|
return errs.BadRequest("missing or empty publicKey")
|
2021-10-08 18:59:57 +00:00
|
|
|
case s.OTT == "":
|
2021-11-19 02:17:36 +00:00
|
|
|
return errs.BadRequest("missing or empty ott")
|
2019-09-26 20:22:07 +00:00
|
|
|
default:
|
2019-11-20 19:51:25 +00:00
|
|
|
// Validate identity signature if provided
|
|
|
|
if s.IdentityCSR.CertificateRequest != nil {
|
|
|
|
if err := s.IdentityCSR.CertificateRequest.CheckSignature(); err != nil {
|
2021-11-19 02:17:36 +00:00
|
|
|
return errs.BadRequestErr(err, "invalid identityCSR")
|
2019-11-20 19:51:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-26 20:22:07 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:35:28 +00:00
|
|
|
// SSHSignResponse is the response object that returns the SSH certificate.
|
|
|
|
type SSHSignResponse struct {
|
2019-11-20 19:51:25 +00:00
|
|
|
Certificate SSHCertificate `json:"crt"`
|
|
|
|
AddUserCertificate *SSHCertificate `json:"addUserCrt,omitempty"`
|
|
|
|
IdentityCertificate []Certificate `json:"identityCrt,omitempty"`
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 01:35:28 +00:00
|
|
|
// SSHRootsResponse represents the response object that returns the SSH user and
|
2019-09-26 20:22:07 +00:00
|
|
|
// host keys.
|
2019-10-09 01:35:28 +00:00
|
|
|
type SSHRootsResponse struct {
|
2019-10-09 01:09:41 +00:00
|
|
|
UserKeys []SSHPublicKey `json:"userKey,omitempty"`
|
|
|
|
HostKeys []SSHPublicKey `json:"hostKey,omitempty"`
|
2019-09-25 02:12:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 01:46:43 +00:00
|
|
|
// SSHCertificate represents the response SSH certificate.
|
|
|
|
type SSHCertificate struct {
|
2019-08-05 23:06:05 +00:00
|
|
|
*ssh.Certificate `json:"omitempty"`
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// SSHGetHostsResponse is the response object that returns the list of valid
|
|
|
|
// hosts for SSH.
|
2019-10-25 20:47:49 +00:00
|
|
|
type SSHGetHostsResponse struct {
|
2021-05-03 19:48:20 +00:00
|
|
|
Hosts []config.Host `json:"hosts"`
|
2019-10-25 20:47:49 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 04:05:36 +00:00
|
|
|
// MarshalJSON implements the json.Marshaler interface. Returns a quoted,
|
|
|
|
// base64 encoded, openssh wire format version of the certificate.
|
2019-07-24 01:46:43 +00:00
|
|
|
func (c SSHCertificate) MarshalJSON() ([]byte, error) {
|
|
|
|
if c.Certificate == nil {
|
|
|
|
return []byte("null"), nil
|
|
|
|
}
|
|
|
|
s := base64.StdEncoding.EncodeToString(c.Certificate.Marshal())
|
|
|
|
return []byte(`"` + s + `"`), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface. The certificate is
|
2019-09-09 04:05:36 +00:00
|
|
|
// expected to be a quoted, base64 encoded, openssh wire formatted block of bytes.
|
2019-07-24 01:46:43 +00:00
|
|
|
func (c *SSHCertificate) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return errors.Wrap(err, "error decoding certificate")
|
|
|
|
}
|
2019-08-03 00:48:34 +00:00
|
|
|
if s == "" {
|
|
|
|
c.Certificate = nil
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-24 01:46:43 +00:00
|
|
|
certData, err := base64.StdEncoding.DecodeString(s)
|
|
|
|
if err != nil {
|
2019-09-09 04:05:36 +00:00
|
|
|
return errors.Wrap(err, "error decoding ssh certificate")
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
pub, err := ssh.ParsePublicKey(certData)
|
|
|
|
if err != nil {
|
2019-09-09 04:05:36 +00:00
|
|
|
return errors.Wrap(err, "error parsing ssh certificate")
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
cert, ok := pub.(*ssh.Certificate)
|
|
|
|
if !ok {
|
2019-09-09 04:05:36 +00:00
|
|
|
return errors.Errorf("error decoding ssh certificate: %T is not an *ssh.Certificate", pub)
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
c.Certificate = cert
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 20:22:07 +00:00
|
|
|
// SSHPublicKey represents a public key in a response object.
|
|
|
|
type SSHPublicKey struct {
|
|
|
|
ssh.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements the json.Marshaler interface. Returns a quoted,
|
|
|
|
// base64 encoded, openssh wire format version of the public key.
|
|
|
|
func (p *SSHPublicKey) MarshalJSON() ([]byte, error) {
|
|
|
|
if p == nil || p.PublicKey == nil {
|
|
|
|
return []byte("null"), nil
|
|
|
|
}
|
|
|
|
s := base64.StdEncoding.EncodeToString(p.PublicKey.Marshal())
|
|
|
|
return []byte(`"` + s + `"`), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements the json.Unmarshaler interface. The public key is
|
|
|
|
// expected to be a quoted, base64 encoded, openssh wire formatted block of
|
|
|
|
// bytes.
|
|
|
|
func (p *SSHPublicKey) UnmarshalJSON(data []byte) error {
|
|
|
|
var s string
|
|
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
|
|
return errors.Wrap(err, "error decoding ssh public key")
|
|
|
|
}
|
|
|
|
if s == "" {
|
|
|
|
p.PublicKey = nil
|
2019-07-24 01:46:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-09-26 20:22:07 +00:00
|
|
|
data, err := base64.StdEncoding.DecodeString(s)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error decoding ssh public key")
|
|
|
|
}
|
|
|
|
pub, err := ssh.ParsePublicKey(data)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error parsing ssh public key")
|
|
|
|
}
|
|
|
|
p.PublicKey = pub
|
|
|
|
return nil
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 02:03:38 +00:00
|
|
|
// Template represents the output of a template.
|
|
|
|
type Template = templates.Output
|
|
|
|
|
|
|
|
// SSHConfigRequest is the request body used to get the SSH configuration
|
|
|
|
// templates.
|
|
|
|
type SSHConfigRequest struct {
|
2019-10-05 00:08:42 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Data map[string]string `json:"data"`
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks the values of the SSHConfigurationRequest.
|
|
|
|
func (r *SSHConfigRequest) Validate() error {
|
|
|
|
switch r.Type {
|
|
|
|
case "":
|
|
|
|
r.Type = provisioner.SSHUserCert
|
|
|
|
return nil
|
|
|
|
case provisioner.SSHUserCert, provisioner.SSHHostCert:
|
|
|
|
return nil
|
|
|
|
default:
|
2021-11-19 02:17:36 +00:00
|
|
|
return errs.BadRequest("invalid type '%s'", r.Type)
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHConfigResponse is the response that returns the rendered templates.
|
|
|
|
type SSHConfigResponse struct {
|
|
|
|
UserTemplates []Template `json:"userTemplates,omitempty"`
|
|
|
|
HostTemplates []Template `json:"hostTemplates,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-10-10 20:08:57 +00:00
|
|
|
// SSHCheckPrincipalRequest is the request body used to check if a principal
|
|
|
|
// certificate has been created. Right now it only supported for hosts
|
|
|
|
// certificates.
|
|
|
|
type SSHCheckPrincipalRequest struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Principal string `json:"principal"`
|
2019-12-10 07:14:56 +00:00
|
|
|
Token string `json:"token,omitempty"`
|
2019-10-10 20:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks the check principal request.
|
|
|
|
func (r *SSHCheckPrincipalRequest) Validate() error {
|
|
|
|
switch {
|
|
|
|
case r.Type != provisioner.SSHHostCert:
|
2021-11-19 02:17:36 +00:00
|
|
|
return errs.BadRequest("unsupported type '%s'", r.Type)
|
2019-10-10 20:08:57 +00:00
|
|
|
case r.Principal == "":
|
2021-11-19 02:17:36 +00:00
|
|
|
return errs.BadRequest("missing or empty principal")
|
2019-10-10 20:08:57 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHCheckPrincipalResponse is the response body used to check if a principal
|
|
|
|
// exists.
|
|
|
|
type SSHCheckPrincipalResponse struct {
|
|
|
|
Exists bool `json:"exists"`
|
|
|
|
}
|
|
|
|
|
2019-11-15 02:24:58 +00:00
|
|
|
// SSHBastionRequest is the request body used to get the bastion for a given
|
|
|
|
// host.
|
|
|
|
type SSHBastionRequest struct {
|
|
|
|
User string `json:"user"`
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate checks the values of the SSHBastionRequest.
|
|
|
|
func (r *SSHBastionRequest) Validate() error {
|
|
|
|
if r.Hostname == "" {
|
2021-11-19 02:17:36 +00:00
|
|
|
return errs.BadRequest("missing or empty hostname")
|
2019-11-15 02:24:58 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SSHBastionResponse is the response body used to return the bastion for a
|
|
|
|
// given host.
|
|
|
|
type SSHBastionResponse struct {
|
2021-05-03 19:48:20 +00:00
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
Bastion *config.Bastion `json:"bastion,omitempty"`
|
2019-11-15 02:24:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 01:35:28 +00:00
|
|
|
// SSHSign is an HTTP handler that reads an SignSSHRequest with a one-time-token
|
2019-07-24 01:46:43 +00:00
|
|
|
// (ott) from the body and creates a new SSH certificate with the information in
|
|
|
|
// the request.
|
2022-04-26 19:58:40 +00:00
|
|
|
func SSHSign(w http.ResponseWriter, r *http.Request) {
|
2019-10-09 01:35:28 +00:00
|
|
|
var body SSHSignRequest
|
2022-03-18 13:25:34 +00:00
|
|
|
if err := read.JSON(r.Body, &body); err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.BadRequestErr(err, "error reading request body"))
|
2019-07-24 01:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logOtt(w, body.OTT)
|
|
|
|
if err := body.Validate(); err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2019-07-24 01:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-26 01:42:32 +00:00
|
|
|
publicKey, err := ssh.ParsePublicKey(body.PublicKey)
|
2019-07-24 01:46:43 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.BadRequestErr(err, "error parsing publicKey"))
|
2019-07-24 01:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-03 00:48:34 +00:00
|
|
|
var addUserPublicKey ssh.PublicKey
|
|
|
|
if body.AddUserPublicKey != nil {
|
|
|
|
addUserPublicKey, err = ssh.ParsePublicKey(body.AddUserPublicKey)
|
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.BadRequestErr(err, "error parsing addUserPublicKey"))
|
2019-08-03 00:48:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 01:24:45 +00:00
|
|
|
opts := provisioner.SignSSHOptions{
|
2020-07-30 21:59:17 +00:00
|
|
|
CertType: body.CertType,
|
|
|
|
KeyID: body.KeyID,
|
|
|
|
Principals: body.Principals,
|
|
|
|
ValidBefore: body.ValidBefore,
|
|
|
|
ValidAfter: body.ValidAfter,
|
|
|
|
TemplateData: body.TemplateData,
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-11 02:01:45 +00:00
|
|
|
ctx := provisioner.NewContextWithMethod(r.Context(), provisioner.SSHSignMethod)
|
2022-05-20 21:41:44 +00:00
|
|
|
ctx = provisioner.NewContextWithToken(ctx, body.OTT)
|
2022-04-26 19:58:40 +00:00
|
|
|
|
|
|
|
a := mustAuthority(ctx)
|
|
|
|
signOpts, err := a.Authorize(ctx, body.OTT)
|
2019-07-24 01:46:43 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.UnauthorizedErr(err))
|
2019-07-24 01:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
cert, err := a.SignSSH(ctx, publicKey, opts, signOpts...)
|
2019-07-24 01:46:43 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.ForbiddenErr(err, "error signing ssh certificate"))
|
2019-07-24 01:46:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-05 23:06:05 +00:00
|
|
|
var addUserCertificate *SSHCertificate
|
2020-04-24 02:42:55 +00:00
|
|
|
if addUserPublicKey != nil && authority.IsValidForAddUser(cert) == nil {
|
2022-04-26 19:58:40 +00:00
|
|
|
addUserCert, err := a.SignSSHAddUser(ctx, addUserPublicKey, cert)
|
2019-08-03 00:48:34 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.ForbiddenErr(err, "error signing ssh certificate"))
|
2019-08-03 00:48:34 +00:00
|
|
|
return
|
|
|
|
}
|
2019-08-05 23:06:05 +00:00
|
|
|
addUserCertificate = &SSHCertificate{addUserCert}
|
2019-08-03 00:48:34 +00:00
|
|
|
}
|
|
|
|
|
2019-11-20 19:51:25 +00:00
|
|
|
// Sign identity certificate if available.
|
|
|
|
var identityCertificate []Certificate
|
|
|
|
if cr := body.IdentityCSR.CertificateRequest; cr != nil {
|
2020-03-11 02:01:45 +00:00
|
|
|
ctx := authority.NewContextWithSkipTokenReuse(r.Context())
|
2019-11-20 19:51:25 +00:00
|
|
|
ctx = provisioner.NewContextWithMethod(ctx, provisioner.SignMethod)
|
2022-04-26 19:58:40 +00:00
|
|
|
signOpts, err := a.Authorize(ctx, body.OTT)
|
2019-11-20 19:51:25 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.UnauthorizedErr(err))
|
2019-11-20 19:51:25 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-31 00:33:04 +00:00
|
|
|
|
|
|
|
// Enforce the same duration as ssh certificate.
|
|
|
|
signOpts = append(signOpts, &identityModifier{
|
|
|
|
NotBefore: time.Unix(int64(cert.ValidAfter), 0),
|
|
|
|
NotAfter: time.Unix(int64(cert.ValidBefore), 0),
|
|
|
|
})
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
certChain, err := a.Sign(cr, provisioner.SignOptions{}, signOpts...)
|
2019-11-20 19:51:25 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.ForbiddenErr(err, "error signing identity certificate"))
|
2019-11-20 19:51:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
identityCertificate = certChainToPEM(certChain)
|
|
|
|
}
|
2023-05-04 13:33:06 +00:00
|
|
|
LogSSHCertificate(w, cert)
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSONStatus(w, &SSHSignResponse{
|
2019-11-20 19:51:25 +00:00
|
|
|
Certificate: SSHCertificate{cert},
|
|
|
|
AddUserCertificate: addUserCertificate,
|
|
|
|
IdentityCertificate: identityCertificate,
|
|
|
|
}, http.StatusCreated)
|
2019-07-24 01:46:43 +00:00
|
|
|
}
|
2019-09-25 02:12:13 +00:00
|
|
|
|
2019-10-09 01:35:28 +00:00
|
|
|
// SSHRoots is an HTTP handler that returns the SSH public keys for user and host
|
2019-09-26 20:22:07 +00:00
|
|
|
// certificates.
|
2022-04-26 19:58:40 +00:00
|
|
|
func SSHRoots(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
keys, err := mustAuthority(ctx).GetSSHRoots(ctx)
|
2019-10-09 01:09:41 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.InternalServerErr(err))
|
2019-10-09 01:09:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(keys.HostKeys) == 0 && len(keys.UserKeys) == 0 {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.NotFound("no keys found"))
|
2019-10-09 01:09:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:35:28 +00:00
|
|
|
resp := new(SSHRootsResponse)
|
2019-10-09 01:09:41 +00:00
|
|
|
for _, k := range keys.HostKeys {
|
|
|
|
resp.HostKeys = append(resp.HostKeys, SSHPublicKey{PublicKey: k})
|
|
|
|
}
|
|
|
|
for _, k := range keys.UserKeys {
|
|
|
|
resp.UserKeys = append(resp.UserKeys, SSHPublicKey{PublicKey: k})
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSON(w, resp)
|
2019-10-09 01:09:41 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 01:35:28 +00:00
|
|
|
// SSHFederation is an HTTP handler that returns the federated SSH public keys
|
|
|
|
// for user and host certificates.
|
2022-04-26 19:58:40 +00:00
|
|
|
func SSHFederation(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
keys, err := mustAuthority(ctx).GetSSHFederation(ctx)
|
2019-09-25 02:12:13 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.InternalServerErr(err))
|
2019-09-25 02:12:13 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:09:41 +00:00
|
|
|
if len(keys.HostKeys) == 0 && len(keys.UserKeys) == 0 {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.NotFound("no keys found"))
|
2019-10-09 01:09:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-09 01:35:28 +00:00
|
|
|
resp := new(SSHRootsResponse)
|
2019-10-09 01:09:41 +00:00
|
|
|
for _, k := range keys.HostKeys {
|
|
|
|
resp.HostKeys = append(resp.HostKeys, SSHPublicKey{PublicKey: k})
|
2019-09-25 02:12:13 +00:00
|
|
|
}
|
2019-10-09 01:09:41 +00:00
|
|
|
for _, k := range keys.UserKeys {
|
|
|
|
resp.UserKeys = append(resp.UserKeys, SSHPublicKey{PublicKey: k})
|
2019-09-25 02:12:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSON(w, resp)
|
2019-09-25 02:12:13 +00:00
|
|
|
}
|
2019-10-04 02:03:38 +00:00
|
|
|
|
|
|
|
// SSHConfig is an HTTP handler that returns rendered templates for ssh clients
|
|
|
|
// and servers.
|
2022-04-26 19:58:40 +00:00
|
|
|
func SSHConfig(w http.ResponseWriter, r *http.Request) {
|
2019-10-04 02:03:38 +00:00
|
|
|
var body SSHConfigRequest
|
2022-03-18 13:25:34 +00:00
|
|
|
if err := read.JSON(r.Body, &body); err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.BadRequestErr(err, "error reading request body"))
|
2019-10-04 02:03:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := body.Validate(); err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2019-10-04 02:03:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
ts, err := mustAuthority(ctx).GetSSHConfig(ctx, body.Type, body.Data)
|
2019-10-04 02:03:38 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.InternalServerErr(err))
|
2019-10-04 02:03:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
var cfg SSHConfigResponse
|
2019-10-04 02:03:38 +00:00
|
|
|
switch body.Type {
|
|
|
|
case provisioner.SSHUserCert:
|
2021-10-08 18:59:57 +00:00
|
|
|
cfg.UserTemplates = ts
|
2019-10-04 02:03:38 +00:00
|
|
|
case provisioner.SSHHostCert:
|
2021-10-08 18:59:57 +00:00
|
|
|
cfg.HostTemplates = ts
|
2019-10-04 02:03:38 +00:00
|
|
|
default:
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.InternalServer("it should hot get here"))
|
2019-10-04 02:03:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSON(w, cfg)
|
2019-10-04 02:03:38 +00:00
|
|
|
}
|
2019-10-10 20:08:57 +00:00
|
|
|
|
|
|
|
// SSHCheckHost is the HTTP handler that returns if a hosts certificate exists or not.
|
2022-04-26 19:58:40 +00:00
|
|
|
func SSHCheckHost(w http.ResponseWriter, r *http.Request) {
|
2019-10-10 20:08:57 +00:00
|
|
|
var body SSHCheckPrincipalRequest
|
2022-03-18 13:25:34 +00:00
|
|
|
if err := read.JSON(r.Body, &body); err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.BadRequestErr(err, "error reading request body"))
|
2019-10-10 20:08:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := body.Validate(); err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2019-10-10 20:08:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
exists, err := mustAuthority(ctx).CheckSSHHost(ctx, body.Principal, body.Token)
|
2019-10-10 20:08:57 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.InternalServerErr(err))
|
2019-10-10 20:08:57 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSON(w, &SSHCheckPrincipalResponse{
|
2019-10-10 20:08:57 +00:00
|
|
|
Exists: exists,
|
|
|
|
})
|
|
|
|
}
|
2019-10-25 20:47:49 +00:00
|
|
|
|
|
|
|
// SSHGetHosts is the HTTP handler that returns a list of valid ssh hosts.
|
2022-04-26 19:58:40 +00:00
|
|
|
func SSHGetHosts(w http.ResponseWriter, r *http.Request) {
|
2019-11-20 20:59:48 +00:00
|
|
|
var cert *x509.Certificate
|
|
|
|
if r.TLS != nil && len(r.TLS.PeerCertificates) > 0 {
|
|
|
|
cert = r.TLS.PeerCertificates[0]
|
2019-11-20 19:32:27 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
hosts, err := mustAuthority(ctx).GetSSHHosts(ctx, cert)
|
2019-10-25 20:47:49 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.InternalServerErr(err))
|
2019-10-25 20:47:49 +00:00
|
|
|
return
|
|
|
|
}
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSON(w, &SSHGetHostsResponse{
|
2019-10-25 20:47:49 +00:00
|
|
|
Hosts: hosts,
|
|
|
|
})
|
|
|
|
}
|
2019-11-15 02:24:58 +00:00
|
|
|
|
|
|
|
// SSHBastion provides returns the bastion configured if any.
|
2022-04-26 19:58:40 +00:00
|
|
|
func SSHBastion(w http.ResponseWriter, r *http.Request) {
|
2019-11-15 02:24:58 +00:00
|
|
|
var body SSHBastionRequest
|
2022-03-18 13:25:34 +00:00
|
|
|
if err := read.JSON(r.Body, &body); err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.BadRequestErr(err, "error reading request body"))
|
2019-11-15 02:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := body.Validate(); err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, err)
|
2019-11-15 02:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-26 19:58:40 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
bastion, err := mustAuthority(ctx).GetSSHBastion(ctx, body.User, body.Hostname)
|
2019-11-15 02:24:58 +00:00
|
|
|
if err != nil {
|
2022-03-30 08:22:22 +00:00
|
|
|
render.Error(w, errs.InternalServerErr(err))
|
2019-11-15 02:24:58 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-30 08:22:22 +00:00
|
|
|
render.JSON(w, &SSHBastionResponse{
|
2019-11-15 02:24:58 +00:00
|
|
|
Hostname: body.Hostname,
|
|
|
|
Bastion: bastion,
|
|
|
|
})
|
|
|
|
}
|
2020-03-31 00:33:04 +00:00
|
|
|
|
|
|
|
// identityModifier is a custom modifier used to force a fixed duration.
|
|
|
|
type identityModifier struct {
|
|
|
|
NotBefore time.Time
|
|
|
|
NotAfter time.Time
|
|
|
|
}
|
|
|
|
|
2020-03-31 18:41:36 +00:00
|
|
|
func (m *identityModifier) Enforce(cert *x509.Certificate) error {
|
2020-03-31 00:33:04 +00:00
|
|
|
cert.NotBefore = m.NotBefore
|
|
|
|
cert.NotAfter = m.NotAfter
|
|
|
|
return nil
|
|
|
|
}
|