config.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. package authority
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. "os"
  7. "time"
  8. "github.com/pkg/errors"
  9. "github.com/smallstep/certificates/authority/provisioner"
  10. "github.com/smallstep/certificates/db"
  11. kms "github.com/smallstep/certificates/kms/apiv1"
  12. "github.com/smallstep/certificates/templates"
  13. "github.com/smallstep/cli/crypto/tlsutil"
  14. "github.com/smallstep/cli/crypto/x509util"
  15. )
  16. var (
  17. // DefaultTLSOptions represents the default TLS version as well as the cipher
  18. // suites used in the TLS certificates.
  19. DefaultTLSOptions = tlsutil.TLSOptions{
  20. CipherSuites: x509util.CipherSuites{
  21. "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
  22. "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  23. "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  24. },
  25. MinVersion: 1.2,
  26. MaxVersion: 1.2,
  27. Renegotiation: false,
  28. }
  29. defaultBackdate = time.Minute
  30. defaultDisableRenewal = false
  31. defaultEnableSSHCA = false
  32. globalProvisionerClaims = provisioner.Claims{
  33. MinTLSDur: &provisioner.Duration{Duration: 5 * time.Minute}, // TLS certs
  34. MaxTLSDur: &provisioner.Duration{Duration: 24 * time.Hour},
  35. DefaultTLSDur: &provisioner.Duration{Duration: 24 * time.Hour},
  36. DisableRenewal: &defaultDisableRenewal,
  37. MinUserSSHDur: &provisioner.Duration{Duration: 5 * time.Minute}, // User SSH certs
  38. MaxUserSSHDur: &provisioner.Duration{Duration: 24 * time.Hour},
  39. DefaultUserSSHDur: &provisioner.Duration{Duration: 16 * time.Hour},
  40. MinHostSSHDur: &provisioner.Duration{Duration: 5 * time.Minute}, // Host SSH certs
  41. MaxHostSSHDur: &provisioner.Duration{Duration: 30 * 24 * time.Hour},
  42. DefaultHostSSHDur: &provisioner.Duration{Duration: 30 * 24 * time.Hour},
  43. EnableSSHCA: &defaultEnableSSHCA,
  44. }
  45. )
  46. // Config represents the CA configuration and it's mapped to a JSON object.
  47. type Config struct {
  48. Root multiString `json:"root"`
  49. FederatedRoots []string `json:"federatedRoots"`
  50. IntermediateCert string `json:"crt"`
  51. IntermediateKey string `json:"key"`
  52. Address string `json:"address"`
  53. DNSNames []string `json:"dnsNames"`
  54. KMS *kms.Options `json:"kms,omitempty"`
  55. SSH *SSHConfig `json:"ssh,omitempty"`
  56. Logger json.RawMessage `json:"logger,omitempty"`
  57. DB *db.Config `json:"db,omitempty"`
  58. Monitoring json.RawMessage `json:"monitoring,omitempty"`
  59. AuthorityConfig *AuthConfig `json:"authority,omitempty"`
  60. TLS *tlsutil.TLSOptions `json:"tls,omitempty"`
  61. Password string `json:"password,omitempty"`
  62. Templates *templates.Templates `json:"templates,omitempty"`
  63. }
  64. // AuthConfig represents the configuration options for the authority.
  65. type AuthConfig struct {
  66. Provisioners provisioner.List `json:"provisioners"`
  67. Template *x509util.ASN1DN `json:"template,omitempty"`
  68. Claims *provisioner.Claims `json:"claims,omitempty"`
  69. DisableIssuedAtCheck bool `json:"disableIssuedAtCheck,omitempty"`
  70. Backdate *provisioner.Duration `json:"backdate,omitempty"`
  71. }
  72. // init initializes the required fields in the AuthConfig if they are not
  73. // provided.
  74. func (c *AuthConfig) init() {
  75. if c.Provisioners == nil {
  76. c.Provisioners = provisioner.List{}
  77. }
  78. if c.Template == nil {
  79. c.Template = &x509util.ASN1DN{}
  80. }
  81. if c.Backdate == nil {
  82. c.Backdate = &provisioner.Duration{
  83. Duration: defaultBackdate,
  84. }
  85. }
  86. }
  87. // Validate validates the authority configuration.
  88. func (c *AuthConfig) Validate(audiences provisioner.Audiences) error {
  89. if c == nil {
  90. return errors.New("authority cannot be undefined")
  91. }
  92. // Initialize required fields.
  93. c.init()
  94. // Check that only one K8sSA is enabled
  95. var k8sCount int
  96. for _, p := range c.Provisioners {
  97. if p.GetType() == provisioner.TypeK8sSA {
  98. k8sCount++
  99. }
  100. }
  101. if k8sCount > 1 {
  102. return errors.New("cannot have more than one kubernetes service account provisioner")
  103. }
  104. if c.Backdate.Duration < 0 {
  105. return errors.New("authority.backdate cannot be less than 0")
  106. }
  107. return nil
  108. }
  109. // LoadConfiguration parses the given filename in JSON format and returns the
  110. // configuration struct.
  111. func LoadConfiguration(filename string) (*Config, error) {
  112. f, err := os.Open(filename)
  113. if err != nil {
  114. return nil, errors.Wrapf(err, "error opening %s", filename)
  115. }
  116. defer f.Close()
  117. var c Config
  118. if err := json.NewDecoder(f).Decode(&c); err != nil {
  119. return nil, errors.Wrapf(err, "error parsing %s", filename)
  120. }
  121. return &c, nil
  122. }
  123. // initializes the minimal configuration required to create an authority. This
  124. // is mainly used on embedded authorities.
  125. func (c *Config) init() {
  126. if c.DNSNames == nil {
  127. c.DNSNames = []string{"localhost", "127.0.0.1", "::1"}
  128. }
  129. if c.TLS == nil {
  130. c.TLS = &DefaultTLSOptions
  131. }
  132. if c.AuthorityConfig == nil {
  133. c.AuthorityConfig = &AuthConfig{}
  134. }
  135. c.AuthorityConfig.init()
  136. }
  137. // Save saves the configuration to the given filename.
  138. func (c *Config) Save(filename string) error {
  139. f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  140. if err != nil {
  141. return errors.Wrapf(err, "error opening %s", filename)
  142. }
  143. defer f.Close()
  144. enc := json.NewEncoder(f)
  145. enc.SetIndent("", "\t")
  146. return errors.Wrapf(enc.Encode(c), "error writing %s", filename)
  147. }
  148. // Validate validates the configuration.
  149. func (c *Config) Validate() error {
  150. switch {
  151. case c.Address == "":
  152. return errors.New("address cannot be empty")
  153. case c.Root.HasEmpties():
  154. return errors.New("root cannot be empty")
  155. case c.IntermediateCert == "":
  156. return errors.New("crt cannot be empty")
  157. case c.IntermediateKey == "":
  158. return errors.New("key cannot be empty")
  159. case len(c.DNSNames) == 0:
  160. return errors.New("dnsNames cannot be empty")
  161. }
  162. // Validate address (a port is required)
  163. if _, _, err := net.SplitHostPort(c.Address); err != nil {
  164. return errors.Errorf("invalid address %s", c.Address)
  165. }
  166. if c.TLS == nil {
  167. c.TLS = &DefaultTLSOptions
  168. } else {
  169. if len(c.TLS.CipherSuites) == 0 {
  170. c.TLS.CipherSuites = DefaultTLSOptions.CipherSuites
  171. }
  172. if c.TLS.MaxVersion == 0 {
  173. c.TLS.MaxVersion = DefaultTLSOptions.MaxVersion
  174. }
  175. if c.TLS.MinVersion == 0 {
  176. c.TLS.MinVersion = c.TLS.MaxVersion
  177. }
  178. if c.TLS.MinVersion > c.TLS.MaxVersion {
  179. return errors.New("tls minVersion cannot exceed tls maxVersion")
  180. }
  181. c.TLS.Renegotiation = c.TLS.Renegotiation || DefaultTLSOptions.Renegotiation
  182. }
  183. // Validate KMS options, nil is ok.
  184. if err := c.KMS.Validate(); err != nil {
  185. return err
  186. }
  187. // Validate ssh: nil is ok
  188. if err := c.SSH.Validate(); err != nil {
  189. return err
  190. }
  191. // Validate templates: nil is ok
  192. if err := c.Templates.Validate(); err != nil {
  193. return err
  194. }
  195. return c.AuthorityConfig.Validate(c.getAudiences())
  196. }
  197. // getAudiences returns the legacy and possible urls without the ports that will
  198. // be used as the default provisioner audiences. The CA might have proxies in
  199. // front so we cannot rely on the port.
  200. func (c *Config) getAudiences() provisioner.Audiences {
  201. audiences := provisioner.Audiences{
  202. Sign: []string{legacyAuthority},
  203. Revoke: []string{legacyAuthority},
  204. SSHSign: []string{},
  205. SSHRevoke: []string{},
  206. SSHRenew: []string{},
  207. }
  208. for _, name := range c.DNSNames {
  209. audiences.Sign = append(audiences.Sign,
  210. fmt.Sprintf("https://%s/1.0/sign", name),
  211. fmt.Sprintf("https://%s/sign", name),
  212. fmt.Sprintf("https://%s/1.0/ssh/sign", name),
  213. fmt.Sprintf("https://%s/ssh/sign", name))
  214. audiences.Revoke = append(audiences.Revoke,
  215. fmt.Sprintf("https://%s/1.0/revoke", name),
  216. fmt.Sprintf("https://%s/revoke", name))
  217. audiences.SSHSign = append(audiences.SSHSign,
  218. fmt.Sprintf("https://%s/1.0/ssh/sign", name),
  219. fmt.Sprintf("https://%s/ssh/sign", name),
  220. fmt.Sprintf("https://%s/1.0/sign", name),
  221. fmt.Sprintf("https://%s/sign", name))
  222. audiences.SSHRevoke = append(audiences.SSHRevoke,
  223. fmt.Sprintf("https://%s/1.0/ssh/revoke", name),
  224. fmt.Sprintf("https://%s/ssh/revoke", name))
  225. audiences.SSHRenew = append(audiences.SSHRenew,
  226. fmt.Sprintf("https://%s/1.0/ssh/renew", name),
  227. fmt.Sprintf("https://%s/ssh/renew", name))
  228. audiences.SSHRekey = append(audiences.SSHRekey,
  229. fmt.Sprintf("https://%s/1.0/ssh/rekey", name),
  230. fmt.Sprintf("https://%s/ssh/rekey", name))
  231. }
  232. return audiences
  233. }