Beispiel #1
0
    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)
Beispiel #2
0
 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)
Beispiel #3
0
    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)
Beispiel #4
0
    def testValidateSyntaxBrokenProvides(self):
        artifact = rdf_artifacts.Artifact(name="Thud",
                                          doc="This is Thud.",
                                          provides=["fqdn", "garbage"],
                                          labels=["Network"])

        with self.assertRaisesRegexp(rdf_artifacts.ArtifactSyntaxError,
                                     "'garbage'"):
            ar.ValidateSyntax(artifact)
Beispiel #5
0
    def testValidateSyntaxInvalidLabel(self):
        artifact = rdf_artifacts.Artifact(name="Norf",
                                          doc="This is Norf.",
                                          provides=["domain"],
                                          labels=["Mail", "Browser", "Reddit"],
                                          supported_os=["Darwin"])

        with self.assertRaisesRegexp(rdf_artifacts.ArtifactSyntaxError,
                                     "'Reddit'"):
            ar.ValidateSyntax(artifact)
Beispiel #6
0
    def testValidateSyntaxInvalidSupportedOs(self):
        artifact = rdf_artifacts.Artifact(name="Quux",
                                          doc="This is Quux.",
                                          provides=["os"],
                                          labels=["Cloud", "Logs"],
                                          supported_os=["Solaris"])

        with self.assertRaisesRegexp(rdf_artifacts.ArtifactSyntaxError,
                                     "'Solaris'"):
            ar.ValidateSyntax(artifact)
Beispiel #7
0
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)
            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)
Beispiel #8
0
    def testValidateSyntaxBadSource(self):
        source = {
            "type": rdf_artifacts.ArtifactSource.SourceType.ARTIFACT_GROUP,
            "attributes": {}
        }

        artifact = rdf_artifacts.Artifact(name="Barf",
                                          doc="This is Barf.",
                                          provides=["os"],
                                          labels=["Logs", "Memory"],
                                          sources=[source])

        with self.assertRaisesRegexp(rdf_artifacts.ArtifactSyntaxError,
                                     "required attributes"):
            ar.ValidateSyntax(artifact)
Beispiel #9
0
    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)
Beispiel #10
0
    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)