keyring_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package credentialprovider
  14. import (
  15. "encoding/base64"
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestUrlsMatch(t *testing.T) {
  21. tests := []struct {
  22. globUrl string
  23. targetUrl string
  24. matchExpected bool
  25. }{
  26. // match when there is no path component
  27. {
  28. globUrl: "*.kubernetes.io",
  29. targetUrl: "prefix.kubernetes.io",
  30. matchExpected: true,
  31. },
  32. {
  33. globUrl: "prefix.*.io",
  34. targetUrl: "prefix.kubernetes.io",
  35. matchExpected: true,
  36. },
  37. {
  38. globUrl: "prefix.kubernetes.*",
  39. targetUrl: "prefix.kubernetes.io",
  40. matchExpected: true,
  41. },
  42. {
  43. globUrl: "*-good.kubernetes.io",
  44. targetUrl: "prefix-good.kubernetes.io",
  45. matchExpected: true,
  46. },
  47. // match with path components
  48. {
  49. globUrl: "*.kubernetes.io/blah",
  50. targetUrl: "prefix.kubernetes.io/blah",
  51. matchExpected: true,
  52. },
  53. {
  54. globUrl: "prefix.*.io/foo",
  55. targetUrl: "prefix.kubernetes.io/foo/bar",
  56. matchExpected: true,
  57. },
  58. // match with path components and ports
  59. {
  60. globUrl: "*.kubernetes.io:1111/blah",
  61. targetUrl: "prefix.kubernetes.io:1111/blah",
  62. matchExpected: true,
  63. },
  64. {
  65. globUrl: "prefix.*.io:1111/foo",
  66. targetUrl: "prefix.kubernetes.io:1111/foo/bar",
  67. matchExpected: true,
  68. },
  69. // no match when number of parts mismatch
  70. {
  71. globUrl: "*.kubernetes.io",
  72. targetUrl: "kubernetes.io",
  73. matchExpected: false,
  74. },
  75. {
  76. globUrl: "*.*.kubernetes.io",
  77. targetUrl: "prefix.kubernetes.io",
  78. matchExpected: false,
  79. },
  80. {
  81. globUrl: "*.*.kubernetes.io",
  82. targetUrl: "kubernetes.io",
  83. matchExpected: false,
  84. },
  85. // no match when some parts mismatch
  86. {
  87. globUrl: "kubernetes.io",
  88. targetUrl: "kubernetes.com",
  89. matchExpected: false,
  90. },
  91. {
  92. globUrl: "k*.io",
  93. targetUrl: "quay.io",
  94. matchExpected: false,
  95. },
  96. // no match when ports mismatch
  97. {
  98. globUrl: "*.kubernetes.io:1234/blah",
  99. targetUrl: "prefix.kubernetes.io:1111/blah",
  100. matchExpected: false,
  101. },
  102. {
  103. globUrl: "prefix.*.io/foo",
  104. targetUrl: "prefix.kubernetes.io:1111/foo/bar",
  105. matchExpected: false,
  106. },
  107. }
  108. for _, test := range tests {
  109. matched, _ := urlsMatchStr(test.globUrl, test.targetUrl)
  110. if matched != test.matchExpected {
  111. t.Errorf("Expected match result of %s and %s to be %t, but was %t",
  112. test.globUrl, test.targetUrl, test.matchExpected, matched)
  113. }
  114. }
  115. }
  116. func TestDockerKeyringForGlob(t *testing.T) {
  117. tests := []struct {
  118. globUrl string
  119. targetUrl string
  120. }{
  121. {
  122. globUrl: "https://hello.kubernetes.io",
  123. targetUrl: "hello.kubernetes.io",
  124. },
  125. {
  126. globUrl: "https://*.docker.io",
  127. targetUrl: "prefix.docker.io",
  128. },
  129. {
  130. globUrl: "https://prefix.*.io",
  131. targetUrl: "prefix.docker.io",
  132. },
  133. {
  134. globUrl: "https://prefix.docker.*",
  135. targetUrl: "prefix.docker.io",
  136. },
  137. {
  138. globUrl: "https://*.docker.io/path",
  139. targetUrl: "prefix.docker.io/path",
  140. },
  141. {
  142. globUrl: "https://prefix.*.io/path",
  143. targetUrl: "prefix.docker.io/path/subpath",
  144. },
  145. {
  146. globUrl: "https://prefix.docker.*/path",
  147. targetUrl: "prefix.docker.io/path",
  148. },
  149. {
  150. globUrl: "https://*.docker.io:8888",
  151. targetUrl: "prefix.docker.io:8888",
  152. },
  153. {
  154. globUrl: "https://prefix.*.io:8888",
  155. targetUrl: "prefix.docker.io:8888",
  156. },
  157. {
  158. globUrl: "https://prefix.docker.*:8888",
  159. targetUrl: "prefix.docker.io:8888",
  160. },
  161. {
  162. globUrl: "https://*.docker.io/path:1111",
  163. targetUrl: "prefix.docker.io/path:1111",
  164. },
  165. {
  166. globUrl: "https://*.docker.io/v1/",
  167. targetUrl: "prefix.docker.io/path:1111",
  168. },
  169. {
  170. globUrl: "https://*.docker.io/v2/",
  171. targetUrl: "prefix.docker.io/path:1111",
  172. },
  173. {
  174. globUrl: "https://prefix.docker.*/path:1111",
  175. targetUrl: "prefix.docker.io/path:1111",
  176. },
  177. {
  178. globUrl: "prefix.docker.io:1111",
  179. targetUrl: "prefix.docker.io:1111/path",
  180. },
  181. {
  182. globUrl: "*.docker.io:1111",
  183. targetUrl: "prefix.docker.io:1111/path",
  184. },
  185. }
  186. for i, test := range tests {
  187. email := "foo@bar.baz"
  188. username := "foo"
  189. password := "bar"
  190. auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
  191. sampleDockerConfig := fmt.Sprintf(`{
  192. "%s": {
  193. "email": %q,
  194. "auth": %q
  195. }
  196. }`, test.globUrl, email, auth)
  197. keyring := &BasicDockerKeyring{}
  198. if cfg, err := readDockerConfigFileFromBytes([]byte(sampleDockerConfig)); err != nil {
  199. t.Errorf("Error processing json blob %q, %v", sampleDockerConfig, err)
  200. } else {
  201. keyring.Add(cfg)
  202. }
  203. creds, ok := keyring.Lookup(test.targetUrl + "/foo/bar")
  204. if !ok {
  205. t.Errorf("%d: Didn't find expected URL: %s", i, test.targetUrl)
  206. continue
  207. }
  208. val := creds[0]
  209. if username != val.Username {
  210. t.Errorf("Unexpected username value, want: %s, got: %s", username, val.Username)
  211. }
  212. if password != val.Password {
  213. t.Errorf("Unexpected password value, want: %s, got: %s", password, val.Password)
  214. }
  215. if email != val.Email {
  216. t.Errorf("Unexpected email value, want: %s, got: %s", email, val.Email)
  217. }
  218. }
  219. }
  220. func TestKeyringMiss(t *testing.T) {
  221. tests := []struct {
  222. globUrl string
  223. lookupUrl string
  224. }{
  225. {
  226. globUrl: "https://hello.kubernetes.io",
  227. lookupUrl: "world.mesos.org/foo/bar",
  228. },
  229. {
  230. globUrl: "https://*.docker.com",
  231. lookupUrl: "prefix.docker.io",
  232. },
  233. {
  234. globUrl: "https://suffix.*.io",
  235. lookupUrl: "prefix.docker.io",
  236. },
  237. {
  238. globUrl: "https://prefix.docker.c*",
  239. lookupUrl: "prefix.docker.io",
  240. },
  241. {
  242. globUrl: "https://prefix.*.io/path:1111",
  243. lookupUrl: "prefix.docker.io/path/subpath:1111",
  244. },
  245. {
  246. globUrl: "suffix.*.io",
  247. lookupUrl: "prefix.docker.io",
  248. },
  249. }
  250. for _, test := range tests {
  251. email := "foo@bar.baz"
  252. username := "foo"
  253. password := "bar"
  254. auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
  255. sampleDockerConfig := fmt.Sprintf(`{
  256. "%s": {
  257. "email": %q,
  258. "auth": %q
  259. }
  260. }`, test.globUrl, email, auth)
  261. keyring := &BasicDockerKeyring{}
  262. if cfg, err := readDockerConfigFileFromBytes([]byte(sampleDockerConfig)); err != nil {
  263. t.Errorf("Error processing json blob %q, %v", sampleDockerConfig, err)
  264. } else {
  265. keyring.Add(cfg)
  266. }
  267. _, ok := keyring.Lookup(test.lookupUrl + "/foo/bar")
  268. if ok {
  269. t.Errorf("Expected not to find URL %s, but found", test.lookupUrl)
  270. }
  271. }
  272. }
  273. func TestKeyringMissWithDockerHubCredentials(t *testing.T) {
  274. url := defaultRegistryKey
  275. email := "foo@bar.baz"
  276. username := "foo"
  277. password := "bar"
  278. auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
  279. sampleDockerConfig := fmt.Sprintf(`{
  280. "https://%s": {
  281. "email": %q,
  282. "auth": %q
  283. }
  284. }`, url, email, auth)
  285. keyring := &BasicDockerKeyring{}
  286. if cfg, err := readDockerConfigFileFromBytes([]byte(sampleDockerConfig)); err != nil {
  287. t.Errorf("Error processing json blob %q, %v", sampleDockerConfig, err)
  288. } else {
  289. keyring.Add(cfg)
  290. }
  291. val, ok := keyring.Lookup("world.mesos.org/foo/bar")
  292. if ok {
  293. t.Errorf("Found unexpected credential: %+v", val)
  294. }
  295. }
  296. func TestKeyringHitWithUnqualifiedDockerHub(t *testing.T) {
  297. url := defaultRegistryKey
  298. email := "foo@bar.baz"
  299. username := "foo"
  300. password := "bar"
  301. auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
  302. sampleDockerConfig := fmt.Sprintf(`{
  303. "https://%s": {
  304. "email": %q,
  305. "auth": %q
  306. }
  307. }`, url, email, auth)
  308. keyring := &BasicDockerKeyring{}
  309. if cfg, err := readDockerConfigFileFromBytes([]byte(sampleDockerConfig)); err != nil {
  310. t.Errorf("Error processing json blob %q, %v", sampleDockerConfig, err)
  311. } else {
  312. keyring.Add(cfg)
  313. }
  314. creds, ok := keyring.Lookup("google/docker-registry")
  315. if !ok {
  316. t.Errorf("Didn't find expected URL: %s", url)
  317. return
  318. }
  319. if len(creds) > 1 {
  320. t.Errorf("Got more hits than expected: %s", creds)
  321. }
  322. val := creds[0]
  323. if username != val.Username {
  324. t.Errorf("Unexpected username value, want: %s, got: %s", username, val.Username)
  325. }
  326. if password != val.Password {
  327. t.Errorf("Unexpected password value, want: %s, got: %s", password, val.Password)
  328. }
  329. if email != val.Email {
  330. t.Errorf("Unexpected email value, want: %s, got: %s", email, val.Email)
  331. }
  332. }
  333. func TestKeyringHitWithUnqualifiedLibraryDockerHub(t *testing.T) {
  334. url := defaultRegistryKey
  335. email := "foo@bar.baz"
  336. username := "foo"
  337. password := "bar"
  338. auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
  339. sampleDockerConfig := fmt.Sprintf(`{
  340. "https://%s": {
  341. "email": %q,
  342. "auth": %q
  343. }
  344. }`, url, email, auth)
  345. keyring := &BasicDockerKeyring{}
  346. if cfg, err := readDockerConfigFileFromBytes([]byte(sampleDockerConfig)); err != nil {
  347. t.Errorf("Error processing json blob %q, %v", sampleDockerConfig, err)
  348. } else {
  349. keyring.Add(cfg)
  350. }
  351. creds, ok := keyring.Lookup("jenkins")
  352. if !ok {
  353. t.Errorf("Didn't find expected URL: %s", url)
  354. return
  355. }
  356. if len(creds) > 1 {
  357. t.Errorf("Got more hits than expected: %s", creds)
  358. }
  359. val := creds[0]
  360. if username != val.Username {
  361. t.Errorf("Unexpected username value, want: %s, got: %s", username, val.Username)
  362. }
  363. if password != val.Password {
  364. t.Errorf("Unexpected password value, want: %s, got: %s", password, val.Password)
  365. }
  366. if email != val.Email {
  367. t.Errorf("Unexpected email value, want: %s, got: %s", email, val.Email)
  368. }
  369. }
  370. func TestKeyringHitWithQualifiedDockerHub(t *testing.T) {
  371. url := defaultRegistryKey
  372. email := "foo@bar.baz"
  373. username := "foo"
  374. password := "bar"
  375. auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", username, password)))
  376. sampleDockerConfig := fmt.Sprintf(`{
  377. "https://%s": {
  378. "email": %q,
  379. "auth": %q
  380. }
  381. }`, url, email, auth)
  382. keyring := &BasicDockerKeyring{}
  383. if cfg, err := readDockerConfigFileFromBytes([]byte(sampleDockerConfig)); err != nil {
  384. t.Errorf("Error processing json blob %q, %v", sampleDockerConfig, err)
  385. } else {
  386. keyring.Add(cfg)
  387. }
  388. creds, ok := keyring.Lookup(url + "/google/docker-registry")
  389. if !ok {
  390. t.Errorf("Didn't find expected URL: %s", url)
  391. return
  392. }
  393. if len(creds) > 2 {
  394. t.Errorf("Got more hits than expected: %s", creds)
  395. }
  396. val := creds[0]
  397. if username != val.Username {
  398. t.Errorf("Unexpected username value, want: %s, got: %s", username, val.Username)
  399. }
  400. if password != val.Password {
  401. t.Errorf("Unexpected password value, want: %s, got: %s", password, val.Password)
  402. }
  403. if email != val.Email {
  404. t.Errorf("Unexpected email value, want: %s, got: %s", email, val.Email)
  405. }
  406. }
  407. func TestIsDefaultRegistryMatch(t *testing.T) {
  408. samples := []map[bool]string{
  409. {true: "foo/bar"},
  410. {true: "docker.io/foo/bar"},
  411. {true: "index.docker.io/foo/bar"},
  412. {true: "foo"},
  413. {false: ""},
  414. {false: "registry.tld/foo/bar"},
  415. {false: "registry:5000/foo/bar"},
  416. {false: "myhostdocker.io/foo/bar"},
  417. }
  418. for _, sample := range samples {
  419. for expected, imageName := range sample {
  420. if got := isDefaultRegistryMatch(imageName); got != expected {
  421. t.Errorf("Expected '%s' to be %t, got %t", imageName, expected, got)
  422. }
  423. }
  424. }
  425. }
  426. type testProvider struct {
  427. Count int
  428. }
  429. // Enabled implements dockerConfigProvider
  430. func (d *testProvider) Enabled() bool {
  431. return true
  432. }
  433. // LazyProvide implements dockerConfigProvider. Should never be called.
  434. func (d *testProvider) LazyProvide(image string) *DockerConfigEntry {
  435. return nil
  436. }
  437. // Provide implements dockerConfigProvider
  438. func (d *testProvider) Provide(image string) DockerConfig {
  439. d.Count++
  440. return DockerConfig{}
  441. }
  442. func TestLazyKeyring(t *testing.T) {
  443. provider := &testProvider{
  444. Count: 0,
  445. }
  446. lazy := &lazyDockerKeyring{
  447. Providers: []DockerConfigProvider{
  448. provider,
  449. },
  450. }
  451. if provider.Count != 0 {
  452. t.Errorf("Unexpected number of Provide calls: %v", provider.Count)
  453. }
  454. lazy.Lookup("foo")
  455. if provider.Count != 1 {
  456. t.Errorf("Unexpected number of Provide calls: %v", provider.Count)
  457. }
  458. lazy.Lookup("foo")
  459. if provider.Count != 2 {
  460. t.Errorf("Unexpected number of Provide calls: %v", provider.Count)
  461. }
  462. lazy.Lookup("foo")
  463. if provider.Count != 3 {
  464. t.Errorf("Unexpected number of Provide calls: %v", provider.Count)
  465. }
  466. }
  467. func TestDockerKeyringLookup(t *testing.T) {
  468. ada := LazyAuthConfiguration{
  469. AuthConfig: AuthConfig{
  470. Username: "ada",
  471. Password: "smash",
  472. Email: "ada@example.com",
  473. },
  474. }
  475. grace := LazyAuthConfiguration{
  476. AuthConfig: AuthConfig{
  477. Username: "grace",
  478. Password: "squash",
  479. Email: "grace@example.com",
  480. },
  481. }
  482. dk := &BasicDockerKeyring{}
  483. dk.Add(DockerConfig{
  484. "bar.example.com/pong": DockerConfigEntry{
  485. Username: grace.Username,
  486. Password: grace.Password,
  487. Email: grace.Email,
  488. },
  489. "bar.example.com": DockerConfigEntry{
  490. Username: ada.Username,
  491. Password: ada.Password,
  492. Email: ada.Email,
  493. },
  494. })
  495. tests := []struct {
  496. image string
  497. match []LazyAuthConfiguration
  498. ok bool
  499. }{
  500. // direct match
  501. {"bar.example.com", []LazyAuthConfiguration{ada}, true},
  502. // direct match deeper than other possible matches
  503. {"bar.example.com/pong", []LazyAuthConfiguration{grace, ada}, true},
  504. // no direct match, deeper path ignored
  505. {"bar.example.com/ping", []LazyAuthConfiguration{ada}, true},
  506. // match first part of path token
  507. {"bar.example.com/pongz", []LazyAuthConfiguration{grace, ada}, true},
  508. // match regardless of sub-path
  509. {"bar.example.com/pong/pang", []LazyAuthConfiguration{grace, ada}, true},
  510. // no host match
  511. {"example.com", []LazyAuthConfiguration{}, false},
  512. {"foo.example.com", []LazyAuthConfiguration{}, false},
  513. }
  514. for i, tt := range tests {
  515. match, ok := dk.Lookup(tt.image)
  516. if tt.ok != ok {
  517. t.Errorf("case %d: expected ok=%t, got %t", i, tt.ok, ok)
  518. }
  519. if !reflect.DeepEqual(tt.match, match) {
  520. t.Errorf("case %d: expected match=%#v, got %#v", i, tt.match, match)
  521. }
  522. }
  523. }
  524. // This validates that dockercfg entries with a scheme and url path are properly matched
  525. // by images that only match the hostname.
  526. // NOTE: the above covers the case of a more specific match trumping just hostname.
  527. func TestIssue3797(t *testing.T) {
  528. rex := LazyAuthConfiguration{
  529. AuthConfig: AuthConfig{
  530. Username: "rex",
  531. Password: "tiny arms",
  532. Email: "rex@example.com",
  533. },
  534. }
  535. dk := &BasicDockerKeyring{}
  536. dk.Add(DockerConfig{
  537. "https://quay.io/v1/": DockerConfigEntry{
  538. Username: rex.Username,
  539. Password: rex.Password,
  540. Email: rex.Email,
  541. },
  542. })
  543. tests := []struct {
  544. image string
  545. match []LazyAuthConfiguration
  546. ok bool
  547. }{
  548. // direct match
  549. {"quay.io", []LazyAuthConfiguration{rex}, true},
  550. // partial matches
  551. {"quay.io/foo", []LazyAuthConfiguration{rex}, true},
  552. {"quay.io/foo/bar", []LazyAuthConfiguration{rex}, true},
  553. }
  554. for i, tt := range tests {
  555. match, ok := dk.Lookup(tt.image)
  556. if tt.ok != ok {
  557. t.Errorf("case %d: expected ok=%t, got %t", i, tt.ok, ok)
  558. }
  559. if !reflect.DeepEqual(tt.match, match) {
  560. t.Errorf("case %d: expected match=%#v, got %#v", i, tt.match, match)
  561. }
  562. }
  563. }