def load_yaml(path, a_file=True): f = None try: if a_file: f = codecs.open(path, encoding='utf-8', errors='strict') s = f.read() else: s = UrlUtils.get_url(path).content except requests.exceptions.Timeout as e: msg = (_('Timeout reaching server "%(path)s": Reason is %(reason)s.') % { 'path': path, 'reason': e }) ExceptionCollector.appendException(URLException(what=msg)) return except requests.exceptions.ConnectionError as e: msg = (_('Error reaching server "%(path)s": Reason is %(reason)s.') % { 'path': path, 'reason': e }) ExceptionCollector.appendException(URLException(what=msg)) return except requests.exceptions.HTTPError as e: msg = (_('Request error "%(path)s": Reason is %(reason)s.') % { 'path': path, 'reason': e }) ExceptionCollector.appendException(URLException(what=msg)) return except Exception as e: raise finally: if f: f.close() return yaml.load(s, Loader=yaml_loader)
def validate(self): """Validate the provided CSAR file.""" self.is_validated = True # validate that the file or URL exists missing_err_msg = (_('"%s" does not exist.') % self.path) if self.a_file: if not os.path.isfile(self.path): ExceptionCollector.appendException( ValidationError(message=missing_err_msg)) return False else: self.csar = self.path else: # a URL if not UrlUtils.validate_url(self.path): ExceptionCollector.appendException( ValidationError(message=missing_err_msg)) return False else: self.csar = BytesIO(UrlUtils.get_url(self.path).content) # validate that it is a valid zip file if not zipfile.is_zipfile(self.csar): err_msg = (_('"%s" is not a valid zip file.') % self.path) ExceptionCollector.appendException( ValidationError(message=err_msg)) return False # validate that it contains the metadata file in the correct location self.zfile = zipfile.ZipFile(self.csar, 'r') filelist = self.zfile.namelist() if 'TOSCA-Metadata/TOSCA.meta' not in filelist: err_msg = (_('"%s" is not a valid CSAR as it does not contain the ' 'required file "TOSCA.meta" in the folder ' '"TOSCA-Metadata".') % self.path) ExceptionCollector.appendException( ValidationError(message=err_msg)) return False # validate that 'Entry-Definitions' property exists in TOSCA.meta data = self.zfile.read('TOSCA-Metadata/TOSCA.meta') invalid_yaml_err_msg = (_('The file "TOSCA-Metadata/TOSCA.meta" in ' 'the CSAR "%s" does not contain valid YAML ' 'content.') % self.path) try: meta = yaml.load(data) if type(meta) is dict: self.metadata = meta else: ExceptionCollector.appendException( ValidationError(message=invalid_yaml_err_msg)) return False except yaml.YAMLError: ExceptionCollector.appendException( ValidationError(message=invalid_yaml_err_msg)) return False if 'Entry-Definitions' not in self.metadata: err_msg = (_('The CSAR "%s" is missing the required metadata ' '"Entry-Definitions" in ' '"TOSCA-Metadata/TOSCA.meta".') % self.path) ExceptionCollector.appendException( ValidationError(message=err_msg)) return False # validate that 'Entry-Definitions' metadata value points to an # existing file in the CSAR entry = self.metadata.get('Entry-Definitions') if entry and entry not in filelist: err_msg = (_('The "Entry-Definitions" file defined in the ' 'CSAR "%s" does not exist.') % self.path) ExceptionCollector.appendException( ValidationError(message=err_msg)) return False # validate that external references in the main template actually # exist and are accessible self._validate_external_references() return not self.error_caught