def test_repo_first_time(self): """ Tests adding keys to a repo that has never had keys before (i.e. the repo keys dir doesn't exist). """ # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo1') repo_keys.add_key('key1', 'KEY1') repo_keys.add_key('key2', 'KEY2') repo_keys.update_filesystem() # Verify self.assertTrue( os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo1'))) key_files = repo_keys.key_filenames() self.assertEqual(2, len(key_files)) for f in key_files: self.assertTrue(os.path.exists(f)) f = open(key_files[0], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEY1') f = open(key_files[1], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEY2')
def test_repo_first_time(self): """ Tests adding keys to a repo that has never had keys before (i.e. the repo keys dir doesn't exist). """ # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo1') repo_keys.add_key('key1', 'KEY1') repo_keys.add_key('key2', 'KEY2') repo_keys.update_filesystem() # Verify self.assertTrue(os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo1'))) key_files = repo_keys.key_filenames() self.assertEqual(2, len(key_files)) for f in key_files: self.assertTrue(os.path.exists(f)) f = open(key_files[0], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEY1') f = open(key_files[1], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEY2')
def test_repo_remove_keys(self): """ Tests calling update_filesystem for a repo that previously had keys but no longer does. """ # Setup repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo4') repo_keys.add_key('key', 'KEY1') repo_keys.update_filesystem() self.assertTrue(os.path.exists(repo_keys.key_filenames()[0])) # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo4') repo_keys.update_filesystem() # Verify self.assertTrue(not os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo3'))) key_list = repo_keys.key_filenames() self.assertTrue(key_list is not None) self.assertEqual(0, len(key_list))
def _handle_gpg_keys(repo, gpg_keys, keys_root_dir): """ Handles the processing of any GPG keys that were specified with the repo. The key files will be written to disk, deleting any existing key files that were there. The repo object will be updated with any values related to GPG key information. """ repo_keys = RepoKeyFiles(keys_root_dir, repo.id) if gpg_keys is not None and len(gpg_keys) > 0: repo['gpgcheck'] = '1' for key_name in gpg_keys: repo_keys.add_key(key_name, gpg_keys[key_name]) key_urls = ['file:' + kfn for kfn in repo_keys.key_filenames()] repo['gpgkey'] = '\n'.join(key_urls) else: repo['gpgcheck'] = '0' repo['gpgkey'] = None # Call this in either case to make sure any existing keys were deleted repo_keys.update_filesystem()
def unbind(repo_filename, mirror_list_filename, keys_root_dir, cert_root_dir, repo_id, lock=None): """ Removes the repo identified by repo_id from the given repo file. If the repo is not bound, this call has no effect. If the mirror list file exists, it will be deleted. The default lock is defined at the module level and is used to ensure that concurrent access to the give files is prevented. Specific locks can be passed in for testing purposes to circumvent the default location of the lock which requires root access. @param repo_filename: full path to the location of the repo file in which the repo will be removed; if this file does not exist this call has no effect @type repo_filename: string @param mirror_list_filename: full path to the location of the mirror list file that may exist for the given repo; if the file does not exist this field will be ignored @type mirror_list_filename: string @param keys_root_dir: absolute path to the root directory in which the keys for all repos will be stored @type keys_root_dir: string @param cert_root_dir: absolute path to the root directory in which the certs for all repos will be stored @type cert_root_dir: string @param repo_id: identifies the repo in the repo file to delete @type repo_id: string @param lock: if the default lock is unacceptable, it may be overridden in this variable @type lock: L{Lock} """ if not lock: lock = Lock(LOCK_FILE) lock.acquire() try: log.info('Unbinding repo [%s]' % repo_id) if not os.path.exists(repo_filename): return # Repo file changes repo_file = RepoFile(repo_filename) repo_file.load() repo_file.remove_repo_by_name(repo_id) # will not throw an error if repo doesn't exist repo_file.save() # Mirror list removal if os.path.exists(mirror_list_filename): os.remove(mirror_list_filename) # Keys removal repo_keys = RepoKeyFiles(keys_root_dir, repo_id) repo_keys.update_filesystem() # cert removal certificates = CertFiles(cert_root_dir, repo_id) certificates.apply() finally: lock.release()
def test_repo_update_key(self): """ Tests adding new contents for a key that already exists. """ # Setup repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo3') repo_keys.add_key('key', 'KEY1') repo_keys.update_filesystem() # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo3') repo_keys.add_key('key', 'KEYX') repo_keys.update_filesystem() # Verify self.assertTrue(os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo3'))) key_files = repo_keys.key_filenames() self.assertEqual(1, len(key_files)) self.assertTrue(os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo3', 'key'))) f = open(key_files[0], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEYX')
def test_repo_existing_keys(self): """ Tests adding a new key when keys have already been written. The new key should be present but the old should be deleted. """ # Setup repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo2') repo_keys.add_key('key1', 'KEY1') repo_keys.update_filesystem() # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo2') repo_keys.add_key('keyX', 'KEYX') repo_keys.update_filesystem() # Verify self.assertTrue(os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo2'))) key_files = repo_keys.key_filenames() self.assertEqual(1, len(key_files)) self.assertTrue(os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo2', 'keyX'))) self.assertTrue(not os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo2', 'key1'))) f = open(key_files[0], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEYX')
def test_repo_remove_keys(self): """ Tests calling update_filesystem for a repo that previously had keys but no longer does. """ # Setup repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo4') repo_keys.add_key('key', 'KEY1') repo_keys.update_filesystem() self.assertTrue(os.path.exists(repo_keys.key_filenames()[0])) # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo4') repo_keys.update_filesystem() # Verify self.assertTrue( not os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo3'))) key_list = repo_keys.key_filenames() self.assertTrue(key_list is not None) self.assertEqual(0, len(key_list))
def test_repo_update_key(self): """ Tests adding new contents for a key that already exists. """ # Setup repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo3') repo_keys.add_key('key', 'KEY1') repo_keys.update_filesystem() # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo3') repo_keys.add_key('key', 'KEYX') repo_keys.update_filesystem() # Verify self.assertTrue( os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo3'))) key_files = repo_keys.key_filenames() self.assertEqual(1, len(key_files)) self.assertTrue( os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo3', 'key'))) f = open(key_files[0], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEYX')
def test_repo_existing_keys(self): """ Tests adding a new key when keys have already been written. The new key should be present but the old should be deleted. """ # Setup repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo2') repo_keys.add_key('key1', 'KEY1') repo_keys.update_filesystem() # Test repo_keys = RepoKeyFiles(TEST_KEYS_ROOT_DIR, 'repo2') repo_keys.add_key('keyX', 'KEYX') repo_keys.update_filesystem() # Verify self.assertTrue( os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo2'))) key_files = repo_keys.key_filenames() self.assertEqual(1, len(key_files)) self.assertTrue( os.path.exists(os.path.join(TEST_KEYS_ROOT_DIR, 'repo2', 'keyX'))) self.assertTrue(not os.path.exists( os.path.join(TEST_KEYS_ROOT_DIR, 'repo2', 'key1'))) f = open(key_files[0], 'r') contents = f.read() f.close() self.assertEqual(contents, 'KEYX')