admins.go 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package authority
  2. import (
  3. "context"
  4. "github.com/smallstep/certificates/authority/admin"
  5. "github.com/smallstep/certificates/authority/provisioner"
  6. "go.step.sm/linkedca"
  7. )
  8. // LoadAdminByID returns an *linkedca.Admin with the given ID.
  9. func (a *Authority) LoadAdminByID(id string) (*linkedca.Admin, bool) {
  10. a.adminMutex.RLock()
  11. defer a.adminMutex.RUnlock()
  12. return a.admins.LoadByID(id)
  13. }
  14. // LoadAdminBySubProv returns an *linkedca.Admin with the given ID.
  15. func (a *Authority) LoadAdminBySubProv(subject, prov string) (*linkedca.Admin, bool) {
  16. a.adminMutex.RLock()
  17. defer a.adminMutex.RUnlock()
  18. return a.admins.LoadBySubProv(subject, prov)
  19. }
  20. // GetAdmins returns a map listing each provisioner and the JWK Key Set
  21. // with their public keys.
  22. func (a *Authority) GetAdmins(cursor string, limit int) ([]*linkedca.Admin, string, error) {
  23. a.adminMutex.RLock()
  24. defer a.adminMutex.RUnlock()
  25. admins, nextCursor := a.admins.Find(cursor, limit)
  26. return admins, nextCursor, nil
  27. }
  28. // StoreAdmin stores an *linkedca.Admin to the authority.
  29. func (a *Authority) StoreAdmin(ctx context.Context, adm *linkedca.Admin, prov provisioner.Interface) error {
  30. a.adminMutex.Lock()
  31. defer a.adminMutex.Unlock()
  32. if adm.ProvisionerId != prov.GetID() {
  33. return admin.NewErrorISE("admin.provisionerId does not match provisioner argument")
  34. }
  35. if _, ok := a.admins.LoadBySubProv(adm.Subject, prov.GetName()); ok {
  36. return admin.NewError(admin.ErrorBadRequestType,
  37. "admin with subject %s and provisioner %s already exists", adm.Subject, prov.GetName())
  38. }
  39. // Store to database -- this will set the ID.
  40. if err := a.adminDB.CreateAdmin(ctx, adm); err != nil {
  41. return admin.WrapErrorISE(err, "error creating admin")
  42. }
  43. if err := a.admins.Store(adm, prov); err != nil {
  44. if err := a.reloadAdminResources(ctx); err != nil {
  45. return admin.WrapErrorISE(err, "error reloading admin resources on failed admin store")
  46. }
  47. return admin.WrapErrorISE(err, "error storing admin in authority cache")
  48. }
  49. return nil
  50. }
  51. // UpdateAdmin stores an *linkedca.Admin to the authority.
  52. func (a *Authority) UpdateAdmin(ctx context.Context, id string, nu *linkedca.Admin) (*linkedca.Admin, error) {
  53. a.adminMutex.Lock()
  54. defer a.adminMutex.Unlock()
  55. adm, err := a.admins.Update(id, nu)
  56. if err != nil {
  57. return nil, admin.WrapErrorISE(err, "error updating cached admin %s", id)
  58. }
  59. if err := a.adminDB.UpdateAdmin(ctx, adm); err != nil {
  60. if err := a.reloadAdminResources(ctx); err != nil {
  61. return nil, admin.WrapErrorISE(err, "error reloading admin resources on failed admin update")
  62. }
  63. return nil, admin.WrapErrorISE(err, "error updating admin %s", id)
  64. }
  65. return adm, nil
  66. }
  67. // RemoveAdmin removes an *linkedca.Admin from the authority.
  68. func (a *Authority) RemoveAdmin(ctx context.Context, id string) error {
  69. a.adminMutex.Lock()
  70. defer a.adminMutex.Unlock()
  71. return a.removeAdmin(ctx, id)
  72. }
  73. // removeAdmin helper that assumes lock.
  74. func (a *Authority) removeAdmin(ctx context.Context, id string) error {
  75. if err := a.admins.Remove(id); err != nil {
  76. return admin.WrapErrorISE(err, "error removing admin %s from authority cache", id)
  77. }
  78. if err := a.adminDB.DeleteAdmin(ctx, id); err != nil {
  79. if err := a.reloadAdminResources(ctx); err != nil {
  80. return admin.WrapErrorISE(err, "error reloading admin resources on failed admin remove")
  81. }
  82. return admin.WrapErrorISE(err, "error deleting admin %s", id)
  83. }
  84. return nil
  85. }