ssh.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. package authority
  2. import (
  3. "context"
  4. "crypto/rand"
  5. "encoding/binary"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "github.com/pkg/errors"
  10. "github.com/smallstep/certificates/authority/provisioner"
  11. "github.com/smallstep/certificates/db"
  12. "github.com/smallstep/certificates/templates"
  13. "github.com/smallstep/cli/crypto/randutil"
  14. "github.com/smallstep/cli/jose"
  15. "golang.org/x/crypto/ssh"
  16. )
  17. const (
  18. // SSHAddUserPrincipal is the principal that will run the add user command.
  19. // Defaults to "provisioner" but it can be changed in the configuration.
  20. SSHAddUserPrincipal = "provisioner"
  21. // SSHAddUserCommand is the default command to run to add a new user.
  22. // Defaults to "sudo useradd -m <principal>; nc -q0 localhost 22" but it can be changed in the
  23. // configuration. The string "<principal>" will be replace by the new
  24. // principal to add.
  25. SSHAddUserCommand = "sudo useradd -m <principal>; nc -q0 localhost 22"
  26. )
  27. // SSHConfig contains the user and host keys.
  28. type SSHConfig struct {
  29. HostKey string `json:"hostKey"`
  30. UserKey string `json:"userKey"`
  31. Keys []*SSHPublicKey `json:"keys,omitempty"`
  32. AddUserPrincipal string `json:"addUserPrincipal"`
  33. AddUserCommand string `json:"addUserCommand"`
  34. }
  35. // Validate checks the fields in SSHConfig.
  36. func (c *SSHConfig) Validate() error {
  37. if c == nil {
  38. return nil
  39. }
  40. for _, k := range c.Keys {
  41. if err := k.Validate(); err != nil {
  42. return err
  43. }
  44. }
  45. return nil
  46. }
  47. // SSHPublicKey contains a public key used by federated CAs to keep old signing
  48. // keys for this ca.
  49. type SSHPublicKey struct {
  50. Type string `json:"type"`
  51. Federated bool `json:"federated"`
  52. Key jose.JSONWebKey `json:"key"`
  53. publicKey ssh.PublicKey
  54. }
  55. // Validate checks the fields in SSHPublicKey.
  56. func (k *SSHPublicKey) Validate() error {
  57. switch {
  58. case k.Type == "":
  59. return errors.New("type cannot be empty")
  60. case k.Type != provisioner.SSHHostCert && k.Type != provisioner.SSHUserCert:
  61. return errors.Errorf("invalid type %s, it must be user or host", k.Type)
  62. case !k.Key.IsPublic():
  63. return errors.New("invalid key type, it must be a public key")
  64. }
  65. key, err := ssh.NewPublicKey(k.Key.Key)
  66. if err != nil {
  67. return errors.Wrap(err, "error creating ssh key")
  68. }
  69. k.publicKey = key
  70. return nil
  71. }
  72. // PublicKey returns the ssh public key.
  73. func (k *SSHPublicKey) PublicKey() ssh.PublicKey {
  74. return k.publicKey
  75. }
  76. // SSHKeys represents the SSH User and Host public keys.
  77. type SSHKeys struct {
  78. UserKeys []ssh.PublicKey
  79. HostKeys []ssh.PublicKey
  80. }
  81. // GetSSHRoots returns the SSH User and Host public keys.
  82. func (a *Authority) GetSSHRoots() (*SSHKeys, error) {
  83. return &SSHKeys{
  84. HostKeys: a.sshCAHostCerts,
  85. UserKeys: a.sshCAUserCerts,
  86. }, nil
  87. }
  88. // GetSSHFederation returns the public keys for federated SSH signers.
  89. func (a *Authority) GetSSHFederation() (*SSHKeys, error) {
  90. return &SSHKeys{
  91. HostKeys: a.sshCAHostFederatedCerts,
  92. UserKeys: a.sshCAUserFederatedCerts,
  93. }, nil
  94. }
  95. // GetSSHConfig returns rendered templates for clients (user) or servers (host).
  96. func (a *Authority) GetSSHConfig(typ string, data map[string]string) ([]templates.Output, error) {
  97. if a.sshCAUserCertSignKey == nil && a.sshCAHostCertSignKey == nil {
  98. return nil, &apiError{
  99. err: errors.New("getSSHConfig: ssh is not configured"),
  100. code: http.StatusNotFound,
  101. }
  102. }
  103. var ts []templates.Template
  104. switch typ {
  105. case provisioner.SSHUserCert:
  106. if a.config.Templates != nil && a.config.Templates.SSH != nil {
  107. ts = a.config.Templates.SSH.User
  108. }
  109. case provisioner.SSHHostCert:
  110. if a.config.Templates != nil && a.config.Templates.SSH != nil {
  111. ts = a.config.Templates.SSH.Host
  112. }
  113. default:
  114. return nil, &apiError{
  115. err: errors.Errorf("getSSHConfig: type %s is not valid", typ),
  116. code: http.StatusBadRequest,
  117. }
  118. }
  119. // Merge user and default data
  120. var mergedData map[string]interface{}
  121. if len(data) == 0 {
  122. mergedData = a.config.Templates.Data
  123. } else {
  124. mergedData = make(map[string]interface{}, len(a.config.Templates.Data)+1)
  125. mergedData["User"] = data
  126. for k, v := range a.config.Templates.Data {
  127. mergedData[k] = v
  128. }
  129. }
  130. // Render templates
  131. output := []templates.Output{}
  132. for _, t := range ts {
  133. o, err := t.Output(mergedData)
  134. if err != nil {
  135. return nil, err
  136. }
  137. output = append(output, o)
  138. }
  139. return output, nil
  140. }
  141. // authorizeSSHSign loads the provisioner from the token, checks that it has not
  142. // been used again and calls the provisioner AuthorizeSSHSign method. Returns a
  143. // list of methods to apply to the signing flow.
  144. func (a *Authority) authorizeSSHSign(ctx context.Context, ott string) ([]provisioner.SignOption, error) {
  145. var errContext = apiCtx{"ott": ott}
  146. p, err := a.authorizeToken(ott)
  147. if err != nil {
  148. return nil, &apiError{errors.Wrap(err, "authorizeSSHSign"), http.StatusUnauthorized, errContext}
  149. }
  150. opts, err := p.AuthorizeSSHSign(ctx, ott)
  151. if err != nil {
  152. return nil, &apiError{errors.Wrap(err, "authorizeSSHSign"), http.StatusUnauthorized, errContext}
  153. }
  154. return opts, nil
  155. }
  156. // SignSSH creates a signed SSH certificate with the given public key and options.
  157. func (a *Authority) SignSSH(key ssh.PublicKey, opts provisioner.SSHOptions, signOpts ...provisioner.SignOption) (*ssh.Certificate, error) {
  158. var mods []provisioner.SSHCertificateModifier
  159. var validators []provisioner.SSHCertificateValidator
  160. for _, op := range signOpts {
  161. switch o := op.(type) {
  162. // modify the ssh.Certificate
  163. case provisioner.SSHCertificateModifier:
  164. mods = append(mods, o)
  165. // modify the ssh.Certificate given the SSHOptions
  166. case provisioner.SSHCertificateOptionModifier:
  167. mods = append(mods, o.Option(opts))
  168. // validate the ssh.Certificate
  169. case provisioner.SSHCertificateValidator:
  170. validators = append(validators, o)
  171. // validate the given SSHOptions
  172. case provisioner.SSHCertificateOptionsValidator:
  173. if err := o.Valid(opts); err != nil {
  174. return nil, &apiError{err: err, code: http.StatusForbidden}
  175. }
  176. default:
  177. return nil, &apiError{
  178. err: errors.Errorf("signSSH: invalid extra option type %T", o),
  179. code: http.StatusInternalServerError,
  180. }
  181. }
  182. }
  183. nonce, err := randutil.ASCII(32)
  184. if err != nil {
  185. return nil, &apiError{err: err, code: http.StatusInternalServerError}
  186. }
  187. var serial uint64
  188. if err := binary.Read(rand.Reader, binary.BigEndian, &serial); err != nil {
  189. return nil, &apiError{
  190. err: errors.Wrap(err, "signSSH: error reading random number"),
  191. code: http.StatusInternalServerError,
  192. }
  193. }
  194. // Build base certificate with the key and some random values
  195. cert := &ssh.Certificate{
  196. Nonce: []byte(nonce),
  197. Key: key,
  198. Serial: serial,
  199. }
  200. // Use opts to modify the certificate
  201. if err := opts.Modify(cert); err != nil {
  202. return nil, &apiError{err: err, code: http.StatusForbidden}
  203. }
  204. // Use provisioner modifiers
  205. for _, m := range mods {
  206. if err := m.Modify(cert); err != nil {
  207. return nil, &apiError{err: err, code: http.StatusForbidden}
  208. }
  209. }
  210. // Get signer from authority keys
  211. var signer ssh.Signer
  212. switch cert.CertType {
  213. case ssh.UserCert:
  214. if a.sshCAUserCertSignKey == nil {
  215. return nil, &apiError{
  216. err: errors.New("signSSH: user certificate signing is not enabled"),
  217. code: http.StatusNotImplemented,
  218. }
  219. }
  220. signer = a.sshCAUserCertSignKey
  221. case ssh.HostCert:
  222. if a.sshCAHostCertSignKey == nil {
  223. return nil, &apiError{
  224. err: errors.New("signSSH: host certificate signing is not enabled"),
  225. code: http.StatusNotImplemented,
  226. }
  227. }
  228. signer = a.sshCAHostCertSignKey
  229. default:
  230. return nil, &apiError{
  231. err: errors.Errorf("signSSH: unexpected ssh certificate type: %d", cert.CertType),
  232. code: http.StatusInternalServerError,
  233. }
  234. }
  235. cert.SignatureKey = signer.PublicKey()
  236. // Get bytes for signing trailing the signature length.
  237. data := cert.Marshal()
  238. data = data[:len(data)-4]
  239. // Sign the certificate
  240. sig, err := signer.Sign(rand.Reader, data)
  241. if err != nil {
  242. return nil, &apiError{
  243. err: errors.Wrap(err, "signSSH: error signing certificate"),
  244. code: http.StatusInternalServerError,
  245. }
  246. }
  247. cert.Signature = sig
  248. // User provisioners validators
  249. for _, v := range validators {
  250. if err := v.Valid(cert); err != nil {
  251. return nil, &apiError{err: err, code: http.StatusForbidden}
  252. }
  253. }
  254. if err = a.db.StoreSSHCertificate(cert); err != nil && err != db.ErrNotImplemented {
  255. return nil, &apiError{
  256. err: errors.Wrap(err, "signSSH: error storing certificate in db"),
  257. code: http.StatusInternalServerError,
  258. }
  259. }
  260. return cert, nil
  261. }
  262. // authorizeSSHRenew authorizes an SSH certificate renewal request, by
  263. // validating the contents of an SSHPOP token.
  264. func (a *Authority) authorizeSSHRenew(ctx context.Context, token string) (*ssh.Certificate, error) {
  265. errContext := map[string]interface{}{"ott": token}
  266. p, err := a.authorizeToken(token)
  267. if err != nil {
  268. return nil, &apiError{
  269. err: errors.Wrap(err, "authorizeSSHRenew"),
  270. code: http.StatusUnauthorized,
  271. context: errContext,
  272. }
  273. }
  274. cert, err := p.AuthorizeSSHRenew(ctx, token)
  275. if err != nil {
  276. return nil, &apiError{
  277. err: errors.Wrap(err, "authorizeSSHRenew"),
  278. code: http.StatusUnauthorized,
  279. context: errContext,
  280. }
  281. }
  282. return cert, nil
  283. }
  284. // RenewSSH creates a signed SSH certificate using the old SSH certificate as a template.
  285. func (a *Authority) RenewSSH(oldCert *ssh.Certificate) (*ssh.Certificate, error) {
  286. nonce, err := randutil.ASCII(32)
  287. if err != nil {
  288. return nil, &apiError{err: err, code: http.StatusInternalServerError}
  289. }
  290. var serial uint64
  291. if err := binary.Read(rand.Reader, binary.BigEndian, &serial); err != nil {
  292. return nil, &apiError{
  293. err: errors.Wrap(err, "renewSSH: error reading random number"),
  294. code: http.StatusInternalServerError,
  295. }
  296. }
  297. if oldCert.ValidAfter == 0 || oldCert.ValidBefore == 0 {
  298. return nil, errors.New("rewnewSSh: cannot renew certificate without validity period")
  299. }
  300. dur := time.Duration(oldCert.ValidBefore-oldCert.ValidAfter) * time.Second
  301. va := time.Now()
  302. vb := va.Add(dur)
  303. // Build base certificate with the key and some random values
  304. cert := &ssh.Certificate{
  305. Nonce: []byte(nonce),
  306. Key: oldCert.Key,
  307. Serial: serial,
  308. CertType: oldCert.CertType,
  309. KeyId: oldCert.KeyId,
  310. ValidPrincipals: oldCert.ValidPrincipals,
  311. Permissions: oldCert.Permissions,
  312. ValidAfter: uint64(va.Unix()),
  313. ValidBefore: uint64(vb.Unix()),
  314. }
  315. // Get signer from authority keys
  316. var signer ssh.Signer
  317. switch cert.CertType {
  318. case ssh.UserCert:
  319. if a.sshCAUserCertSignKey == nil {
  320. return nil, &apiError{
  321. err: errors.New("renewSSH: user certificate signing is not enabled"),
  322. code: http.StatusNotImplemented,
  323. }
  324. }
  325. signer = a.sshCAUserCertSignKey
  326. case ssh.HostCert:
  327. if a.sshCAHostCertSignKey == nil {
  328. return nil, &apiError{
  329. err: errors.New("renewSSH: host certificate signing is not enabled"),
  330. code: http.StatusNotImplemented,
  331. }
  332. }
  333. signer = a.sshCAHostCertSignKey
  334. default:
  335. return nil, &apiError{
  336. err: errors.Errorf("renewSSH: unexpected ssh certificate type: %d", cert.CertType),
  337. code: http.StatusInternalServerError,
  338. }
  339. }
  340. cert.SignatureKey = signer.PublicKey()
  341. // Get bytes for signing trailing the signature length.
  342. data := cert.Marshal()
  343. data = data[:len(data)-4]
  344. // Sign the certificate
  345. sig, err := signer.Sign(rand.Reader, data)
  346. if err != nil {
  347. return nil, &apiError{
  348. err: errors.Wrap(err, "renewSSH: error signing certificate"),
  349. code: http.StatusInternalServerError,
  350. }
  351. }
  352. cert.Signature = sig
  353. if err = a.db.StoreSSHCertificate(cert); err != nil && err != db.ErrNotImplemented {
  354. return nil, &apiError{
  355. err: errors.Wrap(err, "renewSSH: error storing certificate in db"),
  356. code: http.StatusInternalServerError,
  357. }
  358. }
  359. return cert, nil
  360. }
  361. // authorizeSSHRekey authorizes an SSH certificate rekey request, by
  362. // validating the contents of an SSHPOP token.
  363. func (a *Authority) authorizeSSHRekey(ctx context.Context, token string) (*ssh.Certificate, []provisioner.SignOption, error) {
  364. errContext := map[string]interface{}{"ott": token}
  365. p, err := a.authorizeToken(token)
  366. if err != nil {
  367. return nil, nil, &apiError{
  368. err: errors.Wrap(err, "authorizeSSHRenew"),
  369. code: http.StatusUnauthorized,
  370. context: errContext,
  371. }
  372. }
  373. cert, opts, err := p.AuthorizeSSHRekey(ctx, token)
  374. if err != nil {
  375. return nil, nil, &apiError{
  376. err: errors.Wrap(err, "authorizeSSHRekey"),
  377. code: http.StatusUnauthorized,
  378. context: errContext,
  379. }
  380. }
  381. return cert, opts, nil
  382. }
  383. // RekeySSH creates a signed SSH certificate using the old SSH certificate as a template.
  384. func (a *Authority) RekeySSH(oldCert *ssh.Certificate, pub ssh.PublicKey, signOpts ...provisioner.SignOption) (*ssh.Certificate, error) {
  385. var validators []provisioner.SSHCertificateValidator
  386. for _, op := range signOpts {
  387. switch o := op.(type) {
  388. // validate the ssh.Certificate
  389. case provisioner.SSHCertificateValidator:
  390. validators = append(validators, o)
  391. default:
  392. return nil, &apiError{
  393. err: errors.Errorf("rekeySSH: invalid extra option type %T", o),
  394. code: http.StatusInternalServerError,
  395. }
  396. }
  397. }
  398. nonce, err := randutil.ASCII(32)
  399. if err != nil {
  400. return nil, &apiError{err: err, code: http.StatusInternalServerError}
  401. }
  402. var serial uint64
  403. if err := binary.Read(rand.Reader, binary.BigEndian, &serial); err != nil {
  404. return nil, &apiError{
  405. err: errors.Wrap(err, "rekeySSH: error reading random number"),
  406. code: http.StatusInternalServerError,
  407. }
  408. }
  409. if oldCert.ValidAfter == 0 || oldCert.ValidBefore == 0 {
  410. return nil, errors.New("rekeySSh: cannot rekey certificate without validity period")
  411. }
  412. dur := time.Duration(oldCert.ValidBefore-oldCert.ValidAfter) * time.Second
  413. va := time.Now()
  414. vb := va.Add(dur)
  415. // Build base certificate with the key and some random values
  416. cert := &ssh.Certificate{
  417. Nonce: []byte(nonce),
  418. Key: pub,
  419. Serial: serial,
  420. CertType: oldCert.CertType,
  421. KeyId: oldCert.KeyId,
  422. ValidPrincipals: oldCert.ValidPrincipals,
  423. Permissions: oldCert.Permissions,
  424. ValidAfter: uint64(va.Unix()),
  425. ValidBefore: uint64(vb.Unix()),
  426. }
  427. // Get signer from authority keys
  428. var signer ssh.Signer
  429. switch cert.CertType {
  430. case ssh.UserCert:
  431. if a.sshCAUserCertSignKey == nil {
  432. return nil, &apiError{
  433. err: errors.New("rekeySSH: user certificate signing is not enabled"),
  434. code: http.StatusNotImplemented,
  435. }
  436. }
  437. signer = a.sshCAUserCertSignKey
  438. case ssh.HostCert:
  439. if a.sshCAHostCertSignKey == nil {
  440. return nil, &apiError{
  441. err: errors.New("rekeySSH: host certificate signing is not enabled"),
  442. code: http.StatusNotImplemented,
  443. }
  444. }
  445. signer = a.sshCAHostCertSignKey
  446. default:
  447. return nil, &apiError{
  448. err: errors.Errorf("rekeySSH: unexpected ssh certificate type: %d", cert.CertType),
  449. code: http.StatusInternalServerError,
  450. }
  451. }
  452. cert.SignatureKey = signer.PublicKey()
  453. // Get bytes for signing trailing the signature length.
  454. data := cert.Marshal()
  455. data = data[:len(data)-4]
  456. // Sign the certificate
  457. sig, err := signer.Sign(rand.Reader, data)
  458. if err != nil {
  459. return nil, &apiError{
  460. err: errors.Wrap(err, "rekeySSH: error signing certificate"),
  461. code: http.StatusInternalServerError,
  462. }
  463. }
  464. cert.Signature = sig
  465. // User provisioners validators
  466. for _, v := range validators {
  467. if err := v.Valid(cert); err != nil {
  468. return nil, &apiError{err: err, code: http.StatusForbidden}
  469. }
  470. }
  471. if err = a.db.StoreSSHCertificate(cert); err != nil && err != db.ErrNotImplemented {
  472. return nil, &apiError{
  473. err: errors.Wrap(err, "rekeySSH: error storing certificate in db"),
  474. code: http.StatusInternalServerError,
  475. }
  476. }
  477. return cert, nil
  478. }
  479. // authorizeSSHRevoke authorizes an SSH certificate revoke request, by
  480. // validating the contents of an SSHPOP token.
  481. func (a *Authority) authorizeSSHRevoke(ctx context.Context, token string) error {
  482. errContext := map[string]interface{}{"ott": token}
  483. p, err := a.authorizeToken(token)
  484. if err != nil {
  485. return &apiError{errors.Wrap(err, "authorizeSSHRevoke"), http.StatusUnauthorized, errContext}
  486. }
  487. if err = p.AuthorizeSSHRevoke(ctx, token); err != nil {
  488. return &apiError{errors.Wrap(err, "authorizeSSHRevoke"), http.StatusUnauthorized, errContext}
  489. }
  490. return nil
  491. }
  492. // SignSSHAddUser signs a certificate that provisions a new user in a server.
  493. func (a *Authority) SignSSHAddUser(key ssh.PublicKey, subject *ssh.Certificate) (*ssh.Certificate, error) {
  494. if a.sshCAUserCertSignKey == nil {
  495. return nil, &apiError{
  496. err: errors.New("signSSHAddUser: user certificate signing is not enabled"),
  497. code: http.StatusNotImplemented,
  498. }
  499. }
  500. if subject.CertType != ssh.UserCert {
  501. return nil, &apiError{
  502. err: errors.New("signSSHAddUser: certificate is not a user certificate"),
  503. code: http.StatusForbidden,
  504. }
  505. }
  506. if len(subject.ValidPrincipals) != 1 {
  507. return nil, &apiError{
  508. err: errors.New("signSSHAddUser: certificate does not have only one principal"),
  509. code: http.StatusForbidden,
  510. }
  511. }
  512. nonce, err := randutil.ASCII(32)
  513. if err != nil {
  514. return nil, &apiError{err: err, code: http.StatusInternalServerError}
  515. }
  516. var serial uint64
  517. if err := binary.Read(rand.Reader, binary.BigEndian, &serial); err != nil {
  518. return nil, &apiError{
  519. err: errors.Wrap(err, "signSSHAddUser: error reading random number"),
  520. code: http.StatusInternalServerError,
  521. }
  522. }
  523. signer := a.sshCAUserCertSignKey
  524. principal := subject.ValidPrincipals[0]
  525. addUserPrincipal := a.getAddUserPrincipal()
  526. cert := &ssh.Certificate{
  527. Nonce: []byte(nonce),
  528. Key: key,
  529. Serial: serial,
  530. CertType: ssh.UserCert,
  531. KeyId: principal + "-" + addUserPrincipal,
  532. ValidPrincipals: []string{addUserPrincipal},
  533. ValidAfter: subject.ValidAfter,
  534. ValidBefore: subject.ValidBefore,
  535. Permissions: ssh.Permissions{
  536. CriticalOptions: map[string]string{
  537. "force-command": a.getAddUserCommand(principal),
  538. },
  539. },
  540. SignatureKey: signer.PublicKey(),
  541. }
  542. // Get bytes for signing trailing the signature length.
  543. data := cert.Marshal()
  544. data = data[:len(data)-4]
  545. // Sign the certificate
  546. sig, err := signer.Sign(rand.Reader, data)
  547. if err != nil {
  548. return nil, err
  549. }
  550. cert.Signature = sig
  551. if err = a.db.StoreSSHCertificate(cert); err != nil && err != db.ErrNotImplemented {
  552. return nil, &apiError{
  553. err: errors.Wrap(err, "signSSHAddUser: error storing certificate in db"),
  554. code: http.StatusInternalServerError,
  555. }
  556. }
  557. return cert, nil
  558. }
  559. // CheckSSHHost checks the given principal has been registered before.
  560. func (a *Authority) CheckSSHHost(principal string) (bool, error) {
  561. exists, err := a.db.IsSSHHost(principal)
  562. if err != nil {
  563. if err == db.ErrNotImplemented {
  564. return false, &apiError{
  565. err: errors.Wrap(err, "checkSSHHost: isSSHHost is not implemented"),
  566. code: http.StatusNotImplemented,
  567. }
  568. }
  569. return false, &apiError{
  570. err: errors.Wrap(err, "checkSSHHost: error checking if hosts exists"),
  571. code: http.StatusInternalServerError,
  572. }
  573. }
  574. return exists, nil
  575. }
  576. // GetSSHHosts returns a list of valid host principals.
  577. func (a *Authority) GetSSHHosts() ([]string, error) {
  578. ps, err := a.db.GetSSHHostPrincipals()
  579. if err != nil {
  580. return nil, err
  581. }
  582. return ps, nil
  583. }
  584. func (a *Authority) getAddUserPrincipal() (cmd string) {
  585. if a.config.SSH.AddUserPrincipal == "" {
  586. return SSHAddUserPrincipal
  587. }
  588. return a.config.SSH.AddUserPrincipal
  589. }
  590. func (a *Authority) getAddUserCommand(principal string) string {
  591. var cmd string
  592. if a.config.SSH.AddUserCommand == "" {
  593. cmd = SSHAddUserCommand
  594. } else {
  595. cmd = a.config.SSH.AddUserCommand
  596. }
  597. return strings.Replace(cmd, "<principal>", principal, -1)
  598. }