collection_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. package provisioner
  2. import (
  3. "crypto/x509"
  4. "crypto/x509/pkix"
  5. "reflect"
  6. "strings"
  7. "sync"
  8. "testing"
  9. "github.com/smallstep/assert"
  10. "github.com/smallstep/cli/jose"
  11. )
  12. func TestCollection_Load(t *testing.T) {
  13. p, err := generateJWK()
  14. assert.FatalError(t, err)
  15. byID := new(sync.Map)
  16. byID.Store(p.GetID(), p)
  17. byID.Store("string", "a-string")
  18. type fields struct {
  19. byID *sync.Map
  20. }
  21. type args struct {
  22. id string
  23. }
  24. tests := []struct {
  25. name string
  26. fields fields
  27. args args
  28. want Interface
  29. want1 bool
  30. }{
  31. {"ok", fields{byID}, args{p.GetID()}, p, true},
  32. {"fail", fields{byID}, args{"fail"}, nil, false},
  33. {"invalid", fields{byID}, args{"string"}, nil, false},
  34. }
  35. for _, tt := range tests {
  36. t.Run(tt.name, func(t *testing.T) {
  37. c := &Collection{
  38. byID: tt.fields.byID,
  39. }
  40. got, got1 := c.Load(tt.args.id)
  41. if !reflect.DeepEqual(got, tt.want) {
  42. t.Errorf("Collection.Load() got = %v, want %v", got, tt.want)
  43. }
  44. if got1 != tt.want1 {
  45. t.Errorf("Collection.Load() got1 = %v, want %v", got1, tt.want1)
  46. }
  47. })
  48. }
  49. }
  50. func TestCollection_LoadByToken(t *testing.T) {
  51. p1, err := generateJWK()
  52. assert.FatalError(t, err)
  53. p2, err := generateJWK()
  54. assert.FatalError(t, err)
  55. p3, err := generateOIDC()
  56. assert.FatalError(t, err)
  57. byID := new(sync.Map)
  58. byID.Store(p1.GetID(), p1)
  59. byID.Store(p2.GetID(), p2)
  60. byID.Store(p3.GetID(), p3)
  61. byID.Store("string", "a-string")
  62. jwk, err := decryptJSONWebKey(p1.EncryptedKey)
  63. assert.FatalError(t, err)
  64. token, err := generateSimpleToken(p1.Name, testAudiences.Sign[0], jwk)
  65. assert.FatalError(t, err)
  66. t1, c1, err := parseToken(token)
  67. assert.FatalError(t, err)
  68. jwk, err = decryptJSONWebKey(p2.EncryptedKey)
  69. assert.FatalError(t, err)
  70. token, err = generateSimpleToken(p2.Name, testAudiences.Sign[1], jwk)
  71. assert.FatalError(t, err)
  72. t2, c2, err := parseToken(token)
  73. assert.FatalError(t, err)
  74. token, err = generateSimpleToken(p3.configuration.Issuer, p3.ClientID, &p3.keyStore.keySet.Keys[0])
  75. assert.FatalError(t, err)
  76. t3, c3, err := parseToken(token)
  77. assert.FatalError(t, err)
  78. token, err = generateSimpleToken(p3.configuration.Issuer, "string", &p3.keyStore.keySet.Keys[0])
  79. assert.FatalError(t, err)
  80. t4, c4, err := parseToken(token)
  81. assert.FatalError(t, err)
  82. type fields struct {
  83. byID *sync.Map
  84. audiences Audiences
  85. }
  86. type args struct {
  87. token *jose.JSONWebToken
  88. claims *jose.Claims
  89. }
  90. tests := []struct {
  91. name string
  92. fields fields
  93. args args
  94. want Interface
  95. want1 bool
  96. }{
  97. {"ok1", fields{byID, testAudiences}, args{t1, c1}, p1, true},
  98. {"ok2", fields{byID, testAudiences}, args{t2, c2}, p2, true},
  99. {"ok3", fields{byID, testAudiences}, args{t3, c3}, p3, true},
  100. {"bad", fields{byID, testAudiences}, args{t4, c4}, nil, false},
  101. {"fail", fields{byID, Audiences{Sign: []string{"https://foo"}}}, args{t1, c1}, nil, false},
  102. }
  103. for _, tt := range tests {
  104. t.Run(tt.name, func(t *testing.T) {
  105. c := &Collection{
  106. byID: tt.fields.byID,
  107. audiences: tt.fields.audiences,
  108. }
  109. got, got1 := c.LoadByToken(tt.args.token, tt.args.claims)
  110. if !reflect.DeepEqual(got, tt.want) {
  111. t.Errorf("Collection.LoadByToken() got = %v, want %v", got, tt.want)
  112. }
  113. if got1 != tt.want1 {
  114. t.Errorf("Collection.LoadByToken() got1 = %v, want %v", got1, tt.want1)
  115. }
  116. })
  117. }
  118. }
  119. func TestCollection_LoadByCertificate(t *testing.T) {
  120. p1, err := generateJWK()
  121. assert.FatalError(t, err)
  122. p2, err := generateOIDC()
  123. assert.FatalError(t, err)
  124. p3, err := generateACME()
  125. assert.FatalError(t, err)
  126. byID := new(sync.Map)
  127. byID.Store(p1.GetID(), p1)
  128. byID.Store(p2.GetID(), p2)
  129. byID.Store(p3.GetID(), p3)
  130. ok1Ext, err := createProvisionerExtension(1, p1.Name, p1.Key.KeyID)
  131. assert.FatalError(t, err)
  132. ok2Ext, err := createProvisionerExtension(2, p2.Name, p2.ClientID)
  133. assert.FatalError(t, err)
  134. ok3Ext, err := createProvisionerExtension(int(TypeACME), p3.Name, "")
  135. assert.FatalError(t, err)
  136. notFoundExt, err := createProvisionerExtension(1, "foo", "bar")
  137. assert.FatalError(t, err)
  138. ok1Cert := &x509.Certificate{
  139. Extensions: []pkix.Extension{ok1Ext},
  140. }
  141. ok2Cert := &x509.Certificate{
  142. Extensions: []pkix.Extension{ok2Ext},
  143. }
  144. ok3Cert := &x509.Certificate{
  145. Extensions: []pkix.Extension{ok3Ext},
  146. }
  147. notFoundCert := &x509.Certificate{
  148. Extensions: []pkix.Extension{notFoundExt},
  149. }
  150. badCert := &x509.Certificate{
  151. Extensions: []pkix.Extension{
  152. {Id: stepOIDProvisioner, Critical: false, Value: []byte("foobar")},
  153. },
  154. }
  155. type fields struct {
  156. byID *sync.Map
  157. audiences Audiences
  158. }
  159. type args struct {
  160. cert *x509.Certificate
  161. }
  162. tests := []struct {
  163. name string
  164. fields fields
  165. args args
  166. want Interface
  167. want1 bool
  168. }{
  169. {"ok1", fields{byID, testAudiences}, args{ok1Cert}, p1, true},
  170. {"ok2", fields{byID, testAudiences}, args{ok2Cert}, p2, true},
  171. {"ok3", fields{byID, testAudiences}, args{ok3Cert}, p3, true},
  172. {"noExtension", fields{byID, testAudiences}, args{&x509.Certificate{}}, &noop{}, true},
  173. {"notFound", fields{byID, testAudiences}, args{notFoundCert}, nil, false},
  174. {"badCert", fields{byID, testAudiences}, args{badCert}, nil, false},
  175. }
  176. for _, tt := range tests {
  177. t.Run(tt.name, func(t *testing.T) {
  178. c := &Collection{
  179. byID: tt.fields.byID,
  180. audiences: tt.fields.audiences,
  181. }
  182. got, got1 := c.LoadByCertificate(tt.args.cert)
  183. if !reflect.DeepEqual(got, tt.want) {
  184. t.Errorf("Collection.LoadByCertificate() got = %v, want %v", got, tt.want)
  185. }
  186. if got1 != tt.want1 {
  187. t.Errorf("Collection.LoadByCertificate() got1 = %v, want %v", got1, tt.want1)
  188. }
  189. })
  190. }
  191. }
  192. func TestCollection_LoadEncryptedKey(t *testing.T) {
  193. c := NewCollection(testAudiences)
  194. p1, err := generateJWK()
  195. assert.FatalError(t, err)
  196. assert.FatalError(t, c.Store(p1))
  197. p2, err := generateOIDC()
  198. assert.FatalError(t, err)
  199. assert.FatalError(t, c.Store(p2))
  200. // Add oidc in byKey.
  201. // It should not happen.
  202. p2KeyID := p2.keyStore.keySet.Keys[0].KeyID
  203. c.byKey.Store(p2KeyID, p2)
  204. type args struct {
  205. keyID string
  206. }
  207. tests := []struct {
  208. name string
  209. args args
  210. want string
  211. want1 bool
  212. }{
  213. {"ok", args{p1.Key.KeyID}, p1.EncryptedKey, true},
  214. {"oidc", args{p2KeyID}, "", false},
  215. {"notFound", args{"not-found"}, "", false},
  216. }
  217. for _, tt := range tests {
  218. t.Run(tt.name, func(t *testing.T) {
  219. got, got1 := c.LoadEncryptedKey(tt.args.keyID)
  220. if got != tt.want {
  221. t.Errorf("Collection.LoadEncryptedKey() got = %v, want %v", got, tt.want)
  222. }
  223. if got1 != tt.want1 {
  224. t.Errorf("Collection.LoadEncryptedKey() got1 = %v, want %v", got1, tt.want1)
  225. }
  226. })
  227. }
  228. }
  229. func TestCollection_Store(t *testing.T) {
  230. c := NewCollection(testAudiences)
  231. p1, err := generateJWK()
  232. assert.FatalError(t, err)
  233. p2, err := generateOIDC()
  234. assert.FatalError(t, err)
  235. type args struct {
  236. p Interface
  237. }
  238. tests := []struct {
  239. name string
  240. args args
  241. wantErr bool
  242. }{
  243. {"ok1", args{p1}, false},
  244. {"ok2", args{p2}, false},
  245. {"fail1", args{p1}, true},
  246. {"fail2", args{p2}, true},
  247. }
  248. for _, tt := range tests {
  249. t.Run(tt.name, func(t *testing.T) {
  250. if err := c.Store(tt.args.p); (err != nil) != tt.wantErr {
  251. t.Errorf("Collection.Store() error = %v, wantErr %v", err, tt.wantErr)
  252. }
  253. })
  254. }
  255. }
  256. func TestCollection_Find(t *testing.T) {
  257. c, err := generateCollection(10, 10)
  258. assert.FatalError(t, err)
  259. trim := func(s string) string {
  260. return strings.TrimLeft(s, "0")
  261. }
  262. toList := func(ps provisionerSlice) List {
  263. l := List{}
  264. for _, p := range ps {
  265. l = append(l, p.provisioner)
  266. }
  267. return l
  268. }
  269. type args struct {
  270. cursor string
  271. limit int
  272. }
  273. tests := []struct {
  274. name string
  275. args args
  276. want List
  277. want1 string
  278. }{
  279. {"all", args{"", DefaultProvisionersMax}, toList(c.sorted[0:20]), ""},
  280. {"0 to 19", args{"", 20}, toList(c.sorted[0:20]), ""},
  281. {"0 to 9", args{"", 10}, toList(c.sorted[0:10]), trim(c.sorted[10].uid)},
  282. {"9 to 19", args{trim(c.sorted[10].uid), 10}, toList(c.sorted[10:20]), ""},
  283. {"1", args{trim(c.sorted[1].uid), 1}, toList(c.sorted[1:2]), trim(c.sorted[2].uid)},
  284. {"1 to 5", args{trim(c.sorted[1].uid), 4}, toList(c.sorted[1:5]), trim(c.sorted[5].uid)},
  285. {"defaultLimit", args{"", 0}, toList(c.sorted[0:20]), ""},
  286. {"overTheLimit", args{"", DefaultProvisionersMax + 1}, toList(c.sorted[0:20]), ""},
  287. }
  288. for _, tt := range tests {
  289. t.Run(tt.name, func(t *testing.T) {
  290. got, got1 := c.Find(tt.args.cursor, tt.args.limit)
  291. if !reflect.DeepEqual(got, tt.want) {
  292. t.Errorf("Collection.Find() got = %v, want %v", got, tt.want)
  293. }
  294. if got1 != tt.want1 {
  295. t.Errorf("Collection.Find() got1 = %v, want %v", got1, tt.want1)
  296. }
  297. })
  298. }
  299. }
  300. func Test_matchesAudience(t *testing.T) {
  301. type matchesTest struct {
  302. a, b []string
  303. exp bool
  304. }
  305. tests := map[string]matchesTest{
  306. "false arg1 empty": {
  307. a: []string{},
  308. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  309. exp: false,
  310. },
  311. "false arg2 empty": {
  312. a: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  313. b: []string{},
  314. exp: false,
  315. },
  316. "false arg1,arg2 empty": {
  317. a: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  318. b: []string{"step-gateway", "step-cli"},
  319. exp: false,
  320. },
  321. "false": {
  322. a: []string{"step-gateway", "step-cli"},
  323. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  324. exp: false,
  325. },
  326. "true": {
  327. a: []string{"step-gateway", "https://test.ca.smallstep.com/sign"},
  328. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  329. exp: true,
  330. },
  331. "true,portsA": {
  332. a: []string{"step-gateway", "https://test.ca.smallstep.com:9000/sign"},
  333. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  334. exp: true,
  335. },
  336. "true,portsB": {
  337. a: []string{"step-gateway", "https://test.ca.smallstep.com/sign"},
  338. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com:9000/sign"},
  339. exp: true,
  340. },
  341. "true,portsAB": {
  342. a: []string{"step-gateway", "https://test.ca.smallstep.com:9000/sign"},
  343. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com:8000/sign"},
  344. exp: true,
  345. },
  346. }
  347. for name, tc := range tests {
  348. t.Run(name, func(t *testing.T) {
  349. assert.Equals(t, tc.exp, matchesAudience(tc.a, tc.b))
  350. })
  351. }
  352. }
  353. func Test_stripPort(t *testing.T) {
  354. type args struct {
  355. rawurl string
  356. }
  357. tests := []struct {
  358. name string
  359. args args
  360. want string
  361. }{
  362. {"with port", args{"https://ca.smallstep.com:9000/sign"}, "https://ca.smallstep.com/sign"},
  363. {"with no port", args{"https://ca.smallstep.com/sign/"}, "https://ca.smallstep.com/sign/"},
  364. {"bad url", args{"https://a bad url:9000"}, "https://a bad url:9000"},
  365. }
  366. for _, tt := range tests {
  367. t.Run(tt.name, func(t *testing.T) {
  368. if got := stripPort(tt.args.rawurl); got != tt.want {
  369. t.Errorf("stripPort() = %v, want %v", got, tt.want)
  370. }
  371. })
  372. }
  373. }