authority.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. package authority
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/sha256"
  6. "crypto/x509"
  7. "encoding/hex"
  8. "log"
  9. "strings"
  10. "sync"
  11. "time"
  12. "github.com/pkg/errors"
  13. "github.com/smallstep/certificates/authority/admin"
  14. adminDBNosql "github.com/smallstep/certificates/authority/admin/db/nosql"
  15. "github.com/smallstep/certificates/authority/administrator"
  16. "github.com/smallstep/certificates/authority/config"
  17. "github.com/smallstep/certificates/authority/policy"
  18. "github.com/smallstep/certificates/authority/provisioner"
  19. "github.com/smallstep/certificates/cas"
  20. casapi "github.com/smallstep/certificates/cas/apiv1"
  21. "github.com/smallstep/certificates/db"
  22. "github.com/smallstep/certificates/kms"
  23. kmsapi "github.com/smallstep/certificates/kms/apiv1"
  24. "github.com/smallstep/certificates/kms/sshagentkms"
  25. "github.com/smallstep/certificates/scep"
  26. "github.com/smallstep/certificates/templates"
  27. "github.com/smallstep/nosql"
  28. "go.step.sm/crypto/pemutil"
  29. "go.step.sm/linkedca"
  30. "golang.org/x/crypto/ssh"
  31. )
  32. // Authority implements the Certificate Authority internal interface.
  33. type Authority struct {
  34. config *config.Config
  35. keyManager kms.KeyManager
  36. provisioners *provisioner.Collection
  37. admins *administrator.Collection
  38. db db.AuthDB
  39. adminDB admin.DB
  40. templates *templates.Templates
  41. linkedCAToken string
  42. // X509 CA
  43. password []byte
  44. issuerPassword []byte
  45. x509CAService cas.CertificateAuthorityService
  46. rootX509Certs []*x509.Certificate
  47. rootX509CertPool *x509.CertPool
  48. federatedX509Certs []*x509.Certificate
  49. certificates *sync.Map
  50. x509Enforcers []provisioner.CertificateEnforcer
  51. // SCEP CA
  52. scepService *scep.Service
  53. // SSH CA
  54. sshHostPassword []byte
  55. sshUserPassword []byte
  56. sshCAUserCertSignKey ssh.Signer
  57. sshCAHostCertSignKey ssh.Signer
  58. sshCAUserCerts []ssh.PublicKey
  59. sshCAHostCerts []ssh.PublicKey
  60. sshCAUserFederatedCerts []ssh.PublicKey
  61. sshCAHostFederatedCerts []ssh.PublicKey
  62. // Do not re-initialize
  63. initOnce bool
  64. startTime time.Time
  65. // Custom functions
  66. sshBastionFunc func(ctx context.Context, user, hostname string) (*config.Bastion, error)
  67. sshCheckHostFunc func(ctx context.Context, principal string, tok string, roots []*x509.Certificate) (bool, error)
  68. sshGetHostsFunc func(ctx context.Context, cert *x509.Certificate) ([]config.Host, error)
  69. getIdentityFunc provisioner.GetIdentityFunc
  70. // Policy engines
  71. x509Policy policy.X509Policy
  72. sshUserPolicy policy.UserPolicy
  73. sshHostPolicy policy.HostPolicy
  74. adminMutex sync.RWMutex
  75. }
  76. // New creates and initiates a new Authority type.
  77. func New(cfg *config.Config, opts ...Option) (*Authority, error) {
  78. err := cfg.Validate()
  79. if err != nil {
  80. return nil, err
  81. }
  82. var a = &Authority{
  83. config: cfg,
  84. certificates: new(sync.Map),
  85. }
  86. // Apply options.
  87. for _, fn := range opts {
  88. if err := fn(a); err != nil {
  89. return nil, err
  90. }
  91. }
  92. // Initialize authority from options or configuration.
  93. if err := a.init(); err != nil {
  94. return nil, err
  95. }
  96. return a, nil
  97. }
  98. // NewEmbedded initializes an authority that can be embedded in a different
  99. // project without the limitations of the config.
  100. func NewEmbedded(opts ...Option) (*Authority, error) {
  101. a := &Authority{
  102. config: &config.Config{},
  103. certificates: new(sync.Map),
  104. }
  105. // Apply options.
  106. for _, fn := range opts {
  107. if err := fn(a); err != nil {
  108. return nil, err
  109. }
  110. }
  111. // Validate required options
  112. switch {
  113. case a.config == nil:
  114. return nil, errors.New("cannot create an authority without a configuration")
  115. case len(a.rootX509Certs) == 0 && a.config.Root.HasEmpties():
  116. return nil, errors.New("cannot create an authority without a root certificate")
  117. case a.x509CAService == nil && a.config.IntermediateCert == "":
  118. return nil, errors.New("cannot create an authority without an issuer certificate")
  119. case a.x509CAService == nil && a.config.IntermediateKey == "":
  120. return nil, errors.New("cannot create an authority without an issuer signer")
  121. }
  122. // Initialize config required fields.
  123. a.config.Init()
  124. // Initialize authority from options or configuration.
  125. if err := a.init(); err != nil {
  126. return nil, err
  127. }
  128. return a, nil
  129. }
  130. // reloadAdminResources reloads admins and provisioners from the DB.
  131. func (a *Authority) reloadAdminResources(ctx context.Context) error {
  132. var (
  133. provList provisioner.List
  134. adminList []*linkedca.Admin
  135. )
  136. if a.config.AuthorityConfig.EnableAdmin {
  137. provs, err := a.adminDB.GetProvisioners(ctx)
  138. if err != nil {
  139. return admin.WrapErrorISE(err, "error getting provisioners to initialize authority")
  140. }
  141. provList, err = provisionerListToCertificates(provs)
  142. if err != nil {
  143. return admin.WrapErrorISE(err, "error converting provisioner list to certificates")
  144. }
  145. adminList, err = a.adminDB.GetAdmins(ctx)
  146. if err != nil {
  147. return admin.WrapErrorISE(err, "error getting admins to initialize authority")
  148. }
  149. } else {
  150. provList = a.config.AuthorityConfig.Provisioners
  151. adminList = a.config.AuthorityConfig.Admins
  152. }
  153. provisionerConfig, err := a.generateProvisionerConfig(ctx)
  154. if err != nil {
  155. return admin.WrapErrorISE(err, "error generating provisioner config")
  156. }
  157. // Create provisioner collection.
  158. provClxn := provisioner.NewCollection(provisionerConfig.Audiences)
  159. for _, p := range provList {
  160. if err := p.Init(provisionerConfig); err != nil {
  161. return err
  162. }
  163. if err := provClxn.Store(p); err != nil {
  164. return err
  165. }
  166. }
  167. // Create admin collection.
  168. adminClxn := administrator.NewCollection(provClxn)
  169. for _, adm := range adminList {
  170. p, ok := provClxn.Load(adm.ProvisionerId)
  171. if !ok {
  172. return admin.NewErrorISE("provisioner %s not found when loading admin %s",
  173. adm.ProvisionerId, adm.Id)
  174. }
  175. if err := adminClxn.Store(adm, p); err != nil {
  176. return err
  177. }
  178. }
  179. a.config.AuthorityConfig.Provisioners = provList
  180. a.provisioners = provClxn
  181. a.config.AuthorityConfig.Admins = adminList
  182. a.admins = adminClxn
  183. return nil
  184. }
  185. // reloadPolicyEngines reloads x509 and SSH policy engines using
  186. // configuration stored in the DB or from the configuration file.
  187. func (a *Authority) reloadPolicyEngines(ctx context.Context) error {
  188. var (
  189. err error
  190. policyOptions *policy.Options
  191. )
  192. if a.config.AuthorityConfig.EnableAdmin {
  193. linkedPolicy, err := a.adminDB.GetAuthorityPolicy(ctx)
  194. if err != nil {
  195. return admin.WrapErrorISE(err, "error getting policy to (re)load policy engines")
  196. }
  197. policyOptions = policyToCertificates(linkedPolicy)
  198. } else {
  199. policyOptions = a.config.AuthorityConfig.Policy
  200. }
  201. // if no new or updated policy option is set, clear policy engines that (may have)
  202. // been configured before and return early
  203. if policyOptions == nil {
  204. a.x509Policy = nil
  205. a.sshHostPolicy = nil
  206. a.sshUserPolicy = nil
  207. return nil
  208. }
  209. // Initialize the x509 allow/deny policy engine
  210. if a.x509Policy, err = policy.NewX509PolicyEngine(policyOptions.GetX509Options()); err != nil {
  211. return err
  212. }
  213. // // Initialize the SSH allow/deny policy engine for host certificates
  214. if a.sshHostPolicy, err = policy.NewSSHHostPolicyEngine(policyOptions.GetSSHOptions()); err != nil {
  215. return err
  216. }
  217. // // Initialize the SSH allow/deny policy engine for user certificates
  218. if a.sshUserPolicy, err = policy.NewSSHUserPolicyEngine(policyOptions.GetSSHOptions()); err != nil {
  219. return err
  220. }
  221. return nil
  222. }
  223. // init performs validation and initializes the fields of an Authority struct.
  224. func (a *Authority) init() error {
  225. // Check if handler has already been validated/initialized.
  226. if a.initOnce {
  227. return nil
  228. }
  229. var err error
  230. // Set password if they are not set.
  231. var configPassword []byte
  232. if a.config.Password != "" {
  233. configPassword = []byte(a.config.Password)
  234. }
  235. if configPassword != nil && a.password == nil {
  236. a.password = configPassword
  237. }
  238. if a.sshHostPassword == nil {
  239. a.sshHostPassword = a.password
  240. }
  241. if a.sshUserPassword == nil {
  242. a.sshUserPassword = a.password
  243. }
  244. // Automatically enable admin for all linked cas.
  245. if a.linkedCAToken != "" {
  246. a.config.AuthorityConfig.EnableAdmin = true
  247. }
  248. // Initialize step-ca Database if it's not already initialized with WithDB.
  249. // If a.config.DB is nil then a simple, barebones in memory DB will be used.
  250. if a.db == nil {
  251. if a.db, err = db.New(a.config.DB); err != nil {
  252. return err
  253. }
  254. }
  255. // Initialize key manager if it has not been set in the options.
  256. if a.keyManager == nil {
  257. var options kmsapi.Options
  258. if a.config.KMS != nil {
  259. options = *a.config.KMS
  260. }
  261. a.keyManager, err = kms.New(context.Background(), options)
  262. if err != nil {
  263. return err
  264. }
  265. }
  266. // Initialize the X.509 CA Service if it has not been set in the options.
  267. if a.x509CAService == nil {
  268. var options casapi.Options
  269. if a.config.AuthorityConfig.Options != nil {
  270. options = *a.config.AuthorityConfig.Options
  271. }
  272. // Set the issuer password if passed in the flags.
  273. if options.CertificateIssuer != nil && a.issuerPassword != nil {
  274. options.CertificateIssuer.Password = string(a.issuerPassword)
  275. }
  276. // Read intermediate and create X509 signer for default CAS.
  277. if options.Is(casapi.SoftCAS) {
  278. options.CertificateChain, err = pemutil.ReadCertificateBundle(a.config.IntermediateCert)
  279. if err != nil {
  280. return err
  281. }
  282. options.Signer, err = a.keyManager.CreateSigner(&kmsapi.CreateSignerRequest{
  283. SigningKey: a.config.IntermediateKey,
  284. Password: []byte(a.password),
  285. })
  286. if err != nil {
  287. return err
  288. }
  289. }
  290. a.x509CAService, err = cas.New(context.Background(), options)
  291. if err != nil {
  292. return err
  293. }
  294. // Get root certificate from CAS.
  295. if srv, ok := a.x509CAService.(casapi.CertificateAuthorityGetter); ok {
  296. resp, err := srv.GetCertificateAuthority(&casapi.GetCertificateAuthorityRequest{
  297. Name: options.CertificateAuthority,
  298. })
  299. if err != nil {
  300. return err
  301. }
  302. a.rootX509Certs = append(a.rootX509Certs, resp.RootCertificate)
  303. sum := sha256.Sum256(resp.RootCertificate.Raw)
  304. log.Printf("Using root fingerprint '%s'", hex.EncodeToString(sum[:]))
  305. }
  306. }
  307. // Read root certificates and store them in the certificates map.
  308. if len(a.rootX509Certs) == 0 {
  309. a.rootX509Certs = make([]*x509.Certificate, len(a.config.Root))
  310. for i, path := range a.config.Root {
  311. crt, err := pemutil.ReadCertificate(path)
  312. if err != nil {
  313. return err
  314. }
  315. a.rootX509Certs[i] = crt
  316. }
  317. }
  318. for _, crt := range a.rootX509Certs {
  319. sum := sha256.Sum256(crt.Raw)
  320. a.certificates.Store(hex.EncodeToString(sum[:]), crt)
  321. }
  322. a.rootX509CertPool = x509.NewCertPool()
  323. for _, cert := range a.rootX509Certs {
  324. a.rootX509CertPool.AddCert(cert)
  325. }
  326. // Read federated certificates and store them in the certificates map.
  327. if len(a.federatedX509Certs) == 0 {
  328. a.federatedX509Certs = make([]*x509.Certificate, len(a.config.FederatedRoots))
  329. for i, path := range a.config.FederatedRoots {
  330. crt, err := pemutil.ReadCertificate(path)
  331. if err != nil {
  332. return err
  333. }
  334. a.federatedX509Certs[i] = crt
  335. }
  336. }
  337. for _, crt := range a.federatedX509Certs {
  338. sum := sha256.Sum256(crt.Raw)
  339. a.certificates.Store(hex.EncodeToString(sum[:]), crt)
  340. }
  341. // Decrypt and load SSH keys
  342. var tmplVars templates.Step
  343. if a.config.SSH != nil {
  344. if a.config.SSH.HostKey != "" {
  345. signer, err := a.keyManager.CreateSigner(&kmsapi.CreateSignerRequest{
  346. SigningKey: a.config.SSH.HostKey,
  347. Password: []byte(a.sshHostPassword),
  348. })
  349. if err != nil {
  350. return err
  351. }
  352. // If our signer is from sshagentkms, just unwrap it instead of
  353. // wrapping it in another layer, and this prevents crypto from
  354. // erroring out with: ssh: unsupported key type *agent.Key
  355. switch s := signer.(type) {
  356. case *sshagentkms.WrappedSSHSigner:
  357. a.sshCAHostCertSignKey = s.Sshsigner
  358. case crypto.Signer:
  359. a.sshCAHostCertSignKey, err = ssh.NewSignerFromSigner(s)
  360. default:
  361. return errors.Errorf("unsupported signer type %T", signer)
  362. }
  363. if err != nil {
  364. return errors.Wrap(err, "error creating ssh signer")
  365. }
  366. // Append public key to list of host certs
  367. a.sshCAHostCerts = append(a.sshCAHostCerts, a.sshCAHostCertSignKey.PublicKey())
  368. a.sshCAHostFederatedCerts = append(a.sshCAHostFederatedCerts, a.sshCAHostCertSignKey.PublicKey())
  369. }
  370. if a.config.SSH.UserKey != "" {
  371. signer, err := a.keyManager.CreateSigner(&kmsapi.CreateSignerRequest{
  372. SigningKey: a.config.SSH.UserKey,
  373. Password: []byte(a.sshUserPassword),
  374. })
  375. if err != nil {
  376. return err
  377. }
  378. // If our signer is from sshagentkms, just unwrap it instead of
  379. // wrapping it in another layer, and this prevents crypto from
  380. // erroring out with: ssh: unsupported key type *agent.Key
  381. switch s := signer.(type) {
  382. case *sshagentkms.WrappedSSHSigner:
  383. a.sshCAUserCertSignKey = s.Sshsigner
  384. case crypto.Signer:
  385. a.sshCAUserCertSignKey, err = ssh.NewSignerFromSigner(s)
  386. default:
  387. return errors.Errorf("unsupported signer type %T", signer)
  388. }
  389. if err != nil {
  390. return errors.Wrap(err, "error creating ssh signer")
  391. }
  392. // Append public key to list of user certs
  393. a.sshCAUserCerts = append(a.sshCAUserCerts, a.sshCAUserCertSignKey.PublicKey())
  394. a.sshCAUserFederatedCerts = append(a.sshCAUserFederatedCerts, a.sshCAUserCertSignKey.PublicKey())
  395. }
  396. // Append other public keys and add them to the template variables.
  397. for _, key := range a.config.SSH.Keys {
  398. publicKey := key.PublicKey()
  399. switch key.Type {
  400. case provisioner.SSHHostCert:
  401. if key.Federated {
  402. a.sshCAHostFederatedCerts = append(a.sshCAHostFederatedCerts, publicKey)
  403. } else {
  404. a.sshCAHostCerts = append(a.sshCAHostCerts, publicKey)
  405. }
  406. case provisioner.SSHUserCert:
  407. if key.Federated {
  408. a.sshCAUserFederatedCerts = append(a.sshCAUserFederatedCerts, publicKey)
  409. } else {
  410. a.sshCAUserCerts = append(a.sshCAUserCerts, publicKey)
  411. }
  412. default:
  413. return errors.Errorf("unsupported type %s", key.Type)
  414. }
  415. }
  416. }
  417. // Configure template variables. On the template variables HostFederatedKeys
  418. // and UserFederatedKeys we will skip the actual CA that will be available
  419. // in HostKey and UserKey.
  420. //
  421. // We cannot do it in the previous blocks because this configuration can be
  422. // injected using options.
  423. if a.sshCAHostCertSignKey != nil {
  424. tmplVars.SSH.HostKey = a.sshCAHostCertSignKey.PublicKey()
  425. tmplVars.SSH.HostFederatedKeys = append(tmplVars.SSH.HostFederatedKeys, a.sshCAHostFederatedCerts[1:]...)
  426. } else {
  427. tmplVars.SSH.HostFederatedKeys = append(tmplVars.SSH.HostFederatedKeys, a.sshCAHostFederatedCerts...)
  428. }
  429. if a.sshCAUserCertSignKey != nil {
  430. tmplVars.SSH.UserKey = a.sshCAUserCertSignKey.PublicKey()
  431. tmplVars.SSH.UserFederatedKeys = append(tmplVars.SSH.UserFederatedKeys, a.sshCAUserFederatedCerts[1:]...)
  432. } else {
  433. tmplVars.SSH.UserFederatedKeys = append(tmplVars.SSH.UserFederatedKeys, a.sshCAUserFederatedCerts...)
  434. }
  435. // Check if a KMS with decryption capability is required and available
  436. if a.requiresDecrypter() {
  437. if _, ok := a.keyManager.(kmsapi.Decrypter); !ok {
  438. return errors.New("keymanager doesn't provide crypto.Decrypter")
  439. }
  440. }
  441. // TODO: decide if this is a good approach for providing the SCEP functionality
  442. // It currently mirrors the logic for the x509CAService
  443. if a.requiresSCEPService() && a.scepService == nil {
  444. var options scep.Options
  445. // Read intermediate and create X509 signer and decrypter for default CAS.
  446. options.CertificateChain, err = pemutil.ReadCertificateBundle(a.config.IntermediateCert)
  447. if err != nil {
  448. return err
  449. }
  450. options.CertificateChain = append(options.CertificateChain, a.rootX509Certs...)
  451. options.Signer, err = a.keyManager.CreateSigner(&kmsapi.CreateSignerRequest{
  452. SigningKey: a.config.IntermediateKey,
  453. Password: []byte(a.password),
  454. })
  455. if err != nil {
  456. return err
  457. }
  458. if km, ok := a.keyManager.(kmsapi.Decrypter); ok {
  459. options.Decrypter, err = km.CreateDecrypter(&kmsapi.CreateDecrypterRequest{
  460. DecryptionKey: a.config.IntermediateKey,
  461. Password: []byte(a.password),
  462. })
  463. if err != nil {
  464. return err
  465. }
  466. }
  467. a.scepService, err = scep.NewService(context.Background(), options)
  468. if err != nil {
  469. return err
  470. }
  471. // TODO: mimick the x509CAService GetCertificateAuthority here too?
  472. }
  473. if a.config.AuthorityConfig.EnableAdmin {
  474. // Initialize step-ca Admin Database if it's not already initialized using
  475. // WithAdminDB.
  476. if a.adminDB == nil {
  477. if a.linkedCAToken == "" {
  478. // Check if AuthConfig already exists
  479. a.adminDB, err = adminDBNosql.New(a.db.(nosql.DB), admin.DefaultAuthorityID)
  480. if err != nil {
  481. return err
  482. }
  483. } else {
  484. // Use the linkedca client as the admindb.
  485. client, err := newLinkedCAClient(a.linkedCAToken)
  486. if err != nil {
  487. return err
  488. }
  489. // If authorityId is configured make sure it matches the one in the token
  490. if id := a.config.AuthorityConfig.AuthorityID; id != "" && !strings.EqualFold(id, client.authorityID) {
  491. return errors.New("error initializing linkedca: token authority and configured authority do not match")
  492. }
  493. client.Run()
  494. a.adminDB = client
  495. }
  496. }
  497. provs, err := a.adminDB.GetProvisioners(context.Background())
  498. if err != nil {
  499. return admin.WrapErrorISE(err, "error loading provisioners to initialize authority")
  500. }
  501. if len(provs) == 0 && !strings.EqualFold(a.config.AuthorityConfig.DeploymentType, "linked") {
  502. // Create First Provisioner
  503. prov, err := CreateFirstProvisioner(context.Background(), a.adminDB, string(a.password))
  504. if err != nil {
  505. return admin.WrapErrorISE(err, "error creating first provisioner")
  506. }
  507. // Create first admin
  508. if err := a.adminDB.CreateAdmin(context.Background(), &linkedca.Admin{
  509. ProvisionerId: prov.Id,
  510. Subject: "step",
  511. Type: linkedca.Admin_SUPER_ADMIN,
  512. }); err != nil {
  513. return admin.WrapErrorISE(err, "error creating first admin")
  514. }
  515. }
  516. }
  517. // Load Provisioners and Admins
  518. if err := a.reloadAdminResources(context.Background()); err != nil {
  519. return err
  520. }
  521. // Load x509 and SSH Policy Engines
  522. if err := a.reloadPolicyEngines(context.Background()); err != nil {
  523. return err
  524. }
  525. // Configure templates, currently only ssh templates are supported.
  526. if a.sshCAHostCertSignKey != nil || a.sshCAUserCertSignKey != nil {
  527. a.templates = a.config.Templates
  528. if a.templates == nil {
  529. a.templates = templates.DefaultTemplates()
  530. }
  531. if a.templates.Data == nil {
  532. a.templates.Data = make(map[string]interface{})
  533. }
  534. a.templates.Data["Step"] = tmplVars
  535. }
  536. // JWT numeric dates are seconds.
  537. a.startTime = time.Now().Truncate(time.Second)
  538. // Set flag indicating that initialization has been completed, and should
  539. // not be repeated.
  540. a.initOnce = true
  541. return nil
  542. }
  543. // GetDatabase returns the authority database. If the configuration does not
  544. // define a database, GetDatabase will return a db.SimpleDB instance.
  545. func (a *Authority) GetDatabase() db.AuthDB {
  546. return a.db
  547. }
  548. // GetAdminDatabase returns the admin database, if one exists.
  549. func (a *Authority) GetAdminDatabase() admin.DB {
  550. return a.adminDB
  551. }
  552. // IsAdminAPIEnabled returns a boolean indicating whether the Admin API has
  553. // been enabled.
  554. func (a *Authority) IsAdminAPIEnabled() bool {
  555. return a.config.AuthorityConfig.EnableAdmin
  556. }
  557. // Shutdown safely shuts down any clients, databases, etc. held by the Authority.
  558. func (a *Authority) Shutdown() error {
  559. if err := a.keyManager.Close(); err != nil {
  560. log.Printf("error closing the key manager: %v", err)
  561. }
  562. return a.db.Shutdown()
  563. }
  564. // CloseForReload closes internal services, to allow a safe reload.
  565. func (a *Authority) CloseForReload() {
  566. if err := a.keyManager.Close(); err != nil {
  567. log.Printf("error closing the key manager: %v", err)
  568. }
  569. if client, ok := a.adminDB.(*linkedCaClient); ok {
  570. client.Stop()
  571. }
  572. }
  573. // IsRevoked returns whether or not a certificate has been
  574. // revoked before.
  575. func (a *Authority) IsRevoked(sn string) (bool, error) {
  576. // Check the passive revocation table.
  577. if lca, ok := a.adminDB.(interface {
  578. IsRevoked(string) (bool, error)
  579. }); ok {
  580. return lca.IsRevoked(sn)
  581. }
  582. return a.db.IsRevoked(sn)
  583. }
  584. // requiresDecrypter returns whether the Authority
  585. // requires a KMS that provides a crypto.Decrypter
  586. // Currently this is only required when SCEP is
  587. // enabled.
  588. func (a *Authority) requiresDecrypter() bool {
  589. return a.requiresSCEPService()
  590. }
  591. // requiresSCEPService iterates over the configured provisioners
  592. // and determines if one of them is a SCEP provisioner.
  593. func (a *Authority) requiresSCEPService() bool {
  594. for _, p := range a.config.AuthorityConfig.Provisioners {
  595. if p.GetType() == provisioner.TypeSCEP {
  596. return true
  597. }
  598. }
  599. return false
  600. }
  601. // GetSCEPService returns the configured SCEP Service
  602. // TODO: this function is intended to exist temporarily
  603. // in order to make SCEP work more easily. It can be
  604. // made more correct by using the right interfaces/abstractions
  605. // after it works as expected.
  606. func (a *Authority) GetSCEPService() *scep.Service {
  607. return a.scepService
  608. }