config.go 9.4 KB

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