claims.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package provisioner
  2. import (
  3. "time"
  4. "github.com/pkg/errors"
  5. )
  6. // Claims so that individual provisioners can override global claims.
  7. type Claims struct {
  8. globalClaims *Claims
  9. MinTLSDur *Duration `json:"minTLSCertDuration,omitempty"`
  10. MaxTLSDur *Duration `json:"maxTLSCertDuration,omitempty"`
  11. DefaultTLSDur *Duration `json:"defaultTLSCertDuration,omitempty"`
  12. DisableRenewal *bool `json:"disableRenewal,omitempty"`
  13. }
  14. // Init initializes and validates the individual provisioner claims.
  15. func (pc *Claims) Init(global *Claims) (*Claims, error) {
  16. if pc == nil {
  17. pc = &Claims{}
  18. }
  19. pc.globalClaims = global
  20. return pc, pc.Validate()
  21. }
  22. // DefaultTLSCertDuration returns the default TLS cert duration for the
  23. // provisioner. If the default is not set within the provisioner, then the global
  24. // default from the authority configuration will be used.
  25. func (pc *Claims) DefaultTLSCertDuration() time.Duration {
  26. if pc.DefaultTLSDur == nil || pc.DefaultTLSDur.Duration == 0 {
  27. return pc.globalClaims.DefaultTLSCertDuration()
  28. }
  29. return pc.DefaultTLSDur.Duration
  30. }
  31. // MinTLSCertDuration returns the minimum TLS cert duration for the provisioner.
  32. // If the minimum is not set within the provisioner, then the global
  33. // minimum from the authority configuration will be used.
  34. func (pc *Claims) MinTLSCertDuration() time.Duration {
  35. if pc.MinTLSDur == nil || pc.MinTLSDur.Duration == 0 {
  36. return pc.globalClaims.MinTLSCertDuration()
  37. }
  38. return pc.MinTLSDur.Duration
  39. }
  40. // MaxTLSCertDuration returns the maximum TLS cert duration for the provisioner.
  41. // If the maximum is not set within the provisioner, then the global
  42. // maximum from the authority configuration will be used.
  43. func (pc *Claims) MaxTLSCertDuration() time.Duration {
  44. if pc.MaxTLSDur == nil || pc.MaxTLSDur.Duration == 0 {
  45. return pc.globalClaims.MaxTLSCertDuration()
  46. }
  47. return pc.MaxTLSDur.Duration
  48. }
  49. // IsDisableRenewal returns if the renewal flow is disabled for the
  50. // provisioner. If the property is not set within the provisioner, then the
  51. // global value from the authority configuration will be used.
  52. func (pc *Claims) IsDisableRenewal() bool {
  53. if pc.DisableRenewal == nil {
  54. return pc.globalClaims.IsDisableRenewal()
  55. }
  56. return *pc.DisableRenewal
  57. }
  58. // Validate validates and modifies the Claims with default values.
  59. func (pc *Claims) Validate() error {
  60. var (
  61. min = pc.MinTLSCertDuration()
  62. max = pc.MaxTLSCertDuration()
  63. def = pc.DefaultTLSCertDuration()
  64. )
  65. switch {
  66. case min == 0:
  67. return errors.Errorf("claims: MinTLSCertDuration cannot be empty")
  68. case max == 0:
  69. return errors.Errorf("claims: MaxTLSCertDuration cannot be empty")
  70. case def == 0:
  71. return errors.Errorf("claims: DefaultTLSCertDuration cannot be empty")
  72. case max < min:
  73. return errors.Errorf("claims: MaxCertDuration cannot be less "+
  74. "than MinCertDuration: MaxCertDuration - %v, MinCertDuration - %v", max, min)
  75. case def < min:
  76. return errors.Errorf("claims: DefaultCertDuration cannot be less than MinCertDuration: DefaultCertDuration - %v, MinCertDuration - %v", def, min)
  77. case max < def:
  78. return errors.Errorf("claims: MaxCertDuration cannot be less than DefaultCertDuration: MaxCertDuration - %v, DefaultCertDuration - %v", max, def)
  79. default:
  80. return nil
  81. }
  82. }