config.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/cli/crypto/tlsutil"
  11. "github.com/smallstep/cli/crypto/x509util"
  12. )
  13. var (
  14. // DefaultTLSOptions represents the default TLS version as well as the cipher
  15. // suites used in the TLS certificates.
  16. DefaultTLSOptions = tlsutil.TLSOptions{
  17. CipherSuites: x509util.CipherSuites{
  18. "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305",
  19. "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256",
  20. "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384",
  21. },
  22. MinVersion: 1.2,
  23. MaxVersion: 1.2,
  24. Renegotiation: false,
  25. }
  26. defaultDisableRenewal = false
  27. globalProvisionerClaims = provisioner.Claims{
  28. MinTLSDur: &provisioner.Duration{5 * time.Minute},
  29. MaxTLSDur: &provisioner.Duration{24 * time.Hour},
  30. DefaultTLSDur: &provisioner.Duration{24 * time.Hour},
  31. DisableRenewal: &defaultDisableRenewal,
  32. }
  33. )
  34. // Config represents the CA configuration and it's mapped to a JSON object.
  35. type Config struct {
  36. Root multiString `json:"root"`
  37. FederatedRoots []string `json:"federatedRoots"`
  38. IntermediateCert string `json:"crt"`
  39. IntermediateKey string `json:"key"`
  40. Address string `json:"address"`
  41. DNSNames []string `json:"dnsNames"`
  42. Logger json.RawMessage `json:"logger,omitempty"`
  43. Monitoring json.RawMessage `json:"monitoring,omitempty"`
  44. AuthorityConfig *AuthConfig `json:"authority,omitempty"`
  45. TLS *tlsutil.TLSOptions `json:"tls,omitempty"`
  46. Password string `json:"password,omitempty"`
  47. }
  48. // AuthConfig represents the configuration options for the authority.
  49. type AuthConfig struct {
  50. Provisioners provisioner.List `json:"provisioners"`
  51. Template *x509util.ASN1DN `json:"template,omitempty"`
  52. Claims *provisioner.Claims `json:"claims,omitempty"`
  53. DisableIssuedAtCheck bool `json:"disableIssuedAtCheck,omitempty"`
  54. }
  55. // Validate validates the authority configuration.
  56. func (c *AuthConfig) Validate(audiences []string) error {
  57. if c == nil {
  58. return errors.New("authority cannot be undefined")
  59. }
  60. if len(c.Provisioners) == 0 {
  61. return errors.New("authority.provisioners cannot be empty")
  62. }
  63. // Merge global and configuration claims
  64. claimer, err := provisioner.NewClaimer(c.Claims, globalProvisionerClaims)
  65. if err != nil {
  66. return err
  67. }
  68. // Initialize provisioners
  69. config := provisioner.Config{
  70. Claims: claimer.Claims(),
  71. Audiences: audiences,
  72. }
  73. for _, p := range c.Provisioners {
  74. if err := p.Init(config); err != nil {
  75. return err
  76. }
  77. }
  78. if c.Template == nil {
  79. c.Template = &x509util.ASN1DN{}
  80. }
  81. return nil
  82. }
  83. // LoadConfiguration parses the given filename in JSON format and returns the
  84. // configuration struct.
  85. func LoadConfiguration(filename string) (*Config, error) {
  86. f, err := os.Open(filename)
  87. if err != nil {
  88. return nil, errors.Wrapf(err, "error opening %s", filename)
  89. }
  90. defer f.Close()
  91. var c Config
  92. if err := json.NewDecoder(f).Decode(&c); err != nil {
  93. return nil, errors.Wrapf(err, "error parsing %s", filename)
  94. }
  95. return &c, nil
  96. }
  97. // Save saves the configuration to the given filename.
  98. func (c *Config) Save(filename string) error {
  99. f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
  100. if err != nil {
  101. return errors.Wrapf(err, "error opening %s", filename)
  102. }
  103. defer f.Close()
  104. enc := json.NewEncoder(f)
  105. enc.SetIndent("", "\t")
  106. return errors.Wrapf(enc.Encode(c), "error writing %s", filename)
  107. }
  108. // Validate validates the configuration.
  109. func (c *Config) Validate() error {
  110. switch {
  111. case c.Address == "":
  112. return errors.New("address cannot be empty")
  113. case c.Root.HasEmpties():
  114. return errors.New("root cannot be empty")
  115. case c.IntermediateCert == "":
  116. return errors.New("crt cannot be empty")
  117. case c.IntermediateKey == "":
  118. return errors.New("key cannot be empty")
  119. case len(c.DNSNames) == 0:
  120. return errors.New("dnsNames cannot be empty")
  121. }
  122. // Validate address (a port is required)
  123. if _, _, err := net.SplitHostPort(c.Address); err != nil {
  124. return errors.Errorf("invalid address %s", c.Address)
  125. }
  126. if c.TLS == nil {
  127. c.TLS = &DefaultTLSOptions
  128. } else {
  129. if len(c.TLS.CipherSuites) == 0 {
  130. c.TLS.CipherSuites = DefaultTLSOptions.CipherSuites
  131. }
  132. if c.TLS.MaxVersion == 0 {
  133. c.TLS.MaxVersion = DefaultTLSOptions.MaxVersion
  134. }
  135. if c.TLS.MinVersion == 0 {
  136. c.TLS.MinVersion = c.TLS.MaxVersion
  137. }
  138. if c.TLS.MinVersion > c.TLS.MaxVersion {
  139. return errors.New("tls minVersion cannot exceed tls maxVersion")
  140. }
  141. c.TLS.Renegotiation = c.TLS.Renegotiation || DefaultTLSOptions.Renegotiation
  142. }
  143. return c.AuthorityConfig.Validate(c.getAudiences())
  144. }
  145. // getAudiences returns the legacy and possible urls without the ports that will
  146. // be used as the default provisioner audiences. The CA might have proxies in
  147. // front so we cannot rely on the port.
  148. func (c *Config) getAudiences() []string {
  149. audiences := []string{legacyAuthority}
  150. for _, name := range c.DNSNames {
  151. audiences = append(audiences, fmt.Sprintf("https://%s/sign", name), fmt.Sprintf("https://%s/1.0/sign", name))
  152. }
  153. return audiences
  154. }