config.go 4.6 KB

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