ssh.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. package authority
  2. import (
  3. "context"
  4. "crypto/rand"
  5. "crypto/x509"
  6. "encoding/binary"
  7. "errors"
  8. "fmt"
  9. "net/http"
  10. "strings"
  11. "time"
  12. "golang.org/x/crypto/ssh"
  13. "go.step.sm/crypto/randutil"
  14. "go.step.sm/crypto/sshutil"
  15. "github.com/smallstep/certificates/authority/config"
  16. "github.com/smallstep/certificates/authority/provisioner"
  17. "github.com/smallstep/certificates/db"
  18. "github.com/smallstep/certificates/errs"
  19. policy "github.com/smallstep/certificates/policy"
  20. "github.com/smallstep/certificates/templates"
  21. )
  22. const (
  23. // SSHAddUserPrincipal is the principal that will run the add user command.
  24. // Defaults to "provisioner" but it can be changed in the configuration.
  25. SSHAddUserPrincipal = "provisioner"
  26. // SSHAddUserCommand is the default command to run to add a new user.
  27. // Defaults to "sudo useradd -m <principal>; nc -q0 localhost 22" but it can be changed in the
  28. // configuration. The string "<principal>" will be replace by the new
  29. // principal to add.
  30. SSHAddUserCommand = "sudo useradd -m <principal>; nc -q0 localhost 22"
  31. )
  32. // GetSSHRoots returns the SSH User and Host public keys.
  33. func (a *Authority) GetSSHRoots(context.Context) (*config.SSHKeys, error) {
  34. return &config.SSHKeys{
  35. HostKeys: a.sshCAHostCerts,
  36. UserKeys: a.sshCAUserCerts,
  37. }, nil
  38. }
  39. // GetSSHFederation returns the public keys for federated SSH signers.
  40. func (a *Authority) GetSSHFederation(context.Context) (*config.SSHKeys, error) {
  41. return &config.SSHKeys{
  42. HostKeys: a.sshCAHostFederatedCerts,
  43. UserKeys: a.sshCAUserFederatedCerts,
  44. }, nil
  45. }
  46. // GetSSHConfig returns rendered templates for clients (user) or servers (host).
  47. func (a *Authority) GetSSHConfig(ctx context.Context, typ string, data map[string]string) ([]templates.Output, error) {
  48. if a.sshCAUserCertSignKey == nil && a.sshCAHostCertSignKey == nil {
  49. return nil, errs.NotFound("getSSHConfig: ssh is not configured")
  50. }
  51. if a.templates == nil {
  52. return nil, errs.NotFound("getSSHConfig: ssh templates are not configured")
  53. }
  54. var ts []templates.Template
  55. switch typ {
  56. case provisioner.SSHUserCert:
  57. if a.templates != nil && a.templates.SSH != nil {
  58. ts = a.templates.SSH.User
  59. }
  60. case provisioner.SSHHostCert:
  61. if a.templates != nil && a.templates.SSH != nil {
  62. ts = a.templates.SSH.Host
  63. }
  64. default:
  65. return nil, errs.BadRequest("invalid certificate type '%s'", typ)
  66. }
  67. // Merge user and default data
  68. var mergedData map[string]interface{}
  69. if len(data) == 0 {
  70. mergedData = a.templates.Data
  71. } else {
  72. mergedData = make(map[string]interface{}, len(a.templates.Data)+1)
  73. mergedData["User"] = data
  74. for k, v := range a.templates.Data {
  75. mergedData[k] = v
  76. }
  77. }
  78. // Render templates
  79. output := []templates.Output{}
  80. for _, t := range ts {
  81. if err := t.Load(); err != nil {
  82. return nil, err
  83. }
  84. // Check for required variables.
  85. if err := t.ValidateRequiredData(data); err != nil {
  86. return nil, errs.BadRequestErr(err, "%v, please use `--set <key=value>` flag", err)
  87. }
  88. o, err := t.Output(mergedData)
  89. if err != nil {
  90. return nil, err
  91. }
  92. // Backwards compatibility for version of the cli older than v0.18.0.
  93. // Before v0.18.0 we were not passing any value for SSHTemplateVersionKey
  94. // from the cli.
  95. if o.Name == "step_includes.tpl" && data[templates.SSHTemplateVersionKey] == "" {
  96. o.Type = templates.File
  97. o.Path = strings.TrimPrefix(o.Path, "${STEPPATH}/")
  98. }
  99. output = append(output, o)
  100. }
  101. return output, nil
  102. }
  103. // GetSSHBastion returns the bastion configuration, for the given pair user,
  104. // hostname.
  105. func (a *Authority) GetSSHBastion(ctx context.Context, user, hostname string) (*config.Bastion, error) {
  106. if a.sshBastionFunc != nil {
  107. bs, err := a.sshBastionFunc(ctx, user, hostname)
  108. return bs, errs.Wrap(http.StatusInternalServerError, err, "authority.GetSSHBastion")
  109. }
  110. if a.config.SSH != nil {
  111. if a.config.SSH.Bastion != nil && a.config.SSH.Bastion.Hostname != "" {
  112. // Do not return a bastion for a bastion host.
  113. //
  114. // This condition might fail if a different name or IP is used.
  115. // Trying to resolve hostnames to IPs and compare them won't be a
  116. // complete solution because it depends on the network
  117. // configuration, of the CA and clients and can also return false
  118. // positives. Although not perfect, this simple solution will work
  119. // in most cases.
  120. if !strings.EqualFold(hostname, a.config.SSH.Bastion.Hostname) {
  121. return a.config.SSH.Bastion, nil
  122. }
  123. }
  124. return nil, nil
  125. }
  126. return nil, errs.NotFound("authority.GetSSHBastion; ssh is not configured")
  127. }
  128. // SignSSH creates a signed SSH certificate with the given public key and options.
  129. func (a *Authority) SignSSH(ctx context.Context, key ssh.PublicKey, opts provisioner.SignSSHOptions, signOpts ...provisioner.SignOption) (*ssh.Certificate, error) {
  130. var (
  131. certOptions []sshutil.Option
  132. mods []provisioner.SSHCertModifier
  133. validators []provisioner.SSHCertValidator
  134. )
  135. // Validate given options.
  136. if err := opts.Validate(); err != nil {
  137. return nil, err
  138. }
  139. // Set backdate with the configured value
  140. opts.Backdate = a.config.AuthorityConfig.Backdate.Duration
  141. var prov provisioner.Interface
  142. for _, op := range signOpts {
  143. switch o := op.(type) {
  144. // Capture current provisioner
  145. case provisioner.Interface:
  146. prov = o
  147. // add options to NewCertificate
  148. case provisioner.SSHCertificateOptions:
  149. certOptions = append(certOptions, o.Options(opts)...)
  150. // modify the ssh.Certificate
  151. case provisioner.SSHCertModifier:
  152. mods = append(mods, o)
  153. // validate the ssh.Certificate
  154. case provisioner.SSHCertValidator:
  155. validators = append(validators, o)
  156. // validate the given SSHOptions
  157. case provisioner.SSHCertOptionsValidator:
  158. if err := o.Valid(opts); err != nil {
  159. return nil, errs.BadRequestErr(err, "error validating ssh certificate options")
  160. }
  161. default:
  162. return nil, errs.InternalServer("authority.SignSSH: invalid extra option type %T", o)
  163. }
  164. }
  165. // Simulated certificate request with request options.
  166. cr := sshutil.CertificateRequest{
  167. Type: opts.CertType,
  168. KeyID: opts.KeyID,
  169. Principals: opts.Principals,
  170. Key: key,
  171. }
  172. // Create certificate from template.
  173. certificate, err := sshutil.NewCertificate(cr, certOptions...)
  174. if err != nil {
  175. if _, ok := err.(*sshutil.TemplateError); ok {
  176. return nil, errs.ApplyOptions(
  177. errs.BadRequestErr(err, err.Error()),
  178. errs.WithKeyVal("signOptions", signOpts),
  179. )
  180. }
  181. // explicitly check for unmarshaling errors, which are most probably caused by JSON template syntax errors
  182. if strings.HasPrefix(err.Error(), "error unmarshaling certificate") {
  183. return nil, errs.InternalServerErr(templatingError(err),
  184. errs.WithKeyVal("signOptions", signOpts),
  185. errs.WithMessage("error applying certificate template"),
  186. )
  187. }
  188. return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.SignSSH")
  189. }
  190. // Get actual *ssh.Certificate and continue with provisioner modifiers.
  191. certTpl := certificate.GetCertificate()
  192. // Use SignSSHOptions to modify the certificate validity. It will be later
  193. // checked or set if not defined.
  194. if err := opts.ModifyValidity(certTpl); err != nil {
  195. return nil, errs.BadRequestErr(err, err.Error())
  196. }
  197. // Use provisioner modifiers.
  198. for _, m := range mods {
  199. if err := m.Modify(certTpl, opts); err != nil {
  200. return nil, errs.ForbiddenErr(err, "error creating ssh certificate")
  201. }
  202. }
  203. // Get signer from authority keys
  204. var signer ssh.Signer
  205. switch certTpl.CertType {
  206. case ssh.UserCert:
  207. if a.sshCAUserCertSignKey == nil {
  208. return nil, errs.NotImplemented("authority.SignSSH: user certificate signing is not enabled")
  209. }
  210. signer = a.sshCAUserCertSignKey
  211. case ssh.HostCert:
  212. if a.sshCAHostCertSignKey == nil {
  213. return nil, errs.NotImplemented("authority.SignSSH: host certificate signing is not enabled")
  214. }
  215. signer = a.sshCAHostCertSignKey
  216. default:
  217. return nil, errs.InternalServer("authority.SignSSH: unexpected ssh certificate type: %d", certTpl.CertType)
  218. }
  219. // Check if authority is allowed to sign the certificate
  220. if err := a.isAllowedToSignSSHCertificate(certTpl); err != nil {
  221. var pe *policy.NamePolicyError
  222. if errors.As(err, &pe) && pe.Reason == policy.NotAllowed {
  223. return nil, &errs.Error{
  224. // NOTE: custom forbidden error, so that denied name is sent to client
  225. // as well as shown in the logs.
  226. Status: http.StatusForbidden,
  227. Err: fmt.Errorf("authority not allowed to sign: %w", err),
  228. Msg: fmt.Sprintf("The request was forbidden by the certificate authority: %s", err.Error()),
  229. }
  230. }
  231. return nil, errs.InternalServerErr(err,
  232. errs.WithMessage("authority.SignSSH: error creating ssh certificate"),
  233. )
  234. }
  235. // Sign certificate.
  236. cert, err := sshutil.CreateCertificate(certTpl, signer)
  237. if err != nil {
  238. return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.SignSSH: error signing certificate")
  239. }
  240. // User provisioners validators.
  241. for _, v := range validators {
  242. if err := v.Valid(cert, opts); err != nil {
  243. return nil, errs.ForbiddenErr(err, "error validating ssh certificate")
  244. }
  245. }
  246. if err = a.storeSSHCertificate(prov, cert); err != nil && err != db.ErrNotImplemented {
  247. return nil, errs.Wrap(http.StatusInternalServerError, err, "authority.SignSSH: error storing certificate in db")
  248. }
  249. return cert, nil
  250. }
  251. // isAllowedToSignSSHCertificate checks if the Authority is allowed to sign the SSH certificate.
  252. func (a *Authority) isAllowedToSignSSHCertificate(cert *ssh.Certificate) error {
  253. return a.policyEngine.IsSSHCertificateAllowed(cert)
  254. }
  255. // RenewSSH creates a signed SSH certificate using the old SSH certificate as a template.
  256. func (a *Authority) RenewSSH(ctx context.Context, oldCert *ssh.Certificate) (*ssh.Certificate, error) {
  257. if oldCert.ValidAfter == 0 || oldCert.ValidBefore == 0 {
  258. return nil, errs.BadRequest("cannot renew a certificate without validity period")
  259. }
  260. if err := a.authorizeSSHCertificate(ctx, oldCert); err != nil {
  261. return nil, err
  262. }
  263. backdate := a.config.AuthorityConfig.Backdate.Duration
  264. duration := time.Duration(oldCert.ValidBefore-oldCert.ValidAfter) * time.Second
  265. now := time.Now()
  266. va := now.Add(-1 * backdate)
  267. vb := now.Add(duration - backdate)
  268. // Build base certificate with the old key.
  269. // Nonce and serial will be automatically generated on signing.
  270. certTpl := &ssh.Certificate{
  271. Key: oldCert.Key,
  272. CertType: oldCert.CertType,
  273. KeyId: oldCert.KeyId,
  274. ValidPrincipals: oldCert.ValidPrincipals,
  275. Permissions: oldCert.Permissions,
  276. Reserved: oldCert.Reserved,
  277. ValidAfter: uint64(va.Unix()),
  278. ValidBefore: uint64(vb.Unix()),
  279. }
  280. // Get signer from authority keys
  281. var signer ssh.Signer
  282. switch certTpl.CertType {
  283. case ssh.UserCert:
  284. if a.sshCAUserCertSignKey == nil {
  285. return nil, errs.NotImplemented("renewSSH: user certificate signing is not enabled")
  286. }
  287. signer = a.sshCAUserCertSignKey
  288. case ssh.HostCert:
  289. if a.sshCAHostCertSignKey == nil {
  290. return nil, errs.NotImplemented("renewSSH: host certificate signing is not enabled")
  291. }
  292. signer = a.sshCAHostCertSignKey
  293. default:
  294. return nil, errs.InternalServer("renewSSH: unexpected ssh certificate type: %d", certTpl.CertType)
  295. }
  296. // Sign certificate.
  297. cert, err := sshutil.CreateCertificate(certTpl, signer)
  298. if err != nil {
  299. return nil, errs.Wrap(http.StatusInternalServerError, err, "signSSH: error signing certificate")
  300. }
  301. if err = a.storeRenewedSSHCertificate(oldCert, cert); err != nil && err != db.ErrNotImplemented {
  302. return nil, errs.Wrap(http.StatusInternalServerError, err, "renewSSH: error storing certificate in db")
  303. }
  304. return cert, nil
  305. }
  306. // RekeySSH creates a signed SSH certificate using the old SSH certificate as a template.
  307. func (a *Authority) RekeySSH(ctx context.Context, oldCert *ssh.Certificate, pub ssh.PublicKey, signOpts ...provisioner.SignOption) (*ssh.Certificate, error) {
  308. var validators []provisioner.SSHCertValidator
  309. for _, op := range signOpts {
  310. switch o := op.(type) {
  311. // validate the ssh.Certificate
  312. case provisioner.SSHCertValidator:
  313. validators = append(validators, o)
  314. default:
  315. return nil, errs.InternalServer("rekeySSH; invalid extra option type %T", o)
  316. }
  317. }
  318. if oldCert.ValidAfter == 0 || oldCert.ValidBefore == 0 {
  319. return nil, errs.BadRequest("cannot rekey a certificate without validity period")
  320. }
  321. if err := a.authorizeSSHCertificate(ctx, oldCert); err != nil {
  322. return nil, err
  323. }
  324. backdate := a.config.AuthorityConfig.Backdate.Duration
  325. duration := time.Duration(oldCert.ValidBefore-oldCert.ValidAfter) * time.Second
  326. now := time.Now()
  327. va := now.Add(-1 * backdate)
  328. vb := now.Add(duration - backdate)
  329. // Build base certificate with the new key.
  330. // Nonce and serial will be automatically generated on signing.
  331. cert := &ssh.Certificate{
  332. Key: pub,
  333. CertType: oldCert.CertType,
  334. KeyId: oldCert.KeyId,
  335. ValidPrincipals: oldCert.ValidPrincipals,
  336. Permissions: oldCert.Permissions,
  337. Reserved: oldCert.Reserved,
  338. ValidAfter: uint64(va.Unix()),
  339. ValidBefore: uint64(vb.Unix()),
  340. }
  341. // Get signer from authority keys
  342. var signer ssh.Signer
  343. switch cert.CertType {
  344. case ssh.UserCert:
  345. if a.sshCAUserCertSignKey == nil {
  346. return nil, errs.NotImplemented("rekeySSH; user certificate signing is not enabled")
  347. }
  348. signer = a.sshCAUserCertSignKey
  349. case ssh.HostCert:
  350. if a.sshCAHostCertSignKey == nil {
  351. return nil, errs.NotImplemented("rekeySSH; host certificate signing is not enabled")
  352. }
  353. signer = a.sshCAHostCertSignKey
  354. default:
  355. return nil, errs.BadRequest("unexpected certificate type '%d'", cert.CertType)
  356. }
  357. var err error
  358. // Sign certificate.
  359. cert, err = sshutil.CreateCertificate(cert, signer)
  360. if err != nil {
  361. return nil, errs.Wrap(http.StatusInternalServerError, err, "signSSH: error signing certificate")
  362. }
  363. // Apply validators from provisioner.
  364. for _, v := range validators {
  365. if err := v.Valid(cert, provisioner.SignSSHOptions{Backdate: backdate}); err != nil {
  366. return nil, errs.ForbiddenErr(err, "error validating ssh certificate")
  367. }
  368. }
  369. if err = a.storeRenewedSSHCertificate(oldCert, cert); err != nil && err != db.ErrNotImplemented {
  370. return nil, errs.Wrap(http.StatusInternalServerError, err, "rekeySSH; error storing certificate in db")
  371. }
  372. return cert, nil
  373. }
  374. func (a *Authority) storeSSHCertificate(prov provisioner.Interface, cert *ssh.Certificate) error {
  375. type sshCertificateStorer interface {
  376. StoreSSHCertificate(provisioner.Interface, *ssh.Certificate) error
  377. }
  378. // Store certificate in admindb or linkedca
  379. switch s := a.adminDB.(type) {
  380. case sshCertificateStorer:
  381. return s.StoreSSHCertificate(prov, cert)
  382. case db.CertificateStorer:
  383. return s.StoreSSHCertificate(cert)
  384. }
  385. // Store certificate in localdb
  386. switch s := a.db.(type) {
  387. case sshCertificateStorer:
  388. return s.StoreSSHCertificate(prov, cert)
  389. case db.CertificateStorer:
  390. return s.StoreSSHCertificate(cert)
  391. default:
  392. return nil
  393. }
  394. }
  395. func (a *Authority) storeRenewedSSHCertificate(parent, cert *ssh.Certificate) error {
  396. type sshRenewerCertificateStorer interface {
  397. StoreRenewedSSHCertificate(parent, cert *ssh.Certificate) error
  398. }
  399. // Store certificate in admindb or linkedca
  400. switch s := a.adminDB.(type) {
  401. case sshRenewerCertificateStorer:
  402. return s.StoreRenewedSSHCertificate(parent, cert)
  403. case db.CertificateStorer:
  404. return s.StoreSSHCertificate(cert)
  405. }
  406. // Store certificate in localdb
  407. switch s := a.db.(type) {
  408. case sshRenewerCertificateStorer:
  409. return s.StoreRenewedSSHCertificate(parent, cert)
  410. case db.CertificateStorer:
  411. return s.StoreSSHCertificate(cert)
  412. default:
  413. return nil
  414. }
  415. }
  416. // IsValidForAddUser checks if a user provisioner certificate can be issued to
  417. // the given certificate.
  418. func IsValidForAddUser(cert *ssh.Certificate) error {
  419. if cert.CertType != ssh.UserCert {
  420. return errs.Forbidden("certificate is not a user certificate")
  421. }
  422. switch len(cert.ValidPrincipals) {
  423. case 0:
  424. return errs.Forbidden("certificate does not have any principals")
  425. case 1:
  426. return nil
  427. case 2:
  428. // OIDC provisioners adds a second principal with the email address.
  429. // @ cannot be the first character.
  430. if strings.Index(cert.ValidPrincipals[1], "@") > 0 {
  431. return nil
  432. }
  433. return errs.Forbidden("certificate does not have only one principal")
  434. default:
  435. return errs.Forbidden("certificate does not have only one principal")
  436. }
  437. }
  438. // SignSSHAddUser signs a certificate that provisions a new user in a server.
  439. func (a *Authority) SignSSHAddUser(ctx context.Context, key ssh.PublicKey, subject *ssh.Certificate) (*ssh.Certificate, error) {
  440. if a.sshCAUserCertSignKey == nil {
  441. return nil, errs.NotImplemented("signSSHAddUser: user certificate signing is not enabled")
  442. }
  443. if err := IsValidForAddUser(subject); err != nil {
  444. return nil, err
  445. }
  446. nonce, err := randutil.ASCII(32)
  447. if err != nil {
  448. return nil, errs.Wrap(http.StatusInternalServerError, err, "signSSHAddUser")
  449. }
  450. var serial uint64
  451. if err := binary.Read(rand.Reader, binary.BigEndian, &serial); err != nil {
  452. return nil, errs.Wrap(http.StatusInternalServerError, err, "signSSHAddUser: error reading random number")
  453. }
  454. signer := a.sshCAUserCertSignKey
  455. principal := subject.ValidPrincipals[0]
  456. addUserPrincipal := a.getAddUserPrincipal()
  457. cert := &ssh.Certificate{
  458. Nonce: []byte(nonce),
  459. Key: key,
  460. Serial: serial,
  461. CertType: ssh.UserCert,
  462. KeyId: principal + "-" + addUserPrincipal,
  463. ValidPrincipals: []string{addUserPrincipal},
  464. ValidAfter: subject.ValidAfter,
  465. ValidBefore: subject.ValidBefore,
  466. Permissions: ssh.Permissions{
  467. CriticalOptions: map[string]string{
  468. "force-command": a.getAddUserCommand(principal),
  469. },
  470. },
  471. SignatureKey: signer.PublicKey(),
  472. }
  473. // Get bytes for signing trailing the signature length.
  474. data := cert.Marshal()
  475. data = data[:len(data)-4]
  476. // Sign the certificate
  477. sig, err := signer.Sign(rand.Reader, data)
  478. if err != nil {
  479. return nil, err
  480. }
  481. cert.Signature = sig
  482. if err = a.storeRenewedSSHCertificate(subject, cert); err != nil && err != db.ErrNotImplemented {
  483. return nil, errs.Wrap(http.StatusInternalServerError, err, "signSSHAddUser: error storing certificate in db")
  484. }
  485. return cert, nil
  486. }
  487. // CheckSSHHost checks the given principal has been registered before.
  488. func (a *Authority) CheckSSHHost(ctx context.Context, principal, token string) (bool, error) {
  489. if a.sshCheckHostFunc != nil {
  490. exists, err := a.sshCheckHostFunc(ctx, principal, token, a.GetRootCertificates())
  491. if err != nil {
  492. return false, errs.Wrap(http.StatusInternalServerError, err,
  493. "checkSSHHost: error from injected checkSSHHost func")
  494. }
  495. return exists, nil
  496. }
  497. exists, err := a.db.IsSSHHost(principal)
  498. if err != nil {
  499. if err == db.ErrNotImplemented {
  500. return false, errs.Wrap(http.StatusNotImplemented, err,
  501. "checkSSHHost: isSSHHost is not implemented")
  502. }
  503. return false, errs.Wrap(http.StatusInternalServerError, err,
  504. "checkSSHHost: error checking if hosts exists")
  505. }
  506. return exists, nil
  507. }
  508. // GetSSHHosts returns a list of valid host principals.
  509. func (a *Authority) GetSSHHosts(ctx context.Context, cert *x509.Certificate) ([]config.Host, error) {
  510. if a.sshGetHostsFunc != nil {
  511. hosts, err := a.sshGetHostsFunc(ctx, cert)
  512. return hosts, errs.Wrap(http.StatusInternalServerError, err, "getSSHHosts")
  513. }
  514. hostnames, err := a.db.GetSSHHostPrincipals()
  515. if err != nil {
  516. return nil, errs.Wrap(http.StatusInternalServerError, err, "getSSHHosts")
  517. }
  518. hosts := make([]config.Host, len(hostnames))
  519. for i, hn := range hostnames {
  520. hosts[i] = config.Host{Hostname: hn}
  521. }
  522. return hosts, nil
  523. }
  524. func (a *Authority) getAddUserPrincipal() (cmd string) {
  525. if a.config.SSH.AddUserPrincipal == "" {
  526. return SSHAddUserPrincipal
  527. }
  528. return a.config.SSH.AddUserPrincipal
  529. }
  530. func (a *Authority) getAddUserCommand(principal string) string {
  531. var cmd string
  532. if a.config.SSH.AddUserCommand == "" {
  533. cmd = SSHAddUserCommand
  534. } else {
  535. cmd = a.config.SSH.AddUserCommand
  536. }
  537. return strings.ReplaceAll(cmd, "<principal>", principal)
  538. }