provisioner.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. package provisioner
  2. import (
  3. "context"
  4. "crypto/x509"
  5. "encoding/json"
  6. stderrors "errors"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "github.com/pkg/errors"
  11. kmsapi "go.step.sm/crypto/kms/apiv1"
  12. "golang.org/x/crypto/ssh"
  13. "github.com/smallstep/certificates/errs"
  14. )
  15. // Interface is the interface that all provisioner types must implement.
  16. type Interface interface {
  17. GetID() string
  18. GetIDForToken() string
  19. GetTokenID(token string) (string, error)
  20. GetName() string
  21. GetType() Type
  22. GetEncryptedKey() (kid string, key string, ok bool)
  23. Init(config Config) error
  24. AuthorizeSign(ctx context.Context, token string) ([]SignOption, error)
  25. AuthorizeRevoke(ctx context.Context, token string) error
  26. AuthorizeRenew(ctx context.Context, cert *x509.Certificate) error
  27. AuthorizeSSHSign(ctx context.Context, token string) ([]SignOption, error)
  28. AuthorizeSSHRevoke(ctx context.Context, token string) error
  29. AuthorizeSSHRenew(ctx context.Context, token string) (*ssh.Certificate, error)
  30. AuthorizeSSHRekey(ctx context.Context, token string) (*ssh.Certificate, []SignOption, error)
  31. }
  32. // Uninitialized represents a disabled provisioner. Uninitialized provisioners
  33. // are created when the Init methods fails.
  34. type Uninitialized struct {
  35. Interface
  36. Reason error
  37. }
  38. // MarshalJSON returns the JSON encoding of the provisioner with the disabled
  39. // reason.
  40. func (p Uninitialized) MarshalJSON() ([]byte, error) {
  41. provisionerJSON, err := json.Marshal(p.Interface)
  42. if err != nil {
  43. return nil, err
  44. }
  45. reasonJSON, err := json.Marshal(struct {
  46. State string `json:"state"`
  47. StateReason string `json:"stateReason"`
  48. }{"Uninitialized", p.Reason.Error()})
  49. if err != nil {
  50. return nil, err
  51. }
  52. reasonJSON[0] = ','
  53. return append(provisionerJSON[:len(provisionerJSON)-1], reasonJSON...), nil
  54. }
  55. // ErrAllowTokenReuse is an error that is returned by provisioners that allows
  56. // the reuse of tokens.
  57. //
  58. // This is, for example, returned by the Azure provisioner when
  59. // DisableTrustOnFirstUse is set to true. Azure caches tokens for up to 24hr and
  60. // has no mechanism for getting a different token - this can be an issue when
  61. // rebooting a VM. In contrast, AWS and GCP have facilities for requesting a new
  62. // token. Therefore, for the Azure provisioner we are enabling token reuse, with
  63. // the understanding that we are not following security best practices
  64. var ErrAllowTokenReuse = stderrors.New("allow token reuse")
  65. // Audiences stores all supported audiences by request type.
  66. type Audiences struct {
  67. Sign []string
  68. Renew []string
  69. Revoke []string
  70. SSHSign []string
  71. SSHRevoke []string
  72. SSHRenew []string
  73. SSHRekey []string
  74. }
  75. // All returns all supported audiences across all request types in one list.
  76. func (a Audiences) All() (auds []string) {
  77. auds = a.Sign
  78. auds = append(auds, a.Renew...)
  79. auds = append(auds, a.Revoke...)
  80. auds = append(auds, a.SSHSign...)
  81. auds = append(auds, a.SSHRevoke...)
  82. auds = append(auds, a.SSHRenew...)
  83. auds = append(auds, a.SSHRekey...)
  84. return
  85. }
  86. // WithFragment returns a copy of audiences where the url audiences contains the
  87. // given fragment.
  88. func (a Audiences) WithFragment(fragment string) Audiences {
  89. ret := Audiences{
  90. Sign: make([]string, len(a.Sign)),
  91. Renew: make([]string, len(a.Renew)),
  92. Revoke: make([]string, len(a.Revoke)),
  93. SSHSign: make([]string, len(a.SSHSign)),
  94. SSHRevoke: make([]string, len(a.SSHRevoke)),
  95. SSHRenew: make([]string, len(a.SSHRenew)),
  96. SSHRekey: make([]string, len(a.SSHRekey)),
  97. }
  98. for i, s := range a.Sign {
  99. if u, err := url.Parse(s); err == nil {
  100. ret.Sign[i] = u.ResolveReference(&url.URL{Fragment: fragment}).String()
  101. } else {
  102. ret.Sign[i] = s
  103. }
  104. }
  105. for i, s := range a.Renew {
  106. if u, err := url.Parse(s); err == nil {
  107. ret.Renew[i] = u.ResolveReference(&url.URL{Fragment: fragment}).String()
  108. } else {
  109. ret.Renew[i] = s
  110. }
  111. }
  112. for i, s := range a.Revoke {
  113. if u, err := url.Parse(s); err == nil {
  114. ret.Revoke[i] = u.ResolveReference(&url.URL{Fragment: fragment}).String()
  115. } else {
  116. ret.Revoke[i] = s
  117. }
  118. }
  119. for i, s := range a.SSHSign {
  120. if u, err := url.Parse(s); err == nil {
  121. ret.SSHSign[i] = u.ResolveReference(&url.URL{Fragment: fragment}).String()
  122. } else {
  123. ret.SSHSign[i] = s
  124. }
  125. }
  126. for i, s := range a.SSHRevoke {
  127. if u, err := url.Parse(s); err == nil {
  128. ret.SSHRevoke[i] = u.ResolveReference(&url.URL{Fragment: fragment}).String()
  129. } else {
  130. ret.SSHRevoke[i] = s
  131. }
  132. }
  133. for i, s := range a.SSHRenew {
  134. if u, err := url.Parse(s); err == nil {
  135. ret.SSHRenew[i] = u.ResolveReference(&url.URL{Fragment: fragment}).String()
  136. } else {
  137. ret.SSHRenew[i] = s
  138. }
  139. }
  140. for i, s := range a.SSHRekey {
  141. if u, err := url.Parse(s); err == nil {
  142. ret.SSHRekey[i] = u.ResolveReference(&url.URL{Fragment: fragment}).String()
  143. } else {
  144. ret.SSHRekey[i] = s
  145. }
  146. }
  147. return ret
  148. }
  149. // generateSignAudience generates a sign audience with the format
  150. // https://<host>/1.0/sign#provisionerID
  151. func generateSignAudience(caURL, provisionerID string) (string, error) {
  152. u, err := url.Parse(caURL)
  153. if err != nil {
  154. return "", errors.Wrapf(err, "error parsing %s", caURL)
  155. }
  156. return u.ResolveReference(&url.URL{Path: "/1.0/sign", Fragment: provisionerID}).String(), nil
  157. }
  158. // Type indicates the provisioner Type.
  159. type Type int
  160. const (
  161. noopType Type = 0
  162. // TypeJWK is used to indicate the JWK provisioners.
  163. TypeJWK Type = 1
  164. // TypeOIDC is used to indicate the OIDC provisioners.
  165. TypeOIDC Type = 2
  166. // TypeGCP is used to indicate the GCP provisioners.
  167. TypeGCP Type = 3
  168. // TypeAWS is used to indicate the AWS provisioners.
  169. TypeAWS Type = 4
  170. // TypeAzure is used to indicate the Azure provisioners.
  171. TypeAzure Type = 5
  172. // TypeACME is used to indicate the ACME provisioners.
  173. TypeACME Type = 6
  174. // TypeX5C is used to indicate the X5C provisioners.
  175. TypeX5C Type = 7
  176. // TypeK8sSA is used to indicate the X5C provisioners.
  177. TypeK8sSA Type = 8
  178. // TypeSSHPOP is used to indicate the SSHPOP provisioners.
  179. TypeSSHPOP Type = 9
  180. // TypeSCEP is used to indicate the SCEP provisioners
  181. TypeSCEP Type = 10
  182. // TypeNebula is used to indicate the Nebula provisioners
  183. TypeNebula Type = 11
  184. )
  185. // String returns the string representation of the type.
  186. func (t Type) String() string {
  187. switch t {
  188. case TypeJWK:
  189. return "JWK"
  190. case TypeOIDC:
  191. return "OIDC"
  192. case TypeGCP:
  193. return "GCP"
  194. case TypeAWS:
  195. return "AWS"
  196. case TypeAzure:
  197. return "Azure"
  198. case TypeACME:
  199. return "ACME"
  200. case TypeX5C:
  201. return "X5C"
  202. case TypeK8sSA:
  203. return "K8sSA"
  204. case TypeSSHPOP:
  205. return "SSHPOP"
  206. case TypeSCEP:
  207. return "SCEP"
  208. case TypeNebula:
  209. return "Nebula"
  210. default:
  211. return ""
  212. }
  213. }
  214. // SSHKeys represents the SSH User and Host public keys.
  215. type SSHKeys struct {
  216. UserKeys []ssh.PublicKey
  217. HostKeys []ssh.PublicKey
  218. }
  219. // SCEPKeyManager is a KMS interface that combines a KeyManager with a
  220. // Decrypter.
  221. type SCEPKeyManager interface {
  222. kmsapi.KeyManager
  223. kmsapi.Decrypter
  224. }
  225. // Config defines the default parameters used in the initialization of
  226. // provisioners.
  227. type Config struct {
  228. // Claims are the default claims.
  229. Claims Claims
  230. // Audiences are the audiences used in the default provisioner, (JWK).
  231. Audiences Audiences
  232. // SSHKeys are the root SSH public keys.
  233. SSHKeys *SSHKeys
  234. // GetIdentityFunc is a function that returns an identity that will be
  235. // used by the provisioner to populate certificate attributes.
  236. GetIdentityFunc GetIdentityFunc
  237. // AuthorizeRenewFunc is a function that returns nil if a given X.509
  238. // certificate can be renewed.
  239. AuthorizeRenewFunc AuthorizeRenewFunc
  240. // AuthorizeSSHRenewFunc is a function that returns nil if a given SSH
  241. // certificate can be renewed.
  242. AuthorizeSSHRenewFunc AuthorizeSSHRenewFunc
  243. // WebhookClient is an HTTP client used when performing webhook requests.
  244. WebhookClient *http.Client
  245. // SCEPKeyManager, if defined, is the interface used by SCEP provisioners.
  246. SCEPKeyManager SCEPKeyManager
  247. // HTTPClient is an HTTP client that trusts the system cert pool and the CA
  248. // roots.
  249. HTTPClient *http.Client
  250. // WrapTransport references the function that should wrap any [http.Transport] initialized
  251. // down the Config's chain.
  252. WrapTransport TransportWrapper
  253. }
  254. type provisioner struct {
  255. Type string `json:"type"`
  256. }
  257. // List represents a list of provisioners.
  258. type List []Interface
  259. // UnmarshalJSON implements json.Unmarshaler and allows to unmarshal a list of a
  260. // interfaces into the right type.
  261. func (l *List) UnmarshalJSON(data []byte) error {
  262. ps := []json.RawMessage{}
  263. if err := json.Unmarshal(data, &ps); err != nil {
  264. return errors.Wrap(err, "error unmarshaling provisioner list")
  265. }
  266. *l = List{}
  267. for _, data := range ps {
  268. var typ provisioner
  269. if err := json.Unmarshal(data, &typ); err != nil {
  270. return errors.Errorf("error unmarshaling provisioner")
  271. }
  272. var p Interface
  273. switch strings.ToLower(typ.Type) {
  274. case "jwk":
  275. p = &JWK{}
  276. case "oidc":
  277. p = &OIDC{}
  278. case "gcp":
  279. p = &GCP{}
  280. case "aws":
  281. p = &AWS{}
  282. case "azure":
  283. p = &Azure{}
  284. case "acme":
  285. p = &ACME{}
  286. case "x5c":
  287. p = &X5C{}
  288. case "k8ssa":
  289. p = &K8sSA{}
  290. case "sshpop":
  291. p = &SSHPOP{}
  292. case "scep":
  293. p = &SCEP{}
  294. case "nebula":
  295. p = &Nebula{}
  296. default:
  297. // Skip unsupported provisioners. A client using this method may be
  298. // compiled with a version of smallstep/certificates that does not
  299. // support a specific provisioner type. If we don't skip unknown
  300. // provisioners, a client encountering an unknown provisioner will
  301. // break. Rather than break the client, we skip the provisioner.
  302. // TODO: accept a pluggable logger (depending on client) that can
  303. // warn the user that an unknown provisioner was found and suggest
  304. // that the user update their client's dependency on
  305. // step/certificates and recompile.
  306. continue
  307. }
  308. if err := json.Unmarshal(data, p); err != nil {
  309. return errors.Wrap(err, "error unmarshaling provisioner")
  310. }
  311. *l = append(*l, p)
  312. }
  313. return nil
  314. }
  315. type base struct{}
  316. // AuthorizeSign returns an unimplemented error. Provisioners should overwrite
  317. // this method if they will support authorizing tokens for signing x509 Certificates.
  318. func (b *base) AuthorizeSign(context.Context, string) ([]SignOption, error) {
  319. return nil, errs.Unauthorized("provisioner.AuthorizeSign not implemented")
  320. }
  321. // AuthorizeRevoke returns an unimplemented error. Provisioners should overwrite
  322. // this method if they will support authorizing tokens for revoking x509 Certificates.
  323. func (b *base) AuthorizeRevoke(context.Context, string) error {
  324. return errs.Unauthorized("provisioner.AuthorizeRevoke not implemented")
  325. }
  326. // AuthorizeRenew returns an unimplemented error. Provisioners should overwrite
  327. // this method if they will support authorizing tokens for renewing x509 Certificates.
  328. func (b *base) AuthorizeRenew(context.Context, *x509.Certificate) error {
  329. return errs.Unauthorized("provisioner.AuthorizeRenew not implemented")
  330. }
  331. // AuthorizeSSHSign returns an unimplemented error. Provisioners should overwrite
  332. // this method if they will support authorizing tokens for signing SSH Certificates.
  333. func (b *base) AuthorizeSSHSign(context.Context, string) ([]SignOption, error) {
  334. return nil, errs.Unauthorized("provisioner.AuthorizeSSHSign not implemented")
  335. }
  336. // AuthorizeSSHRevoke returns an unimplemented error. Provisioners should overwrite
  337. // this method if they will support authorizing tokens for revoking SSH Certificates.
  338. func (b *base) AuthorizeSSHRevoke(context.Context, string) error {
  339. return errs.Unauthorized("provisioner.AuthorizeSSHRevoke not implemented")
  340. }
  341. // AuthorizeSSHRenew returns an unimplemented error. Provisioners should overwrite
  342. // this method if they will support authorizing tokens for renewing SSH Certificates.
  343. func (b *base) AuthorizeSSHRenew(context.Context, string) (*ssh.Certificate, error) {
  344. return nil, errs.Unauthorized("provisioner.AuthorizeSSHRenew not implemented")
  345. }
  346. // AuthorizeSSHRekey returns an unimplemented error. Provisioners should overwrite
  347. // this method if they will support authorizing tokens for rekeying SSH Certificates.
  348. func (b *base) AuthorizeSSHRekey(context.Context, string) (*ssh.Certificate, []SignOption, error) {
  349. return nil, nil, errs.Unauthorized("provisioner.AuthorizeSSHRekey not implemented")
  350. }
  351. // Permissions defines extra extensions and critical options to grant to an SSH certificate.
  352. type Permissions struct {
  353. Extensions map[string]string `json:"extensions"`
  354. CriticalOptions map[string]string `json:"criticalOptions"`
  355. }
  356. // RAInfo is the information about a provisioner present in RA tokens generated
  357. // by StepCAS.
  358. type RAInfo struct {
  359. AuthorityID string `json:"authorityId,omitempty"`
  360. EndpointID string `json:"endpointId,omitempty"`
  361. ProvisionerID string `json:"provisionerId,omitempty"`
  362. ProvisionerType string `json:"provisionerType,omitempty"`
  363. ProvisionerName string `json:"provisionerName,omitempty"`
  364. }
  365. // raProvisioner wraps a provisioner with RA data.
  366. type raProvisioner struct {
  367. Interface
  368. raInfo *RAInfo
  369. }
  370. // RAInfo returns the RAInfo in the wrapped provisioner.
  371. func (p *raProvisioner) RAInfo() *RAInfo {
  372. return p.raInfo
  373. }
  374. // MockProvisioner for testing
  375. type MockProvisioner struct {
  376. Mret1, Mret2, Mret3 interface{}
  377. Merr error
  378. MgetID func() string
  379. MgetIDForToken func() string
  380. MgetTokenID func(string) (string, error)
  381. MgetName func() string
  382. MgetType func() Type
  383. MgetEncryptedKey func() (string, string, bool)
  384. Minit func(Config) error
  385. MauthorizeSign func(ctx context.Context, ott string) ([]SignOption, error)
  386. MauthorizeRenew func(ctx context.Context, cert *x509.Certificate) error
  387. MauthorizeRevoke func(ctx context.Context, ott string) error
  388. MauthorizeSSHSign func(ctx context.Context, ott string) ([]SignOption, error)
  389. MauthorizeSSHRenew func(ctx context.Context, ott string) (*ssh.Certificate, error)
  390. MauthorizeSSHRekey func(ctx context.Context, ott string) (*ssh.Certificate, []SignOption, error)
  391. MauthorizeSSHRevoke func(ctx context.Context, ott string) error
  392. }
  393. // GetID mock
  394. func (m *MockProvisioner) GetID() string {
  395. if m.MgetID != nil {
  396. return m.MgetID()
  397. }
  398. return m.Mret1.(string)
  399. }
  400. // GetIDForToken mock
  401. func (m *MockProvisioner) GetIDForToken() string {
  402. if m.MgetIDForToken != nil {
  403. return m.MgetIDForToken()
  404. }
  405. return m.Mret1.(string)
  406. }
  407. // GetTokenID mock
  408. func (m *MockProvisioner) GetTokenID(token string) (string, error) {
  409. if m.MgetTokenID != nil {
  410. return m.MgetTokenID(token)
  411. }
  412. if m.Mret1 == nil {
  413. return "", m.Merr
  414. }
  415. return m.Mret1.(string), m.Merr
  416. }
  417. // GetName mock
  418. func (m *MockProvisioner) GetName() string {
  419. if m.MgetName != nil {
  420. return m.MgetName()
  421. }
  422. return m.Mret1.(string)
  423. }
  424. // GetType mock
  425. func (m *MockProvisioner) GetType() Type {
  426. if m.MgetType != nil {
  427. return m.MgetType()
  428. }
  429. return m.Mret1.(Type)
  430. }
  431. // GetEncryptedKey mock
  432. func (m *MockProvisioner) GetEncryptedKey() (string, string, bool) {
  433. if m.MgetEncryptedKey != nil {
  434. return m.MgetEncryptedKey()
  435. }
  436. return m.Mret1.(string), m.Mret2.(string), m.Mret3.(bool)
  437. }
  438. // Init mock
  439. func (m *MockProvisioner) Init(c Config) error {
  440. if m.Minit != nil {
  441. return m.Minit(c)
  442. }
  443. return m.Merr
  444. }
  445. // AuthorizeSign mock
  446. func (m *MockProvisioner) AuthorizeSign(ctx context.Context, ott string) ([]SignOption, error) {
  447. if m.MauthorizeSign != nil {
  448. return m.MauthorizeSign(ctx, ott)
  449. }
  450. return m.Mret1.([]SignOption), m.Merr
  451. }
  452. // AuthorizeRevoke mock
  453. func (m *MockProvisioner) AuthorizeRevoke(ctx context.Context, ott string) error {
  454. if m.MauthorizeRevoke != nil {
  455. return m.MauthorizeRevoke(ctx, ott)
  456. }
  457. return m.Merr
  458. }
  459. // AuthorizeRenew mock
  460. func (m *MockProvisioner) AuthorizeRenew(ctx context.Context, c *x509.Certificate) error {
  461. if m.MauthorizeRenew != nil {
  462. return m.MauthorizeRenew(ctx, c)
  463. }
  464. return m.Merr
  465. }
  466. // AuthorizeSSHSign mock
  467. func (m *MockProvisioner) AuthorizeSSHSign(ctx context.Context, ott string) ([]SignOption, error) {
  468. if m.MauthorizeSign != nil {
  469. return m.MauthorizeSign(ctx, ott)
  470. }
  471. return m.Mret1.([]SignOption), m.Merr
  472. }
  473. // AuthorizeSSHRenew mock
  474. func (m *MockProvisioner) AuthorizeSSHRenew(ctx context.Context, ott string) (*ssh.Certificate, error) {
  475. if m.MauthorizeRenew != nil {
  476. return m.MauthorizeSSHRenew(ctx, ott)
  477. }
  478. return m.Mret1.(*ssh.Certificate), m.Merr
  479. }
  480. // AuthorizeSSHRekey mock
  481. func (m *MockProvisioner) AuthorizeSSHRekey(ctx context.Context, ott string) (*ssh.Certificate, []SignOption, error) {
  482. if m.MauthorizeSSHRekey != nil {
  483. return m.MauthorizeSSHRekey(ctx, ott)
  484. }
  485. return m.Mret1.(*ssh.Certificate), m.Mret2.([]SignOption), m.Merr
  486. }
  487. // AuthorizeSSHRevoke mock
  488. func (m *MockProvisioner) AuthorizeSSHRevoke(ctx context.Context, ott string) error {
  489. if m.MauthorizeSSHRevoke != nil {
  490. return m.MauthorizeSSHRevoke(ctx, ott)
  491. }
  492. return m.Merr
  493. }