4
0

api.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. package api
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/dsa" //nolint
  6. "crypto/ecdsa"
  7. "crypto/ed25519"
  8. "crypto/rsa"
  9. "crypto/x509"
  10. "encoding/asn1"
  11. "encoding/base64"
  12. "encoding/json"
  13. "encoding/pem"
  14. "fmt"
  15. "net/http"
  16. "strconv"
  17. "strings"
  18. "time"
  19. "github.com/go-chi/chi"
  20. "github.com/pkg/errors"
  21. "github.com/smallstep/certificates/authority"
  22. "github.com/smallstep/certificates/authority/config"
  23. "github.com/smallstep/certificates/authority/provisioner"
  24. "github.com/smallstep/certificates/errs"
  25. "github.com/smallstep/certificates/logging"
  26. "github.com/smallstep/certificates/templates"
  27. "go.step.sm/linkedca"
  28. "golang.org/x/crypto/ssh"
  29. )
  30. // Authority is the interface implemented by a CA authority.
  31. type Authority interface {
  32. SSHAuthority
  33. // context specifies the Authorize[Sign|Revoke|etc.] method.
  34. Authorize(ctx context.Context, ott string) ([]provisioner.SignOption, error)
  35. AuthorizeSign(ott string) ([]provisioner.SignOption, error)
  36. GetTLSOptions() *config.TLSOptions
  37. Root(shasum string) (*x509.Certificate, error)
  38. Sign(cr *x509.CertificateRequest, opts provisioner.SignOptions, signOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
  39. Renew(peer *x509.Certificate) ([]*x509.Certificate, error)
  40. Rekey(peer *x509.Certificate, pk crypto.PublicKey) ([]*x509.Certificate, error)
  41. LoadProvisionerByCertificate(*x509.Certificate) (provisioner.Interface, error)
  42. LoadProvisionerByName(string) (provisioner.Interface, error)
  43. GetProvisioners(cursor string, limit int) (provisioner.List, string, error)
  44. Revoke(context.Context, *authority.RevokeOptions) error
  45. GetEncryptedKey(kid string) (string, error)
  46. GetRoots() (federation []*x509.Certificate, err error)
  47. GetFederation() ([]*x509.Certificate, error)
  48. Version() authority.Version
  49. }
  50. type LinkedAuthority interface { // TODO(hs): name is not great; it is related to LinkedCA, though
  51. Authority
  52. IsAdminAPIEnabled() bool
  53. LoadAdminByID(id string) (*linkedca.Admin, bool)
  54. GetAdmins(cursor string, limit int) ([]*linkedca.Admin, string, error)
  55. StoreAdmin(ctx context.Context, adm *linkedca.Admin, prov provisioner.Interface) error
  56. UpdateAdmin(ctx context.Context, id string, nu *linkedca.Admin) (*linkedca.Admin, error)
  57. RemoveAdmin(ctx context.Context, id string) error
  58. AuthorizeAdminToken(r *http.Request, token string) (*linkedca.Admin, error)
  59. StoreProvisioner(ctx context.Context, prov *linkedca.Provisioner) error
  60. LoadProvisionerByID(id string) (provisioner.Interface, error)
  61. UpdateProvisioner(ctx context.Context, nu *linkedca.Provisioner) error
  62. RemoveProvisioner(ctx context.Context, id string) error
  63. }
  64. // TimeDuration is an alias of provisioner.TimeDuration
  65. type TimeDuration = provisioner.TimeDuration
  66. // NewTimeDuration returns a TimeDuration with the defined time.
  67. func NewTimeDuration(t time.Time) TimeDuration {
  68. return provisioner.NewTimeDuration(t)
  69. }
  70. // ParseTimeDuration returns a new TimeDuration parsing the RFC 3339 time or
  71. // time.Duration string.
  72. func ParseTimeDuration(s string) (TimeDuration, error) {
  73. return provisioner.ParseTimeDuration(s)
  74. }
  75. // Certificate wraps a *x509.Certificate and adds the json.Marshaler interface.
  76. type Certificate struct {
  77. *x509.Certificate
  78. }
  79. // NewCertificate is a helper method that returns a Certificate from a
  80. // *x509.Certificate.
  81. func NewCertificate(cr *x509.Certificate) Certificate {
  82. return Certificate{
  83. Certificate: cr,
  84. }
  85. }
  86. // reset sets the inner x509.CertificateRequest to nil
  87. func (c *Certificate) reset() {
  88. if c != nil {
  89. c.Certificate = nil
  90. }
  91. }
  92. // MarshalJSON implements the json.Marshaler interface. The certificate is
  93. // quoted string using the PEM encoding.
  94. func (c Certificate) MarshalJSON() ([]byte, error) {
  95. if c.Certificate == nil {
  96. return []byte("null"), nil
  97. }
  98. block := pem.EncodeToMemory(&pem.Block{
  99. Type: "CERTIFICATE",
  100. Bytes: c.Raw,
  101. })
  102. return json.Marshal(string(block))
  103. }
  104. // UnmarshalJSON implements the json.Unmarshaler interface. The certificate is
  105. // expected to be a quoted string using the PEM encoding.
  106. func (c *Certificate) UnmarshalJSON(data []byte) error {
  107. var s string
  108. if err := json.Unmarshal(data, &s); err != nil {
  109. return errors.Wrap(err, "error decoding certificate")
  110. }
  111. // Make sure the inner x509.Certificate is nil
  112. if s == "null" || s == "" {
  113. c.reset()
  114. return nil
  115. }
  116. block, _ := pem.Decode([]byte(s))
  117. if block == nil {
  118. return errors.New("error decoding certificate")
  119. }
  120. cert, err := x509.ParseCertificate(block.Bytes)
  121. if err != nil {
  122. return errors.Wrap(err, "error decoding certificate")
  123. }
  124. c.Certificate = cert
  125. return nil
  126. }
  127. // CertificateRequest wraps a *x509.CertificateRequest and adds the
  128. // json.Unmarshaler interface.
  129. type CertificateRequest struct {
  130. *x509.CertificateRequest
  131. }
  132. // NewCertificateRequest is a helper method that returns a CertificateRequest
  133. // from a *x509.CertificateRequest.
  134. func NewCertificateRequest(cr *x509.CertificateRequest) CertificateRequest {
  135. return CertificateRequest{
  136. CertificateRequest: cr,
  137. }
  138. }
  139. // reset sets the inner x509.CertificateRequest to nil
  140. func (c *CertificateRequest) reset() {
  141. if c != nil {
  142. c.CertificateRequest = nil
  143. }
  144. }
  145. // MarshalJSON implements the json.Marshaler interface. The certificate request
  146. // is a quoted string using the PEM encoding.
  147. func (c CertificateRequest) MarshalJSON() ([]byte, error) {
  148. if c.CertificateRequest == nil {
  149. return []byte("null"), nil
  150. }
  151. block := pem.EncodeToMemory(&pem.Block{
  152. Type: "CERTIFICATE REQUEST",
  153. Bytes: c.Raw,
  154. })
  155. return json.Marshal(string(block))
  156. }
  157. // UnmarshalJSON implements the json.Unmarshaler interface. The certificate
  158. // request is expected to be a quoted string using the PEM encoding.
  159. func (c *CertificateRequest) UnmarshalJSON(data []byte) error {
  160. var s string
  161. if err := json.Unmarshal(data, &s); err != nil {
  162. return errors.Wrap(err, "error decoding csr")
  163. }
  164. // Make sure the inner x509.CertificateRequest is nil
  165. if s == "null" || s == "" {
  166. c.reset()
  167. return nil
  168. }
  169. block, _ := pem.Decode([]byte(s))
  170. if block == nil {
  171. return errors.New("error decoding csr")
  172. }
  173. cr, err := x509.ParseCertificateRequest(block.Bytes)
  174. if err != nil {
  175. return errors.Wrap(err, "error decoding csr")
  176. }
  177. c.CertificateRequest = cr
  178. return nil
  179. }
  180. // Router defines a common router interface.
  181. type Router interface {
  182. // MethodFunc adds routes for `pattern` that matches
  183. // the `method` HTTP method.
  184. MethodFunc(method, pattern string, h http.HandlerFunc)
  185. }
  186. // RouterHandler is the interface that a HTTP handler that manages multiple
  187. // endpoints will implement.
  188. type RouterHandler interface {
  189. Route(r Router)
  190. }
  191. // VersionResponse is the response object that returns the version of the
  192. // server.
  193. type VersionResponse struct {
  194. Version string `json:"version"`
  195. RequireClientAuthentication bool `json:"requireClientAuthentication,omitempty"`
  196. }
  197. // HealthResponse is the response object that returns the health of the server.
  198. type HealthResponse struct {
  199. Status string `json:"status"`
  200. }
  201. // RootResponse is the response object that returns the PEM of a root certificate.
  202. type RootResponse struct {
  203. RootPEM Certificate `json:"ca"`
  204. }
  205. // ProvisionersResponse is the response object that returns the list of
  206. // provisioners.
  207. type ProvisionersResponse struct {
  208. Provisioners provisioner.List `json:"provisioners"`
  209. NextCursor string `json:"nextCursor"`
  210. }
  211. // ProvisionerKeyResponse is the response object that returns the encrypted key
  212. // of a provisioner.
  213. type ProvisionerKeyResponse struct {
  214. Key string `json:"key"`
  215. }
  216. // RootsResponse is the response object of the roots request.
  217. type RootsResponse struct {
  218. Certificates []Certificate `json:"crts"`
  219. }
  220. // FederationResponse is the response object of the federation request.
  221. type FederationResponse struct {
  222. Certificates []Certificate `json:"crts"`
  223. }
  224. // caHandler is the type used to implement the different CA HTTP endpoints.
  225. type caHandler struct {
  226. Authority Authority
  227. }
  228. // New creates a new RouterHandler with the CA endpoints.
  229. func New(auth Authority) RouterHandler {
  230. return &caHandler{
  231. Authority: auth,
  232. }
  233. }
  234. func (h *caHandler) Route(r Router) {
  235. r.MethodFunc("GET", "/version", h.Version)
  236. r.MethodFunc("GET", "/health", h.Health)
  237. r.MethodFunc("GET", "/root/{sha}", h.Root)
  238. r.MethodFunc("POST", "/sign", h.Sign)
  239. r.MethodFunc("POST", "/renew", h.Renew)
  240. r.MethodFunc("POST", "/rekey", h.Rekey)
  241. r.MethodFunc("POST", "/revoke", h.Revoke)
  242. r.MethodFunc("GET", "/provisioners", h.Provisioners)
  243. r.MethodFunc("GET", "/provisioners/{kid}/encrypted-key", h.ProvisionerKey)
  244. r.MethodFunc("GET", "/roots", h.Roots)
  245. r.MethodFunc("GET", "/federation", h.Federation)
  246. // SSH CA
  247. r.MethodFunc("POST", "/ssh/sign", h.SSHSign)
  248. r.MethodFunc("POST", "/ssh/renew", h.SSHRenew)
  249. r.MethodFunc("POST", "/ssh/revoke", h.SSHRevoke)
  250. r.MethodFunc("POST", "/ssh/rekey", h.SSHRekey)
  251. r.MethodFunc("GET", "/ssh/roots", h.SSHRoots)
  252. r.MethodFunc("GET", "/ssh/federation", h.SSHFederation)
  253. r.MethodFunc("POST", "/ssh/config", h.SSHConfig)
  254. r.MethodFunc("POST", "/ssh/config/{type}", h.SSHConfig)
  255. r.MethodFunc("POST", "/ssh/check-host", h.SSHCheckHost)
  256. r.MethodFunc("GET", "/ssh/hosts", h.SSHGetHosts)
  257. r.MethodFunc("POST", "/ssh/bastion", h.SSHBastion)
  258. // For compatibility with old code:
  259. r.MethodFunc("POST", "/re-sign", h.Renew)
  260. r.MethodFunc("POST", "/sign-ssh", h.SSHSign)
  261. r.MethodFunc("GET", "/ssh/get-hosts", h.SSHGetHosts)
  262. }
  263. // Version is an HTTP handler that returns the version of the server.
  264. func (h *caHandler) Version(w http.ResponseWriter, r *http.Request) {
  265. v := h.Authority.Version()
  266. JSON(w, VersionResponse{
  267. Version: v.Version,
  268. RequireClientAuthentication: v.RequireClientAuthentication,
  269. })
  270. }
  271. // Health is an HTTP handler that returns the status of the server.
  272. func (h *caHandler) Health(w http.ResponseWriter, r *http.Request) {
  273. JSON(w, HealthResponse{Status: "ok"})
  274. }
  275. // Root is an HTTP handler that using the SHA256 from the URL, returns the root
  276. // certificate for the given SHA256.
  277. func (h *caHandler) Root(w http.ResponseWriter, r *http.Request) {
  278. sha := chi.URLParam(r, "sha")
  279. sum := strings.ToLower(strings.ReplaceAll(sha, "-", ""))
  280. // Load root certificate with the
  281. cert, err := h.Authority.Root(sum)
  282. if err != nil {
  283. WriteError(w, errs.Wrapf(http.StatusNotFound, err, "%s was not found", r.RequestURI))
  284. return
  285. }
  286. JSON(w, &RootResponse{RootPEM: Certificate{cert}})
  287. }
  288. func certChainToPEM(certChain []*x509.Certificate) []Certificate {
  289. certChainPEM := make([]Certificate, 0, len(certChain))
  290. for _, c := range certChain {
  291. certChainPEM = append(certChainPEM, Certificate{c})
  292. }
  293. return certChainPEM
  294. }
  295. // Provisioners returns the list of provisioners configured in the authority.
  296. func (h *caHandler) Provisioners(w http.ResponseWriter, r *http.Request) {
  297. cursor, limit, err := ParseCursor(r)
  298. if err != nil {
  299. WriteError(w, err)
  300. return
  301. }
  302. p, next, err := h.Authority.GetProvisioners(cursor, limit)
  303. if err != nil {
  304. WriteError(w, errs.InternalServerErr(err))
  305. return
  306. }
  307. JSON(w, &ProvisionersResponse{
  308. Provisioners: p,
  309. NextCursor: next,
  310. })
  311. }
  312. // ProvisionerKey returns the encrypted key of a provisioner by it's key id.
  313. func (h *caHandler) ProvisionerKey(w http.ResponseWriter, r *http.Request) {
  314. kid := chi.URLParam(r, "kid")
  315. key, err := h.Authority.GetEncryptedKey(kid)
  316. if err != nil {
  317. WriteError(w, errs.NotFoundErr(err))
  318. return
  319. }
  320. JSON(w, &ProvisionerKeyResponse{key})
  321. }
  322. // Roots returns all the root certificates for the CA.
  323. func (h *caHandler) Roots(w http.ResponseWriter, r *http.Request) {
  324. roots, err := h.Authority.GetRoots()
  325. if err != nil {
  326. WriteError(w, errs.ForbiddenErr(err, "error getting roots"))
  327. return
  328. }
  329. certs := make([]Certificate, len(roots))
  330. for i := range roots {
  331. certs[i] = Certificate{roots[i]}
  332. }
  333. JSONStatus(w, &RootsResponse{
  334. Certificates: certs,
  335. }, http.StatusCreated)
  336. }
  337. // Federation returns all the public certificates in the federation.
  338. func (h *caHandler) Federation(w http.ResponseWriter, r *http.Request) {
  339. federated, err := h.Authority.GetFederation()
  340. if err != nil {
  341. WriteError(w, errs.ForbiddenErr(err, "error getting federated roots"))
  342. return
  343. }
  344. certs := make([]Certificate, len(federated))
  345. for i := range federated {
  346. certs[i] = Certificate{federated[i]}
  347. }
  348. JSONStatus(w, &FederationResponse{
  349. Certificates: certs,
  350. }, http.StatusCreated)
  351. }
  352. var oidStepProvisioner = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 37476, 9000, 64, 1}
  353. type stepProvisioner struct {
  354. Type int
  355. Name []byte
  356. CredentialID []byte
  357. }
  358. func logOtt(w http.ResponseWriter, token string) {
  359. if rl, ok := w.(logging.ResponseLogger); ok {
  360. rl.WithFields(map[string]interface{}{
  361. "ott": token,
  362. })
  363. }
  364. }
  365. // LogCertificate add certificate fields to the log message.
  366. func LogCertificate(w http.ResponseWriter, cert *x509.Certificate) {
  367. if rl, ok := w.(logging.ResponseLogger); ok {
  368. m := map[string]interface{}{
  369. "serial": cert.SerialNumber.String(),
  370. "subject": cert.Subject.CommonName,
  371. "issuer": cert.Issuer.CommonName,
  372. "valid-from": cert.NotBefore.Format(time.RFC3339),
  373. "valid-to": cert.NotAfter.Format(time.RFC3339),
  374. "public-key": fmtPublicKey(cert),
  375. "certificate": base64.StdEncoding.EncodeToString(cert.Raw),
  376. }
  377. for _, ext := range cert.Extensions {
  378. if !ext.Id.Equal(oidStepProvisioner) {
  379. continue
  380. }
  381. val := &stepProvisioner{}
  382. rest, err := asn1.Unmarshal(ext.Value, val)
  383. if err != nil || len(rest) > 0 {
  384. break
  385. }
  386. if len(val.CredentialID) > 0 {
  387. m["provisioner"] = fmt.Sprintf("%s (%s)", val.Name, val.CredentialID)
  388. } else {
  389. m["provisioner"] = string(val.Name)
  390. }
  391. break
  392. }
  393. rl.WithFields(m)
  394. }
  395. }
  396. // ParseCursor parses the cursor and limit from the request query params.
  397. func ParseCursor(r *http.Request) (cursor string, limit int, err error) {
  398. q := r.URL.Query()
  399. cursor = q.Get("cursor")
  400. if v := q.Get("limit"); len(v) > 0 {
  401. limit, err = strconv.Atoi(v)
  402. if err != nil {
  403. return "", 0, errs.BadRequestErr(err, "limit '%s' is not an integer", v)
  404. }
  405. }
  406. return
  407. }
  408. func fmtPublicKey(cert *x509.Certificate) string {
  409. var params string
  410. switch pk := cert.PublicKey.(type) {
  411. case *ecdsa.PublicKey:
  412. params = pk.Curve.Params().Name
  413. case *rsa.PublicKey:
  414. params = strconv.Itoa(pk.Size() * 8)
  415. case ed25519.PublicKey:
  416. return cert.PublicKeyAlgorithm.String()
  417. case *dsa.PublicKey:
  418. params = strconv.Itoa(pk.Q.BitLen() * 8)
  419. default:
  420. params = "unknown"
  421. }
  422. return fmt.Sprintf("%s %s", cert.PublicKeyAlgorithm, params)
  423. }
  424. type MockAuthority struct {
  425. ret1, ret2 interface{}
  426. err error
  427. authorizeSign func(ott string) ([]provisioner.SignOption, error)
  428. getTLSOptions func() *authority.TLSOptions
  429. root func(shasum string) (*x509.Certificate, error)
  430. sign func(cr *x509.CertificateRequest, opts provisioner.SignOptions, signOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
  431. renew func(cert *x509.Certificate) ([]*x509.Certificate, error)
  432. rekey func(oldCert *x509.Certificate, pk crypto.PublicKey) ([]*x509.Certificate, error)
  433. loadProvisionerByCertificate func(cert *x509.Certificate) (provisioner.Interface, error)
  434. MockLoadProvisionerByName func(name string) (provisioner.Interface, error)
  435. MockGetProvisioners func(nextCursor string, limit int) (provisioner.List, string, error)
  436. revoke func(context.Context, *authority.RevokeOptions) error
  437. getEncryptedKey func(kid string) (string, error)
  438. getRoots func() ([]*x509.Certificate, error)
  439. getFederation func() ([]*x509.Certificate, error)
  440. signSSH func(ctx context.Context, key ssh.PublicKey, opts provisioner.SignSSHOptions, signOpts ...provisioner.SignOption) (*ssh.Certificate, error)
  441. signSSHAddUser func(ctx context.Context, key ssh.PublicKey, cert *ssh.Certificate) (*ssh.Certificate, error)
  442. renewSSH func(ctx context.Context, cert *ssh.Certificate) (*ssh.Certificate, error)
  443. rekeySSH func(ctx context.Context, cert *ssh.Certificate, key ssh.PublicKey, signOpts ...provisioner.SignOption) (*ssh.Certificate, error)
  444. getSSHHosts func(ctx context.Context, cert *x509.Certificate) ([]authority.Host, error)
  445. getSSHRoots func(ctx context.Context) (*authority.SSHKeys, error)
  446. getSSHFederation func(ctx context.Context) (*authority.SSHKeys, error)
  447. getSSHConfig func(ctx context.Context, typ string, data map[string]string) ([]templates.Output, error)
  448. checkSSHHost func(ctx context.Context, principal, token string) (bool, error)
  449. getSSHBastion func(ctx context.Context, user string, hostname string) (*authority.Bastion, error)
  450. version func() authority.Version
  451. MockRet1, MockRet2 interface{} // TODO: refactor the ret1/ret2 into those two
  452. MockErr error
  453. MockIsAdminAPIEnabled func() bool
  454. MockLoadAdminByID func(id string) (*linkedca.Admin, bool)
  455. MockGetAdmins func(cursor string, limit int) ([]*linkedca.Admin, string, error)
  456. MockStoreAdmin func(ctx context.Context, adm *linkedca.Admin, prov provisioner.Interface) error
  457. MockUpdateAdmin func(ctx context.Context, id string, nu *linkedca.Admin) (*linkedca.Admin, error)
  458. MockRemoveAdmin func(ctx context.Context, id string) error
  459. MockAuthorizeAdminToken func(r *http.Request, token string) (*linkedca.Admin, error)
  460. MockStoreProvisioner func(ctx context.Context, prov *linkedca.Provisioner) error
  461. MockLoadProvisionerByID func(id string) (provisioner.Interface, error)
  462. MockUpdateProvisioner func(ctx context.Context, nu *linkedca.Provisioner) error
  463. MockRemoveProvisioner func(ctx context.Context, id string) error
  464. }
  465. // TODO: remove once Authorize is deprecated.
  466. func (m *MockAuthority) Authorize(ctx context.Context, ott string) ([]provisioner.SignOption, error) {
  467. return m.AuthorizeSign(ott)
  468. }
  469. func (m *MockAuthority) AuthorizeSign(ott string) ([]provisioner.SignOption, error) {
  470. if m.authorizeSign != nil {
  471. return m.authorizeSign(ott)
  472. }
  473. return m.ret1.([]provisioner.SignOption), m.err
  474. }
  475. func (m *MockAuthority) GetTLSOptions() *authority.TLSOptions {
  476. if m.getTLSOptions != nil {
  477. return m.getTLSOptions()
  478. }
  479. return m.ret1.(*authority.TLSOptions)
  480. }
  481. func (m *MockAuthority) Root(shasum string) (*x509.Certificate, error) {
  482. if m.root != nil {
  483. return m.root(shasum)
  484. }
  485. return m.ret1.(*x509.Certificate), m.err
  486. }
  487. func (m *MockAuthority) Sign(cr *x509.CertificateRequest, opts provisioner.SignOptions, signOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
  488. if m.sign != nil {
  489. return m.sign(cr, opts, signOpts...)
  490. }
  491. return []*x509.Certificate{m.ret1.(*x509.Certificate), m.ret2.(*x509.Certificate)}, m.err
  492. }
  493. func (m *MockAuthority) Renew(cert *x509.Certificate) ([]*x509.Certificate, error) {
  494. if m.renew != nil {
  495. return m.renew(cert)
  496. }
  497. return []*x509.Certificate{m.ret1.(*x509.Certificate), m.ret2.(*x509.Certificate)}, m.err
  498. }
  499. func (m *MockAuthority) Rekey(oldcert *x509.Certificate, pk crypto.PublicKey) ([]*x509.Certificate, error) {
  500. if m.rekey != nil {
  501. return m.rekey(oldcert, pk)
  502. }
  503. return []*x509.Certificate{m.ret1.(*x509.Certificate), m.ret2.(*x509.Certificate)}, m.err
  504. }
  505. func (m *MockAuthority) GetProvisioners(nextCursor string, limit int) (provisioner.List, string, error) {
  506. if m.MockGetProvisioners != nil {
  507. return m.MockGetProvisioners(nextCursor, limit)
  508. }
  509. return m.ret1.(provisioner.List), m.ret2.(string), m.err
  510. }
  511. func (m *MockAuthority) LoadProvisionerByCertificate(cert *x509.Certificate) (provisioner.Interface, error) {
  512. if m.loadProvisionerByCertificate != nil {
  513. return m.loadProvisionerByCertificate(cert)
  514. }
  515. return m.ret1.(provisioner.Interface), m.err
  516. }
  517. func (m *MockAuthority) LoadProvisionerByName(name string) (provisioner.Interface, error) {
  518. if m.MockLoadProvisionerByName != nil {
  519. return m.MockLoadProvisionerByName(name)
  520. }
  521. return m.ret1.(provisioner.Interface), m.err
  522. }
  523. func (m *MockAuthority) Revoke(ctx context.Context, opts *authority.RevokeOptions) error {
  524. if m.revoke != nil {
  525. return m.revoke(ctx, opts)
  526. }
  527. return m.err
  528. }
  529. func (m *MockAuthority) GetEncryptedKey(kid string) (string, error) {
  530. if m.getEncryptedKey != nil {
  531. return m.getEncryptedKey(kid)
  532. }
  533. return m.ret1.(string), m.err
  534. }
  535. func (m *MockAuthority) GetRoots() ([]*x509.Certificate, error) {
  536. if m.getRoots != nil {
  537. return m.getRoots()
  538. }
  539. return m.ret1.([]*x509.Certificate), m.err
  540. }
  541. func (m *MockAuthority) GetFederation() ([]*x509.Certificate, error) {
  542. if m.getFederation != nil {
  543. return m.getFederation()
  544. }
  545. return m.ret1.([]*x509.Certificate), m.err
  546. }
  547. func (m *MockAuthority) SignSSH(ctx context.Context, key ssh.PublicKey, opts provisioner.SignSSHOptions, signOpts ...provisioner.SignOption) (*ssh.Certificate, error) {
  548. if m.signSSH != nil {
  549. return m.signSSH(ctx, key, opts, signOpts...)
  550. }
  551. return m.ret1.(*ssh.Certificate), m.err
  552. }
  553. func (m *MockAuthority) SignSSHAddUser(ctx context.Context, key ssh.PublicKey, cert *ssh.Certificate) (*ssh.Certificate, error) {
  554. if m.signSSHAddUser != nil {
  555. return m.signSSHAddUser(ctx, key, cert)
  556. }
  557. return m.ret1.(*ssh.Certificate), m.err
  558. }
  559. func (m *MockAuthority) RenewSSH(ctx context.Context, cert *ssh.Certificate) (*ssh.Certificate, error) {
  560. if m.renewSSH != nil {
  561. return m.renewSSH(ctx, cert)
  562. }
  563. return m.ret1.(*ssh.Certificate), m.err
  564. }
  565. func (m *MockAuthority) RekeySSH(ctx context.Context, cert *ssh.Certificate, key ssh.PublicKey, signOpts ...provisioner.SignOption) (*ssh.Certificate, error) {
  566. if m.rekeySSH != nil {
  567. return m.rekeySSH(ctx, cert, key, signOpts...)
  568. }
  569. return m.ret1.(*ssh.Certificate), m.err
  570. }
  571. func (m *MockAuthority) GetSSHHosts(ctx context.Context, cert *x509.Certificate) ([]authority.Host, error) {
  572. if m.getSSHHosts != nil {
  573. return m.getSSHHosts(ctx, cert)
  574. }
  575. return m.ret1.([]authority.Host), m.err
  576. }
  577. func (m *MockAuthority) GetSSHRoots(ctx context.Context) (*authority.SSHKeys, error) {
  578. if m.getSSHRoots != nil {
  579. return m.getSSHRoots(ctx)
  580. }
  581. return m.ret1.(*authority.SSHKeys), m.err
  582. }
  583. func (m *MockAuthority) GetSSHFederation(ctx context.Context) (*authority.SSHKeys, error) {
  584. if m.getSSHFederation != nil {
  585. return m.getSSHFederation(ctx)
  586. }
  587. return m.ret1.(*authority.SSHKeys), m.err
  588. }
  589. func (m *MockAuthority) GetSSHConfig(ctx context.Context, typ string, data map[string]string) ([]templates.Output, error) {
  590. if m.getSSHConfig != nil {
  591. return m.getSSHConfig(ctx, typ, data)
  592. }
  593. return m.ret1.([]templates.Output), m.err
  594. }
  595. func (m *MockAuthority) CheckSSHHost(ctx context.Context, principal, token string) (bool, error) {
  596. if m.checkSSHHost != nil {
  597. return m.checkSSHHost(ctx, principal, token)
  598. }
  599. return m.ret1.(bool), m.err
  600. }
  601. func (m *MockAuthority) GetSSHBastion(ctx context.Context, user, hostname string) (*authority.Bastion, error) {
  602. if m.getSSHBastion != nil {
  603. return m.getSSHBastion(ctx, user, hostname)
  604. }
  605. return m.ret1.(*authority.Bastion), m.err
  606. }
  607. func (m *MockAuthority) Version() authority.Version {
  608. if m.version != nil {
  609. return m.version()
  610. }
  611. return m.ret1.(authority.Version)
  612. }
  613. func (m *MockAuthority) IsAdminAPIEnabled() bool {
  614. if m.MockIsAdminAPIEnabled != nil {
  615. return m.MockIsAdminAPIEnabled()
  616. }
  617. return m.MockRet1.(bool)
  618. }
  619. func (m *MockAuthority) LoadAdminByID(id string) (*linkedca.Admin, bool) {
  620. if m.MockLoadAdminByID != nil {
  621. return m.MockLoadAdminByID(id)
  622. }
  623. return m.MockRet1.(*linkedca.Admin), m.MockRet2.(bool)
  624. }
  625. func (m *MockAuthority) GetAdmins(cursor string, limit int) ([]*linkedca.Admin, string, error) {
  626. if m.MockGetAdmins != nil {
  627. return m.MockGetAdmins(cursor, limit)
  628. }
  629. return m.MockRet1.([]*linkedca.Admin), m.MockRet2.(string), m.MockErr
  630. }
  631. func (m *MockAuthority) StoreAdmin(ctx context.Context, adm *linkedca.Admin, prov provisioner.Interface) error {
  632. if m.MockStoreAdmin != nil {
  633. return m.MockStoreAdmin(ctx, adm, prov)
  634. }
  635. return m.MockErr
  636. }
  637. func (m *MockAuthority) UpdateAdmin(ctx context.Context, id string, nu *linkedca.Admin) (*linkedca.Admin, error) {
  638. if m.MockUpdateAdmin != nil {
  639. return m.MockUpdateAdmin(ctx, id, nu)
  640. }
  641. return m.MockRet1.(*linkedca.Admin), m.MockErr
  642. }
  643. func (m *MockAuthority) RemoveAdmin(ctx context.Context, id string) error {
  644. if m.MockRemoveAdmin != nil {
  645. return m.MockRemoveAdmin(ctx, id)
  646. }
  647. return m.MockErr
  648. }
  649. func (m *MockAuthority) AuthorizeAdminToken(r *http.Request, token string) (*linkedca.Admin, error) {
  650. if m.MockAuthorizeAdminToken != nil {
  651. return m.MockAuthorizeAdminToken(r, token)
  652. }
  653. return m.MockRet1.(*linkedca.Admin), m.MockErr
  654. }
  655. func (m *MockAuthority) StoreProvisioner(ctx context.Context, prov *linkedca.Provisioner) error {
  656. if m.MockStoreProvisioner != nil {
  657. return m.MockStoreProvisioner(ctx, prov)
  658. }
  659. return m.MockErr
  660. }
  661. func (m *MockAuthority) LoadProvisionerByID(id string) (provisioner.Interface, error) {
  662. if m.MockLoadProvisionerByID != nil {
  663. return m.MockLoadProvisionerByID(id)
  664. }
  665. return m.MockRet1.(provisioner.Interface), m.MockErr
  666. }
  667. func (m *MockAuthority) UpdateProvisioner(ctx context.Context, nu *linkedca.Provisioner) error {
  668. if m.MockUpdateProvisioner != nil {
  669. return m.MockUpdateProvisioner(ctx, nu)
  670. }
  671. return m.MockErr
  672. }
  673. func (m *MockAuthority) RemoveProvisioner(ctx context.Context, id string) error {
  674. if m.MockRemoveProvisioner != nil {
  675. return m.MockRemoveProvisioner(ctx, id)
  676. }
  677. return m.MockErr
  678. }