Пример #1
0
    def testValidateSyntaxWithSources(self):
        registry_key_source = {
            "type": artifacts.ArtifactSource.SourceType.REGISTRY_KEY,
            "attributes": {
                "keys": [
                    r"%%current_control_set%%\Control\Session "
                    r"Manager\Environment\Path"
                ],
            }
        }

        file_source = {
            "type": artifacts.ArtifactSource.SourceType.FILE,
            "attributes": {
                "paths": [r"%%environ_systemdrive%%\Temp"]
            }
        }

        artifact = 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)
Пример #2
0
 def testValidateSyntaxSimple(self):
     artifact = artifacts.Artifact(name="Foo",
                                   doc="This is Foo.",
                                   provides=["fqdn", "domain"],
                                   supported_os=["Windows"],
                                   urls=["https://example.com"])
     ar.ValidateSyntax(artifact)
Пример #3
0
    def ArtifactsFromYaml(self, yaml_content):
        """Get a list of Artifacts from yaml."""
        raw_list = list(yaml.safe_load_all(yaml_content))

        # TODO(hanuszczak): I am very sceptical about that "doing the right thing"
        # below. What are the real use cases?

        # Try to do the right thing with json/yaml formatted as a list.
        if (isinstance(raw_list, list) and len(raw_list) == 1
                and isinstance(raw_list[0], list)):
            raw_list = raw_list[0]

        # Convert json into artifact and validate.
        valid_artifacts = []
        for artifact_dict in raw_list:
            # In this case we are feeding parameters directly from potentially
            # untrusted yaml/json to our RDFValue class. However, safe_load ensures
            # these are all primitive types as long as there is no other
            # deserialization involved, and we are passing these into protobuf
            # primitive types.
            try:
                artifact_value = artifacts.Artifact(**artifact_dict)
                valid_artifacts.append(artifact_value)
            except (TypeError, AttributeError, type_info.TypeValueError) as e:
                name = artifact_dict.get("name")
                raise artifacts.ArtifactDefinitionError(name,
                                                        "invalid definition",
                                                        cause=e)

        return valid_artifacts
Пример #4
0
    def testValidateSyntaxMissingDoc(self):
        artifact = artifacts.Artifact(name="Baz",
                                      provides=["os"],
                                      supported_os=["Linux"])

        with self.assertRaisesRegexp(artifacts.ArtifactSyntaxError,
                                     "missing doc"):
            ar.ValidateSyntax(artifact)
Пример #5
0
    def testValidateSyntaxBrokenProvides(self):
        artifact = artifacts.Artifact(name="Thud",
                                      doc="This is Thud.",
                                      provides=["fqdn", "garbage"],
                                      labels=["Network"])

        with self.assertRaisesRegexp(artifacts.ArtifactSyntaxError,
                                     "'garbage'"):
            ar.ValidateSyntax(artifact)
Пример #6
0
    def testValidateSyntaxInvalidLabel(self):
        artifact = artifacts.Artifact(name="Norf",
                                      doc="This is Norf.",
                                      provides=["domain"],
                                      labels=["Mail", "Browser", "Reddit"],
                                      supported_os=["Darwin"])

        with self.assertRaisesRegexp(artifacts.ArtifactSyntaxError,
                                     "'Reddit'"):
            ar.ValidateSyntax(artifact)
Пример #7
0
    def testValidateSyntaxInvalidSupportedOs(self):
        artifact = artifacts.Artifact(name="Quux",
                                      doc="This is Quux.",
                                      provides=["os"],
                                      labels=["Cloud", "Logs"],
                                      supported_os=["Solaris"])

        with self.assertRaisesRegexp(artifacts.ArtifactSyntaxError,
                                     "'Solaris'"):
            ar.ValidateSyntax(artifact)
Пример #8
0
    def testValidateSyntaxBadSource(self):
        source = {
            "type": artifacts.ArtifactSource.SourceType.ARTIFACT_GROUP,
            "attributes": {}
        }

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

        with self.assertRaisesRegexp(artifacts.ArtifactSyntaxError,
                                     "required attributes"):
            ar.ValidateSyntax(artifact)