provisioners.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. package authority
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/x509"
  6. "encoding/json"
  7. "encoding/pem"
  8. "fmt"
  9. "os"
  10. "github.com/pkg/errors"
  11. "github.com/smallstep/certificates/authority/admin"
  12. "github.com/smallstep/certificates/authority/config"
  13. "github.com/smallstep/certificates/authority/provisioner"
  14. "github.com/smallstep/certificates/errs"
  15. "go.step.sm/cli-utils/step"
  16. "go.step.sm/cli-utils/ui"
  17. "go.step.sm/crypto/jose"
  18. "go.step.sm/linkedca"
  19. "gopkg.in/square/go-jose.v2/jwt"
  20. )
  21. // GetEncryptedKey returns the JWE key corresponding to the given kid argument.
  22. func (a *Authority) GetEncryptedKey(kid string) (string, error) {
  23. a.adminMutex.RLock()
  24. defer a.adminMutex.RUnlock()
  25. key, ok := a.provisioners.LoadEncryptedKey(kid)
  26. if !ok {
  27. return "", errs.NotFound("encrypted key with kid %s was not found", kid)
  28. }
  29. return key, nil
  30. }
  31. // GetProvisioners returns a map listing each provisioner and the JWK Key Set
  32. // with their public keys.
  33. func (a *Authority) GetProvisioners(cursor string, limit int) (provisioner.List, string, error) {
  34. a.adminMutex.RLock()
  35. defer a.adminMutex.RUnlock()
  36. provisioners, nextCursor := a.provisioners.Find(cursor, limit)
  37. return provisioners, nextCursor, nil
  38. }
  39. // LoadProvisionerByCertificate returns an interface to the provisioner that
  40. // provisioned the certificate.
  41. func (a *Authority) LoadProvisionerByCertificate(crt *x509.Certificate) (provisioner.Interface, error) {
  42. a.adminMutex.RLock()
  43. defer a.adminMutex.RUnlock()
  44. p, ok := a.provisioners.LoadByCertificate(crt)
  45. if !ok {
  46. return nil, admin.NewError(admin.ErrorNotFoundType, "unable to load provisioner from certificate")
  47. }
  48. return p, nil
  49. }
  50. // LoadProvisionerByToken returns an interface to the provisioner that
  51. // provisioned the token.
  52. func (a *Authority) LoadProvisionerByToken(token *jwt.JSONWebToken, claims *jwt.Claims) (provisioner.Interface, error) {
  53. a.adminMutex.RLock()
  54. defer a.adminMutex.RUnlock()
  55. p, ok := a.provisioners.LoadByToken(token, claims)
  56. if !ok {
  57. return nil, admin.NewError(admin.ErrorNotFoundType, "unable to load provisioner from token")
  58. }
  59. return p, nil
  60. }
  61. // LoadProvisionerByID returns an interface to the provisioner with the given ID.
  62. func (a *Authority) LoadProvisionerByID(id string) (provisioner.Interface, error) {
  63. a.adminMutex.RLock()
  64. defer a.adminMutex.RUnlock()
  65. p, ok := a.provisioners.Load(id)
  66. if !ok {
  67. return nil, admin.NewError(admin.ErrorNotFoundType, "provisioner %s not found", id)
  68. }
  69. return p, nil
  70. }
  71. // LoadProvisionerByName returns an interface to the provisioner with the given Name.
  72. func (a *Authority) LoadProvisionerByName(name string) (provisioner.Interface, error) {
  73. a.adminMutex.RLock()
  74. defer a.adminMutex.RUnlock()
  75. p, ok := a.provisioners.LoadByName(name)
  76. if !ok {
  77. return nil, admin.NewError(admin.ErrorNotFoundType, "provisioner %s not found", name)
  78. }
  79. return p, nil
  80. }
  81. func (a *Authority) generateProvisionerConfig(ctx context.Context) (*provisioner.Config, error) {
  82. // Merge global and configuration claims
  83. claimer, err := provisioner.NewClaimer(a.config.AuthorityConfig.Claims, config.GlobalProvisionerClaims)
  84. if err != nil {
  85. return nil, err
  86. }
  87. // TODO: should we also be combining the ssh federated roots here?
  88. // If we rotate ssh roots keys, sshpop provisioner will lose ability to
  89. // validate old SSH certificates, unless they are added as federated certs.
  90. sshKeys, err := a.GetSSHRoots(ctx)
  91. if err != nil {
  92. return nil, err
  93. }
  94. return &provisioner.Config{
  95. Claims: claimer.Claims(),
  96. Audiences: a.config.GetAudiences(),
  97. DB: a.db,
  98. SSHKeys: &provisioner.SSHKeys{
  99. UserKeys: sshKeys.UserKeys,
  100. HostKeys: sshKeys.HostKeys,
  101. },
  102. GetIdentityFunc: a.getIdentityFunc,
  103. }, nil
  104. }
  105. // StoreProvisioner stores an provisioner.Interface to the authority.
  106. func (a *Authority) StoreProvisioner(ctx context.Context, prov *linkedca.Provisioner) error {
  107. a.adminMutex.Lock()
  108. defer a.adminMutex.Unlock()
  109. certProv, err := ProvisionerToCertificates(prov)
  110. if err != nil {
  111. return admin.WrapErrorISE(err,
  112. "error converting to certificates provisioner from linkedca provisioner")
  113. }
  114. if _, ok := a.provisioners.LoadByName(prov.GetName()); ok {
  115. return admin.NewError(admin.ErrorBadRequestType,
  116. "provisioner with name %s already exists", prov.GetName())
  117. }
  118. if _, ok := a.provisioners.LoadByTokenID(certProv.GetIDForToken()); ok {
  119. return admin.NewError(admin.ErrorBadRequestType,
  120. "provisioner with token ID %s already exists", certProv.GetIDForToken())
  121. }
  122. // Store to database -- this will set the ID.
  123. if err := a.adminDB.CreateProvisioner(ctx, prov); err != nil {
  124. return admin.WrapErrorISE(err, "error creating admin")
  125. }
  126. // We need a new conversion that has the newly set ID.
  127. certProv, err = ProvisionerToCertificates(prov)
  128. if err != nil {
  129. return admin.WrapErrorISE(err,
  130. "error converting to certificates provisioner from linkedca provisioner")
  131. }
  132. provisionerConfig, err := a.generateProvisionerConfig(ctx)
  133. if err != nil {
  134. return admin.WrapErrorISE(err, "error generating provisioner config")
  135. }
  136. if err := certProv.Init(*provisionerConfig); err != nil {
  137. return admin.WrapErrorISE(err, "error initializing provisioner %s", prov.Name)
  138. }
  139. if err := a.provisioners.Store(certProv); err != nil {
  140. if err := a.reloadAdminResources(ctx); err != nil {
  141. return admin.WrapErrorISE(err, "error reloading admin resources on failed provisioner store")
  142. }
  143. return admin.WrapErrorISE(err, "error storing provisioner in authority cache")
  144. }
  145. return nil
  146. }
  147. // UpdateProvisioner stores an provisioner.Interface to the authority.
  148. func (a *Authority) UpdateProvisioner(ctx context.Context, nu *linkedca.Provisioner) error {
  149. a.adminMutex.Lock()
  150. defer a.adminMutex.Unlock()
  151. certProv, err := ProvisionerToCertificates(nu)
  152. if err != nil {
  153. return admin.WrapErrorISE(err,
  154. "error converting to certificates provisioner from linkedca provisioner")
  155. }
  156. provisionerConfig, err := a.generateProvisionerConfig(ctx)
  157. if err != nil {
  158. return admin.WrapErrorISE(err, "error generating provisioner config")
  159. }
  160. if err := certProv.Init(*provisionerConfig); err != nil {
  161. return admin.WrapErrorISE(err, "error initializing provisioner %s", nu.Name)
  162. }
  163. if err := a.provisioners.Update(certProv); err != nil {
  164. return admin.WrapErrorISE(err, "error updating provisioner '%s' in authority cache", nu.Name)
  165. }
  166. if err := a.adminDB.UpdateProvisioner(ctx, nu); err != nil {
  167. if err := a.reloadAdminResources(ctx); err != nil {
  168. return admin.WrapErrorISE(err, "error reloading admin resources on failed provisioner update")
  169. }
  170. return admin.WrapErrorISE(err, "error updating provisioner '%s'", nu.Name)
  171. }
  172. return nil
  173. }
  174. // RemoveProvisioner removes an provisioner.Interface from the authority.
  175. func (a *Authority) RemoveProvisioner(ctx context.Context, id string) error {
  176. a.adminMutex.Lock()
  177. defer a.adminMutex.Unlock()
  178. p, ok := a.provisioners.Load(id)
  179. if !ok {
  180. return admin.NewError(admin.ErrorBadRequestType,
  181. "provisioner %s not found", id)
  182. }
  183. provName, provID := p.GetName(), p.GetID()
  184. // Validate
  185. // - Check that there will be SUPER_ADMINs that remain after we
  186. // remove this provisioner.
  187. if a.admins.SuperCount() == a.admins.SuperCountByProvisioner(provName) {
  188. return admin.NewError(admin.ErrorBadRequestType,
  189. "cannot remove provisioner %s because no super admins will remain", provName)
  190. }
  191. // Delete all admins associated with the provisioner.
  192. admins, ok := a.admins.LoadByProvisioner(provName)
  193. if ok {
  194. for _, adm := range admins {
  195. if err := a.removeAdmin(ctx, adm.Id); err != nil {
  196. return admin.WrapErrorISE(err, "error deleting admin %s, as part of provisioner %s deletion", adm.Subject, provName)
  197. }
  198. }
  199. }
  200. // Remove provisioner from authority caches.
  201. if err := a.provisioners.Remove(provID); err != nil {
  202. return admin.WrapErrorISE(err, "error removing admin from authority cache")
  203. }
  204. // Remove provisioner from database.
  205. if err := a.adminDB.DeleteProvisioner(ctx, provID); err != nil {
  206. if err := a.reloadAdminResources(ctx); err != nil {
  207. return admin.WrapErrorISE(err, "error reloading admin resources on failed provisioner remove")
  208. }
  209. return admin.WrapErrorISE(err, "error deleting provisioner %s", provName)
  210. }
  211. return nil
  212. }
  213. // CreateFirstProvisioner creates and stores the first provisioner when using
  214. // admin database provisioner storage.
  215. func CreateFirstProvisioner(ctx context.Context, db admin.DB, password string) (*linkedca.Provisioner, error) {
  216. if password == "" {
  217. pass, err := ui.PromptPasswordGenerate("Please enter the password to encrypt your first provisioner, leave empty and we'll generate one")
  218. if err != nil {
  219. return nil, err
  220. }
  221. password = string(pass)
  222. }
  223. jwk, jwe, err := jose.GenerateDefaultKeyPair([]byte(password))
  224. if err != nil {
  225. return nil, admin.WrapErrorISE(err, "error generating JWK key pair")
  226. }
  227. jwkPubBytes, err := jwk.MarshalJSON()
  228. if err != nil {
  229. return nil, admin.WrapErrorISE(err, "error marshaling JWK")
  230. }
  231. jwePrivStr, err := jwe.CompactSerialize()
  232. if err != nil {
  233. return nil, admin.WrapErrorISE(err, "error serializing JWE")
  234. }
  235. p := &linkedca.Provisioner{
  236. Name: "Admin JWK",
  237. Type: linkedca.Provisioner_JWK,
  238. Details: &linkedca.ProvisionerDetails{
  239. Data: &linkedca.ProvisionerDetails_JWK{
  240. JWK: &linkedca.JWKProvisioner{
  241. PublicKey: jwkPubBytes,
  242. EncryptedPrivateKey: []byte(jwePrivStr),
  243. },
  244. },
  245. },
  246. Claims: &linkedca.Claims{
  247. X509: &linkedca.X509Claims{
  248. Enabled: true,
  249. Durations: &linkedca.Durations{
  250. Default: "5m",
  251. },
  252. },
  253. },
  254. }
  255. if err := db.CreateProvisioner(ctx, p); err != nil {
  256. return nil, admin.WrapErrorISE(err, "error creating provisioner")
  257. }
  258. return p, nil
  259. }
  260. // ValidateClaims validates the Claims type.
  261. func ValidateClaims(c *linkedca.Claims) error {
  262. if c == nil {
  263. return nil
  264. }
  265. if c.X509 != nil {
  266. if c.X509.Durations != nil {
  267. if err := ValidateDurations(c.X509.Durations); err != nil {
  268. return err
  269. }
  270. }
  271. }
  272. if c.Ssh != nil {
  273. if c.Ssh.UserDurations != nil {
  274. if err := ValidateDurations(c.Ssh.UserDurations); err != nil {
  275. return err
  276. }
  277. }
  278. if c.Ssh.HostDurations != nil {
  279. if err := ValidateDurations(c.Ssh.HostDurations); err != nil {
  280. return err
  281. }
  282. }
  283. }
  284. return nil
  285. }
  286. // ValidateDurations validates the Durations type.
  287. func ValidateDurations(d *linkedca.Durations) error {
  288. var (
  289. err error
  290. min, max, def *provisioner.Duration
  291. )
  292. if d.Min != "" {
  293. min, err = provisioner.NewDuration(d.Min)
  294. if err != nil {
  295. return admin.WrapError(admin.ErrorBadRequestType, err, "min duration '%s' is invalid", d.Min)
  296. }
  297. if min.Value() < 0 {
  298. return admin.WrapError(admin.ErrorBadRequestType, err, "min duration '%s' cannot be less than 0", d.Min)
  299. }
  300. }
  301. if d.Max != "" {
  302. max, err = provisioner.NewDuration(d.Max)
  303. if err != nil {
  304. return admin.WrapError(admin.ErrorBadRequestType, err, "max duration '%s' is invalid", d.Max)
  305. }
  306. if max.Value() < 0 {
  307. return admin.WrapError(admin.ErrorBadRequestType, err, "max duration '%s' cannot be less than 0", d.Max)
  308. }
  309. }
  310. if d.Default != "" {
  311. def, err = provisioner.NewDuration(d.Default)
  312. if err != nil {
  313. return admin.WrapError(admin.ErrorBadRequestType, err, "default duration '%s' is invalid", d.Default)
  314. }
  315. if def.Value() < 0 {
  316. return admin.WrapError(admin.ErrorBadRequestType, err, "default duration '%s' cannot be less than 0", d.Default)
  317. }
  318. }
  319. if d.Min != "" && d.Max != "" && min.Value() > max.Value() {
  320. return admin.NewError(admin.ErrorBadRequestType,
  321. "min duration '%s' cannot be greater than max duration '%s'", d.Min, d.Max)
  322. }
  323. if d.Min != "" && d.Default != "" && min.Value() > def.Value() {
  324. return admin.NewError(admin.ErrorBadRequestType,
  325. "min duration '%s' cannot be greater than default duration '%s'", d.Min, d.Default)
  326. }
  327. if d.Default != "" && d.Max != "" && min.Value() > def.Value() {
  328. return admin.NewError(admin.ErrorBadRequestType,
  329. "default duration '%s' cannot be greater than max duration '%s'", d.Default, d.Max)
  330. }
  331. return nil
  332. }
  333. func provisionerListToCertificates(l []*linkedca.Provisioner) (provisioner.List, error) {
  334. var nu provisioner.List
  335. for _, p := range l {
  336. certProv, err := ProvisionerToCertificates(p)
  337. if err != nil {
  338. return nil, err
  339. }
  340. nu = append(nu, certProv)
  341. }
  342. return nu, nil
  343. }
  344. func optionsToCertificates(p *linkedca.Provisioner) *provisioner.Options {
  345. ops := &provisioner.Options{
  346. X509: &provisioner.X509Options{},
  347. SSH: &provisioner.SSHOptions{},
  348. }
  349. if p.X509Template != nil {
  350. ops.X509.Template = string(p.X509Template.Template)
  351. ops.X509.TemplateData = p.X509Template.Data
  352. }
  353. if p.SshTemplate != nil {
  354. ops.SSH.Template = string(p.SshTemplate.Template)
  355. ops.SSH.TemplateData = p.SshTemplate.Data
  356. }
  357. return ops
  358. }
  359. func durationsToCertificates(d *linkedca.Durations) (min, max, def *provisioner.Duration, err error) {
  360. if len(d.Min) > 0 {
  361. min, err = provisioner.NewDuration(d.Min)
  362. if err != nil {
  363. return nil, nil, nil, admin.WrapErrorISE(err, "error parsing minimum duration '%s'", d.Min)
  364. }
  365. }
  366. if len(d.Max) > 0 {
  367. max, err = provisioner.NewDuration(d.Max)
  368. if err != nil {
  369. return nil, nil, nil, admin.WrapErrorISE(err, "error parsing maximum duration '%s'", d.Max)
  370. }
  371. }
  372. if len(d.Default) > 0 {
  373. def, err = provisioner.NewDuration(d.Default)
  374. if err != nil {
  375. return nil, nil, nil, admin.WrapErrorISE(err, "error parsing default duration '%s'", d.Default)
  376. }
  377. }
  378. return
  379. }
  380. func durationsToLinkedca(d *provisioner.Duration) string {
  381. if d == nil {
  382. return ""
  383. }
  384. return d.Duration.String()
  385. }
  386. // claimsToCertificates converts the linkedca provisioner claims type to the
  387. // certifictes claims type.
  388. func claimsToCertificates(c *linkedca.Claims) (*provisioner.Claims, error) {
  389. if c == nil {
  390. return nil, nil
  391. }
  392. pc := &provisioner.Claims{
  393. DisableRenewal: &c.DisableRenewal,
  394. }
  395. var err error
  396. if xc := c.X509; xc != nil {
  397. if d := xc.Durations; d != nil {
  398. pc.MinTLSDur, pc.MaxTLSDur, pc.DefaultTLSDur, err = durationsToCertificates(d)
  399. if err != nil {
  400. return nil, err
  401. }
  402. }
  403. }
  404. if sc := c.Ssh; sc != nil {
  405. pc.EnableSSHCA = &sc.Enabled
  406. if d := sc.UserDurations; d != nil {
  407. pc.MinUserSSHDur, pc.MaxUserSSHDur, pc.DefaultUserSSHDur, err = durationsToCertificates(d)
  408. if err != nil {
  409. return nil, err
  410. }
  411. }
  412. if d := sc.HostDurations; d != nil {
  413. pc.MinHostSSHDur, pc.MaxHostSSHDur, pc.DefaultHostSSHDur, err = durationsToCertificates(d)
  414. if err != nil {
  415. return nil, err
  416. }
  417. }
  418. }
  419. return pc, nil
  420. }
  421. func claimsToLinkedca(c *provisioner.Claims) *linkedca.Claims {
  422. if c == nil {
  423. return nil
  424. }
  425. disableRenewal := config.DefaultDisableRenewal
  426. if c.DisableRenewal != nil {
  427. disableRenewal = *c.DisableRenewal
  428. }
  429. lc := &linkedca.Claims{
  430. DisableRenewal: disableRenewal,
  431. }
  432. if c.DefaultTLSDur != nil || c.MinTLSDur != nil || c.MaxTLSDur != nil {
  433. lc.X509 = &linkedca.X509Claims{
  434. Enabled: true,
  435. Durations: &linkedca.Durations{
  436. Default: durationsToLinkedca(c.DefaultTLSDur),
  437. Min: durationsToLinkedca(c.MinTLSDur),
  438. Max: durationsToLinkedca(c.MaxTLSDur),
  439. },
  440. }
  441. }
  442. if c.EnableSSHCA != nil && *c.EnableSSHCA {
  443. lc.Ssh = &linkedca.SSHClaims{
  444. Enabled: true,
  445. }
  446. if c.DefaultUserSSHDur != nil || c.MinUserSSHDur != nil || c.MaxUserSSHDur != nil {
  447. lc.Ssh.UserDurations = &linkedca.Durations{
  448. Default: durationsToLinkedca(c.DefaultUserSSHDur),
  449. Min: durationsToLinkedca(c.MinUserSSHDur),
  450. Max: durationsToLinkedca(c.MaxUserSSHDur),
  451. }
  452. }
  453. if c.DefaultHostSSHDur != nil || c.MinHostSSHDur != nil || c.MaxHostSSHDur != nil {
  454. lc.Ssh.HostDurations = &linkedca.Durations{
  455. Default: durationsToLinkedca(c.DefaultHostSSHDur),
  456. Min: durationsToLinkedca(c.MinHostSSHDur),
  457. Max: durationsToLinkedca(c.MaxHostSSHDur),
  458. }
  459. }
  460. }
  461. return lc
  462. }
  463. func provisionerOptionsToLinkedca(p *provisioner.Options) (*linkedca.Template, *linkedca.Template, error) {
  464. var err error
  465. var x509Template, sshTemplate *linkedca.Template
  466. if p == nil {
  467. return nil, nil, nil
  468. }
  469. if p.X509 != nil && p.X509.HasTemplate() {
  470. x509Template = &linkedca.Template{
  471. Template: nil,
  472. Data: nil,
  473. }
  474. if p.X509.Template != "" {
  475. x509Template.Template = []byte(p.SSH.Template)
  476. } else if p.X509.TemplateFile != "" {
  477. filename := step.Abs(p.X509.TemplateFile)
  478. if x509Template.Template, err = os.ReadFile(filename); err != nil {
  479. return nil, nil, errors.Wrap(err, "error reading x509 template")
  480. }
  481. }
  482. }
  483. if p.SSH != nil && p.SSH.HasTemplate() {
  484. sshTemplate = &linkedca.Template{
  485. Template: nil,
  486. Data: nil,
  487. }
  488. if p.SSH.Template != "" {
  489. sshTemplate.Template = []byte(p.SSH.Template)
  490. } else if p.SSH.TemplateFile != "" {
  491. filename := step.Abs(p.SSH.TemplateFile)
  492. if sshTemplate.Template, err = os.ReadFile(filename); err != nil {
  493. return nil, nil, errors.Wrap(err, "error reading ssh template")
  494. }
  495. }
  496. }
  497. return x509Template, sshTemplate, nil
  498. }
  499. func provisionerPEMToLinkedca(b []byte) [][]byte {
  500. var roots [][]byte
  501. var block *pem.Block
  502. for {
  503. if block, b = pem.Decode(b); block == nil {
  504. break
  505. }
  506. roots = append(roots, pem.EncodeToMemory(block))
  507. }
  508. return roots
  509. }
  510. // ProvisionerToCertificates converts the linkedca provisioner type to the certificates provisioner
  511. // interface.
  512. func ProvisionerToCertificates(p *linkedca.Provisioner) (provisioner.Interface, error) {
  513. claims, err := claimsToCertificates(p.Claims)
  514. if err != nil {
  515. return nil, err
  516. }
  517. details := p.Details.GetData()
  518. if details == nil {
  519. return nil, errors.New("provisioner does not have any details")
  520. }
  521. options := optionsToCertificates(p)
  522. switch d := details.(type) {
  523. case *linkedca.ProvisionerDetails_JWK:
  524. jwk := new(jose.JSONWebKey)
  525. if err := json.Unmarshal(d.JWK.PublicKey, &jwk); err != nil {
  526. return nil, errors.Wrap(err, "error unmarshaling public key")
  527. }
  528. return &provisioner.JWK{
  529. ID: p.Id,
  530. Type: p.Type.String(),
  531. Name: p.Name,
  532. Key: jwk,
  533. EncryptedKey: string(d.JWK.EncryptedPrivateKey),
  534. Claims: claims,
  535. Options: options,
  536. }, nil
  537. case *linkedca.ProvisionerDetails_X5C:
  538. var roots []byte
  539. for i, root := range d.X5C.GetRoots() {
  540. if i > 0 {
  541. roots = append(roots, '\n')
  542. }
  543. roots = append(roots, root...)
  544. }
  545. return &provisioner.X5C{
  546. ID: p.Id,
  547. Type: p.Type.String(),
  548. Name: p.Name,
  549. Roots: roots,
  550. Claims: claims,
  551. Options: options,
  552. }, nil
  553. case *linkedca.ProvisionerDetails_K8SSA:
  554. var publicKeys []byte
  555. for i, k := range d.K8SSA.GetPublicKeys() {
  556. if i > 0 {
  557. publicKeys = append(publicKeys, '\n')
  558. }
  559. publicKeys = append(publicKeys, k...)
  560. }
  561. return &provisioner.K8sSA{
  562. ID: p.Id,
  563. Type: p.Type.String(),
  564. Name: p.Name,
  565. PubKeys: publicKeys,
  566. Claims: claims,
  567. Options: options,
  568. }, nil
  569. case *linkedca.ProvisionerDetails_SSHPOP:
  570. return &provisioner.SSHPOP{
  571. ID: p.Id,
  572. Type: p.Type.String(),
  573. Name: p.Name,
  574. Claims: claims,
  575. }, nil
  576. case *linkedca.ProvisionerDetails_ACME:
  577. cfg := d.ACME
  578. return &provisioner.ACME{
  579. ID: p.Id,
  580. Type: p.Type.String(),
  581. Name: p.Name,
  582. ForceCN: cfg.ForceCn,
  583. RequireEAB: cfg.RequireEab,
  584. Claims: claims,
  585. Options: options,
  586. }, nil
  587. case *linkedca.ProvisionerDetails_OIDC:
  588. cfg := d.OIDC
  589. return &provisioner.OIDC{
  590. ID: p.Id,
  591. Type: p.Type.String(),
  592. Name: p.Name,
  593. TenantID: cfg.TenantId,
  594. ClientID: cfg.ClientId,
  595. ClientSecret: cfg.ClientSecret,
  596. ConfigurationEndpoint: cfg.ConfigurationEndpoint,
  597. Admins: cfg.Admins,
  598. Domains: cfg.Domains,
  599. Groups: cfg.Groups,
  600. ListenAddress: cfg.ListenAddress,
  601. Claims: claims,
  602. Options: options,
  603. }, nil
  604. case *linkedca.ProvisionerDetails_AWS:
  605. cfg := d.AWS
  606. instanceAge, err := parseInstanceAge(cfg.InstanceAge)
  607. if err != nil {
  608. return nil, err
  609. }
  610. return &provisioner.AWS{
  611. ID: p.Id,
  612. Type: p.Type.String(),
  613. Name: p.Name,
  614. Accounts: cfg.Accounts,
  615. DisableCustomSANs: cfg.DisableCustomSans,
  616. DisableTrustOnFirstUse: cfg.DisableTrustOnFirstUse,
  617. InstanceAge: instanceAge,
  618. Claims: claims,
  619. Options: options,
  620. }, nil
  621. case *linkedca.ProvisionerDetails_GCP:
  622. cfg := d.GCP
  623. instanceAge, err := parseInstanceAge(cfg.InstanceAge)
  624. if err != nil {
  625. return nil, err
  626. }
  627. return &provisioner.GCP{
  628. ID: p.Id,
  629. Type: p.Type.String(),
  630. Name: p.Name,
  631. ServiceAccounts: cfg.ServiceAccounts,
  632. ProjectIDs: cfg.ProjectIds,
  633. DisableCustomSANs: cfg.DisableCustomSans,
  634. DisableTrustOnFirstUse: cfg.DisableTrustOnFirstUse,
  635. InstanceAge: instanceAge,
  636. Claims: claims,
  637. Options: options,
  638. }, nil
  639. case *linkedca.ProvisionerDetails_Azure:
  640. cfg := d.Azure
  641. return &provisioner.Azure{
  642. ID: p.Id,
  643. Type: p.Type.String(),
  644. Name: p.Name,
  645. TenantID: cfg.TenantId,
  646. ResourceGroups: cfg.ResourceGroups,
  647. Audience: cfg.Audience,
  648. DisableCustomSANs: cfg.DisableCustomSans,
  649. DisableTrustOnFirstUse: cfg.DisableTrustOnFirstUse,
  650. Claims: claims,
  651. Options: options,
  652. }, nil
  653. case *linkedca.ProvisionerDetails_Nebula:
  654. var roots []byte
  655. for i, root := range d.Nebula.GetRoots() {
  656. if i > 0 && !bytes.HasSuffix(root, []byte{'\n'}) {
  657. roots = append(roots, '\n')
  658. }
  659. roots = append(roots, root...)
  660. }
  661. return &provisioner.Nebula{
  662. ID: p.Id,
  663. Type: p.Type.String(),
  664. Name: p.Name,
  665. Roots: roots,
  666. Claims: claims,
  667. Options: options,
  668. }, nil
  669. default:
  670. return nil, fmt.Errorf("provisioner %s not implemented", p.Type)
  671. }
  672. }
  673. // ProvisionerToLinkedca converts a provisioner.Interface to a
  674. // linkedca.Provisioner type.
  675. func ProvisionerToLinkedca(p provisioner.Interface) (*linkedca.Provisioner, error) {
  676. switch p := p.(type) {
  677. case *provisioner.JWK:
  678. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  679. if err != nil {
  680. return nil, err
  681. }
  682. publicKey, err := json.Marshal(p.Key)
  683. if err != nil {
  684. return nil, errors.Wrap(err, "error marshaling key")
  685. }
  686. return &linkedca.Provisioner{
  687. Id: p.ID,
  688. Type: linkedca.Provisioner_JWK,
  689. Name: p.GetName(),
  690. Details: &linkedca.ProvisionerDetails{
  691. Data: &linkedca.ProvisionerDetails_JWK{
  692. JWK: &linkedca.JWKProvisioner{
  693. PublicKey: publicKey,
  694. EncryptedPrivateKey: []byte(p.EncryptedKey),
  695. },
  696. },
  697. },
  698. Claims: claimsToLinkedca(p.Claims),
  699. X509Template: x509Template,
  700. SshTemplate: sshTemplate,
  701. }, nil
  702. case *provisioner.OIDC:
  703. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  704. if err != nil {
  705. return nil, err
  706. }
  707. return &linkedca.Provisioner{
  708. Id: p.ID,
  709. Type: linkedca.Provisioner_OIDC,
  710. Name: p.GetName(),
  711. Details: &linkedca.ProvisionerDetails{
  712. Data: &linkedca.ProvisionerDetails_OIDC{
  713. OIDC: &linkedca.OIDCProvisioner{
  714. ClientId: p.ClientID,
  715. ClientSecret: p.ClientSecret,
  716. ConfigurationEndpoint: p.ConfigurationEndpoint,
  717. Admins: p.Admins,
  718. Domains: p.Domains,
  719. Groups: p.Groups,
  720. ListenAddress: p.ListenAddress,
  721. TenantId: p.TenantID,
  722. },
  723. },
  724. },
  725. Claims: claimsToLinkedca(p.Claims),
  726. X509Template: x509Template,
  727. SshTemplate: sshTemplate,
  728. }, nil
  729. case *provisioner.GCP:
  730. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  731. if err != nil {
  732. return nil, err
  733. }
  734. return &linkedca.Provisioner{
  735. Id: p.ID,
  736. Type: linkedca.Provisioner_GCP,
  737. Name: p.GetName(),
  738. Details: &linkedca.ProvisionerDetails{
  739. Data: &linkedca.ProvisionerDetails_GCP{
  740. GCP: &linkedca.GCPProvisioner{
  741. ServiceAccounts: p.ServiceAccounts,
  742. ProjectIds: p.ProjectIDs,
  743. DisableCustomSans: p.DisableCustomSANs,
  744. DisableTrustOnFirstUse: p.DisableTrustOnFirstUse,
  745. InstanceAge: p.InstanceAge.String(),
  746. },
  747. },
  748. },
  749. Claims: claimsToLinkedca(p.Claims),
  750. X509Template: x509Template,
  751. SshTemplate: sshTemplate,
  752. }, nil
  753. case *provisioner.AWS:
  754. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  755. if err != nil {
  756. return nil, err
  757. }
  758. return &linkedca.Provisioner{
  759. Id: p.ID,
  760. Type: linkedca.Provisioner_AWS,
  761. Name: p.GetName(),
  762. Details: &linkedca.ProvisionerDetails{
  763. Data: &linkedca.ProvisionerDetails_AWS{
  764. AWS: &linkedca.AWSProvisioner{
  765. Accounts: p.Accounts,
  766. DisableCustomSans: p.DisableCustomSANs,
  767. DisableTrustOnFirstUse: p.DisableTrustOnFirstUse,
  768. InstanceAge: p.InstanceAge.String(),
  769. },
  770. },
  771. },
  772. Claims: claimsToLinkedca(p.Claims),
  773. X509Template: x509Template,
  774. SshTemplate: sshTemplate,
  775. }, nil
  776. case *provisioner.Azure:
  777. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  778. if err != nil {
  779. return nil, err
  780. }
  781. return &linkedca.Provisioner{
  782. Id: p.ID,
  783. Type: linkedca.Provisioner_AZURE,
  784. Name: p.GetName(),
  785. Details: &linkedca.ProvisionerDetails{
  786. Data: &linkedca.ProvisionerDetails_Azure{
  787. Azure: &linkedca.AzureProvisioner{
  788. TenantId: p.TenantID,
  789. ResourceGroups: p.ResourceGroups,
  790. Audience: p.Audience,
  791. DisableCustomSans: p.DisableCustomSANs,
  792. DisableTrustOnFirstUse: p.DisableTrustOnFirstUse,
  793. },
  794. },
  795. },
  796. Claims: claimsToLinkedca(p.Claims),
  797. X509Template: x509Template,
  798. SshTemplate: sshTemplate,
  799. }, nil
  800. case *provisioner.ACME:
  801. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  802. if err != nil {
  803. return nil, err
  804. }
  805. return &linkedca.Provisioner{
  806. Id: p.ID,
  807. Type: linkedca.Provisioner_ACME,
  808. Name: p.GetName(),
  809. Details: &linkedca.ProvisionerDetails{
  810. Data: &linkedca.ProvisionerDetails_ACME{
  811. ACME: &linkedca.ACMEProvisioner{
  812. ForceCn: p.ForceCN,
  813. },
  814. },
  815. },
  816. Claims: claimsToLinkedca(p.Claims),
  817. X509Template: x509Template,
  818. SshTemplate: sshTemplate,
  819. }, nil
  820. case *provisioner.X5C:
  821. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  822. if err != nil {
  823. return nil, err
  824. }
  825. return &linkedca.Provisioner{
  826. Id: p.ID,
  827. Type: linkedca.Provisioner_X5C,
  828. Name: p.GetName(),
  829. Details: &linkedca.ProvisionerDetails{
  830. Data: &linkedca.ProvisionerDetails_X5C{
  831. X5C: &linkedca.X5CProvisioner{
  832. Roots: provisionerPEMToLinkedca(p.Roots),
  833. },
  834. },
  835. },
  836. Claims: claimsToLinkedca(p.Claims),
  837. X509Template: x509Template,
  838. SshTemplate: sshTemplate,
  839. }, nil
  840. case *provisioner.K8sSA:
  841. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  842. if err != nil {
  843. return nil, err
  844. }
  845. return &linkedca.Provisioner{
  846. Id: p.ID,
  847. Type: linkedca.Provisioner_K8SSA,
  848. Name: p.GetName(),
  849. Details: &linkedca.ProvisionerDetails{
  850. Data: &linkedca.ProvisionerDetails_K8SSA{
  851. K8SSA: &linkedca.K8SSAProvisioner{
  852. PublicKeys: provisionerPEMToLinkedca(p.PubKeys),
  853. },
  854. },
  855. },
  856. Claims: claimsToLinkedca(p.Claims),
  857. X509Template: x509Template,
  858. SshTemplate: sshTemplate,
  859. }, nil
  860. case *provisioner.SSHPOP:
  861. return &linkedca.Provisioner{
  862. Id: p.ID,
  863. Type: linkedca.Provisioner_SSHPOP,
  864. Name: p.GetName(),
  865. Details: &linkedca.ProvisionerDetails{
  866. Data: &linkedca.ProvisionerDetails_SSHPOP{
  867. SSHPOP: &linkedca.SSHPOPProvisioner{},
  868. },
  869. },
  870. Claims: claimsToLinkedca(p.Claims),
  871. }, nil
  872. case *provisioner.SCEP:
  873. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  874. if err != nil {
  875. return nil, err
  876. }
  877. return &linkedca.Provisioner{
  878. Id: p.ID,
  879. Type: linkedca.Provisioner_SCEP,
  880. Name: p.GetName(),
  881. Details: &linkedca.ProvisionerDetails{
  882. Data: &linkedca.ProvisionerDetails_SCEP{
  883. SCEP: &linkedca.SCEPProvisioner{
  884. ForceCn: p.ForceCN,
  885. Challenge: p.GetChallengePassword(),
  886. Capabilities: p.Capabilities,
  887. MinimumPublicKeyLength: int32(p.MinimumPublicKeyLength),
  888. },
  889. },
  890. },
  891. Claims: claimsToLinkedca(p.Claims),
  892. X509Template: x509Template,
  893. SshTemplate: sshTemplate,
  894. }, nil
  895. case *provisioner.Nebula:
  896. x509Template, sshTemplate, err := provisionerOptionsToLinkedca(p.Options)
  897. if err != nil {
  898. return nil, err
  899. }
  900. return &linkedca.Provisioner{
  901. Id: p.ID,
  902. Type: linkedca.Provisioner_NEBULA,
  903. Name: p.GetName(),
  904. Details: &linkedca.ProvisionerDetails{
  905. Data: &linkedca.ProvisionerDetails_Nebula{
  906. Nebula: &linkedca.NebulaProvisioner{
  907. Roots: provisionerPEMToLinkedca(p.Roots),
  908. },
  909. },
  910. },
  911. Claims: claimsToLinkedca(p.Claims),
  912. X509Template: x509Template,
  913. SshTemplate: sshTemplate,
  914. }, nil
  915. default:
  916. return nil, fmt.Errorf("provisioner %s not implemented", p.GetType())
  917. }
  918. }
  919. func parseInstanceAge(age string) (provisioner.Duration, error) {
  920. var instanceAge provisioner.Duration
  921. if age != "" {
  922. iap, err := provisioner.NewDuration(age)
  923. if err != nil {
  924. return instanceAge, err
  925. }
  926. instanceAge = *iap
  927. }
  928. return instanceAge, nil
  929. }