| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673 |
- package ca
- import (
- "bytes"
- "crypto/x509"
- "encoding/json"
- "encoding/pem"
- "fmt"
- "net/http"
- "net/http/httptest"
- "net/url"
- "reflect"
- "testing"
- "time"
- "github.com/smallstep/certificates/api"
- "github.com/smallstep/certificates/authority/provisioner"
- )
- const (
- rootPEM = `-----BEGIN CERTIFICATE-----
- MIIEBDCCAuygAwIBAgIDAjppMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT
- MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i
- YWwgQ0EwHhcNMTMwNDA1MTUxNTU1WhcNMTUwNDA0MTUxNTU1WjBJMQswCQYDVQQG
- EwJVUzETMBEGA1UEChM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EHC8o9p1ywWN9ySpJ8Zj/5ubmx9v2bY67GbuVFEa8iAp+S00x
- /Z8nD6/JsoKtexuHyGr3ixWFzlBqXDuugukIDFUOVDCbuGw4Io4/hEMu4Zz0TIFk
- Uu/wf2z75Tt8EkosKLu2wieKcY7n7Vhog/0tqexqWlWtJH0tvq4djsGoSvA62WPs
- 0iXXj+aZIARPNhECAwEAAaAAMA0GCSqGSIb3DQEBCwUAA4ICAQA0vyHIndAkIs/I
- Nnz5yZWCokRjokoKv3Aj4VilyjncL+W0UIPULLU/47ZyoHVSUj2t8gknr9xu/Kd+
- g/2z0RiF3CIp8IUH49w/HYWaR95glzVNAAzr8qD9UbUqloLVQW3lObSRGtezhdZO
- sspw5dC+inhAb1LZhx8PVxB3SAeJ8h11IEBr0s2Hxt9viKKd7YPtIFZkZdOkVx4R
- if1DMawj1P6fEomf8z7m+dmbUYTqqosbCbRL01mzEga/kF6JyH/OzpNlcsAiyM8e
- BxPWH6TtPqwmyy4y7j1outmM0RnyUw5A0HmIbWh+rHpXiHVsnNqse0XfzmaxM8+z
- dxYeDax8aMWZKfvY1Zew+xIxl7DtEy1BpxrZcawumJYt5+LL+bwF/OtL0inQLnw8
- zyqydsXNdrpIQJnfmWPld7ThWbQw2FBE70+nFSxHeG2ULnpF3M9xf6ZNAF4gqaNE
- Q7vMNPBWrJWu+A++vHY61WGET+h4lY3GFr2I8OE4IiHPQi1D7Y0+fwOmStwuRPM4
- 2rARcJChNdiYBkkuvs4kixKTTjdXhB8RQtuBSrJ0M1tzq2qMbm7F8G01rOg4KlXU
- 58jHzJwr1K7cx0lpWfGTtc5bseCGtTKmDBXTziw04yl8eE1+ZFOganixGwCtl4Tt
- DCbKzWTW8lqVdp9Kyf7XEhhc2R8C5w==
- -----END CERTIFICATE REQUEST-----`
- )
- func parseCertificate(data string) *x509.Certificate {
- block, _ := pem.Decode([]byte(data))
- if block == nil {
- panic("failed to parse certificate PEM")
- }
- cert, err := x509.ParseCertificate(block.Bytes)
- if err != nil {
- panic("failed to parse certificate: " + err.Error())
- }
- return cert
- }
- func parseCertificateRequest(data string) *x509.CertificateRequest {
- block, _ := pem.Decode([]byte(csrPEM))
- if block == nil {
- panic("failed to parse certificate request PEM")
- }
- csr, err := x509.ParseCertificateRequest(block.Bytes)
- if err != nil {
- panic("failed to parse certificate request: " + err.Error())
- }
- return csr
- }
- func equalJSON(t *testing.T, a interface{}, b interface{}) bool {
- if reflect.DeepEqual(a, b) {
- return true
- }
- ab, err := json.Marshal(a)
- if err != nil {
- t.Error(err)
- return false
- }
- bb, err := json.Marshal(b)
- if err != nil {
- t.Error(err)
- return false
- }
- return bytes.Equal(ab, bb)
- }
- func TestClient_Health(t *testing.T) {
- ok := &api.HealthResponse{Status: "ok"}
- nok := api.InternalServerError(fmt.Errorf("Internal Server Error"))
- tests := []struct {
- name string
- response interface{}
- responseCode int
- wantErr bool
- }{
- {"ok", ok, 200, false},
- {"not ok", nok, 500, true},
- }
- srv := httptest.NewServer(nil)
- defer srv.Close()
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c, err := NewClient(srv.URL, WithTransport(http.DefaultTransport))
- if err != nil {
- t.Errorf("NewClient() error = %v", err)
- return
- }
- srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
- w.WriteHeader(tt.responseCode)
- api.JSON(w, tt.response)
- })
- got, err := c.Health()
- if (err != nil) != tt.wantErr {
- fmt.Printf("%+v", err)
- t.Errorf("Client.Health() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- switch {
- case err != nil:
- if got != nil {
- t.Errorf("Client.Health() = %v, want nil", got)
- }
- if !reflect.DeepEqual(err, tt.response) {
- t.Errorf("Client.Health() error = %v, want %v", err, tt.response)
- }
- default:
- if !reflect.DeepEqual(got, tt.response) {
- t.Errorf("Client.Health() = %v, want %v", got, tt.response)
- }
- }
- })
- }
- }
- func TestClient_Root(t *testing.T) {
- ok := &api.RootResponse{
- RootPEM: api.Certificate{Certificate: parseCertificate(rootPEM)},
- }
- notFound := api.NotFound(fmt.Errorf("Not Found"))
- tests := []struct {
- name string
- shasum string
- response interface{}
- responseCode int
- wantErr bool
- }{
- {"ok", "a047a37fa2d2e118a4f5095fe074d6cfe0e352425a7632bf8659c03919a6c81d", ok, 200, false},
- {"not found", "invalid", notFound, 404, true},
- }
- srv := httptest.NewServer(nil)
- defer srv.Close()
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c, err := NewClient(srv.URL, WithTransport(http.DefaultTransport))
- if err != nil {
- t.Errorf("NewClient() error = %v", err)
- return
- }
- srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
- expected := "/root/" + tt.shasum
- if req.RequestURI != expected {
- t.Errorf("RequestURI = %s, want %s", req.RequestURI, expected)
- }
- w.WriteHeader(tt.responseCode)
- api.JSON(w, tt.response)
- })
- got, err := c.Root(tt.shasum)
- if (err != nil) != tt.wantErr {
- t.Errorf("Client.Root() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- switch {
- case err != nil:
- if got != nil {
- t.Errorf("Client.Root() = %v, want nil", got)
- }
- if !reflect.DeepEqual(err, tt.response) {
- t.Errorf("Client.Root() error = %v, want %v", err, tt.response)
- }
- default:
- if !reflect.DeepEqual(got, tt.response) {
- t.Errorf("Client.Root() = %v, want %v", got, tt.response)
- }
- }
- })
- }
- }
- func TestClient_Sign(t *testing.T) {
- ok := &api.SignResponse{
- ServerPEM: api.Certificate{Certificate: parseCertificate(certPEM)},
- CaPEM: api.Certificate{Certificate: parseCertificate(rootPEM)},
- }
- request := &api.SignRequest{
- CsrPEM: api.CertificateRequest{CertificateRequest: parseCertificateRequest(csrPEM)},
- OTT: "the-ott",
- NotBefore: api.NewTimeDuration(time.Now()),
- NotAfter: api.NewTimeDuration(time.Now().AddDate(0, 1, 0)),
- }
- unauthorized := api.Unauthorized(fmt.Errorf("Unauthorized"))
- badRequest := api.BadRequest(fmt.Errorf("Bad Request"))
- tests := []struct {
- name string
- request *api.SignRequest
- response interface{}
- responseCode int
- wantErr bool
- }{
- {"ok", request, ok, 200, false},
- {"unauthorized", request, unauthorized, 401, true},
- {"empty request", &api.SignRequest{}, badRequest, 403, true},
- {"nil request", nil, badRequest, 403, true},
- }
- srv := httptest.NewServer(nil)
- defer srv.Close()
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c, err := NewClient(srv.URL, WithTransport(http.DefaultTransport))
- if err != nil {
- t.Errorf("NewClient() error = %v", err)
- return
- }
- srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
- body := new(api.SignRequest)
- if err := api.ReadJSON(req.Body, body); err != nil {
- api.WriteError(w, badRequest)
- return
- } else if !equalJSON(t, body, tt.request) {
- if tt.request == nil {
- if !reflect.DeepEqual(body, &api.SignRequest{}) {
- t.Errorf("Client.Sign() request = %v, wants %v", body, tt.request)
- }
- } else {
- t.Errorf("Client.Sign() request = %v, wants %v", body, tt.request)
- }
- }
- w.WriteHeader(tt.responseCode)
- api.JSON(w, tt.response)
- })
- got, err := c.Sign(tt.request)
- if (err != nil) != tt.wantErr {
- fmt.Printf("%+v", err)
- t.Errorf("Client.Sign() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- switch {
- case err != nil:
- if got != nil {
- t.Errorf("Client.Sign() = %v, want nil", got)
- }
- if !reflect.DeepEqual(err, tt.response) {
- t.Errorf("Client.Sign() error = %v, want %v", err, tt.response)
- }
- default:
- if !reflect.DeepEqual(got, tt.response) {
- t.Errorf("Client.Sign() = %v, want %v", got, tt.response)
- }
- }
- })
- }
- }
- func TestClient_Renew(t *testing.T) {
- ok := &api.SignResponse{
- ServerPEM: api.Certificate{Certificate: parseCertificate(certPEM)},
- CaPEM: api.Certificate{Certificate: parseCertificate(rootPEM)},
- }
- unauthorized := api.Unauthorized(fmt.Errorf("Unauthorized"))
- badRequest := api.BadRequest(fmt.Errorf("Bad Request"))
- tests := []struct {
- name string
- response interface{}
- responseCode int
- wantErr bool
- }{
- {"ok", ok, 200, false},
- {"unauthorized", unauthorized, 401, true},
- {"empty request", badRequest, 403, true},
- {"nil request", badRequest, 403, true},
- }
- srv := httptest.NewServer(nil)
- defer srv.Close()
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c, err := NewClient(srv.URL, WithTransport(http.DefaultTransport))
- if err != nil {
- t.Errorf("NewClient() error = %v", err)
- return
- }
- srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
- w.WriteHeader(tt.responseCode)
- api.JSON(w, tt.response)
- })
- got, err := c.Renew(nil)
- if (err != nil) != tt.wantErr {
- fmt.Printf("%+v", err)
- t.Errorf("Client.Renew() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- switch {
- case err != nil:
- if got != nil {
- t.Errorf("Client.Renew() = %v, want nil", got)
- }
- if !reflect.DeepEqual(err, tt.response) {
- t.Errorf("Client.Renew() error = %v, want %v", err, tt.response)
- }
- default:
- if !reflect.DeepEqual(got, tt.response) {
- t.Errorf("Client.Renew() = %v, want %v", got, tt.response)
- }
- }
- })
- }
- }
- func TestClient_Provisioners(t *testing.T) {
- ok := &api.ProvisionersResponse{
- Provisioners: provisioner.List{},
- }
- internalServerError := api.InternalServerError(fmt.Errorf("Internal Server Error"))
- tests := []struct {
- name string
- args []ProvisionerOption
- expectedURI string
- response interface{}
- responseCode int
- wantErr bool
- }{
- {"ok", nil, "/provisioners", ok, 200, false},
- {"ok with cursor", []ProvisionerOption{WithProvisionerCursor("abc")}, "/provisioners?cursor=abc", ok, 200, false},
- {"ok with limit", []ProvisionerOption{WithProvisionerLimit(10)}, "/provisioners?limit=10", ok, 200, false},
- {"ok with cursor+limit", []ProvisionerOption{WithProvisionerCursor("abc"), WithProvisionerLimit(10)}, "/provisioners?cursor=abc&limit=10", ok, 200, false},
- {"fail", nil, "/provisioners", internalServerError, 500, true},
- }
- srv := httptest.NewServer(nil)
- defer srv.Close()
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c, err := NewClient(srv.URL, WithTransport(http.DefaultTransport))
- if err != nil {
- t.Errorf("NewClient() error = %v", err)
- return
- }
- srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
- if req.RequestURI != tt.expectedURI {
- t.Errorf("RequestURI = %s, want %s", req.RequestURI, tt.expectedURI)
- }
- w.WriteHeader(tt.responseCode)
- api.JSON(w, tt.response)
- })
- got, err := c.Provisioners(tt.args...)
- if (err != nil) != tt.wantErr {
- t.Errorf("Client.Provisioners() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- switch {
- case err != nil:
- if got != nil {
- t.Errorf("Client.Provisioners() = %v, want nil", got)
- }
- if !reflect.DeepEqual(err, tt.response) {
- t.Errorf("Client.Provisioners() error = %v, want %v", err, tt.response)
- }
- default:
- if !reflect.DeepEqual(got, tt.response) {
- t.Errorf("Client.Provisioners() = %v, want %v", got, tt.response)
- }
- }
- })
- }
- }
- func TestClient_ProvisionerKey(t *testing.T) {
- ok := &api.ProvisionerKeyResponse{
- Key: "an encrypted key",
- }
- notFound := api.NotFound(fmt.Errorf("Not Found"))
- tests := []struct {
- name string
- kid string
- response interface{}
- responseCode int
- wantErr bool
- }{
- {"ok", "kid", ok, 200, false},
- {"fail", "invalid", notFound, 500, true},
- }
- srv := httptest.NewServer(nil)
- defer srv.Close()
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c, err := NewClient(srv.URL, WithTransport(http.DefaultTransport))
- if err != nil {
- t.Errorf("NewClient() error = %v", err)
- return
- }
- srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
- expected := "/provisioners/" + tt.kid + "/encrypted-key"
- if req.RequestURI != expected {
- t.Errorf("RequestURI = %s, want %s", req.RequestURI, expected)
- }
- w.WriteHeader(tt.responseCode)
- api.JSON(w, tt.response)
- })
- got, err := c.ProvisionerKey(tt.kid)
- if (err != nil) != tt.wantErr {
- t.Errorf("Client.ProvisionerKey() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- switch {
- case err != nil:
- if got != nil {
- t.Errorf("Client.ProvisionerKey() = %v, want nil", got)
- }
- if !reflect.DeepEqual(err, tt.response) {
- t.Errorf("Client.ProvisionerKey() error = %v, want %v", err, tt.response)
- }
- default:
- if !reflect.DeepEqual(got, tt.response) {
- t.Errorf("Client.ProvisionerKey() = %v, want %v", got, tt.response)
- }
- }
- })
- }
- }
- func TestClient_Roots(t *testing.T) {
- ok := &api.RootsResponse{
- Certificates: []api.Certificate{
- {Certificate: parseCertificate(rootPEM)},
- },
- }
- unauthorized := api.Unauthorized(fmt.Errorf("Unauthorized"))
- badRequest := api.BadRequest(fmt.Errorf("Bad Request"))
- tests := []struct {
- name string
- response interface{}
- responseCode int
- wantErr bool
- }{
- {"ok", ok, 200, false},
- {"unauthorized", unauthorized, 401, true},
- {"empty request", badRequest, 403, true},
- {"nil request", badRequest, 403, true},
- }
- srv := httptest.NewServer(nil)
- defer srv.Close()
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c, err := NewClient(srv.URL, WithTransport(http.DefaultTransport))
- if err != nil {
- t.Errorf("NewClient() error = %v", err)
- return
- }
- srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
- w.WriteHeader(tt.responseCode)
- api.JSON(w, tt.response)
- })
- got, err := c.Roots()
- if (err != nil) != tt.wantErr {
- fmt.Printf("%+v", err)
- t.Errorf("Client.Roots() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- switch {
- case err != nil:
- if got != nil {
- t.Errorf("Client.Roots() = %v, want nil", got)
- }
- if !reflect.DeepEqual(err, tt.response) {
- t.Errorf("Client.Roots() error = %v, want %v", err, tt.response)
- }
- default:
- if !reflect.DeepEqual(got, tt.response) {
- t.Errorf("Client.Roots() = %v, want %v", got, tt.response)
- }
- }
- })
- }
- }
- func TestClient_Federation(t *testing.T) {
- ok := &api.FederationResponse{
- Certificates: []api.Certificate{
- {Certificate: parseCertificate(rootPEM)},
- },
- }
- unauthorized := api.Unauthorized(fmt.Errorf("Unauthorized"))
- badRequest := api.BadRequest(fmt.Errorf("Bad Request"))
- tests := []struct {
- name string
- response interface{}
- responseCode int
- wantErr bool
- }{
- {"ok", ok, 200, false},
- {"unauthorized", unauthorized, 401, true},
- {"empty request", badRequest, 403, true},
- {"nil request", badRequest, 403, true},
- }
- srv := httptest.NewServer(nil)
- defer srv.Close()
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c, err := NewClient(srv.URL, WithTransport(http.DefaultTransport))
- if err != nil {
- t.Errorf("NewClient() error = %v", err)
- return
- }
- srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
- w.WriteHeader(tt.responseCode)
- api.JSON(w, tt.response)
- })
- got, err := c.Federation()
- if (err != nil) != tt.wantErr {
- fmt.Printf("%+v", err)
- t.Errorf("Client.Federation() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- switch {
- case err != nil:
- if got != nil {
- t.Errorf("Client.Federation() = %v, want nil", got)
- }
- if !reflect.DeepEqual(err, tt.response) {
- t.Errorf("Client.Federation() error = %v, want %v", err, tt.response)
- }
- default:
- if !reflect.DeepEqual(got, tt.response) {
- t.Errorf("Client.Federation() = %v, want %v", got, tt.response)
- }
- }
- })
- }
- }
- func Test_parseEndpoint(t *testing.T) {
- expected1 := &url.URL{Scheme: "https", Host: "ca.smallstep.com"}
- expected2 := &url.URL{Scheme: "https", Host: "ca.smallstep.com", Path: "/1.0/sign"}
- type args struct {
- endpoint string
- }
- tests := []struct {
- name string
- args args
- want *url.URL
- wantErr bool
- }{
- {"ok", args{"https://ca.smallstep.com"}, expected1, false},
- {"ok no scheme", args{"//ca.smallstep.com"}, expected1, false},
- {"ok only host", args{"ca.smallstep.com"}, expected1, false},
- {"ok no bars", args{"https://ca.smallstep.com"}, expected1, false},
- {"ok schema, host and path", args{"https://ca.smallstep.com/1.0/sign"}, expected2, false},
- {"ok no bars with path", args{"https://ca.smallstep.com/1.0/sign"}, expected2, false},
- {"ok host and path", args{"ca.smallstep.com/1.0/sign"}, expected2, false},
- {"ok host and port", args{"ca.smallstep.com:443"}, &url.URL{Scheme: "https", Host: "ca.smallstep.com:443"}, false},
- {"ok host, path and port", args{"ca.smallstep.com:443/1.0/sign"}, &url.URL{Scheme: "https", Host: "ca.smallstep.com:443", Path: "/1.0/sign"}, false},
- {"fail bad url", args{"://ca.smallstep.com"}, nil, true},
- {"fail no host", args{"https://"}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := parseEndpoint(tt.args.endpoint)
- if (err != nil) != tt.wantErr {
- t.Errorf("parseEndpoint() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("parseEndpoint() = %v, want %v", got, tt.want)
- }
- })
- }
- }
|