provisioner.go 17 KB

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