collection_test.go 11 KB

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