provisioner.go 14 KB

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