provisioner.go 15 KB

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