collection_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. "go.step.sm/crypto/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. byTokenID: tt.fields.byID,
  123. audiences: tt.fields.audiences,
  124. }
  125. got, got1 := c.LoadByToken(tt.args.token, tt.args.claims)
  126. if !reflect.DeepEqual(got, tt.want) {
  127. t.Errorf("Collection.LoadByToken() got = %v, want %v", got, tt.want)
  128. }
  129. if got1 != tt.want1 {
  130. t.Errorf("Collection.LoadByToken() got1 = %v, want %v", got1, tt.want1)
  131. }
  132. })
  133. }
  134. }
  135. func TestCollection_LoadByCertificate(t *testing.T) {
  136. mustExtension := func(typ Type, name, credentialID string) pkix.Extension {
  137. e := Extension{
  138. Type: typ, Name: name, CredentialID: credentialID,
  139. }
  140. ext, err := e.ToExtension()
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. return ext
  145. }
  146. p1, err := generateJWK()
  147. assert.FatalError(t, err)
  148. p2, err := generateOIDC()
  149. assert.FatalError(t, err)
  150. p3, err := generateACME()
  151. assert.FatalError(t, err)
  152. byName := new(sync.Map)
  153. byName.Store(p1.GetName(), p1)
  154. byName.Store(p2.GetName(), p2)
  155. byName.Store(p3.GetName(), p3)
  156. ok1Cert := &x509.Certificate{
  157. Extensions: []pkix.Extension{mustExtension(1, p1.Name, p1.Key.KeyID)},
  158. }
  159. ok2Cert := &x509.Certificate{
  160. Extensions: []pkix.Extension{mustExtension(2, p2.Name, p2.ClientID)},
  161. }
  162. ok3Cert := &x509.Certificate{
  163. Extensions: []pkix.Extension{mustExtension(TypeACME, p3.Name, "")},
  164. }
  165. notFoundCert := &x509.Certificate{
  166. Extensions: []pkix.Extension{mustExtension(1, "foo", "bar")},
  167. }
  168. badCert := &x509.Certificate{
  169. Extensions: []pkix.Extension{
  170. {Id: StepOIDProvisioner, Critical: false, Value: []byte("foobar")},
  171. },
  172. }
  173. type fields struct {
  174. byName *sync.Map
  175. audiences Audiences
  176. }
  177. type args struct {
  178. cert *x509.Certificate
  179. }
  180. tests := []struct {
  181. name string
  182. fields fields
  183. args args
  184. want Interface
  185. want1 bool
  186. }{
  187. {"ok1", fields{byName, testAudiences}, args{ok1Cert}, p1, true},
  188. {"ok2", fields{byName, testAudiences}, args{ok2Cert}, p2, true},
  189. {"ok3", fields{byName, testAudiences}, args{ok3Cert}, p3, true},
  190. {"noExtension", fields{byName, testAudiences}, args{&x509.Certificate{}}, &noop{}, true},
  191. {"notFound", fields{byName, testAudiences}, args{notFoundCert}, nil, false},
  192. {"badCert", fields{byName, testAudiences}, args{badCert}, nil, false},
  193. }
  194. for _, tt := range tests {
  195. t.Run(tt.name, func(t *testing.T) {
  196. c := &Collection{
  197. byName: tt.fields.byName,
  198. audiences: tt.fields.audiences,
  199. }
  200. got, got1 := c.LoadByCertificate(tt.args.cert)
  201. if !reflect.DeepEqual(got, tt.want) {
  202. t.Errorf("Collection.LoadByCertificate() got = %v, want %v", got, tt.want)
  203. }
  204. if got1 != tt.want1 {
  205. t.Errorf("Collection.LoadByCertificate() got1 = %v, want %v", got1, tt.want1)
  206. }
  207. })
  208. }
  209. }
  210. func TestCollection_LoadEncryptedKey(t *testing.T) {
  211. c := NewCollection(testAudiences)
  212. p1, err := generateJWK()
  213. assert.FatalError(t, err)
  214. assert.FatalError(t, c.Store(p1))
  215. p2, err := generateOIDC()
  216. assert.FatalError(t, err)
  217. assert.FatalError(t, c.Store(p2))
  218. // Add oidc in byKey.
  219. // It should not happen.
  220. p2KeyID := p2.keyStore.keySet.Keys[0].KeyID
  221. c.byKey.Store(p2KeyID, p2)
  222. type args struct {
  223. keyID string
  224. }
  225. tests := []struct {
  226. name string
  227. args args
  228. want string
  229. want1 bool
  230. }{
  231. {"ok", args{p1.Key.KeyID}, p1.EncryptedKey, true},
  232. {"oidc", args{p2KeyID}, "", false},
  233. {"notFound", args{"not-found"}, "", false},
  234. }
  235. for _, tt := range tests {
  236. t.Run(tt.name, func(t *testing.T) {
  237. got, got1 := c.LoadEncryptedKey(tt.args.keyID)
  238. if got != tt.want {
  239. t.Errorf("Collection.LoadEncryptedKey() got = %v, want %v", got, tt.want)
  240. }
  241. if got1 != tt.want1 {
  242. t.Errorf("Collection.LoadEncryptedKey() got1 = %v, want %v", got1, tt.want1)
  243. }
  244. })
  245. }
  246. }
  247. func TestCollection_Store(t *testing.T) {
  248. c := NewCollection(testAudiences)
  249. p1, err := generateJWK()
  250. assert.FatalError(t, err)
  251. p2, err := generateOIDC()
  252. assert.FatalError(t, err)
  253. type args struct {
  254. p Interface
  255. }
  256. tests := []struct {
  257. name string
  258. args args
  259. wantErr bool
  260. }{
  261. {"ok1", args{p1}, false},
  262. {"ok2", args{p2}, false},
  263. {"fail1", args{p1}, true},
  264. {"fail2", args{p2}, true},
  265. }
  266. for _, tt := range tests {
  267. t.Run(tt.name, func(t *testing.T) {
  268. if err := c.Store(tt.args.p); (err != nil) != tt.wantErr {
  269. t.Errorf("Collection.Store() error = %v, wantErr %v", err, tt.wantErr)
  270. }
  271. })
  272. }
  273. }
  274. func TestCollection_Find(t *testing.T) {
  275. c, err := generateCollection(10, 10)
  276. assert.FatalError(t, err)
  277. trim := func(s string) string {
  278. return strings.TrimLeft(s, "0")
  279. }
  280. toList := func(ps provisionerSlice) List {
  281. l := List{}
  282. for _, p := range ps {
  283. l = append(l, p.provisioner)
  284. }
  285. return l
  286. }
  287. type args struct {
  288. cursor string
  289. limit int
  290. }
  291. tests := []struct {
  292. name string
  293. args args
  294. want List
  295. want1 string
  296. }{
  297. {"all", args{"", DefaultProvisionersMax}, toList(c.sorted[0:20]), ""},
  298. {"0 to 19", args{"", 20}, toList(c.sorted[0:20]), ""},
  299. {"0 to 9", args{"", 10}, toList(c.sorted[0:10]), trim(c.sorted[10].uid)},
  300. {"9 to 19", args{trim(c.sorted[10].uid), 10}, toList(c.sorted[10:20]), ""},
  301. {"1", args{trim(c.sorted[1].uid), 1}, toList(c.sorted[1:2]), trim(c.sorted[2].uid)},
  302. {"1 to 5", args{trim(c.sorted[1].uid), 4}, toList(c.sorted[1:5]), trim(c.sorted[5].uid)},
  303. {"defaultLimit", args{"", 0}, toList(c.sorted[0:20]), ""},
  304. {"overTheLimit", args{"", DefaultProvisionersMax + 1}, toList(c.sorted[0:20]), ""},
  305. }
  306. for _, tt := range tests {
  307. t.Run(tt.name, func(t *testing.T) {
  308. got, got1 := c.Find(tt.args.cursor, tt.args.limit)
  309. if !reflect.DeepEqual(got, tt.want) {
  310. t.Errorf("Collection.Find() got = %v, want %v", got, tt.want)
  311. }
  312. if got1 != tt.want1 {
  313. t.Errorf("Collection.Find() got1 = %v, want %v", got1, tt.want1)
  314. }
  315. })
  316. }
  317. }
  318. func Test_matchesAudience(t *testing.T) {
  319. type matchesTest struct {
  320. a, b []string
  321. exp bool
  322. }
  323. tests := map[string]matchesTest{
  324. "false arg1 empty": {
  325. a: []string{},
  326. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  327. exp: false,
  328. },
  329. "false arg2 empty": {
  330. a: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  331. b: []string{},
  332. exp: false,
  333. },
  334. "false arg1,arg2 empty": {
  335. a: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  336. b: []string{"step-gateway", "step-cli"},
  337. exp: false,
  338. },
  339. "false": {
  340. a: []string{"step-gateway", "step-cli"},
  341. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  342. exp: false,
  343. },
  344. "true": {
  345. a: []string{"step-gateway", "https://test.ca.smallstep.com/sign"},
  346. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  347. exp: true,
  348. },
  349. "true,portsA": {
  350. a: []string{"step-gateway", "https://test.ca.smallstep.com:9000/sign"},
  351. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com/sign"},
  352. exp: true,
  353. },
  354. "true,portsB": {
  355. a: []string{"step-gateway", "https://test.ca.smallstep.com/sign"},
  356. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com:9000/sign"},
  357. exp: true,
  358. },
  359. "true,portsAB": {
  360. a: []string{"step-gateway", "https://test.ca.smallstep.com:9000/sign"},
  361. b: []string{"https://127.0.0.1:0/sign", "https://test.ca.smallstep.com:8000/sign"},
  362. exp: true,
  363. },
  364. }
  365. for name, tc := range tests {
  366. t.Run(name, func(t *testing.T) {
  367. assert.Equals(t, tc.exp, matchesAudience(tc.a, tc.b))
  368. })
  369. }
  370. }
  371. func Test_stripPort(t *testing.T) {
  372. type args struct {
  373. rawurl string
  374. }
  375. tests := []struct {
  376. name string
  377. args args
  378. want string
  379. }{
  380. {"with port", args{"https://ca.smallstep.com:9000/sign"}, "https://ca.smallstep.com/sign"},
  381. {"with no port", args{"https://ca.smallstep.com/sign/"}, "https://ca.smallstep.com/sign/"},
  382. {"bad url", args{"https://a bad url:9000"}, "https://a bad url:9000"},
  383. }
  384. for _, tt := range tests {
  385. t.Run(tt.name, func(t *testing.T) {
  386. if got := stripPort(tt.args.rawurl); got != tt.want {
  387. t.Errorf("stripPort() = %v, want %v", got, tt.want)
  388. }
  389. })
  390. }
  391. }