Skip to content

Commit ca74bb1

Browse files
committed
Add ssh api tests.
1 parent 57a529c commit ca74bb1

File tree

3 files changed

+344
-10
lines changed

3 files changed

+344
-10
lines changed

api/api_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ import (
2323
"testing"
2424
"time"
2525

26-
"golang.org/x/crypto/ssh"
27-
2826
"github.com/go-chi/chi"
2927
"github.com/smallstep/certificates/authority"
3028
"github.com/smallstep/certificates/authority/provisioner"
3129
"github.com/smallstep/certificates/logging"
3230
"github.com/smallstep/cli/crypto/tlsutil"
3331
"github.com/smallstep/cli/jose"
32+
"golang.org/x/crypto/ssh"
3433
)
3534

3635
const (
@@ -498,6 +497,7 @@ type mockAuthority struct {
498497
root func(shasum string) (*x509.Certificate, error)
499498
sign func(cr *x509.CertificateRequest, opts provisioner.Options, signOpts ...provisioner.SignOption) (*x509.Certificate, *x509.Certificate, error)
500499
singSSH func(key ssh.PublicKey, opts provisioner.SSHOptions, signOpts ...provisioner.SignOption) (*ssh.Certificate, error)
500+
singSSHAddUser func(key ssh.PublicKey, cert *ssh.Certificate) (*ssh.Certificate, error)
501501
renew func(cert *x509.Certificate) (*x509.Certificate, *x509.Certificate, error)
502502
loadProvisionerByCertificate func(cert *x509.Certificate) (provisioner.Interface, error)
503503
getProvisioners func(nextCursor string, limit int) (provisioner.List, string, error)
@@ -547,6 +547,13 @@ func (m *mockAuthority) SignSSH(key ssh.PublicKey, opts provisioner.SSHOptions,
547547
return m.ret1.(*ssh.Certificate), m.err
548548
}
549549

550+
func (m *mockAuthority) SignSSHAddUser(key ssh.PublicKey, cert *ssh.Certificate) (*ssh.Certificate, error) {
551+
if m.singSSHAddUser != nil {
552+
return m.singSSHAddUser(key, cert)
553+
}
554+
return m.ret1.(*ssh.Certificate), m.err
555+
}
556+
550557
func (m *mockAuthority) Renew(cert *x509.Certificate) (*x509.Certificate, *x509.Certificate, error) {
551558
if m.renew != nil {
552559
return m.renew(cert)

api/ssh.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ type SignSSHRequest struct {
3030

3131
// SignSSHResponse is the response object that returns the SSH certificate.
3232
type SignSSHResponse struct {
33-
Certificate SSHCertificate `json:"crt"`
34-
AddUserCertificate SSHCertificate `json:"addUserCrt"`
33+
Certificate SSHCertificate `json:"crt"`
34+
AddUserCertificate *SSHCertificate `json:"addUserCrt,omitempty"`
3535
}
3636

3737
// SSHCertificate represents the response SSH certificate.
3838
type SSHCertificate struct {
39-
*ssh.Certificate
39+
*ssh.Certificate `json:"omitempty"`
4040
}
4141

4242
// MarshalJSON implements the json.Marshaler interface. The certificate is
@@ -102,7 +102,7 @@ func (h *caHandler) SignSSH(w http.ResponseWriter, r *http.Request) {
102102

103103
logOtt(w, body.OTT)
104104
if err := body.Validate(); err != nil {
105-
WriteError(w, err)
105+
WriteError(w, BadRequest(err))
106106
return
107107
}
108108

@@ -141,19 +141,19 @@ func (h *caHandler) SignSSH(w http.ResponseWriter, r *http.Request) {
141141
return
142142
}
143143

144-
var addUserCert *ssh.Certificate
144+
var addUserCertificate *SSHCertificate
145145
if addUserPublicKey != nil && cert.CertType == ssh.UserCert && len(cert.ValidPrincipals) == 1 {
146-
addUserCert, err = h.Authority.SignSSHAddUser(addUserPublicKey, cert)
146+
addUserCert, err := h.Authority.SignSSHAddUser(addUserPublicKey, cert)
147147
if err != nil {
148148
WriteError(w, Forbidden(err))
149149
return
150150
}
151+
addUserCertificate = &SSHCertificate{addUserCert}
151152
}
152153

153154
w.WriteHeader(http.StatusCreated)
154-
// logCertificate(w, cert)
155155
JSON(w, &SignSSHResponse{
156156
Certificate: SSHCertificate{cert},
157-
AddUserCertificate: SSHCertificate{addUserCert},
157+
AddUserCertificate: addUserCertificate,
158158
})
159159
}

0 commit comments

Comments
 (0)