provisioner.go 17 KB

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