policy.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package authority
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "go.step.sm/linkedca"
  7. "github.com/smallstep/certificates/authority/admin"
  8. authPolicy "github.com/smallstep/certificates/authority/policy"
  9. policy "github.com/smallstep/certificates/policy"
  10. )
  11. type policyErrorType int
  12. const (
  13. AdminLockOut policyErrorType = iota + 1
  14. StoreFailure
  15. ReloadFailure
  16. ConfigurationFailure
  17. EvaluationFailure
  18. InternalFailure
  19. )
  20. type PolicyError struct {
  21. Typ policyErrorType
  22. Err error
  23. }
  24. func (p *PolicyError) Error() string {
  25. return p.Err.Error()
  26. }
  27. func (a *Authority) GetAuthorityPolicy(ctx context.Context) (*linkedca.Policy, error) {
  28. a.adminMutex.Lock()
  29. defer a.adminMutex.Unlock()
  30. p, err := a.adminDB.GetAuthorityPolicy(ctx)
  31. if err != nil {
  32. return nil, &PolicyError{
  33. Typ: InternalFailure,
  34. Err: err,
  35. }
  36. }
  37. return p, nil
  38. }
  39. func (a *Authority) CreateAuthorityPolicy(ctx context.Context, adm *linkedca.Admin, p *linkedca.Policy) (*linkedca.Policy, error) {
  40. a.adminMutex.Lock()
  41. defer a.adminMutex.Unlock()
  42. if err := a.checkAuthorityPolicy(ctx, adm, p); err != nil {
  43. return nil, err
  44. }
  45. if err := a.adminDB.CreateAuthorityPolicy(ctx, p); err != nil {
  46. return nil, &PolicyError{
  47. Typ: StoreFailure,
  48. Err: err,
  49. }
  50. }
  51. if err := a.reloadPolicyEngines(ctx); err != nil {
  52. return nil, &PolicyError{
  53. Typ: ReloadFailure,
  54. Err: fmt.Errorf("error reloading policy engines when creating authority policy: %w", err),
  55. }
  56. }
  57. return p, nil
  58. }
  59. func (a *Authority) UpdateAuthorityPolicy(ctx context.Context, adm *linkedca.Admin, p *linkedca.Policy) (*linkedca.Policy, error) {
  60. a.adminMutex.Lock()
  61. defer a.adminMutex.Unlock()
  62. if err := a.checkAuthorityPolicy(ctx, adm, p); err != nil {
  63. return nil, err
  64. }
  65. if err := a.adminDB.UpdateAuthorityPolicy(ctx, p); err != nil {
  66. return nil, &PolicyError{
  67. Typ: StoreFailure,
  68. Err: err,
  69. }
  70. }
  71. if err := a.reloadPolicyEngines(ctx); err != nil {
  72. return nil, &PolicyError{
  73. Typ: ReloadFailure,
  74. Err: fmt.Errorf("error reloading policy engines when updating authority policy: %w", err),
  75. }
  76. }
  77. return p, nil
  78. }
  79. func (a *Authority) RemoveAuthorityPolicy(ctx context.Context) error {
  80. a.adminMutex.Lock()
  81. defer a.adminMutex.Unlock()
  82. if err := a.adminDB.DeleteAuthorityPolicy(ctx); err != nil {
  83. return &PolicyError{
  84. Typ: StoreFailure,
  85. Err: err,
  86. }
  87. }
  88. if err := a.reloadPolicyEngines(ctx); err != nil {
  89. return &PolicyError{
  90. Typ: ReloadFailure,
  91. Err: fmt.Errorf("error reloading policy engines when deleting authority policy: %w", err),
  92. }
  93. }
  94. return nil
  95. }
  96. func (a *Authority) checkAuthorityPolicy(ctx context.Context, currentAdmin *linkedca.Admin, p *linkedca.Policy) error {
  97. // no policy and thus nothing to evaluate; return early
  98. if p == nil {
  99. return nil
  100. }
  101. // get all current admins from the database
  102. allAdmins, err := a.adminDB.GetAdmins(ctx)
  103. if err != nil {
  104. return &PolicyError{
  105. Typ: InternalFailure,
  106. Err: fmt.Errorf("error retrieving admins: %w", err),
  107. }
  108. }
  109. return a.checkPolicy(ctx, currentAdmin, allAdmins, p)
  110. }
  111. func (a *Authority) checkProvisionerPolicy(ctx context.Context, provName string, p *linkedca.Policy) error {
  112. // no policy and thus nothing to evaluate; return early
  113. if p == nil {
  114. return nil
  115. }
  116. // get all admins for the provisioner; ignoring case in which they're not found
  117. allProvisionerAdmins, _ := a.admins.LoadByProvisioner(provName)
  118. // check the policy; pass in nil as the current admin, as all admins for the
  119. // provisioner will be checked by looping through allProvisionerAdmins. Also,
  120. // the current admin may be a super admin not belonging to the provisioner, so
  121. // can't be blocked, but is not required to be in the policy, either.
  122. return a.checkPolicy(ctx, nil, allProvisionerAdmins, p)
  123. }
  124. // checkPolicy checks if a new or updated policy configuration results in the user
  125. // locking themselves or other admins out of the CA.
  126. func (a *Authority) checkPolicy(ctx context.Context, currentAdmin *linkedca.Admin, otherAdmins []*linkedca.Admin, p *linkedca.Policy) error {
  127. // convert the policy; return early if nil
  128. policyOptions := authPolicy.LinkedToCertificates(p)
  129. if policyOptions == nil {
  130. return nil
  131. }
  132. engine, err := authPolicy.NewX509PolicyEngine(policyOptions.GetX509Options())
  133. if err != nil {
  134. return &PolicyError{
  135. Typ: ConfigurationFailure,
  136. Err: err,
  137. }
  138. }
  139. // when an empty X.509 policy is provided, the resulting engine is nil
  140. // and there's no policy to evaluate.
  141. if engine == nil {
  142. return nil
  143. }
  144. // TODO(hs): Provide option to force the policy, even when the admin subject would be locked out?
  145. // check if the admin user that instructed the authority policy to be
  146. // created or updated, would still be allowed when the provided policy
  147. // would be applied. This case is skipped when current admin is nil, which
  148. // is the case when a provisioner policy is checked.
  149. if currentAdmin != nil {
  150. sans := []string{currentAdmin.GetSubject()}
  151. if err := isAllowed(engine, sans); err != nil {
  152. return err
  153. }
  154. }
  155. // loop through admins to verify that none of them would be
  156. // locked out when the new policy were to be applied. Returns
  157. // an error with a message that includes the admin subject that
  158. // would be locked out.
  159. for _, adm := range otherAdmins {
  160. sans := []string{adm.GetSubject()}
  161. if err := isAllowed(engine, sans); err != nil {
  162. return err
  163. }
  164. }
  165. // TODO(hs): mask the error message for non-super admins?
  166. return nil
  167. }
  168. // reloadPolicyEngines reloads x509 and SSH policy engines using
  169. // configuration stored in the DB or from the configuration file.
  170. func (a *Authority) reloadPolicyEngines(ctx context.Context) error {
  171. var (
  172. err error
  173. policyOptions *authPolicy.Options
  174. )
  175. if a.config.AuthorityConfig.EnableAdmin {
  176. // temporarily disable policy loading when LinkedCA is in use
  177. if _, ok := a.adminDB.(*linkedCaClient); ok {
  178. return nil
  179. }
  180. linkedPolicy, err := a.adminDB.GetAuthorityPolicy(ctx)
  181. if err != nil {
  182. var ae *admin.Error
  183. if isAdminError := errors.As(err, &ae); (isAdminError && ae.Type != admin.ErrorNotFoundType.String()) || !isAdminError {
  184. return fmt.Errorf("error getting policy to (re)load policy engines: %w", err)
  185. }
  186. }
  187. policyOptions = authPolicy.LinkedToCertificates(linkedPolicy)
  188. } else {
  189. policyOptions = a.config.AuthorityConfig.Policy
  190. }
  191. engine, err := authPolicy.New(policyOptions)
  192. if err != nil {
  193. return err
  194. }
  195. // only update the policy engine when no error was returned
  196. a.policyEngine = engine
  197. return nil
  198. }
  199. func isAllowed(engine authPolicy.X509Policy, sans []string) error {
  200. if err := engine.AreSANsAllowed(sans); err != nil {
  201. var policyErr *policy.NamePolicyError
  202. isNamePolicyError := errors.As(err, &policyErr)
  203. if isNamePolicyError && policyErr.Reason == policy.NotAllowed {
  204. return &PolicyError{
  205. Typ: AdminLockOut,
  206. Err: fmt.Errorf("the provided policy would lock out %s from the CA. Please update your policy to include %s as an allowed name", sans, sans),
  207. }
  208. }
  209. return &PolicyError{
  210. Typ: EvaluationFailure,
  211. Err: err,
  212. }
  213. }
  214. return nil
  215. }