def proof(path, is_bundle, debug): path = os.path.abspath(path) home_path = utils.get_home() if not home_path: # expansion failed return ['Could not determine home directory'], 200 if not path.startswith(home_path): return [ 'For security reasons, only paths under ' 'your home directory can be accessed' ], 200 if not is_bundle: try: c = Charm(path) except: try: c = Bundle(path, debug) except Exception as e: return [ "FATAL: No bundle.yaml (Bundle) or metadata.yaml " "(Charm) found, cannot proof" ], 200 else: try: c = Bundle(path, debug) except Exception as e: return ["FATAL: %s" % e.message], 200 lint, err_code = c.proof() return lint, err_code
def proof(path, is_bundle, debug): messages = [] exit_code = 0 path = os.path.abspath(path) home_path = utils.get_home() home_msg = ('For security reasons, only paths under ' 'your home directory can be accessed') if not home_path: # expansion failed messages.append('Could not determine home directory') messages.append(home_msg) elif not path.startswith(home_path): messages.append('The path {} is not under your ' 'home directory'.format(path)) messages.append(home_msg) if not os.access(path, os.R_OK): messages.append('Unable to read from {}'.format(path)) exit_code = 200 return messages, exit_code if not is_bundle: try: c = Charm(path) except: try: c = Bundle(path, debug) except Exception as e: return ["FATAL: No bundle.yaml (Bundle) or metadata.yaml " "(Charm) found, cannot proof"], 200 else: try: c = Bundle(path, debug) except Exception as e: return ["FATAL: %s" % e.message], 200 lint, err_code = c.proof() return lint, err_code
def create_charm(self): """Gather user configuration and hand it off to the template plugin to create the files and directories for the new charm. """ home_path = get_home() output_path = self._get_output_path() if not home_path: # expansion failed raise CharmGeneratorException('Could not determine home directory') if not os.path.abspath(output_path).startswith(home_path): raise CharmGeneratorException('For security reasons, only paths ' 'under your home directory can be ' 'accessed') if os.path.exists(output_path): raise CharmGeneratorException( '{} exists. Please move it out of the way.'.format( output_path)) log.info('Generating charm for %s in %s', self.opts.charmname, output_path) metadata = self._get_metadata() user_config = self._get_user_config() user_config.update(metadata=metadata) tempdir = self._get_tempdir() try: self.plugin.create_charm(user_config, tempdir) shutil.copytree(tempdir, output_path, symlinks=True) finally: self._cleanup(tempdir)
def _check_home(self, path_to_check): home_dir = utils.get_home() home_msg = ('For security reasons, only paths under your ' 'home directory can be accessed, including ' 'the build output dir, JUJU_REPOSITORY, ' 'LAYER_PATH, INTERFACE_PATH, and any ' 'wheelhouse overrides.') if not home_dir: # expansion failed if not self._warned_home: log.warn(home_msg) log.warn('Could not determine home directory.') self._warned_home = True elif not os.path.abspath(path_to_check).startswith(home_dir): if not self._warned_home: log.warn(home_msg) log.warn('The path {} is not under your ' 'home directory.'.format(path_to_check)) self._warned_home = True
def _check_path(self, path_to_check, need_write=False): home_dir = utils.get_home() home_msg = ('For security reasons, only paths under your ' 'home directory can be accessed, including ' 'the build output dir, JUJU_REPOSITORY, ' 'LAYER_PATH, INTERFACE_PATH, and any ' 'wheelhouse overrides.') if not home_dir: # expansion failed log.warn('Could not determine home directory.') log.warn(home_msg) elif os.path.abspath(path_to_check).startswith(home_dir): log.warn('The path {} is not under your ' 'home directory.'.format(home_dir)) log.warn(home_msg) if not os.access(path_to_check, os.R_OK): raise BuildError('Unable to read from: {}'.format(path_to_check)) if need_write and not os.access(path_to_check, os.W_OK): raise BuildError('Unable to write to: {}'.format(path_to_check))
def test_get_no_home(self): # some uids don't have pw_names with mock.patch('os.getuid', lambda: 12): self.assertIs(utils.get_home(), None)
def test_get_home(self): # expanduser('~') works in test env, but not in snap assert utils.get_home() == os.path.expanduser('~') with mock.patch('os.path.expanduser', lambda u: u): assert utils.get_home() is None
def download_item(item, dir_): series_dir = None if item.startswith(LAYER_PREFIX): dir_ = dir_ or os.environ.get('LAYER_PATH') name = item[len(LAYER_PREFIX):] elif item.startswith(INTERFACE_PREFIX): dir_ = dir_ or os.environ.get('INTERFACE_PATH') name = item[len(INTERFACE_PREFIX):] else: dir_ = dir_ or os.environ.get('JUJU_REPOSITORY') if not item.startswith(CHARM_PREFIX): item = CHARM_PREFIX + item url_parts = item[len(CHARM_PREFIX):].split('/') name = url_parts[-1] if len(url_parts) == 2 and not url_parts[0].startswith('~'): series_dir = url_parts[0] elif len(url_parts) == 3: series_dir = url_parts[1] dir_ = dir_ or os.getcwd() dir_ = os.path.abspath(os.path.expanduser(dir_)) home_path = utils.get_home() home_msg = ('For security reasons, only paths under ' 'your home directory can be accessed') if not home_path: # expansion failed print('Could not determine home directory') print(home_msg) elif not dir_.startswith(home_path): print('The path {} is not under your home directory'.format(home_path)) print(home_msg) if not os.access(dir_, os.W_OK): print('Unable to write to {}'.format(dir_)) return 200 # Create series dir if we need to if series_dir: series_path = os.path.join(dir_, series_dir) if not os.path.exists(series_path): os.mkdir(series_path) dir_ = series_path # Abort if destination dir already exists final_dest_dir = os.path.join(dir_, name) if os.path.exists(final_dest_dir): print("{}: {}".format(ERR_DIR_EXISTS, final_dest_dir)) return 1 # Create tempdir for initial download tempdir = tempfile.mkdtemp() atexit.register(shutil.rmtree, tempdir) try: # Download the item fetcher = fetchers.get_fetcher(item) download_dir = fetcher.fetch(tempdir) except fetchers.FetchError: print("Can't find source for {}".format(item)) return 1 # Copy download dir to final destination dir shutil.copytree(download_dir, final_dest_dir, symlinks=True) print('Downloaded {} to {}'.format(item, final_dest_dir))
def _check_path(self, path_to_check): home_dir = utils.get_home() if not home_dir: # expansion failed raise BuildError('Could not determine home directory') return os.path.abspath(path_to_check).startswith(home_dir)