Exemplo n.º 1
0
 def setup_method(self):
     self.tmp_keys = []
     if sys.platform.startswith('linux'):
         if find_executable('docker-credential-' + DEFAULT_LINUX_STORE):
             self.store = Store(DEFAULT_LINUX_STORE)
         elif find_executable('docker-credential-pass'):
             self.store = Store('pass')
         else:
             raise Exception('No supported docker-credential store in PATH')
     elif sys.platform.startswith('darwin'):
         self.store = Store(DEFAULT_OSX_STORE)
Exemplo n.º 2
0
    def get_credential_store_instance(self, registry, dockercfg_path):
        '''
        Return an instance of docker.credentials.Store used by the given registry.

        :return: A Store or None
        :rtype: Union[docker.credentials.Store, NoneType]
        '''

        # Older versions of docker-py don't have this feature.
        try:
            credstore_env = self.client.credstore_env
        except AttributeError:
            credstore_env = None

        config = auth.load_config(config_path=dockercfg_path)

        if hasattr(auth, 'get_credential_store'):
            store_name = auth.get_credential_store(config, registry)
        elif 'credsStore' in config:
            store_name = config['credsStore']
        else:
            store_name = None

        # Make sure that there is a credential helper before trying to instantiate a
        # Store object.
        if store_name:
            self.log("Found credential store %s" % store_name)
            return Store(store_name, environment=credstore_env)

        return DockerFileStore(dockercfg_path)
Exemplo n.º 3
0
 def setup_method(self):
     self.tmp_keys = []
     if sys.platform.startswith('linux'):
         if find_executable('docker-credential-' + DEFAULT_LINUX_STORE):
             self.store = Store(DEFAULT_LINUX_STORE)
         elif find_executable('docker-credential-pass'):
             self.store = Store('pass')
         else:
             raise Exception('No supported docker-credential store in PATH')
     elif sys.platform.startswith('darwin'):
         self.store = Store(DEFAULT_OSX_STORE)
Exemplo n.º 4
0
class TestStore(object):
    def teardown_method(self):
        for server in self.tmp_keys:
            try:
                self.store.erase(server)
            except StoreError:
                pass

    def setup_method(self):
        self.tmp_keys = []
        if sys.platform.startswith('linux'):
            if find_executable('docker-credential-' + DEFAULT_LINUX_STORE):
                self.store = Store(DEFAULT_LINUX_STORE)
            elif find_executable('docker-credential-pass'):
                self.store = Store('pass')
            else:
                raise Exception('No supported docker-credential store in PATH')
        elif sys.platform.startswith('darwin'):
            self.store = Store(DEFAULT_OSX_STORE)

    def get_random_servername(self):
        res = 'pycreds_test_{:x}'.format(random.getrandbits(32))
        self.tmp_keys.append(res)
        return res

    def test_store_and_get(self):
        key = self.get_random_servername()
        self.store.store(server=key, username='******', secret='pass')
        data = self.store.get(key)
        assert data == {
            'ServerURL': key,
            'Username': '******',
            'Secret': 'pass'
        }

    def test_get_nonexistent(self):
        key = self.get_random_servername()
        with pytest.raises(CredentialsNotFound):
            self.store.get(key)

    def test_store_and_erase(self):
        key = self.get_random_servername()
        self.store.store(server=key, username='******', secret='pass')
        self.store.erase(key)
        with pytest.raises(CredentialsNotFound):
            self.store.get(key)

    def test_unicode_strings(self):
        key = self.get_random_servername()
        key = six.u(key)
        self.store.store(server=key, username='******', secret='pass')
        data = self.store.get(key)
        assert data
        self.store.erase(key)
        with pytest.raises(CredentialsNotFound):
            self.store.get(key)

    def test_list(self):
        names = (self.get_random_servername(), self.get_random_servername())
        self.store.store(names[0], username='******', secret='izayoi')
        self.store.store(names[1], username='******', secret='hakurei')
        data = self.store.list()
        assert names[0] in data
        assert data[names[0]] == 'sakuya'
        assert names[1] in data
        assert data[names[1]] == 'reimu'

    def test_execute_with_env_override(self):
        self.store.exe = 'env'
        self.store.environment = {'FOO': 'bar'}
        data = self.store._execute('--null', '')
        assert b'\0FOO=bar\0' in data
        assert 'FOO' not in os.environ
Exemplo n.º 5
0
class TestStore:
    def teardown_method(self):
        for server in self.tmp_keys:
            try:
                self.store.erase(server)
            except StoreError:
                pass

    def setup_method(self):
        self.tmp_keys = []
        if sys.platform.startswith('linux'):
            if find_executable('docker-credential-' + DEFAULT_LINUX_STORE):
                self.store = Store(DEFAULT_LINUX_STORE)
            elif find_executable('docker-credential-pass'):
                self.store = Store('pass')
            else:
                raise Exception('No supported docker-credential store in PATH')
        elif sys.platform.startswith('darwin'):
            self.store = Store(DEFAULT_OSX_STORE)

    def get_random_servername(self):
        res = f'pycreds_test_{random.getrandbits(32):x}'
        self.tmp_keys.append(res)
        return res

    def test_store_and_get(self):
        key = self.get_random_servername()
        self.store.store(server=key, username='******', secret='pass')
        data = self.store.get(key)
        assert data == {'ServerURL': key, 'Username': '******', 'Secret': 'pass'}

    def test_get_nonexistent(self):
        key = self.get_random_servername()
        with pytest.raises(CredentialsNotFound):
            self.store.get(key)

    def test_store_and_erase(self):
        key = self.get_random_servername()
        self.store.store(server=key, username='******', secret='pass')
        self.store.erase(key)
        with pytest.raises(CredentialsNotFound):
            self.store.get(key)

    def test_unicode_strings(self):
        key = self.get_random_servername()
        key = key
        self.store.store(server=key, username='******', secret='pass')
        data = self.store.get(key)
        assert data
        self.store.erase(key)
        with pytest.raises(CredentialsNotFound):
            self.store.get(key)

    def test_list(self):
        names = (self.get_random_servername(), self.get_random_servername())
        self.store.store(names[0], username='******', secret='izayoi')
        self.store.store(names[1], username='******', secret='hakurei')
        data = self.store.list()
        assert names[0] in data
        assert data[names[0]] == 'sakuya'
        assert names[1] in data
        assert data[names[1]] == 'reimu'

    def test_execute_with_env_override(self):
        self.store.exe = 'env'
        self.store.environment = {'FOO': 'bar'}
        data = self.store._execute('--null', '')
        assert b'\0FOO=bar\0' in data
        assert 'FOO' not in os.environ