provisioner.go 15 KB

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