claims.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package provisioner
  2. import (
  3. "time"
  4. "github.com/pkg/errors"
  5. "golang.org/x/crypto/ssh"
  6. )
  7. // Claims so that individual provisioners can override global claims.
  8. type Claims struct {
  9. // TLS CA properties
  10. MinTLSDur *Duration `json:"minTLSCertDuration,omitempty"`
  11. MaxTLSDur *Duration `json:"maxTLSCertDuration,omitempty"`
  12. DefaultTLSDur *Duration `json:"defaultTLSCertDuration,omitempty"`
  13. // SSH CA properties
  14. MinUserSSHDur *Duration `json:"minUserSSHCertDuration,omitempty"`
  15. MaxUserSSHDur *Duration `json:"maxUserSSHCertDuration,omitempty"`
  16. DefaultUserSSHDur *Duration `json:"defaultUserSSHCertDuration,omitempty"`
  17. MinHostSSHDur *Duration `json:"minHostSSHCertDuration,omitempty"`
  18. MaxHostSSHDur *Duration `json:"maxHostSSHCertDuration,omitempty"`
  19. DefaultHostSSHDur *Duration `json:"defaultHostSSHCertDuration,omitempty"`
  20. EnableSSHCA *bool `json:"enableSSHCA,omitempty"`
  21. // Renewal properties
  22. DisableRenewal *bool `json:"disableRenewal,omitempty"`
  23. AllowRenewalAfterExpiry *bool `json:"allowRenewalAfterExpiry,omitempty"`
  24. }
  25. // Claimer is the type that controls claims. It provides an interface around the
  26. // current claim and the global one.
  27. type Claimer struct {
  28. global Claims
  29. claims *Claims
  30. }
  31. // NewClaimer initializes a new claimer with the given claims.
  32. func NewClaimer(claims *Claims, global Claims) (*Claimer, error) {
  33. c := &Claimer{global: global, claims: claims}
  34. err := c.Validate()
  35. return c, err
  36. }
  37. // Claims returns the merge of the inner and global claims.
  38. func (c *Claimer) Claims() Claims {
  39. disableRenewal := c.IsDisableRenewal()
  40. allowRenewalAfterExpiry := c.AllowRenewalAfterExpiry()
  41. enableSSHCA := c.IsSSHCAEnabled()
  42. return Claims{
  43. MinTLSDur: &Duration{c.MinTLSCertDuration()},
  44. MaxTLSDur: &Duration{c.MaxTLSCertDuration()},
  45. DefaultTLSDur: &Duration{c.DefaultTLSCertDuration()},
  46. MinUserSSHDur: &Duration{c.MinUserSSHCertDuration()},
  47. MaxUserSSHDur: &Duration{c.MaxUserSSHCertDuration()},
  48. DefaultUserSSHDur: &Duration{c.DefaultUserSSHCertDuration()},
  49. MinHostSSHDur: &Duration{c.MinHostSSHCertDuration()},
  50. MaxHostSSHDur: &Duration{c.MaxHostSSHCertDuration()},
  51. DefaultHostSSHDur: &Duration{c.DefaultHostSSHCertDuration()},
  52. EnableSSHCA: &enableSSHCA,
  53. DisableRenewal: &disableRenewal,
  54. AllowRenewalAfterExpiry: &allowRenewalAfterExpiry,
  55. }
  56. }
  57. // DefaultTLSCertDuration returns the default TLS cert duration for the
  58. // provisioner. If the default is not set within the provisioner, then the global
  59. // default from the authority configuration will be used.
  60. func (c *Claimer) DefaultTLSCertDuration() time.Duration {
  61. if c.claims == nil || c.claims.DefaultTLSDur == nil {
  62. return c.global.DefaultTLSDur.Duration
  63. }
  64. return c.claims.DefaultTLSDur.Duration
  65. }
  66. // MinTLSCertDuration returns the minimum TLS cert duration for the provisioner.
  67. // If the minimum is not set within the provisioner, then the global
  68. // minimum from the authority configuration will be used.
  69. func (c *Claimer) MinTLSCertDuration() time.Duration {
  70. if c.claims == nil || c.claims.MinTLSDur == nil {
  71. if c.claims != nil && c.claims.DefaultTLSDur != nil && c.claims.DefaultTLSDur.Duration < c.global.MinTLSDur.Duration {
  72. return c.claims.DefaultTLSDur.Duration
  73. }
  74. return c.global.MinTLSDur.Duration
  75. }
  76. return c.claims.MinTLSDur.Duration
  77. }
  78. // MaxTLSCertDuration returns the maximum TLS cert duration for the provisioner.
  79. // If the maximum is not set within the provisioner, then the global
  80. // maximum from the authority configuration will be used.
  81. func (c *Claimer) MaxTLSCertDuration() time.Duration {
  82. if c.claims == nil || c.claims.MaxTLSDur == nil {
  83. if c.claims != nil && c.claims.DefaultTLSDur != nil && c.claims.DefaultTLSDur.Duration > c.global.MaxTLSDur.Duration {
  84. return c.claims.DefaultTLSDur.Duration
  85. }
  86. return c.global.MaxTLSDur.Duration
  87. }
  88. return c.claims.MaxTLSDur.Duration
  89. }
  90. // IsDisableRenewal returns if the renewal flow is disabled for the
  91. // provisioner. If the property is not set within the provisioner, then the
  92. // global value from the authority configuration will be used.
  93. func (c *Claimer) IsDisableRenewal() bool {
  94. if c.claims == nil || c.claims.DisableRenewal == nil {
  95. return *c.global.DisableRenewal
  96. }
  97. return *c.claims.DisableRenewal
  98. }
  99. // AllowRenewalAfterExpiry returns if the renewal flow is authorized if the
  100. // certificate is expired. If the property is not set within the provisioner
  101. // then the global value from the authority configuration will be used.
  102. func (c *Claimer) AllowRenewalAfterExpiry() bool {
  103. if c.claims == nil || c.claims.AllowRenewalAfterExpiry == nil {
  104. return *c.global.AllowRenewalAfterExpiry
  105. }
  106. return *c.claims.AllowRenewalAfterExpiry
  107. }
  108. // DefaultSSHCertDuration returns the default SSH certificate duration for the
  109. // given certificate type.
  110. func (c *Claimer) DefaultSSHCertDuration(certType uint32) (time.Duration, error) {
  111. switch certType {
  112. case ssh.UserCert:
  113. return c.DefaultUserSSHCertDuration(), nil
  114. case ssh.HostCert:
  115. return c.DefaultHostSSHCertDuration(), nil
  116. case 0:
  117. return 0, errors.New("ssh certificate type has not been set")
  118. default:
  119. return 0, errors.Errorf("ssh certificate has an unknown type: %d", certType)
  120. }
  121. }
  122. // DefaultUserSSHCertDuration returns the default SSH user cert duration for the
  123. // provisioner. If the default is not set within the provisioner, then the
  124. // global default from the authority configuration will be used.
  125. func (c *Claimer) DefaultUserSSHCertDuration() time.Duration {
  126. if c.claims == nil || c.claims.DefaultUserSSHDur == nil {
  127. return c.global.DefaultUserSSHDur.Duration
  128. }
  129. return c.claims.DefaultUserSSHDur.Duration
  130. }
  131. // MinUserSSHCertDuration returns the minimum SSH user cert duration for the
  132. // provisioner. If the minimum is not set within the provisioner, then the
  133. // global minimum from the authority configuration will be used.
  134. func (c *Claimer) MinUserSSHCertDuration() time.Duration {
  135. if c.claims == nil || c.claims.MinUserSSHDur == nil {
  136. if c.claims != nil && c.claims.DefaultUserSSHDur != nil && c.claims.DefaultUserSSHDur.Duration < c.global.MinUserSSHDur.Duration {
  137. return c.claims.DefaultUserSSHDur.Duration
  138. }
  139. return c.global.MinUserSSHDur.Duration
  140. }
  141. return c.claims.MinUserSSHDur.Duration
  142. }
  143. // MaxUserSSHCertDuration returns the maximum SSH user cert duration for the
  144. // provisioner. If the maximum is not set within the provisioner, then the
  145. // global maximum from the authority configuration will be used.
  146. func (c *Claimer) MaxUserSSHCertDuration() time.Duration {
  147. if c.claims == nil || c.claims.MaxUserSSHDur == nil {
  148. if c.claims != nil && c.claims.DefaultUserSSHDur != nil && c.claims.DefaultUserSSHDur.Duration > c.global.MaxUserSSHDur.Duration {
  149. return c.claims.DefaultUserSSHDur.Duration
  150. }
  151. return c.global.MaxUserSSHDur.Duration
  152. }
  153. return c.claims.MaxUserSSHDur.Duration
  154. }
  155. // DefaultHostSSHCertDuration returns the default SSH host cert duration for the
  156. // provisioner. If the default is not set within the provisioner, then the
  157. // global default from the authority configuration will be used.
  158. func (c *Claimer) DefaultHostSSHCertDuration() time.Duration {
  159. if c.claims == nil || c.claims.DefaultHostSSHDur == nil {
  160. return c.global.DefaultHostSSHDur.Duration
  161. }
  162. return c.claims.DefaultHostSSHDur.Duration
  163. }
  164. // MinHostSSHCertDuration returns the minimum SSH host cert duration for the
  165. // provisioner. If the minimum is not set within the provisioner, then the
  166. // global minimum from the authority configuration will be used.
  167. func (c *Claimer) MinHostSSHCertDuration() time.Duration {
  168. if c.claims == nil || c.claims.MinHostSSHDur == nil {
  169. if c.claims != nil && c.claims.DefaultHostSSHDur != nil && c.claims.DefaultHostSSHDur.Duration < c.global.MinHostSSHDur.Duration {
  170. return c.claims.DefaultHostSSHDur.Duration
  171. }
  172. return c.global.MinHostSSHDur.Duration
  173. }
  174. return c.claims.MinHostSSHDur.Duration
  175. }
  176. // MaxHostSSHCertDuration returns the maximum SSH Host cert duration for the
  177. // provisioner. If the maximum is not set within the provisioner, then the
  178. // global maximum from the authority configuration will be used.
  179. func (c *Claimer) MaxHostSSHCertDuration() time.Duration {
  180. if c.claims == nil || c.claims.MaxHostSSHDur == nil {
  181. if c.claims != nil && c.claims.DefaultHostSSHDur != nil && c.claims.DefaultHostSSHDur.Duration > c.global.MaxHostSSHDur.Duration {
  182. return c.claims.DefaultHostSSHDur.Duration
  183. }
  184. return c.global.MaxHostSSHDur.Duration
  185. }
  186. return c.claims.MaxHostSSHDur.Duration
  187. }
  188. // IsSSHCAEnabled returns if the SSH CA is enabled for the provisioner. If the
  189. // property is not set within the provisioner, then the global value from the
  190. // authority configuration will be used.
  191. func (c *Claimer) IsSSHCAEnabled() bool {
  192. if c.claims == nil || c.claims.EnableSSHCA == nil {
  193. return *c.global.EnableSSHCA
  194. }
  195. return *c.claims.EnableSSHCA
  196. }
  197. // Validate validates and modifies the Claims with default values.
  198. func (c *Claimer) Validate() error {
  199. var (
  200. min = c.MinTLSCertDuration()
  201. max = c.MaxTLSCertDuration()
  202. def = c.DefaultTLSCertDuration()
  203. )
  204. switch {
  205. case min <= 0:
  206. return errors.Errorf("claims: MinTLSCertDuration must be greater than 0")
  207. case max <= 0:
  208. return errors.Errorf("claims: MaxTLSCertDuration must be greater than 0")
  209. case def <= 0:
  210. return errors.Errorf("claims: DefaultTLSCertDuration must be greater than 0")
  211. case max < min:
  212. return errors.Errorf("claims: MaxCertDuration cannot be less "+
  213. "than MinCertDuration: MaxCertDuration - %v, MinCertDuration - %v", max, min)
  214. case def < min:
  215. return errors.Errorf("claims: DefaultCertDuration cannot be less than MinCertDuration: DefaultCertDuration - %v, MinCertDuration - %v", def, min)
  216. case max < def:
  217. return errors.Errorf("claims: MaxCertDuration cannot be less than DefaultCertDuration: MaxCertDuration - %v, DefaultCertDuration - %v", max, def)
  218. default:
  219. return nil
  220. }
  221. }