config.go 5.2 KB

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