authority.go 20 KB

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