ssh.go 19 KB

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