|
10 | 10 | import unittest
|
11 | 11 |
|
12 | 12 | from docker import auth, errors
|
| 13 | +import dockerpycreds |
13 | 14 | import pytest
|
14 | 15 |
|
15 | 16 | try:
|
@@ -226,57 +227,6 @@ def test_resolve_auth_with_empty_credstore_and_auth_dict(self):
|
226 | 227 | )['username']
|
227 | 228 |
|
228 | 229 |
|
229 |
| -class CredStoreTest(unittest.TestCase): |
230 |
| - def test_get_credential_store(self): |
231 |
| - auth_config = auth.AuthConfig({ |
232 |
| - 'credHelpers': { |
233 |
| - 'registry1.io': 'truesecret', |
234 |
| - 'registry2.io': 'powerlock' |
235 |
| - }, |
236 |
| - 'credsStore': 'blackbox', |
237 |
| - }) |
238 |
| - |
239 |
| - assert auth.get_credential_store( |
240 |
| - auth_config, 'registry1.io' |
241 |
| - ) == 'truesecret' |
242 |
| - assert auth.get_credential_store( |
243 |
| - auth_config, 'registry2.io' |
244 |
| - ) == 'powerlock' |
245 |
| - assert auth.get_credential_store( |
246 |
| - auth_config, 'registry3.io' |
247 |
| - ) == 'blackbox' |
248 |
| - |
249 |
| - def test_get_credential_store_no_default(self): |
250 |
| - auth_config = auth.AuthConfig({ |
251 |
| - 'credHelpers': { |
252 |
| - 'registry1.io': 'truesecret', |
253 |
| - 'registry2.io': 'powerlock' |
254 |
| - }, |
255 |
| - }) |
256 |
| - assert auth.get_credential_store( |
257 |
| - auth_config, 'registry2.io' |
258 |
| - ) == 'powerlock' |
259 |
| - assert auth.get_credential_store( |
260 |
| - auth_config, 'registry3.io' |
261 |
| - ) is None |
262 |
| - |
263 |
| - def test_get_credential_store_default_index(self): |
264 |
| - auth_config = auth.AuthConfig({ |
265 |
| - 'credHelpers': { |
266 |
| - 'https://index.docker.io/v1/': 'powerlock' |
267 |
| - }, |
268 |
| - 'credsStore': 'truesecret' |
269 |
| - }) |
270 |
| - |
271 |
| - assert auth.get_credential_store(auth_config, None) == 'powerlock' |
272 |
| - assert auth.get_credential_store( |
273 |
| - auth_config, 'docker.io' |
274 |
| - ) == 'powerlock' |
275 |
| - assert auth.get_credential_store( |
276 |
| - auth_config, 'images.io' |
277 |
| - ) == 'truesecret' |
278 |
| - |
279 |
| - |
280 | 230 | class LoadConfigTest(unittest.TestCase):
|
281 | 231 | def test_load_config_no_file(self):
|
282 | 232 | folder = tempfile.mkdtemp()
|
@@ -506,3 +456,232 @@ def test_load_config_identity_token(self):
|
506 | 456 | cfg = cfg.auths[registry]
|
507 | 457 | assert 'IdentityToken' in cfg
|
508 | 458 | assert cfg['IdentityToken'] == token
|
| 459 | + |
| 460 | + |
| 461 | +class CredstoreTest(unittest.TestCase): |
| 462 | + def setUp(self): |
| 463 | + self.authconfig = auth.AuthConfig({'credsStore': 'default'}) |
| 464 | + self.default_store = InMemoryStore('default') |
| 465 | + self.authconfig._stores['default'] = self.default_store |
| 466 | + self.default_store.store( |
| 467 | + 'https://gensokyo.jp/v2', 'sakuya', 'izayoi', |
| 468 | + ) |
| 469 | + self.default_store.store( |
| 470 | + 'https://default.com/v2', 'user', 'hunter2', |
| 471 | + ) |
| 472 | + |
| 473 | + def test_get_credential_store(self): |
| 474 | + auth_config = auth.AuthConfig({ |
| 475 | + 'credHelpers': { |
| 476 | + 'registry1.io': 'truesecret', |
| 477 | + 'registry2.io': 'powerlock' |
| 478 | + }, |
| 479 | + 'credsStore': 'blackbox', |
| 480 | + }) |
| 481 | + |
| 482 | + assert auth_config.get_credential_store('registry1.io') == 'truesecret' |
| 483 | + assert auth_config.get_credential_store('registry2.io') == 'powerlock' |
| 484 | + assert auth_config.get_credential_store('registry3.io') == 'blackbox' |
| 485 | + |
| 486 | + def test_get_credential_store_no_default(self): |
| 487 | + auth_config = auth.AuthConfig({ |
| 488 | + 'credHelpers': { |
| 489 | + 'registry1.io': 'truesecret', |
| 490 | + 'registry2.io': 'powerlock' |
| 491 | + }, |
| 492 | + }) |
| 493 | + assert auth_config.get_credential_store('registry2.io') == 'powerlock' |
| 494 | + assert auth_config.get_credential_store('registry3.io') is None |
| 495 | + |
| 496 | + def test_get_credential_store_default_index(self): |
| 497 | + auth_config = auth.AuthConfig({ |
| 498 | + 'credHelpers': { |
| 499 | + 'https://index.docker.io/v1/': 'powerlock' |
| 500 | + }, |
| 501 | + 'credsStore': 'truesecret' |
| 502 | + }) |
| 503 | + |
| 504 | + assert auth_config.get_credential_store(None) == 'powerlock' |
| 505 | + assert auth_config.get_credential_store('docker.io') == 'powerlock' |
| 506 | + assert auth_config.get_credential_store('images.io') == 'truesecret' |
| 507 | + |
| 508 | + def test_get_credential_store_with_plain_dict(self): |
| 509 | + auth_config = { |
| 510 | + 'credHelpers': { |
| 511 | + 'registry1.io': 'truesecret', |
| 512 | + 'registry2.io': 'powerlock' |
| 513 | + }, |
| 514 | + 'credsStore': 'blackbox', |
| 515 | + } |
| 516 | + |
| 517 | + assert auth.get_credential_store( |
| 518 | + auth_config, 'registry1.io' |
| 519 | + ) == 'truesecret' |
| 520 | + assert auth.get_credential_store( |
| 521 | + auth_config, 'registry2.io' |
| 522 | + ) == 'powerlock' |
| 523 | + assert auth.get_credential_store( |
| 524 | + auth_config, 'registry3.io' |
| 525 | + ) == 'blackbox' |
| 526 | + |
| 527 | + def test_get_all_credentials_credstore_only(self): |
| 528 | + assert self.authconfig.get_all_credentials() == { |
| 529 | + 'https://gensokyo.jp/v2': { |
| 530 | + 'Username': 'sakuya', |
| 531 | + 'Password': 'izayoi', |
| 532 | + 'ServerAddress': 'https://gensokyo.jp/v2', |
| 533 | + }, |
| 534 | + 'https://default.com/v2': { |
| 535 | + 'Username': 'user', |
| 536 | + 'Password': 'hunter2', |
| 537 | + 'ServerAddress': 'https://default.com/v2', |
| 538 | + }, |
| 539 | + } |
| 540 | + |
| 541 | + def test_get_all_credentials_with_empty_credhelper(self): |
| 542 | + self.authconfig['credHelpers'] = { |
| 543 | + 'registry1.io': 'truesecret', |
| 544 | + } |
| 545 | + self.authconfig._stores['truesecret'] = InMemoryStore() |
| 546 | + assert self.authconfig.get_all_credentials() == { |
| 547 | + 'https://gensokyo.jp/v2': { |
| 548 | + 'Username': 'sakuya', |
| 549 | + 'Password': 'izayoi', |
| 550 | + 'ServerAddress': 'https://gensokyo.jp/v2', |
| 551 | + }, |
| 552 | + 'https://default.com/v2': { |
| 553 | + 'Username': 'user', |
| 554 | + 'Password': 'hunter2', |
| 555 | + 'ServerAddress': 'https://default.com/v2', |
| 556 | + }, |
| 557 | + 'registry1.io': None, |
| 558 | + } |
| 559 | + |
| 560 | + def test_get_all_credentials_with_credhelpers_only(self): |
| 561 | + del self.authconfig['credsStore'] |
| 562 | + assert self.authconfig.get_all_credentials() == {} |
| 563 | + |
| 564 | + self.authconfig['credHelpers'] = { |
| 565 | + 'https://gensokyo.jp/v2': 'default', |
| 566 | + 'https://default.com/v2': 'default', |
| 567 | + } |
| 568 | + |
| 569 | + assert self.authconfig.get_all_credentials() == { |
| 570 | + 'https://gensokyo.jp/v2': { |
| 571 | + 'Username': 'sakuya', |
| 572 | + 'Password': 'izayoi', |
| 573 | + 'ServerAddress': 'https://gensokyo.jp/v2', |
| 574 | + }, |
| 575 | + 'https://default.com/v2': { |
| 576 | + 'Username': 'user', |
| 577 | + 'Password': 'hunter2', |
| 578 | + 'ServerAddress': 'https://default.com/v2', |
| 579 | + }, |
| 580 | + } |
| 581 | + |
| 582 | + def test_get_all_credentials_with_auths_entries(self): |
| 583 | + self.authconfig.add_auth('registry1.io', { |
| 584 | + 'ServerAddress': 'registry1.io', |
| 585 | + 'Username': 'reimu', |
| 586 | + 'Password': 'hakurei', |
| 587 | + }) |
| 588 | + |
| 589 | + assert self.authconfig.get_all_credentials() == { |
| 590 | + 'https://gensokyo.jp/v2': { |
| 591 | + 'Username': 'sakuya', |
| 592 | + 'Password': 'izayoi', |
| 593 | + 'ServerAddress': 'https://gensokyo.jp/v2', |
| 594 | + }, |
| 595 | + 'https://default.com/v2': { |
| 596 | + 'Username': 'user', |
| 597 | + 'Password': 'hunter2', |
| 598 | + 'ServerAddress': 'https://default.com/v2', |
| 599 | + }, |
| 600 | + 'registry1.io': { |
| 601 | + 'ServerAddress': 'registry1.io', |
| 602 | + 'Username': 'reimu', |
| 603 | + 'Password': 'hakurei', |
| 604 | + }, |
| 605 | + } |
| 606 | + |
| 607 | + def test_get_all_credentials_helpers_override_default(self): |
| 608 | + self.authconfig['credHelpers'] = { |
| 609 | + 'https://default.com/v2': 'truesecret', |
| 610 | + } |
| 611 | + truesecret = InMemoryStore('truesecret') |
| 612 | + truesecret.store('https://default.com/v2', 'reimu', 'hakurei') |
| 613 | + self.authconfig._stores['truesecret'] = truesecret |
| 614 | + assert self.authconfig.get_all_credentials() == { |
| 615 | + 'https://gensokyo.jp/v2': { |
| 616 | + 'Username': 'sakuya', |
| 617 | + 'Password': 'izayoi', |
| 618 | + 'ServerAddress': 'https://gensokyo.jp/v2', |
| 619 | + }, |
| 620 | + 'https://default.com/v2': { |
| 621 | + 'Username': 'reimu', |
| 622 | + 'Password': 'hakurei', |
| 623 | + 'ServerAddress': 'https://default.com/v2', |
| 624 | + }, |
| 625 | + } |
| 626 | + |
| 627 | + def test_get_all_credentials_3_sources(self): |
| 628 | + self.authconfig['credHelpers'] = { |
| 629 | + 'registry1.io': 'truesecret', |
| 630 | + } |
| 631 | + truesecret = InMemoryStore('truesecret') |
| 632 | + truesecret.store('registry1.io', 'reimu', 'hakurei') |
| 633 | + self.authconfig._stores['truesecret'] = truesecret |
| 634 | + self.authconfig.add_auth('registry2.io', { |
| 635 | + 'ServerAddress': 'registry2.io', |
| 636 | + 'Username': 'reimu', |
| 637 | + 'Password': 'hakurei', |
| 638 | + }) |
| 639 | + |
| 640 | + assert self.authconfig.get_all_credentials() == { |
| 641 | + 'https://gensokyo.jp/v2': { |
| 642 | + 'Username': 'sakuya', |
| 643 | + 'Password': 'izayoi', |
| 644 | + 'ServerAddress': 'https://gensokyo.jp/v2', |
| 645 | + }, |
| 646 | + 'https://default.com/v2': { |
| 647 | + 'Username': 'user', |
| 648 | + 'Password': 'hunter2', |
| 649 | + 'ServerAddress': 'https://default.com/v2', |
| 650 | + }, |
| 651 | + 'registry1.io': { |
| 652 | + 'ServerAddress': 'registry1.io', |
| 653 | + 'Username': 'reimu', |
| 654 | + 'Password': 'hakurei', |
| 655 | + }, |
| 656 | + 'registry2.io': { |
| 657 | + 'ServerAddress': 'registry2.io', |
| 658 | + 'Username': 'reimu', |
| 659 | + 'Password': 'hakurei', |
| 660 | + } |
| 661 | + } |
| 662 | + |
| 663 | + |
| 664 | +class InMemoryStore(dockerpycreds.Store): |
| 665 | + def __init__(self, *args, **kwargs): |
| 666 | + self.__store = {} |
| 667 | + |
| 668 | + def get(self, server): |
| 669 | + try: |
| 670 | + return self.__store[server] |
| 671 | + except KeyError: |
| 672 | + raise dockerpycreds.errors.CredentialsNotFound() |
| 673 | + |
| 674 | + def store(self, server, username, secret): |
| 675 | + self.__store[server] = { |
| 676 | + 'ServerURL': server, |
| 677 | + 'Username': username, |
| 678 | + 'Secret': secret, |
| 679 | + } |
| 680 | + |
| 681 | + def list(self): |
| 682 | + return dict( |
| 683 | + [(k, v['Username']) for k, v in self.__store.items()] |
| 684 | + ) |
| 685 | + |
| 686 | + def erase(self, server): |
| 687 | + del self.__store[server] |
0 commit comments