registry_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. package registry // import "github.com/docker/docker/registry"
  2. import (
  3. "testing"
  4. "github.com/distribution/reference"
  5. "github.com/docker/docker/api/types/registry"
  6. "gotest.tools/v3/assert"
  7. is "gotest.tools/v3/assert/cmp"
  8. )
  9. func TestParseRepositoryInfo(t *testing.T) {
  10. type staticRepositoryInfo struct {
  11. Index *registry.IndexInfo
  12. RemoteName string
  13. CanonicalName string
  14. LocalName string
  15. Official bool
  16. }
  17. expectedRepoInfos := map[string]staticRepositoryInfo{
  18. "fooo/bar": {
  19. Index: &registry.IndexInfo{
  20. Name: IndexName,
  21. Official: true,
  22. },
  23. RemoteName: "fooo/bar",
  24. LocalName: "fooo/bar",
  25. CanonicalName: "docker.io/fooo/bar",
  26. Official: false,
  27. },
  28. "library/ubuntu": {
  29. Index: &registry.IndexInfo{
  30. Name: IndexName,
  31. Official: true,
  32. },
  33. RemoteName: "library/ubuntu",
  34. LocalName: "ubuntu",
  35. CanonicalName: "docker.io/library/ubuntu",
  36. Official: true,
  37. },
  38. "nonlibrary/ubuntu": {
  39. Index: &registry.IndexInfo{
  40. Name: IndexName,
  41. Official: true,
  42. },
  43. RemoteName: "nonlibrary/ubuntu",
  44. LocalName: "nonlibrary/ubuntu",
  45. CanonicalName: "docker.io/nonlibrary/ubuntu",
  46. Official: false,
  47. },
  48. "ubuntu": {
  49. Index: &registry.IndexInfo{
  50. Name: IndexName,
  51. Official: true,
  52. },
  53. RemoteName: "library/ubuntu",
  54. LocalName: "ubuntu",
  55. CanonicalName: "docker.io/library/ubuntu",
  56. Official: true,
  57. },
  58. "other/library": {
  59. Index: &registry.IndexInfo{
  60. Name: IndexName,
  61. Official: true,
  62. },
  63. RemoteName: "other/library",
  64. LocalName: "other/library",
  65. CanonicalName: "docker.io/other/library",
  66. Official: false,
  67. },
  68. "127.0.0.1:8000/private/moonbase": {
  69. Index: &registry.IndexInfo{
  70. Name: "127.0.0.1:8000",
  71. Official: false,
  72. },
  73. RemoteName: "private/moonbase",
  74. LocalName: "127.0.0.1:8000/private/moonbase",
  75. CanonicalName: "127.0.0.1:8000/private/moonbase",
  76. Official: false,
  77. },
  78. "127.0.0.1:8000/privatebase": {
  79. Index: &registry.IndexInfo{
  80. Name: "127.0.0.1:8000",
  81. Official: false,
  82. },
  83. RemoteName: "privatebase",
  84. LocalName: "127.0.0.1:8000/privatebase",
  85. CanonicalName: "127.0.0.1:8000/privatebase",
  86. Official: false,
  87. },
  88. "localhost:8000/private/moonbase": {
  89. Index: &registry.IndexInfo{
  90. Name: "localhost:8000",
  91. Official: false,
  92. },
  93. RemoteName: "private/moonbase",
  94. LocalName: "localhost:8000/private/moonbase",
  95. CanonicalName: "localhost:8000/private/moonbase",
  96. Official: false,
  97. },
  98. "localhost:8000/privatebase": {
  99. Index: &registry.IndexInfo{
  100. Name: "localhost:8000",
  101. Official: false,
  102. },
  103. RemoteName: "privatebase",
  104. LocalName: "localhost:8000/privatebase",
  105. CanonicalName: "localhost:8000/privatebase",
  106. Official: false,
  107. },
  108. "example.com/private/moonbase": {
  109. Index: &registry.IndexInfo{
  110. Name: "example.com",
  111. Official: false,
  112. },
  113. RemoteName: "private/moonbase",
  114. LocalName: "example.com/private/moonbase",
  115. CanonicalName: "example.com/private/moonbase",
  116. Official: false,
  117. },
  118. "example.com/privatebase": {
  119. Index: &registry.IndexInfo{
  120. Name: "example.com",
  121. Official: false,
  122. },
  123. RemoteName: "privatebase",
  124. LocalName: "example.com/privatebase",
  125. CanonicalName: "example.com/privatebase",
  126. Official: false,
  127. },
  128. "example.com:8000/private/moonbase": {
  129. Index: &registry.IndexInfo{
  130. Name: "example.com:8000",
  131. Official: false,
  132. },
  133. RemoteName: "private/moonbase",
  134. LocalName: "example.com:8000/private/moonbase",
  135. CanonicalName: "example.com:8000/private/moonbase",
  136. Official: false,
  137. },
  138. "example.com:8000/privatebase": {
  139. Index: &registry.IndexInfo{
  140. Name: "example.com:8000",
  141. Official: false,
  142. },
  143. RemoteName: "privatebase",
  144. LocalName: "example.com:8000/privatebase",
  145. CanonicalName: "example.com:8000/privatebase",
  146. Official: false,
  147. },
  148. "localhost/private/moonbase": {
  149. Index: &registry.IndexInfo{
  150. Name: "localhost",
  151. Official: false,
  152. },
  153. RemoteName: "private/moonbase",
  154. LocalName: "localhost/private/moonbase",
  155. CanonicalName: "localhost/private/moonbase",
  156. Official: false,
  157. },
  158. "localhost/privatebase": {
  159. Index: &registry.IndexInfo{
  160. Name: "localhost",
  161. Official: false,
  162. },
  163. RemoteName: "privatebase",
  164. LocalName: "localhost/privatebase",
  165. CanonicalName: "localhost/privatebase",
  166. Official: false,
  167. },
  168. IndexName + "/public/moonbase": {
  169. Index: &registry.IndexInfo{
  170. Name: IndexName,
  171. Official: true,
  172. },
  173. RemoteName: "public/moonbase",
  174. LocalName: "public/moonbase",
  175. CanonicalName: "docker.io/public/moonbase",
  176. Official: false,
  177. },
  178. "index." + IndexName + "/public/moonbase": {
  179. Index: &registry.IndexInfo{
  180. Name: IndexName,
  181. Official: true,
  182. },
  183. RemoteName: "public/moonbase",
  184. LocalName: "public/moonbase",
  185. CanonicalName: "docker.io/public/moonbase",
  186. Official: false,
  187. },
  188. "ubuntu-12.04-base": {
  189. Index: &registry.IndexInfo{
  190. Name: IndexName,
  191. Official: true,
  192. },
  193. RemoteName: "library/ubuntu-12.04-base",
  194. LocalName: "ubuntu-12.04-base",
  195. CanonicalName: "docker.io/library/ubuntu-12.04-base",
  196. Official: true,
  197. },
  198. IndexName + "/ubuntu-12.04-base": {
  199. Index: &registry.IndexInfo{
  200. Name: IndexName,
  201. Official: true,
  202. },
  203. RemoteName: "library/ubuntu-12.04-base",
  204. LocalName: "ubuntu-12.04-base",
  205. CanonicalName: "docker.io/library/ubuntu-12.04-base",
  206. Official: true,
  207. },
  208. "index." + IndexName + "/ubuntu-12.04-base": {
  209. Index: &registry.IndexInfo{
  210. Name: IndexName,
  211. Official: true,
  212. },
  213. RemoteName: "library/ubuntu-12.04-base",
  214. LocalName: "ubuntu-12.04-base",
  215. CanonicalName: "docker.io/library/ubuntu-12.04-base",
  216. Official: true,
  217. },
  218. }
  219. for reposName, expectedRepoInfo := range expectedRepoInfos {
  220. named, err := reference.ParseNormalizedNamed(reposName)
  221. if err != nil {
  222. t.Error(err)
  223. }
  224. repoInfo, err := ParseRepositoryInfo(named)
  225. if err != nil {
  226. t.Error(err)
  227. } else {
  228. assert.Check(t, is.Equal(repoInfo.Index.Name, expectedRepoInfo.Index.Name), reposName)
  229. assert.Check(t, is.Equal(reference.Path(repoInfo.Name), expectedRepoInfo.RemoteName), reposName)
  230. assert.Check(t, is.Equal(reference.FamiliarName(repoInfo.Name), expectedRepoInfo.LocalName), reposName)
  231. assert.Check(t, is.Equal(repoInfo.Name.Name(), expectedRepoInfo.CanonicalName), reposName)
  232. assert.Check(t, is.Equal(repoInfo.Index.Official, expectedRepoInfo.Index.Official), reposName)
  233. assert.Check(t, is.Equal(repoInfo.Official, expectedRepoInfo.Official), reposName)
  234. }
  235. }
  236. }
  237. func TestNewIndexInfo(t *testing.T) {
  238. testIndexInfo := func(config *serviceConfig, expectedIndexInfos map[string]*registry.IndexInfo) {
  239. for indexName, expectedIndexInfo := range expectedIndexInfos {
  240. index, err := newIndexInfo(config, indexName)
  241. if err != nil {
  242. t.Fatal(err)
  243. } else {
  244. assert.Check(t, is.Equal(index.Name, expectedIndexInfo.Name), indexName+" name")
  245. assert.Check(t, is.Equal(index.Official, expectedIndexInfo.Official), indexName+" is official")
  246. assert.Check(t, is.Equal(index.Secure, expectedIndexInfo.Secure), indexName+" is secure")
  247. assert.Check(t, is.Equal(len(index.Mirrors), len(expectedIndexInfo.Mirrors)), indexName+" mirrors")
  248. }
  249. }
  250. }
  251. config := emptyServiceConfig
  252. var noMirrors []string
  253. expectedIndexInfos := map[string]*registry.IndexInfo{
  254. IndexName: {
  255. Name: IndexName,
  256. Official: true,
  257. Secure: true,
  258. Mirrors: noMirrors,
  259. },
  260. "index." + IndexName: {
  261. Name: IndexName,
  262. Official: true,
  263. Secure: true,
  264. Mirrors: noMirrors,
  265. },
  266. "example.com": {
  267. Name: "example.com",
  268. Official: false,
  269. Secure: true,
  270. Mirrors: noMirrors,
  271. },
  272. "127.0.0.1:5000": {
  273. Name: "127.0.0.1:5000",
  274. Official: false,
  275. Secure: false,
  276. Mirrors: noMirrors,
  277. },
  278. }
  279. testIndexInfo(config, expectedIndexInfos)
  280. publicMirrors := []string{"http://mirror1.local", "http://mirror2.local"}
  281. var err error
  282. config, err = makeServiceConfig(publicMirrors, []string{"example.com"})
  283. if err != nil {
  284. t.Fatal(err)
  285. }
  286. expectedIndexInfos = map[string]*registry.IndexInfo{
  287. IndexName: {
  288. Name: IndexName,
  289. Official: true,
  290. Secure: true,
  291. Mirrors: publicMirrors,
  292. },
  293. "index." + IndexName: {
  294. Name: IndexName,
  295. Official: true,
  296. Secure: true,
  297. Mirrors: publicMirrors,
  298. },
  299. "example.com": {
  300. Name: "example.com",
  301. Official: false,
  302. Secure: false,
  303. Mirrors: noMirrors,
  304. },
  305. "example.com:5000": {
  306. Name: "example.com:5000",
  307. Official: false,
  308. Secure: true,
  309. Mirrors: noMirrors,
  310. },
  311. "127.0.0.1": {
  312. Name: "127.0.0.1",
  313. Official: false,
  314. Secure: false,
  315. Mirrors: noMirrors,
  316. },
  317. "127.0.0.1:5000": {
  318. Name: "127.0.0.1:5000",
  319. Official: false,
  320. Secure: false,
  321. Mirrors: noMirrors,
  322. },
  323. "other.com": {
  324. Name: "other.com",
  325. Official: false,
  326. Secure: true,
  327. Mirrors: noMirrors,
  328. },
  329. }
  330. testIndexInfo(config, expectedIndexInfos)
  331. config, err = makeServiceConfig(nil, []string{"42.42.0.0/16"})
  332. if err != nil {
  333. t.Fatal(err)
  334. }
  335. expectedIndexInfos = map[string]*registry.IndexInfo{
  336. "example.com": {
  337. Name: "example.com",
  338. Official: false,
  339. Secure: false,
  340. Mirrors: noMirrors,
  341. },
  342. "example.com:5000": {
  343. Name: "example.com:5000",
  344. Official: false,
  345. Secure: false,
  346. Mirrors: noMirrors,
  347. },
  348. "127.0.0.1": {
  349. Name: "127.0.0.1",
  350. Official: false,
  351. Secure: false,
  352. Mirrors: noMirrors,
  353. },
  354. "127.0.0.1:5000": {
  355. Name: "127.0.0.1:5000",
  356. Official: false,
  357. Secure: false,
  358. Mirrors: noMirrors,
  359. },
  360. "other.com": {
  361. Name: "other.com",
  362. Official: false,
  363. Secure: true,
  364. Mirrors: noMirrors,
  365. },
  366. }
  367. testIndexInfo(config, expectedIndexInfos)
  368. }
  369. func TestMirrorEndpointLookup(t *testing.T) {
  370. containsMirror := func(endpoints []APIEndpoint) bool {
  371. for _, pe := range endpoints {
  372. if pe.URL.Host == "my.mirror" {
  373. return true
  374. }
  375. }
  376. return false
  377. }
  378. cfg, err := makeServiceConfig([]string{"https://my.mirror"}, nil)
  379. if err != nil {
  380. t.Fatal(err)
  381. }
  382. s := Service{config: cfg}
  383. imageName, err := reference.WithName(IndexName + "/test/image")
  384. if err != nil {
  385. t.Error(err)
  386. }
  387. pushAPIEndpoints, err := s.LookupPushEndpoints(reference.Domain(imageName))
  388. if err != nil {
  389. t.Fatal(err)
  390. }
  391. if containsMirror(pushAPIEndpoints) {
  392. t.Fatal("Push endpoint should not contain mirror")
  393. }
  394. pullAPIEndpoints, err := s.LookupPullEndpoints(reference.Domain(imageName))
  395. if err != nil {
  396. t.Fatal(err)
  397. }
  398. if !containsMirror(pullAPIEndpoints) {
  399. t.Fatal("Pull endpoint should contain mirror")
  400. }
  401. }
  402. func TestAllowNondistributableArtifacts(t *testing.T) {
  403. tests := []struct {
  404. addr string
  405. registries []string
  406. expected bool
  407. }{
  408. {IndexName, nil, false},
  409. {"example.com", []string{}, false},
  410. {"example.com", []string{"example.com"}, true},
  411. {"localhost", []string{"localhost:5000"}, false},
  412. {"localhost:5000", []string{"localhost:5000"}, true},
  413. {"localhost", []string{"example.com"}, false},
  414. {"127.0.0.1:5000", []string{"127.0.0.1:5000"}, true},
  415. {"localhost", nil, false},
  416. {"localhost:5000", nil, false},
  417. {"127.0.0.1", nil, false},
  418. {"localhost", []string{"example.com"}, false},
  419. {"127.0.0.1", []string{"example.com"}, false},
  420. {"example.com", nil, false},
  421. {"example.com", []string{"example.com"}, true},
  422. {"127.0.0.1", []string{"example.com"}, false},
  423. {"127.0.0.1:5000", []string{"example.com"}, false},
  424. {"example.com:5000", []string{"42.42.0.0/16"}, true},
  425. {"example.com", []string{"42.42.0.0/16"}, true},
  426. {"example.com:5000", []string{"42.42.42.42/8"}, true},
  427. {"127.0.0.1:5000", []string{"127.0.0.0/8"}, true},
  428. {"42.42.42.42:5000", []string{"42.1.1.1/8"}, true},
  429. {"invalid.example.com", []string{"42.42.0.0/16"}, false},
  430. {"invalid.example.com", []string{"invalid.example.com"}, true},
  431. {"invalid.example.com:5000", []string{"invalid.example.com"}, false},
  432. {"invalid.example.com:5000", []string{"invalid.example.com:5000"}, true},
  433. }
  434. for _, tt := range tests {
  435. config, err := newServiceConfig(ServiceOptions{
  436. AllowNondistributableArtifacts: tt.registries,
  437. })
  438. if err != nil {
  439. t.Error(err)
  440. }
  441. if v := config.allowNondistributableArtifacts(tt.addr); v != tt.expected {
  442. t.Errorf("allowNondistributableArtifacts failed for %q %v, expected %v got %v", tt.addr, tt.registries, tt.expected, v)
  443. }
  444. }
  445. }
  446. func TestIsSecureIndex(t *testing.T) {
  447. tests := []struct {
  448. addr string
  449. insecureRegistries []string
  450. expected bool
  451. }{
  452. {IndexName, nil, true},
  453. {"example.com", []string{}, true},
  454. {"example.com", []string{"example.com"}, false},
  455. {"localhost", []string{"localhost:5000"}, false},
  456. {"localhost:5000", []string{"localhost:5000"}, false},
  457. {"localhost", []string{"example.com"}, false},
  458. {"127.0.0.1:5000", []string{"127.0.0.1:5000"}, false},
  459. {"localhost", nil, false},
  460. {"localhost:5000", nil, false},
  461. {"127.0.0.1", nil, false},
  462. {"localhost", []string{"example.com"}, false},
  463. {"127.0.0.1", []string{"example.com"}, false},
  464. {"example.com", nil, true},
  465. {"example.com", []string{"example.com"}, false},
  466. {"127.0.0.1", []string{"example.com"}, false},
  467. {"127.0.0.1:5000", []string{"example.com"}, false},
  468. {"example.com:5000", []string{"42.42.0.0/16"}, false},
  469. {"example.com", []string{"42.42.0.0/16"}, false},
  470. {"example.com:5000", []string{"42.42.42.42/8"}, false},
  471. {"127.0.0.1:5000", []string{"127.0.0.0/8"}, false},
  472. {"42.42.42.42:5000", []string{"42.1.1.1/8"}, false},
  473. {"invalid.example.com", []string{"42.42.0.0/16"}, true},
  474. {"invalid.example.com", []string{"invalid.example.com"}, false},
  475. {"invalid.example.com:5000", []string{"invalid.example.com"}, true},
  476. {"invalid.example.com:5000", []string{"invalid.example.com:5000"}, false},
  477. }
  478. for _, tt := range tests {
  479. config, err := makeServiceConfig(nil, tt.insecureRegistries)
  480. if err != nil {
  481. t.Error(err)
  482. }
  483. if sec := config.isSecureIndex(tt.addr); sec != tt.expected {
  484. t.Errorf("isSecureIndex failed for %q %v, expected %v got %v", tt.addr, tt.insecureRegistries, tt.expected, sec)
  485. }
  486. }
  487. }