collection_test.go 10 KB

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