Ejemplo n.º 1
0
    def map_variables(
        self, fields: Dict[str, "ModelField"], suppress_warnings: bool = False
    ) -> Dict[str, Any]:
        """
        Map CI variables to settings

        If the source name starts with '=', get the value from mapper's
        attribute. Otwerwise read the value from environment.
        """
        values = {}

        for name_to, name_from in self.MAPPING.items():
            if name_to not in fields:
                if not suppress_warnings:
                    logger.warning(
                        message=f"CI variable mapping failed, no setting called {name_to}"
                    )
                continue

            if name_from.startswith("="):
                name_from = name_from[1:]
                try:
                    value = getattr(self, name_from)
                except AttributeError:
                    if not suppress_warnings:
                        logger.warning(
                            message=f"CI variable mapping failed, no mapper attribute called {name_from}"
                        )
                    continue
            elif name_from in os.environ:
                raw_value = os.environ.get(name_from)
                value, error = fields[name_to].validate(raw_value, {}, loc=name_from)
                if error:
                    raise ImproperlyConfigured(
                        f"Invalid valid value: {name_from}={raw_value}"
                    )
            else:
                value = None

            if value is not None:
                values[name_to] = value

        return values
Ejemplo n.º 2
0
    def _parse_file_secrets(
            self,
            filesecrets: Dict[str,
                              str]) -> Tuple[Dict[str, str], Dict[str, str]]:
        if settings.active_ci:
            valid_prefixes = settings.active_ci.VALID_FILE_SECRET_PATH_PREFIXES
        else:
            raise ImproperlyConfigured("An active CI is needed")

        filecontents = {}
        mapping = {}
        for name, filename in filesecrets.items():
            path = Path(filename)
            if not validate_file_secret_path(path, valid_prefixes):
                logger.warning(f'Not a valid file path: "{path}". Skipping.')
                continue
            try:
                filecontents[name] = self._b64_encode_file(path)
                mapping[name] = f"{settings.K8S_FILE_SECRET_MOUNTPATH}/{name}"
            except OSError as e:
                logger.error(f'Error while reading a file: "{path}"', error=e)

        return filecontents, mapping