provisioner.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. )
  11. // Interface is the interface that all provisioner types must implement.
  12. type Interface interface {
  13. GetID() string
  14. GetTokenID(token string) (string, error)
  15. GetName() string
  16. GetType() Type
  17. GetEncryptedKey() (kid string, key string, ok bool)
  18. Init(config Config) error
  19. AuthorizeSign(ctx context.Context, token string) ([]SignOption, error)
  20. AuthorizeRenewal(cert *x509.Certificate) error
  21. AuthorizeRevoke(token string) error
  22. }
  23. // Audiences stores all supported audiences by request type.
  24. type Audiences struct {
  25. Sign []string
  26. Revoke []string
  27. }
  28. // All returns all supported audiences across all request types in one list.
  29. func (a Audiences) All() []string {
  30. return append(a.Sign, a.Revoke...)
  31. }
  32. // WithFragment returns a copy of audiences where the url audiences contains the
  33. // given fragment.
  34. func (a Audiences) WithFragment(fragment string) Audiences {
  35. ret := Audiences{
  36. Sign: make([]string, len(a.Sign)),
  37. Revoke: make([]string, len(a.Revoke)),
  38. }
  39. for i, s := range a.Sign {
  40. if u, err := url.Parse(s); err == nil {
  41. ret.Sign[i] = u.ResolveReference(&url.URL{Fragment: fragment}).String()
  42. } else {
  43. ret.Sign[i] = s
  44. }
  45. }
  46. for i, s := range a.Revoke {
  47. if u, err := url.Parse(s); err == nil {
  48. ret.Revoke[i] = u.ResolveReference(&url.URL{Fragment: fragment}).String()
  49. } else {
  50. ret.Revoke[i] = s
  51. }
  52. }
  53. return ret
  54. }
  55. // generateSignAudience generates a sign audience with the format
  56. // https://<host>/1.0/sign#provisionerID
  57. func generateSignAudience(caURL string, provisionerID string) (string, error) {
  58. u, err := url.Parse(caURL)
  59. if err != nil {
  60. return "", errors.Wrapf(err, "error parsing %s", caURL)
  61. }
  62. return u.ResolveReference(&url.URL{Path: "/1.0/sign", Fragment: provisionerID}).String(), nil
  63. }
  64. // Type indicates the provisioner Type.
  65. type Type int
  66. const (
  67. noopType Type = 0
  68. // TypeJWK is used to indicate the JWK provisioners.
  69. TypeJWK Type = 1
  70. // TypeOIDC is used to indicate the OIDC provisioners.
  71. TypeOIDC Type = 2
  72. // TypeGCP is used to indicate the GCP provisioners.
  73. TypeGCP Type = 3
  74. // TypeAWS is used to indicate the AWS provisioners.
  75. TypeAWS Type = 4
  76. // TypeAzure is used to indicate the Azure provisioners.
  77. TypeAzure Type = 5
  78. // TypeACME is used to indicate the ACME provisioners.
  79. TypeACME Type = 6
  80. // TypeX5C is used to indicate the X5C provisioners.
  81. TypeX5C Type = 7
  82. // RevokeAudienceKey is the key for the 'revoke' audiences in the audiences map.
  83. RevokeAudienceKey = "revoke"
  84. // SignAudienceKey is the key for the 'sign' audiences in the audiences map.
  85. SignAudienceKey = "sign"
  86. )
  87. // String returns the string representation of the type.
  88. func (t Type) String() string {
  89. switch t {
  90. case TypeJWK:
  91. return "JWK"
  92. case TypeOIDC:
  93. return "OIDC"
  94. case TypeGCP:
  95. return "GCP"
  96. case TypeAWS:
  97. return "AWS"
  98. case TypeAzure:
  99. return "Azure"
  100. case TypeACME:
  101. return "ACME"
  102. case TypeX5C:
  103. return "X5C"
  104. default:
  105. return ""
  106. }
  107. }
  108. // Config defines the default parameters used in the initialization of
  109. // provisioners.
  110. type Config struct {
  111. // Claims are the default claims.
  112. Claims Claims
  113. // Audiences are the audiences used in the default provisioner, (JWK).
  114. Audiences Audiences
  115. }
  116. type provisioner struct {
  117. Type string `json:"type"`
  118. }
  119. // List represents a list of provisioners.
  120. type List []Interface
  121. // UnmarshalJSON implements json.Unmarshaler and allows to unmarshal a list of a
  122. // interfaces into the right type.
  123. func (l *List) UnmarshalJSON(data []byte) error {
  124. ps := []json.RawMessage{}
  125. if err := json.Unmarshal(data, &ps); err != nil {
  126. return errors.Wrap(err, "error unmarshaling provisioner list")
  127. }
  128. *l = List{}
  129. for _, data := range ps {
  130. var typ provisioner
  131. if err := json.Unmarshal(data, &typ); err != nil {
  132. return errors.Errorf("error unmarshaling provisioner")
  133. }
  134. var p Interface
  135. switch strings.ToLower(typ.Type) {
  136. case "jwk":
  137. p = &JWK{}
  138. case "oidc":
  139. p = &OIDC{}
  140. case "gcp":
  141. p = &GCP{}
  142. case "aws":
  143. p = &AWS{}
  144. case "azure":
  145. p = &Azure{}
  146. case "acme":
  147. p = &ACME{}
  148. case "x5c":
  149. p = &X5C{}
  150. default:
  151. // Skip unsupported provisioners. A client using this method may be
  152. // compiled with a version of smallstep/certificates that does not
  153. // support a specific provisioner type. If we don't skip unknown
  154. // provisioners, a client encountering an unknown provisioner will
  155. // break. Rather than break the client, we skip the provisioner.
  156. // TODO: accept a pluggable logger (depending on client) that can
  157. // warn the user that an unknown provisioner was found and suggest
  158. // that the user update their client's dependency on
  159. // step/certificates and recompile.
  160. continue
  161. }
  162. if err := json.Unmarshal(data, p); err != nil {
  163. return errors.Errorf("error unmarshaling provisioner")
  164. }
  165. *l = append(*l, p)
  166. }
  167. return nil
  168. }
  169. var sshUserRegex = regexp.MustCompile("^[a-z][-a-z0-9_]*$")
  170. // SanitizeSSHUserPrincipal grabs an email or a string with the format
  171. // local@domain and returns a sanitized version of the local, valid to be used
  172. // as a user name. If the email starts with a letter between a and z, the
  173. // resulting string will match the regular expression `^[a-z][-a-z0-9_]*$`.
  174. func SanitizeSSHUserPrincipal(email string) string {
  175. if i := strings.LastIndex(email, "@"); i >= 0 {
  176. email = email[:i]
  177. }
  178. return strings.Map(func(r rune) rune {
  179. switch {
  180. case r >= 'a' && r <= 'z':
  181. return r
  182. case r >= '0' && r <= '9':
  183. return r
  184. case r == '-':
  185. return '-'
  186. case r == '.': // drop dots
  187. return -1
  188. default:
  189. return '_'
  190. }
  191. }, strings.ToLower(email))
  192. }
  193. // MockProvisioner for testing
  194. type MockProvisioner struct {
  195. Mret1, Mret2, Mret3 interface{}
  196. Merr error
  197. MgetID func() string
  198. MgetTokenID func(string) (string, error)
  199. MgetName func() string
  200. MgetType func() Type
  201. MgetEncryptedKey func() (string, string, bool)
  202. Minit func(Config) error
  203. MauthorizeRevoke func(ott string) error
  204. MauthorizeSign func(ctx context.Context, ott string) ([]SignOption, error)
  205. MauthorizeRenewal func(*x509.Certificate) error
  206. }
  207. // GetID mock
  208. func (m *MockProvisioner) GetID() string {
  209. if m.MgetID != nil {
  210. return m.MgetID()
  211. }
  212. return m.Mret1.(string)
  213. }
  214. // GetTokenID mock
  215. func (m *MockProvisioner) GetTokenID(token string) (string, error) {
  216. if m.MgetTokenID != nil {
  217. return m.MgetTokenID(token)
  218. }
  219. if m.Mret1 == nil {
  220. return "", m.Merr
  221. }
  222. return m.Mret1.(string), m.Merr
  223. }
  224. // GetName mock
  225. func (m *MockProvisioner) GetName() string {
  226. if m.MgetName != nil {
  227. return m.MgetName()
  228. }
  229. return m.Mret1.(string)
  230. }
  231. // GetType mock
  232. func (m *MockProvisioner) GetType() Type {
  233. if m.MgetType != nil {
  234. return m.MgetType()
  235. }
  236. return m.Mret1.(Type)
  237. }
  238. // GetEncryptedKey mock
  239. func (m *MockProvisioner) GetEncryptedKey() (string, string, bool) {
  240. if m.MgetEncryptedKey != nil {
  241. return m.MgetEncryptedKey()
  242. }
  243. return m.Mret1.(string), m.Mret2.(string), m.Mret3.(bool)
  244. }
  245. // Init mock
  246. func (m *MockProvisioner) Init(c Config) error {
  247. if m.Minit != nil {
  248. return m.Minit(c)
  249. }
  250. return m.Merr
  251. }
  252. // AuthorizeRevoke mock
  253. func (m *MockProvisioner) AuthorizeRevoke(ott string) error {
  254. if m.MauthorizeRevoke != nil {
  255. return m.MauthorizeRevoke(ott)
  256. }
  257. return m.Merr
  258. }
  259. // AuthorizeSign mock
  260. func (m *MockProvisioner) AuthorizeSign(ctx context.Context, ott string) ([]SignOption, error) {
  261. if m.MauthorizeSign != nil {
  262. return m.MauthorizeSign(ctx, ott)
  263. }
  264. return m.Mret1.([]SignOption), m.Merr
  265. }
  266. // AuthorizeRenewal mock
  267. func (m *MockProvisioner) AuthorizeRenewal(c *x509.Certificate) error {
  268. if m.MauthorizeRenewal != nil {
  269. return m.MauthorizeRenewal(c)
  270. }
  271. return m.Merr
  272. }