provisioner.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. }
  177. type provisioner struct {
  178. Type string `json:"type"`
  179. }
  180. // List represents a list of provisioners.
  181. type List []Interface
  182. // UnmarshalJSON implements json.Unmarshaler and allows to unmarshal a list of a
  183. // interfaces into the right type.
  184. func (l *List) UnmarshalJSON(data []byte) error {
  185. ps := []json.RawMessage{}
  186. if err := json.Unmarshal(data, &ps); err != nil {
  187. return errors.Wrap(err, "error unmarshaling provisioner list")
  188. }
  189. *l = List{}
  190. for _, data := range ps {
  191. var typ provisioner
  192. if err := json.Unmarshal(data, &typ); err != nil {
  193. return errors.Errorf("error unmarshaling provisioner")
  194. }
  195. var p Interface
  196. switch strings.ToLower(typ.Type) {
  197. case "jwk":
  198. p = &JWK{}
  199. case "oidc":
  200. p = &OIDC{}
  201. case "gcp":
  202. p = &GCP{}
  203. case "aws":
  204. p = &AWS{}
  205. case "azure":
  206. p = &Azure{}
  207. case "acme":
  208. p = &ACME{}
  209. case "x5c":
  210. p = &X5C{}
  211. case "k8ssa":
  212. p = &K8sSA{}
  213. case "sshpop":
  214. p = &SSHPOP{}
  215. default:
  216. // Skip unsupported provisioners. A client using this method may be
  217. // compiled with a version of smallstep/certificates that does not
  218. // support a specific provisioner type. If we don't skip unknown
  219. // provisioners, a client encountering an unknown provisioner will
  220. // break. Rather than break the client, we skip the provisioner.
  221. // TODO: accept a pluggable logger (depending on client) that can
  222. // warn the user that an unknown provisioner was found and suggest
  223. // that the user update their client's dependency on
  224. // step/certificates and recompile.
  225. continue
  226. }
  227. if err := json.Unmarshal(data, p); err != nil {
  228. return errors.Errorf("error unmarshaling provisioner")
  229. }
  230. *l = append(*l, p)
  231. }
  232. return nil
  233. }
  234. var sshUserRegex = regexp.MustCompile("^[a-z][-a-z0-9_]*$")
  235. // SanitizeSSHUserPrincipal grabs an email or a string with the format
  236. // local@domain and returns a sanitized version of the local, valid to be used
  237. // as a user name. If the email starts with a letter between a and z, the
  238. // resulting string will match the regular expression `^[a-z][-a-z0-9_]*$`.
  239. func SanitizeSSHUserPrincipal(email string) string {
  240. if i := strings.LastIndex(email, "@"); i >= 0 {
  241. email = email[:i]
  242. }
  243. return strings.Map(func(r rune) rune {
  244. switch {
  245. case r >= 'a' && r <= 'z':
  246. return r
  247. case r >= '0' && r <= '9':
  248. return r
  249. case r == '-':
  250. return '-'
  251. case r == '.': // drop dots
  252. return -1
  253. default:
  254. return '_'
  255. }
  256. }, strings.ToLower(email))
  257. }
  258. type base struct{}
  259. // AuthorizeSign returns an unimplmented error. Provisioners should overwrite
  260. // this method if they will support authorizing tokens for signing x509 Certificates.
  261. func (b *base) AuthorizeSign(ctx context.Context, token string) ([]SignOption, error) {
  262. return nil, errors.New("not implemented; provisioner does not implement AuthorizeSign")
  263. }
  264. // AuthorizeRevoke returns an unimplmented error. Provisioners should overwrite
  265. // this method if they will support authorizing tokens for revoking x509 Certificates.
  266. func (b *base) AuthorizeRevoke(ctx context.Context, token string) error {
  267. return errors.New("not implemented; provisioner does not implement AuthorizeRevoke")
  268. }
  269. // AuthorizeRenew returns an unimplmented error. Provisioners should overwrite
  270. // this method if they will support authorizing tokens for renewing x509 Certificates.
  271. func (b *base) AuthorizeRenew(ctx context.Context, cert *x509.Certificate) error {
  272. return errors.New("not implemented; provisioner does not implement AuthorizeRenew")
  273. }
  274. // AuthorizeSSHSign returns an unimplmented error. Provisioners should overwrite
  275. // this method if they will support authorizing tokens for signing SSH Certificates.
  276. func (b *base) AuthorizeSSHSign(ctx context.Context, token string) ([]SignOption, error) {
  277. return nil, errors.New("not implemented; provisioner does not implement AuthorizeSSHSign")
  278. }
  279. // AuthorizeRevoke returns an unimplmented error. Provisioners should overwrite
  280. // this method if they will support authorizing tokens for revoking SSH Certificates.
  281. func (b *base) AuthorizeSSHRevoke(ctx context.Context, token string) error {
  282. return errors.New("not implemented; provisioner does not implement AuthorizeSSHRevoke")
  283. }
  284. // AuthorizeSSHRenew returns an unimplmented error. Provisioners should overwrite
  285. // this method if they will support authorizing tokens for renewing SSH Certificates.
  286. func (b *base) AuthorizeSSHRenew(ctx context.Context, token string) (*ssh.Certificate, error) {
  287. return nil, errors.New("not implemented; provisioner does not implement AuthorizeSSHRenew")
  288. }
  289. // AuthorizeSSHRekey returns an unimplmented error. Provisioners should overwrite
  290. // this method if they will support authorizing tokens for renewing SSH Certificates.
  291. func (b *base) AuthorizeSSHRekey(ctx context.Context, token string) (*ssh.Certificate, []SignOption, error) {
  292. return nil, nil, errors.New("not implemented; provisioner does not implement AuthorizeSSHRekey")
  293. }
  294. // MockProvisioner for testing
  295. type MockProvisioner struct {
  296. Mret1, Mret2, Mret3 interface{}
  297. Merr error
  298. MgetID func() string
  299. MgetTokenID func(string) (string, error)
  300. MgetName func() string
  301. MgetType func() Type
  302. MgetEncryptedKey func() (string, string, bool)
  303. Minit func(Config) error
  304. MauthorizeRevoke func(ott string) error
  305. MauthorizeSign func(ctx context.Context, ott string) ([]SignOption, error)
  306. MauthorizeRenewal func(*x509.Certificate) error
  307. }
  308. // GetID mock
  309. func (m *MockProvisioner) GetID() string {
  310. if m.MgetID != nil {
  311. return m.MgetID()
  312. }
  313. return m.Mret1.(string)
  314. }
  315. // GetTokenID mock
  316. func (m *MockProvisioner) GetTokenID(token string) (string, error) {
  317. if m.MgetTokenID != nil {
  318. return m.MgetTokenID(token)
  319. }
  320. if m.Mret1 == nil {
  321. return "", m.Merr
  322. }
  323. return m.Mret1.(string), m.Merr
  324. }
  325. // GetName mock
  326. func (m *MockProvisioner) GetName() string {
  327. if m.MgetName != nil {
  328. return m.MgetName()
  329. }
  330. return m.Mret1.(string)
  331. }
  332. // GetType mock
  333. func (m *MockProvisioner) GetType() Type {
  334. if m.MgetType != nil {
  335. return m.MgetType()
  336. }
  337. return m.Mret1.(Type)
  338. }
  339. // GetEncryptedKey mock
  340. func (m *MockProvisioner) GetEncryptedKey() (string, string, bool) {
  341. if m.MgetEncryptedKey != nil {
  342. return m.MgetEncryptedKey()
  343. }
  344. return m.Mret1.(string), m.Mret2.(string), m.Mret3.(bool)
  345. }
  346. // Init mock
  347. func (m *MockProvisioner) Init(c Config) error {
  348. if m.Minit != nil {
  349. return m.Minit(c)
  350. }
  351. return m.Merr
  352. }
  353. // AuthorizeRevoke mock
  354. func (m *MockProvisioner) AuthorizeRevoke(ott string) error {
  355. if m.MauthorizeRevoke != nil {
  356. return m.MauthorizeRevoke(ott)
  357. }
  358. return m.Merr
  359. }
  360. // AuthorizeSign mock
  361. func (m *MockProvisioner) AuthorizeSign(ctx context.Context, ott string) ([]SignOption, error) {
  362. if m.MauthorizeSign != nil {
  363. return m.MauthorizeSign(ctx, ott)
  364. }
  365. return m.Mret1.([]SignOption), m.Merr
  366. }
  367. // AuthorizeRenewal mock
  368. func (m *MockProvisioner) AuthorizeRenewal(c *x509.Certificate) error {
  369. if m.MauthorizeRenewal != nil {
  370. return m.MauthorizeRenewal(c)
  371. }
  372. return m.Merr
  373. }