config.go 4.4 KB

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