config.go 9.1 KB

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