sshpop provisioner + ssh renew | revoke | rekey first pass
parent
b5f15531d8
commit
a9ea292bd4
@ -0,0 +1,78 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/smallstep/certificates/authority/provisioner"
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SSHRekeyRequest is the request body of an SSH certificate request.
|
||||||
|
type SSHRekeyRequest struct {
|
||||||
|
OTT string `json:"ott"`
|
||||||
|
PublicKey []byte `json:"publicKey"` //base64 encoded
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate validates the SSHSignRekey.
|
||||||
|
func (s *SSHRekeyRequest) Validate() error {
|
||||||
|
switch {
|
||||||
|
case len(s.OTT) == 0:
|
||||||
|
return errors.New("missing or empty ott")
|
||||||
|
case len(s.PublicKey) == 0:
|
||||||
|
return errors.New("missing or empty public key")
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SSHRekeyResponse is the response object that returns the SSH certificate.
|
||||||
|
type SSHRekeyResponse struct {
|
||||||
|
Certificate SSHCertificate `json:"crt"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SSHRekey is an HTTP handler that reads an RekeySSHRequest with a one-time-token
|
||||||
|
// (ott) from the body and creates a new SSH certificate with the information in
|
||||||
|
// the request.
|
||||||
|
func (h *caHandler) SSHRekey(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var body SSHRekeyRequest
|
||||||
|
if err := ReadJSON(r.Body, &body); err != nil {
|
||||||
|
WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logOtt(w, body.OTT)
|
||||||
|
if err := body.Validate(); err != nil {
|
||||||
|
WriteError(w, BadRequest(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
publicKey, err := ssh.ParsePublicKey(body.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
WriteError(w, BadRequest(errors.Wrap(err, "error parsing publicKey")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.RekeySSHMethod)
|
||||||
|
signOpts, err := h.Authority.Authorize(ctx, body.OTT)
|
||||||
|
if err != nil {
|
||||||
|
WriteError(w, Unauthorized(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
oldCert, err := provisioner.ExtractSSHPOPCert(body.OTT)
|
||||||
|
if err != nil {
|
||||||
|
WriteError(w, InternalServerError(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
newCert, err := h.Authority.RekeySSH(oldCert, publicKey, signOpts...)
|
||||||
|
if err != nil {
|
||||||
|
WriteError(w, Forbidden(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
JSON(w, &SSHSignResponse{
|
||||||
|
Certificate: SSHCertificate{newCert},
|
||||||
|
})
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/smallstep/certificates/authority/provisioner"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SSHRenewRequest is the request body of an SSH certificate request.
|
||||||
|
type SSHRenewRequest struct {
|
||||||
|
OTT string `json:"ott"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate validates the SSHSignRequest.
|
||||||
|
func (s *SSHRenewRequest) Validate() error {
|
||||||
|
switch {
|
||||||
|
case len(s.OTT) == 0:
|
||||||
|
return errors.New("missing or empty ott")
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// SSHRenewResponse is the response object that returns the SSH certificate.
|
||||||
|
type SSHRenewResponse struct {
|
||||||
|
Certificate SSHCertificate `json:"crt"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SSHRenew is an HTTP handler that reads an RenewSSHRequest with a one-time-token
|
||||||
|
// (ott) from the body and creates a new SSH certificate with the information in
|
||||||
|
// the request.
|
||||||
|
func (h *caHandler) SSHRenew(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var body SSHRenewRequest
|
||||||
|
if err := ReadJSON(r.Body, &body); err != nil {
|
||||||
|
WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logOtt(w, body.OTT)
|
||||||
|
if err := body.Validate(); err != nil {
|
||||||
|
WriteError(w, BadRequest(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.RenewSSHMethod)
|
||||||
|
_, err := h.Authority.Authorize(ctx, body.OTT)
|
||||||
|
if err != nil {
|
||||||
|
WriteError(w, Unauthorized(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
oldCert, err := provisioner.ExtractSSHPOPCert(body.OTT)
|
||||||
|
if err != nil {
|
||||||
|
WriteError(w, InternalServerError(err))
|
||||||
|
}
|
||||||
|
|
||||||
|
newCert, err := h.Authority.RenewSSH(oldCert)
|
||||||
|
if err != nil {
|
||||||
|
WriteError(w, Forbidden(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
JSON(w, &SSHSignResponse{
|
||||||
|
Certificate: SSHCertificate{newCert},
|
||||||
|
})
|
||||||
|
}
|
@ -0,0 +1,98 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/smallstep/certificates/authority"
|
||||||
|
"github.com/smallstep/certificates/authority/provisioner"
|
||||||
|
"github.com/smallstep/certificates/logging"
|
||||||
|
"golang.org/x/crypto/ocsp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SSHRevokeResponse is the response object that returns the health of the server.
|
||||||
|
type SSHRevokeResponse struct {
|
||||||
|
Status string `json:"status"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// SSHRevokeRequest is the request body for a revocation request.
|
||||||
|
type SSHRevokeRequest struct {
|
||||||
|
Serial string `json:"serial"`
|
||||||
|
OTT string `json:"ott"`
|
||||||
|
ReasonCode int `json:"reasonCode"`
|
||||||
|
Reason string `json:"reason"`
|
||||||
|
Passive bool `json:"passive"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate checks the fields of the RevokeRequest and returns nil if they are ok
|
||||||
|
// or an error if something is wrong.
|
||||||
|
func (r *SSHRevokeRequest) Validate() (err error) {
|
||||||
|
if r.Serial == "" {
|
||||||
|
return BadRequest(errors.New("missing serial"))
|
||||||
|
}
|
||||||
|
if r.ReasonCode < ocsp.Unspecified || r.ReasonCode > ocsp.AACompromise {
|
||||||
|
return BadRequest(errors.New("reasonCode out of bounds"))
|
||||||
|
}
|
||||||
|
if !r.Passive {
|
||||||
|
return NotImplemented(errors.New("non-passive revocation not implemented"))
|
||||||
|
}
|
||||||
|
if len(r.OTT) == 0 {
|
||||||
|
return BadRequest(errors.New("missing ott"))
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Revoke supports handful of different methods that revoke a Certificate.
|
||||||
|
//
|
||||||
|
// NOTE: currently only Passive revocation is supported.
|
||||||
|
func (h *caHandler) SSHRevoke(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var body SSHRevokeRequest
|
||||||
|
if err := ReadJSON(r.Body, &body); err != nil {
|
||||||
|
WriteError(w, BadRequest(errors.Wrap(err, "error reading request body")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := body.Validate(); err != nil {
|
||||||
|
WriteError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := &authority.RevokeOptions{
|
||||||
|
Serial: body.Serial,
|
||||||
|
Reason: body.Reason,
|
||||||
|
ReasonCode: body.ReasonCode,
|
||||||
|
PassiveOnly: body.Passive,
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx := provisioner.NewContextWithMethod(context.Background(), provisioner.RevokeSSHMethod)
|
||||||
|
// A token indicates that we are using the api via a provisioner token,
|
||||||
|
// otherwise it is assumed that the certificate is revoking itself over mTLS.
|
||||||
|
logOtt(w, body.OTT)
|
||||||
|
if _, err := h.Authority.Authorize(ctx, body.OTT); err != nil {
|
||||||
|
WriteError(w, Unauthorized(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
opts.OTT = body.OTT
|
||||||
|
|
||||||
|
if err := h.Authority.Revoke(ctx, opts); err != nil {
|
||||||
|
WriteError(w, Forbidden(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
logSSHRevoke(w, opts)
|
||||||
|
JSON(w, &SSHRevokeResponse{Status: "ok"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func logSSHRevoke(w http.ResponseWriter, ri *authority.RevokeOptions) {
|
||||||
|
if rl, ok := w.(logging.ResponseLogger); ok {
|
||||||
|
rl.WithFields(map[string]interface{}{
|
||||||
|
"serial": ri.Serial,
|
||||||
|
"reasonCode": ri.ReasonCode,
|
||||||
|
"reason": ri.Reason,
|
||||||
|
"passiveOnly": ri.PassiveOnly,
|
||||||
|
"mTLS": ri.MTLS,
|
||||||
|
"ssh": true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue