provisioners.go 28 KB

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