provisioner.go 15 KB

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