ssh.go 20 KB

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