claims.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. AllowRenewAfterExpiry *bool `json:"allowRenewAfterExpiry,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. return c, c.Validate()
  35. }
  36. // Claims returns the merge of the inner and global claims.
  37. func (c *Claimer) Claims() Claims {
  38. disableRenewal := c.IsDisableRenewal()
  39. allowRenewAfterExpiry := c.AllowRenewAfterExpiry()
  40. enableSSHCA := c.IsSSHCAEnabled()
  41. return Claims{
  42. MinTLSDur: &Duration{c.MinTLSCertDuration()},
  43. MaxTLSDur: &Duration{c.MaxTLSCertDuration()},
  44. DefaultTLSDur: &Duration{c.DefaultTLSCertDuration()},
  45. MinUserSSHDur: &Duration{c.MinUserSSHCertDuration()},
  46. MaxUserSSHDur: &Duration{c.MaxUserSSHCertDuration()},
  47. DefaultUserSSHDur: &Duration{c.DefaultUserSSHCertDuration()},
  48. MinHostSSHDur: &Duration{c.MinHostSSHCertDuration()},
  49. MaxHostSSHDur: &Duration{c.MaxHostSSHCertDuration()},
  50. DefaultHostSSHDur: &Duration{c.DefaultHostSSHCertDuration()},
  51. EnableSSHCA: &enableSSHCA,
  52. DisableRenewal: &disableRenewal,
  53. AllowRenewAfterExpiry: &allowRenewAfterExpiry,
  54. }
  55. }
  56. // DefaultTLSCertDuration returns the default TLS cert duration for the
  57. // provisioner. If the default is not set within the provisioner, then the global
  58. // default from the authority configuration will be used.
  59. func (c *Claimer) DefaultTLSCertDuration() time.Duration {
  60. if c.claims == nil || c.claims.DefaultTLSDur == nil {
  61. return c.global.DefaultTLSDur.Duration
  62. }
  63. return c.claims.DefaultTLSDur.Duration
  64. }
  65. // MinTLSCertDuration returns the minimum TLS cert duration for the provisioner.
  66. // If the minimum is not set within the provisioner, then the global
  67. // minimum from the authority configuration will be used.
  68. func (c *Claimer) MinTLSCertDuration() time.Duration {
  69. if c.claims == nil || c.claims.MinTLSDur == nil {
  70. if c.claims != nil && c.claims.DefaultTLSDur != nil && c.claims.DefaultTLSDur.Duration < c.global.MinTLSDur.Duration {
  71. return c.claims.DefaultTLSDur.Duration
  72. }
  73. return c.global.MinTLSDur.Duration
  74. }
  75. return c.claims.MinTLSDur.Duration
  76. }
  77. // MaxTLSCertDuration returns the maximum TLS cert duration for the provisioner.
  78. // If the maximum is not set within the provisioner, then the global
  79. // maximum from the authority configuration will be used.
  80. func (c *Claimer) MaxTLSCertDuration() time.Duration {
  81. if c.claims == nil || c.claims.MaxTLSDur == nil {
  82. if c.claims != nil && c.claims.DefaultTLSDur != nil && c.claims.DefaultTLSDur.Duration > c.global.MaxTLSDur.Duration {
  83. return c.claims.DefaultTLSDur.Duration
  84. }
  85. return c.global.MaxTLSDur.Duration
  86. }
  87. return c.claims.MaxTLSDur.Duration
  88. }
  89. // IsDisableRenewal returns if the renewal flow is disabled for the
  90. // provisioner. If the property is not set within the provisioner, then the
  91. // global value from the authority configuration will be used.
  92. func (c *Claimer) IsDisableRenewal() bool {
  93. if c.claims == nil || c.claims.DisableRenewal == nil {
  94. return *c.global.DisableRenewal
  95. }
  96. return *c.claims.DisableRenewal
  97. }
  98. // AllowRenewAfterExpiry returns if the renewal flow is authorized if the
  99. // certificate is expired. If the property is not set within the provisioner
  100. // then the global value from the authority configuration will be used.
  101. func (c *Claimer) AllowRenewAfterExpiry() bool {
  102. if c.claims == nil || c.claims.AllowRenewAfterExpiry == nil {
  103. return *c.global.AllowRenewAfterExpiry
  104. }
  105. return *c.claims.AllowRenewAfterExpiry
  106. }
  107. // DefaultSSHCertDuration returns the default SSH certificate duration for the
  108. // given certificate type.
  109. func (c *Claimer) DefaultSSHCertDuration(certType uint32) (time.Duration, error) {
  110. switch certType {
  111. case ssh.UserCert:
  112. return c.DefaultUserSSHCertDuration(), nil
  113. case ssh.HostCert:
  114. return c.DefaultHostSSHCertDuration(), nil
  115. case 0:
  116. return 0, errors.New("ssh certificate type has not been set")
  117. default:
  118. return 0, errors.Errorf("ssh certificate has an unknown type: %d", certType)
  119. }
  120. }
  121. // DefaultUserSSHCertDuration returns the default SSH user cert duration for the
  122. // provisioner. If the default is not set within the provisioner, then the
  123. // global default from the authority configuration will be used.
  124. func (c *Claimer) DefaultUserSSHCertDuration() time.Duration {
  125. if c.claims == nil || c.claims.DefaultUserSSHDur == nil {
  126. return c.global.DefaultUserSSHDur.Duration
  127. }
  128. return c.claims.DefaultUserSSHDur.Duration
  129. }
  130. // MinUserSSHCertDuration returns the minimum SSH user cert duration for the
  131. // provisioner. If the minimum is not set within the provisioner, then the
  132. // global minimum from the authority configuration will be used.
  133. func (c *Claimer) MinUserSSHCertDuration() time.Duration {
  134. if c.claims == nil || c.claims.MinUserSSHDur == nil {
  135. if c.claims != nil && c.claims.DefaultUserSSHDur != nil && c.claims.DefaultUserSSHDur.Duration < c.global.MinUserSSHDur.Duration {
  136. return c.claims.DefaultUserSSHDur.Duration
  137. }
  138. return c.global.MinUserSSHDur.Duration
  139. }
  140. return c.claims.MinUserSSHDur.Duration
  141. }
  142. // MaxUserSSHCertDuration returns the maximum SSH user cert duration for the
  143. // provisioner. If the maximum is not set within the provisioner, then the
  144. // global maximum from the authority configuration will be used.
  145. func (c *Claimer) MaxUserSSHCertDuration() time.Duration {
  146. if c.claims == nil || c.claims.MaxUserSSHDur == nil {
  147. if c.claims != nil && c.claims.DefaultUserSSHDur != nil && c.claims.DefaultUserSSHDur.Duration > c.global.MaxUserSSHDur.Duration {
  148. return c.claims.DefaultUserSSHDur.Duration
  149. }
  150. return c.global.MaxUserSSHDur.Duration
  151. }
  152. return c.claims.MaxUserSSHDur.Duration
  153. }
  154. // DefaultHostSSHCertDuration returns the default SSH host cert duration for the
  155. // provisioner. If the default is not set within the provisioner, then the
  156. // global default from the authority configuration will be used.
  157. func (c *Claimer) DefaultHostSSHCertDuration() time.Duration {
  158. if c.claims == nil || c.claims.DefaultHostSSHDur == nil {
  159. return c.global.DefaultHostSSHDur.Duration
  160. }
  161. return c.claims.DefaultHostSSHDur.Duration
  162. }
  163. // MinHostSSHCertDuration returns the minimum SSH host cert duration for the
  164. // provisioner. If the minimum is not set within the provisioner, then the
  165. // global minimum from the authority configuration will be used.
  166. func (c *Claimer) MinHostSSHCertDuration() time.Duration {
  167. if c.claims == nil || c.claims.MinHostSSHDur == nil {
  168. if c.claims != nil && c.claims.DefaultHostSSHDur != nil && c.claims.DefaultHostSSHDur.Duration < c.global.MinHostSSHDur.Duration {
  169. return c.claims.DefaultHostSSHDur.Duration
  170. }
  171. return c.global.MinHostSSHDur.Duration
  172. }
  173. return c.claims.MinHostSSHDur.Duration
  174. }
  175. // MaxHostSSHCertDuration returns the maximum SSH Host cert duration for the
  176. // provisioner. If the maximum is not set within the provisioner, then the
  177. // global maximum from the authority configuration will be used.
  178. func (c *Claimer) MaxHostSSHCertDuration() time.Duration {
  179. if c.claims == nil || c.claims.MaxHostSSHDur == nil {
  180. if c.claims != nil && c.claims.DefaultHostSSHDur != nil && c.claims.DefaultHostSSHDur.Duration > c.global.MaxHostSSHDur.Duration {
  181. return c.claims.DefaultHostSSHDur.Duration
  182. }
  183. return c.global.MaxHostSSHDur.Duration
  184. }
  185. return c.claims.MaxHostSSHDur.Duration
  186. }
  187. // IsSSHCAEnabled returns if the SSH CA is enabled for the provisioner. If the
  188. // property is not set within the provisioner, then the global value from the
  189. // authority configuration will be used.
  190. func (c *Claimer) IsSSHCAEnabled() bool {
  191. if c.claims == nil || c.claims.EnableSSHCA == nil {
  192. return *c.global.EnableSSHCA
  193. }
  194. return *c.claims.EnableSSHCA
  195. }
  196. // Validate validates and modifies the Claims with default values.
  197. func (c *Claimer) Validate() error {
  198. var (
  199. min = c.MinTLSCertDuration()
  200. max = c.MaxTLSCertDuration()
  201. def = c.DefaultTLSCertDuration()
  202. )
  203. switch {
  204. case min <= 0:
  205. return errors.Errorf("claims: MinTLSCertDuration must be greater than 0")
  206. case max <= 0:
  207. return errors.Errorf("claims: MaxTLSCertDuration must be greater than 0")
  208. case def <= 0:
  209. return errors.Errorf("claims: DefaultTLSCertDuration must be greater than 0")
  210. case max < min:
  211. return errors.Errorf("claims: MaxCertDuration cannot be less "+
  212. "than MinCertDuration: MaxCertDuration - %v, MinCertDuration - %v", max, min)
  213. case def < min:
  214. return errors.Errorf("claims: DefaultCertDuration cannot be less than MinCertDuration: DefaultCertDuration - %v, MinCertDuration - %v", def, min)
  215. case max < def:
  216. return errors.Errorf("claims: MaxCertDuration cannot be less than DefaultCertDuration: MaxCertDuration - %v, DefaultCertDuration - %v", max, def)
  217. default:
  218. return nil
  219. }
  220. }