def testValidateSyntaxSimple(self): artifact = rdf_artifacts.Artifact(name="Foo", doc="This is Foo.", provides=["fqdn", "domain"], supported_os=["Windows"], urls=["https://example.com"]) ar.ValidateSyntax(artifact)
def testValidateSyntaxWithSources(self): registry_key_source = { "type": rdf_artifacts.ArtifactSource.SourceType.REGISTRY_KEY, "attributes": { "keys": [ r"%%current_control_set%%\Control\Session " r"Manager\Environment\Path" ], } } file_source = { "type": rdf_artifacts.ArtifactSource.SourceType.FILE, "attributes": { "paths": [r"%%environ_systemdrive%%\Temp"] } } artifact = rdf_artifacts.Artifact( name="Bar", doc="This is Bar.", provides=["environ_windir"], supported_os=["Windows"], urls=["https://example.com"], sources=[registry_key_source, file_source]) ar.ValidateSyntax(artifact)
def testValidateSyntaxMissingDoc(self): artifact = rdf_artifacts.Artifact( name="Baz", provides=["os"], supported_os=["Linux"]) with self.assertRaisesRegexp(rdf_artifacts.ArtifactSyntaxError, "missing doc"): ar.ValidateSyntax(artifact)
def testValidateSyntax(self): sources = [{ "type": rdf_artifacts.ArtifactSource.SourceType.REGISTRY_KEY, "attributes": { "keys": [ r"%%current_control_set%%\Control\Session " r"Manager\Environment\Path" ] } }, { "type": rdf_artifacts.ArtifactSource.SourceType.FILE, "attributes": { "paths": [r"%%environ_systemdrive%%\Temp"] } }] artifact = rdf_artifacts.Artifact( name="good", doc="Doco", provides=["environ_windir"], supported_os=["Windows"], urls=["http://blah"], sources=sources) ar.ValidateSyntax(artifact)
def UploadArtifactYamlFile(file_content, overwrite=True, overwrite_system_artifacts=False): """Upload a yaml or json file as an artifact to the datastore.""" loaded_artifacts = [] registry_obj = artifact_registry.REGISTRY # Make sure all artifacts are loaded so we don't accidentally overwrite one. registry_obj.GetArtifacts(reload_datastore_artifacts=True) new_artifacts = registry_obj.ArtifactsFromYaml(file_content) # A quick syntax check before we upload anything. for artifact_value in new_artifacts: artifact_registry.ValidateSyntax(artifact_value) for artifact_value in new_artifacts: registry_obj.RegisterArtifact( artifact_value, source="datastore", overwrite_if_exists=overwrite, overwrite_system_artifacts=overwrite_system_artifacts) data_store.REL_DB.WriteArtifact(artifact_value) loaded_artifacts.append(artifact_value) name = artifact_value.name logging.info("Uploaded artifact %s.", name) # Once all artifacts are loaded we can validate dependencies. Note that we do # not have to perform a syntax validation because it is already done after # YAML is parsed. for artifact_value in loaded_artifacts: artifact_registry.ValidateDependencies(artifact_value)
def testValidateSyntaxBrokenProvides(self): artifact = rdf_artifacts.Artifact(name="Thud", doc="This is Thud.", provides=["fqdn", "garbage"]) with self.assertRaisesRegex(rdf_artifacts.ArtifactSyntaxError, "'garbage'"): ar.ValidateSyntax(artifact)
def testValidateSyntaxInvalidSupportedOs(self): artifact = rdf_artifacts.Artifact(name="Quux", doc="This is Quux.", provides=["os"], supported_os=["Solaris"]) with self.assertRaisesRegex(rdf_artifacts.ArtifactSyntaxError, "'Solaris'"): ar.ValidateSyntax(artifact)
def testValidateSyntaxInvalidLabel(self): artifact = rdf_artifacts.Artifact(name="Norf", doc="This is Norf.", provides=["domain"], labels=["Mail", "Browser", "Reddit"], supported_os=["Darwin"]) with self.assertRaisesRegex(rdf_artifacts.ArtifactSyntaxError, "'Reddit'"): ar.ValidateSyntax(artifact)
def UploadArtifactYamlFile(file_content, overwrite=True, overwrite_system_artifacts=False): """Upload a yaml or json file as an artifact to the datastore.""" loaded_artifacts = [] registry_obj = artifact_registry.REGISTRY # Make sure all artifacts are loaded so we don't accidentally overwrite one. registry_obj.GetArtifacts(reload_datastore_artifacts=True) new_artifacts = registry_obj.ArtifactsFromYaml(file_content) new_artifact_names = set() # A quick syntax check before we upload anything. for artifact_value in new_artifacts: artifact_registry.ValidateSyntax(artifact_value) new_artifact_names.add(artifact_value.name) # Iterate through each artifact adding it to the collection. artifact_coll = artifact_registry.ArtifactCollection( ARTIFACT_STORE_ROOT_URN) current_artifacts = list(artifact_coll) # We need to remove artifacts we are overwriting. filtered_artifacts = [ art for art in current_artifacts if art.name not in new_artifact_names ] artifact_coll.Delete() with data_store.DB.GetMutationPool() as pool: for artifact_value in filtered_artifacts: artifact_coll.Add(artifact_value, mutation_pool=pool) for artifact_value in new_artifacts: registry_obj.RegisterArtifact( artifact_value, source="datastore:%s" % ARTIFACT_STORE_ROOT_URN, overwrite_if_exists=overwrite, overwrite_system_artifacts=overwrite_system_artifacts) artifact_coll.Add(artifact_value, mutation_pool=pool) if data_store.RelationalDBWriteEnabled(): data_store.REL_DB.WriteArtifact(artifact_value) loaded_artifacts.append(artifact_value) name = artifact_value.name logging.info("Uploaded artifact %s to %s", name, ARTIFACT_STORE_ROOT_URN) # Once all artifacts are loaded we can validate dependencies. Note that we do # not have to perform a syntax validation because it is already done after # YAML is parsed. for artifact_value in loaded_artifacts: artifact_registry.ValidateDependencies(artifact_value)
def testValidateSyntaxBadSource(self): source = { "type": rdf_artifacts.ArtifactSource.SourceType.ARTIFACT_GROUP, "attributes": {} } artifact = rdf_artifacts.Artifact(name="Barf", doc="This is Barf.", provides=["os"], sources=[source]) with self.assertRaisesRegex(rdf_artifacts.ArtifactSyntaxError, "required attributes"): ar.ValidateSyntax(artifact)
def testValidateSyntaxBadPathDependency(self): sources = [{ "type": rdf_artifacts.ArtifactSource.SourceType.FILE, "attributes": { "paths": [r"%%systemdrive%%\Temp"] } }] artifact = rdf_artifacts.Artifact(name="bad", doc="Doco", provides=["environ_windir"], supported_os=["Windows"], urls=["http://blah"], sources=sources) with self.assertRaises(rdf_artifacts.ArtifactDefinitionError): ar.ValidateSyntax(artifact)