pki.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. package pki
  2. import (
  3. "context"
  4. "crypto"
  5. "crypto/sha256"
  6. "crypto/x509"
  7. "crypto/x509/pkix"
  8. "encoding/hex"
  9. "encoding/json"
  10. "encoding/pem"
  11. "fmt"
  12. "html"
  13. "net"
  14. "os"
  15. "path/filepath"
  16. "strconv"
  17. "strings"
  18. "time"
  19. "github.com/pkg/errors"
  20. "github.com/smallstep/certificates/authority"
  21. "github.com/smallstep/certificates/authority/provisioner"
  22. "github.com/smallstep/certificates/ca"
  23. "github.com/smallstep/certificates/cas"
  24. "github.com/smallstep/certificates/cas/apiv1"
  25. "github.com/smallstep/certificates/db"
  26. "go.step.sm/cli-utils/config"
  27. "go.step.sm/cli-utils/errs"
  28. "go.step.sm/cli-utils/fileutil"
  29. "go.step.sm/cli-utils/ui"
  30. "go.step.sm/crypto/jose"
  31. "go.step.sm/crypto/keyutil"
  32. "go.step.sm/crypto/pemutil"
  33. "golang.org/x/crypto/ssh"
  34. )
  35. const (
  36. // ConfigPath is the directory name under the step path where the configuration
  37. // files will be stored.
  38. configPath = "config"
  39. // PublicPath is the directory name under the step path where the public keys
  40. // will be stored.
  41. publicPath = "certs"
  42. // PublicPath is the directory name under the step path where the private keys
  43. // will be stored.
  44. privatePath = "secrets"
  45. // DBPath is the directory name under the step path where the private keys
  46. // will be stored.
  47. dbPath = "db"
  48. // templatesPath is the directory to store templates
  49. templatesPath = "templates"
  50. )
  51. // GetDBPath returns the path where the file-system persistence is stored
  52. // based on the STEPPATH environment variable.
  53. func GetDBPath() string {
  54. return filepath.Join(config.StepPath(), dbPath)
  55. }
  56. // GetConfigPath returns the directory where the configuration files are stored
  57. // based on the STEPPATH environment variable.
  58. func GetConfigPath() string {
  59. return filepath.Join(config.StepPath(), configPath)
  60. }
  61. // GetPublicPath returns the directory where the public keys are stored based on
  62. // the STEPPATH environment variable.
  63. func GetPublicPath() string {
  64. return filepath.Join(config.StepPath(), publicPath)
  65. }
  66. // GetSecretsPath returns the directory where the private keys are stored based
  67. // on the STEPPATH environment variable.
  68. func GetSecretsPath() string {
  69. return filepath.Join(config.StepPath(), privatePath)
  70. }
  71. // GetRootCAPath returns the path where the root CA is stored based on the
  72. // STEPPATH environment variable.
  73. func GetRootCAPath() string {
  74. return filepath.Join(config.StepPath(), publicPath, "root_ca.crt")
  75. }
  76. // GetOTTKeyPath returns the path where the one-time token key is stored based
  77. // on the STEPPATH environment variable.
  78. func GetOTTKeyPath() string {
  79. return filepath.Join(config.StepPath(), privatePath, "ott_key")
  80. }
  81. // GetTemplatesPath returns the path where the templates are stored.
  82. func GetTemplatesPath() string {
  83. return filepath.Join(config.StepPath(), templatesPath)
  84. }
  85. // GetProvisioners returns the map of provisioners on the given CA.
  86. func GetProvisioners(caURL, rootFile string) (provisioner.List, error) {
  87. if len(rootFile) == 0 {
  88. rootFile = GetRootCAPath()
  89. }
  90. client, err := ca.NewClient(caURL, ca.WithRootFile(rootFile))
  91. if err != nil {
  92. return nil, err
  93. }
  94. cursor := ""
  95. provisioners := provisioner.List{}
  96. for {
  97. resp, err := client.Provisioners(ca.WithProvisionerCursor(cursor), ca.WithProvisionerLimit(100))
  98. if err != nil {
  99. return nil, err
  100. }
  101. provisioners = append(provisioners, resp.Provisioners...)
  102. if resp.NextCursor == "" {
  103. return provisioners, nil
  104. }
  105. cursor = resp.NextCursor
  106. }
  107. }
  108. // GetProvisionerKey returns the encrypted provisioner key with the for the
  109. // given kid.
  110. func GetProvisionerKey(caURL, rootFile, kid string) (string, error) {
  111. if len(rootFile) == 0 {
  112. rootFile = GetRootCAPath()
  113. }
  114. client, err := ca.NewClient(caURL, ca.WithRootFile(rootFile))
  115. if err != nil {
  116. return "", err
  117. }
  118. resp, err := client.ProvisionerKey(kid)
  119. if err != nil {
  120. return "", err
  121. }
  122. return resp.Key, nil
  123. }
  124. // PKI represents the Public Key Infrastructure used by a certificate authority.
  125. type PKI struct {
  126. casOptions apiv1.Options
  127. caCreator apiv1.CertificateAuthorityCreator
  128. root, rootKey, rootFingerprint string
  129. intermediate, intermediateKey string
  130. sshHostPubKey, sshHostKey string
  131. sshUserPubKey, sshUserKey string
  132. config, defaults string
  133. ottPublicKey *jose.JSONWebKey
  134. ottPrivateKey *jose.JSONWebEncryption
  135. provisioner string
  136. address string
  137. dnsNames []string
  138. caURL string
  139. enableSSH bool
  140. }
  141. // New creates a new PKI configuration.
  142. func New(opts apiv1.Options) (*PKI, error) {
  143. caCreator, err := cas.NewCreator(context.Background(), opts)
  144. if err != nil {
  145. return nil, err
  146. }
  147. public := GetPublicPath()
  148. private := GetSecretsPath()
  149. config := GetConfigPath()
  150. // Create directories
  151. dirs := []string{public, private, config, GetTemplatesPath()}
  152. for _, name := range dirs {
  153. if _, err := os.Stat(name); os.IsNotExist(err) {
  154. if err = os.MkdirAll(name, 0700); err != nil {
  155. return nil, errs.FileError(err, name)
  156. }
  157. }
  158. }
  159. // get absolute path for dir/name
  160. getPath := func(dir string, name string) (string, error) {
  161. s, err := filepath.Abs(filepath.Join(dir, name))
  162. return s, errors.Wrapf(err, "error getting absolute path for %s", name)
  163. }
  164. p := &PKI{
  165. casOptions: opts,
  166. caCreator: caCreator,
  167. provisioner: "step-cli",
  168. address: "127.0.0.1:9000",
  169. dnsNames: []string{"127.0.0.1"},
  170. }
  171. if p.root, err = getPath(public, "root_ca.crt"); err != nil {
  172. return nil, err
  173. }
  174. if p.rootKey, err = getPath(private, "root_ca_key"); err != nil {
  175. return nil, err
  176. }
  177. if p.intermediate, err = getPath(public, "intermediate_ca.crt"); err != nil {
  178. return nil, err
  179. }
  180. if p.intermediateKey, err = getPath(private, "intermediate_ca_key"); err != nil {
  181. return nil, err
  182. }
  183. if p.sshHostPubKey, err = getPath(public, "ssh_host_ca_key.pub"); err != nil {
  184. return nil, err
  185. }
  186. if p.sshUserPubKey, err = getPath(public, "ssh_user_ca_key.pub"); err != nil {
  187. return nil, err
  188. }
  189. if p.sshHostKey, err = getPath(private, "ssh_host_ca_key"); err != nil {
  190. return nil, err
  191. }
  192. if p.sshUserKey, err = getPath(private, "ssh_user_ca_key"); err != nil {
  193. return nil, err
  194. }
  195. if len(config) > 0 {
  196. if p.config, err = getPath(config, "ca.json"); err != nil {
  197. return nil, err
  198. }
  199. if p.defaults, err = getPath(config, "defaults.json"); err != nil {
  200. return nil, err
  201. }
  202. }
  203. return p, nil
  204. }
  205. // GetCAConfigPath returns the path of the CA configuration file.
  206. func (p *PKI) GetCAConfigPath() string {
  207. return p.config
  208. }
  209. // GetRootFingerprint returns the root fingerprint.
  210. func (p *PKI) GetRootFingerprint() string {
  211. return p.rootFingerprint
  212. }
  213. // SetProvisioner sets the provisioner name of the OTT keys.
  214. func (p *PKI) SetProvisioner(s string) {
  215. p.provisioner = s
  216. }
  217. // SetAddress sets the listening address of the CA.
  218. func (p *PKI) SetAddress(s string) {
  219. p.address = s
  220. }
  221. // SetDNSNames sets the dns names of the CA.
  222. func (p *PKI) SetDNSNames(s []string) {
  223. p.dnsNames = s
  224. }
  225. // SetCAURL sets the ca-url to use in the defaults.json.
  226. func (p *PKI) SetCAURL(s string) {
  227. p.caURL = s
  228. }
  229. // GenerateKeyPairs generates the key pairs used by the certificate authority.
  230. func (p *PKI) GenerateKeyPairs(pass []byte) error {
  231. var err error
  232. // Create OTT key pair, the user doesn't need to know about this.
  233. p.ottPublicKey, p.ottPrivateKey, err = jose.GenerateDefaultKeyPair(pass)
  234. if err != nil {
  235. return err
  236. }
  237. return nil
  238. }
  239. // GenerateRootCertificate generates a root certificate with the given name
  240. // and using the default key type.
  241. func (p *PKI) GenerateRootCertificate(name, org, resource string, pass []byte) (*apiv1.CreateCertificateAuthorityResponse, error) {
  242. resp, err := p.caCreator.CreateCertificateAuthority(&apiv1.CreateCertificateAuthorityRequest{
  243. Name: resource + "-Root-CA",
  244. Type: apiv1.RootCA,
  245. Lifetime: 10 * 365 * 24 * time.Hour,
  246. CreateKey: nil, // use default
  247. Template: &x509.Certificate{
  248. Subject: pkix.Name{
  249. CommonName: name + " Root CA",
  250. Organization: []string{org},
  251. },
  252. KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
  253. BasicConstraintsValid: true,
  254. IsCA: true,
  255. MaxPathLen: 1,
  256. MaxPathLenZero: false,
  257. },
  258. })
  259. if err != nil {
  260. return nil, err
  261. }
  262. // PrivateKey will only be set if we have access to it (SoftCAS).
  263. if err := p.WriteRootCertificate(resp.Certificate, resp.PrivateKey, pass); err != nil {
  264. return nil, err
  265. }
  266. return resp, nil
  267. }
  268. // GenerateIntermediateCertificate generates an intermediate certificate with
  269. // the given name and using the default key type.
  270. func (p *PKI) GenerateIntermediateCertificate(name, org, resource string, parent *apiv1.CreateCertificateAuthorityResponse, pass []byte) error {
  271. resp, err := p.caCreator.CreateCertificateAuthority(&apiv1.CreateCertificateAuthorityRequest{
  272. Name: resource + "-Intermediate-CA",
  273. Type: apiv1.IntermediateCA,
  274. Lifetime: 10 * 365 * 24 * time.Hour,
  275. CreateKey: nil, // use default
  276. Template: &x509.Certificate{
  277. Subject: pkix.Name{
  278. CommonName: name + " Intermediate CA",
  279. Organization: []string{org},
  280. },
  281. KeyUsage: x509.KeyUsageCertSign | x509.KeyUsageCRLSign,
  282. BasicConstraintsValid: true,
  283. IsCA: true,
  284. MaxPathLen: 0,
  285. MaxPathLenZero: true,
  286. },
  287. Parent: parent,
  288. })
  289. if err != nil {
  290. return err
  291. }
  292. p.casOptions.CertificateAuthority = resp.Name
  293. return p.WriteIntermediateCertificate(resp.Certificate, resp.PrivateKey, pass)
  294. }
  295. // WriteRootCertificate writes to disk the given certificate and key.
  296. func (p *PKI) WriteRootCertificate(rootCrt *x509.Certificate, rootKey interface{}, pass []byte) error {
  297. if err := fileutil.WriteFile(p.root, pem.EncodeToMemory(&pem.Block{
  298. Type: "CERTIFICATE",
  299. Bytes: rootCrt.Raw,
  300. }), 0600); err != nil {
  301. return err
  302. }
  303. if rootKey != nil {
  304. _, err := pemutil.Serialize(rootKey, pemutil.WithPassword(pass), pemutil.ToFile(p.rootKey, 0600))
  305. if err != nil {
  306. return err
  307. }
  308. }
  309. sum := sha256.Sum256(rootCrt.Raw)
  310. p.rootFingerprint = strings.ToLower(hex.EncodeToString(sum[:]))
  311. return nil
  312. }
  313. // WriteIntermediateCertificate writes to disk the given certificate and key.
  314. func (p *PKI) WriteIntermediateCertificate(crt *x509.Certificate, key interface{}, pass []byte) error {
  315. if err := fileutil.WriteFile(p.intermediate, pem.EncodeToMemory(&pem.Block{
  316. Type: "CERTIFICATE",
  317. Bytes: crt.Raw,
  318. }), 0600); err != nil {
  319. return err
  320. }
  321. if key != nil {
  322. _, err := pemutil.Serialize(key, pemutil.WithPassword(pass), pemutil.ToFile(p.intermediateKey, 0600))
  323. if err != nil {
  324. return err
  325. }
  326. }
  327. return nil
  328. }
  329. // CreateCertificateAuthorityResponse returns a
  330. // CreateCertificateAuthorityResponse that can be used as a parent of a
  331. // CreateCertificateAuthority request.
  332. func (p *PKI) CreateCertificateAuthorityResponse(cert *x509.Certificate, key crypto.PrivateKey) *apiv1.CreateCertificateAuthorityResponse {
  333. signer, _ := key.(crypto.Signer)
  334. return &apiv1.CreateCertificateAuthorityResponse{
  335. Certificate: cert,
  336. PrivateKey: key,
  337. Signer: signer,
  338. }
  339. }
  340. // GetCertificateAuthority attempts to load the certificate authority from the
  341. // RA.
  342. func (p *PKI) GetCertificateAuthority() error {
  343. srv, ok := p.caCreator.(apiv1.CertificateAuthorityGetter)
  344. if !ok {
  345. return nil
  346. }
  347. resp, err := srv.GetCertificateAuthority(&apiv1.GetCertificateAuthorityRequest{
  348. Name: p.casOptions.CertificateAuthority,
  349. })
  350. if err != nil {
  351. return err
  352. }
  353. if err := p.WriteRootCertificate(resp.RootCertificate, nil, nil); err != nil {
  354. return err
  355. }
  356. // Issuer is in the RA
  357. p.intermediate = ""
  358. p.intermediateKey = ""
  359. return nil
  360. }
  361. // GenerateSSHSigningKeys generates and encrypts a private key used for signing
  362. // SSH user certificates and a private key used for signing host certificates.
  363. func (p *PKI) GenerateSSHSigningKeys(password []byte) error {
  364. var pubNames = []string{p.sshHostPubKey, p.sshUserPubKey}
  365. var privNames = []string{p.sshHostKey, p.sshUserKey}
  366. for i := 0; i < 2; i++ {
  367. pub, priv, err := keyutil.GenerateDefaultKeyPair()
  368. if err != nil {
  369. return err
  370. }
  371. if _, ok := priv.(crypto.Signer); !ok {
  372. return errors.Errorf("key of type %T is not a crypto.Signer", priv)
  373. }
  374. sshKey, err := ssh.NewPublicKey(pub)
  375. if err != nil {
  376. return errors.Wrapf(err, "error converting public key")
  377. }
  378. _, err = pemutil.Serialize(priv, pemutil.WithFilename(privNames[i]), pemutil.WithPassword(password))
  379. if err != nil {
  380. return err
  381. }
  382. if err = fileutil.WriteFile(pubNames[i], ssh.MarshalAuthorizedKey(sshKey), 0600); err != nil {
  383. return err
  384. }
  385. }
  386. p.enableSSH = true
  387. return nil
  388. }
  389. func (p *PKI) askFeedback() {
  390. ui.Println()
  391. ui.Printf("\033[1mFEEDBACK\033[0m %s %s\n",
  392. html.UnescapeString("&#"+strconv.Itoa(128525)+";"),
  393. html.UnescapeString("&#"+strconv.Itoa(127867)+";"))
  394. ui.Println(" The \033[1mstep\033[0m utility is not instrumented for usage statistics. It does not")
  395. ui.Println(" phone home. But your feedback is extremely valuable. Any information you")
  396. ui.Println(" can provide regarding how you’re using `step` helps. Please send us a")
  397. ui.Println(" sentence or two, good or bad: \033[1mfeedback@smallstep.com\033[0m or join")
  398. ui.Println(" \033[1mhttps://github.com/smallstep/certificates/discussions\033[0m.")
  399. }
  400. // TellPKI outputs the locations of public and private keys generated
  401. // generated for a new PKI. Generally this will consist of a root certificate
  402. // and key and an intermediate certificate and key.
  403. func (p *PKI) TellPKI() {
  404. p.tellPKI()
  405. p.askFeedback()
  406. }
  407. func (p *PKI) tellPKI() {
  408. ui.Println()
  409. if p.casOptions.Is(apiv1.SoftCAS) {
  410. ui.PrintSelected("Root certificate", p.root)
  411. ui.PrintSelected("Root private key", p.rootKey)
  412. ui.PrintSelected("Root fingerprint", p.rootFingerprint)
  413. ui.PrintSelected("Intermediate certificate", p.intermediate)
  414. ui.PrintSelected("Intermediate private key", p.intermediateKey)
  415. } else if p.rootFingerprint != "" {
  416. ui.PrintSelected("Root certificate", p.root)
  417. ui.PrintSelected("Root fingerprint", p.rootFingerprint)
  418. } else {
  419. ui.Printf(`{{ "%s" | red }} {{ "Root certificate:" | bold }} failed to retrieve it from RA`+"\n", ui.IconBad)
  420. }
  421. if p.enableSSH {
  422. ui.PrintSelected("SSH user root certificate", p.sshUserPubKey)
  423. ui.PrintSelected("SSH user root private key", p.sshUserKey)
  424. ui.PrintSelected("SSH host root certificate", p.sshHostPubKey)
  425. ui.PrintSelected("SSH host root private key", p.sshHostKey)
  426. }
  427. }
  428. type caDefaults struct {
  429. CAUrl string `json:"ca-url"`
  430. CAConfig string `json:"ca-config"`
  431. Fingerprint string `json:"fingerprint"`
  432. Root string `json:"root"`
  433. }
  434. // Option is the type for modifiers over the auth config object.
  435. type Option func(c *authority.Config) error
  436. // WithDefaultDB is a configuration modifier that adds a default DB stanza to
  437. // the authority config.
  438. func WithDefaultDB() Option {
  439. return func(c *authority.Config) error {
  440. c.DB = &db.Config{
  441. Type: "badger",
  442. DataSource: GetDBPath(),
  443. }
  444. return nil
  445. }
  446. }
  447. // WithoutDB is a configuration modifier that adds a default DB stanza to
  448. // the authority config.
  449. func WithoutDB() Option {
  450. return func(c *authority.Config) error {
  451. c.DB = nil
  452. return nil
  453. }
  454. }
  455. // GenerateConfig returns the step certificates configuration.
  456. func (p *PKI) GenerateConfig(opt ...Option) (*authority.Config, error) {
  457. key, err := p.ottPrivateKey.CompactSerialize()
  458. if err != nil {
  459. return nil, errors.Wrap(err, "error serializing private key")
  460. }
  461. prov := &provisioner.JWK{
  462. Name: p.provisioner,
  463. Type: "JWK",
  464. Key: p.ottPublicKey,
  465. EncryptedKey: key,
  466. }
  467. var authorityOptions *apiv1.Options
  468. if !p.casOptions.Is(apiv1.SoftCAS) {
  469. authorityOptions = &p.casOptions
  470. }
  471. config := &authority.Config{
  472. Root: []string{p.root},
  473. FederatedRoots: []string{},
  474. IntermediateCert: p.intermediate,
  475. IntermediateKey: p.intermediateKey,
  476. Address: p.address,
  477. DNSNames: p.dnsNames,
  478. Logger: []byte(`{"format": "text"}`),
  479. DB: &db.Config{
  480. Type: "badger",
  481. DataSource: GetDBPath(),
  482. },
  483. AuthorityConfig: &authority.AuthConfig{
  484. Options: authorityOptions,
  485. DisableIssuedAtCheck: false,
  486. Provisioners: provisioner.List{prov},
  487. },
  488. TLS: &authority.TLSOptions{
  489. MinVersion: authority.DefaultTLSMinVersion,
  490. MaxVersion: authority.DefaultTLSMaxVersion,
  491. Renegotiation: authority.DefaultTLSRenegotiation,
  492. CipherSuites: authority.DefaultTLSCipherSuites,
  493. },
  494. Templates: p.getTemplates(),
  495. }
  496. if p.enableSSH {
  497. enableSSHCA := true
  498. config.SSH = &authority.SSHConfig{
  499. HostKey: p.sshHostKey,
  500. UserKey: p.sshUserKey,
  501. }
  502. // Enable SSH authorization for default JWK provisioner
  503. prov.Claims = &provisioner.Claims{
  504. EnableSSHCA: &enableSSHCA,
  505. }
  506. // Add default SSHPOP provisioner
  507. sshpop := &provisioner.SSHPOP{
  508. Type: "SSHPOP",
  509. Name: "sshpop",
  510. Claims: &provisioner.Claims{
  511. EnableSSHCA: &enableSSHCA,
  512. },
  513. }
  514. config.AuthorityConfig.Provisioners = append(config.AuthorityConfig.Provisioners, sshpop)
  515. }
  516. // Apply configuration modifiers
  517. for _, o := range opt {
  518. if err = o(config); err != nil {
  519. return nil, err
  520. }
  521. }
  522. return config, nil
  523. }
  524. // Save stores the pki on a json file that will be used as the certificate
  525. // authority configuration.
  526. func (p *PKI) Save(opt ...Option) error {
  527. p.tellPKI()
  528. // Generate and write ca.json
  529. config, err := p.GenerateConfig(opt...)
  530. if err != nil {
  531. return err
  532. }
  533. b, err := json.MarshalIndent(config, "", "\t")
  534. if err != nil {
  535. return errors.Wrapf(err, "error marshaling %s", p.config)
  536. }
  537. if err = fileutil.WriteFile(p.config, b, 0644); err != nil {
  538. return errs.FileError(err, p.config)
  539. }
  540. // Generate the CA URL.
  541. if p.caURL == "" {
  542. p.caURL = p.dnsNames[0]
  543. var port string
  544. _, port, err = net.SplitHostPort(p.address)
  545. if err != nil {
  546. return errors.Wrapf(err, "error parsing %s", p.address)
  547. }
  548. if port == "443" {
  549. p.caURL = fmt.Sprintf("https://%s", p.caURL)
  550. } else {
  551. p.caURL = fmt.Sprintf("https://%s:%s", p.caURL, port)
  552. }
  553. }
  554. // Generate and write defaults.json
  555. defaults := &caDefaults{
  556. Root: p.root,
  557. CAConfig: p.config,
  558. CAUrl: p.caURL,
  559. Fingerprint: p.rootFingerprint,
  560. }
  561. b, err = json.MarshalIndent(defaults, "", "\t")
  562. if err != nil {
  563. return errors.Wrapf(err, "error marshaling %s", p.defaults)
  564. }
  565. if err = fileutil.WriteFile(p.defaults, b, 0644); err != nil {
  566. return errs.FileError(err, p.defaults)
  567. }
  568. // Generate and write templates
  569. if err := generateTemplates(config.Templates); err != nil {
  570. return err
  571. }
  572. if config.DB != nil {
  573. ui.PrintSelected("Database folder", config.DB.DataSource)
  574. }
  575. if config.Templates != nil {
  576. ui.PrintSelected("Templates folder", GetTemplatesPath())
  577. }
  578. ui.PrintSelected("Default configuration", p.defaults)
  579. ui.PrintSelected("Certificate Authority configuration", p.config)
  580. ui.Println()
  581. if p.casOptions.Is(apiv1.SoftCAS) {
  582. ui.Println("Your PKI is ready to go. To generate certificates for individual services see 'step help ca'.")
  583. } else {
  584. ui.Println("Your registration authority is ready to go. To generate certificates for individual services see 'step help ca'.")
  585. }
  586. p.askFeedback()
  587. return nil
  588. }