Example #1
0
def test_stix2_object_result(cleanup_stix2):
    spec = {
        "Foobar": {
            "type": "object",
            "import": "common-properties",
            "required": ["id", "some-property", "type"],
            "properties": {
                "type": "x-foobar",
                "id": {
                    "type": "string",
                    "semantics": "stix-id",
                    "stix-type": "x-foobar"
                },
                "some-property": {
                    "type": "string",
                    "semantics": "word"
                }
            }
        }
    }

    stix2_register_custom(spec['Foobar'], "x-foobar", "2.1")
    processor = stix2generator.create_default_language_processor(
        extra_specs=spec, stix_version="2.1"
    )
    obj = processor.build_graph("Foobar.")[0]

    assert isinstance(obj, stix2.base._STIXBase)
def main():
    args = parse_args()

    stix2generator.logging.config_logging(args.verbose)

    proto_lang_file = getattr(args, "language-file")
    with open(proto_lang_file, "r", encoding=args.encoding) as f:
        proto_lang = f.read()

    extra_specs = None
    if args.extra_specs:
        with open(args.extra_specs, "r", encoding=args.encoding) as f:
            extra_specs = json.load(f)

    tmp_config = {}
    if args.config:
        config_parser = configparser.SafeConfigParser()
        config_parser.read(args.config)
        tmp_config = config_parser['main']

    generator_config = stix2generator.generation.object_generator.Config(
        **tmp_config)

    processor = stix2generator.create_default_language_processor(
        generator_config, extra_specs, args.stix_version)
    stix_objs = processor.build_graph(
        proto_lang, embed_variable_names=args.embed_variable_names)

    if args.bundle:
        bundle = stix2generator.utils.make_bundle(stix_objs, args.stix_version)

        print(bundle.serialize(pretty=True))

    else:
        for obj in stix_objs:
            print(obj.serialize(pretty=True))
def processor():
    return stix2generator.create_default_language_processor(stix_version="2.1")
CONFIG_FILE = "config.ini"
EXTRA_SPECS = "custom_registry.json"

config_parser = configparser.SafeConfigParser()
config_parser.read(CONFIG_FILE)
tmp_config = config_parser['main']
_generator_config = stix2generator.generation.object_generator.Config(
    **tmp_config)

with open(EXTRA_SPECS, "r", encoding="utf-8") as f:
    extra_specs = json.load(f)

stix2_auto_register_all_custom(extra_specs, "2.1")

_processor = stix2generator.create_default_language_processor(
    object_generator_config=_generator_config,
    extra_specs=extra_specs,
)


def json_print(inpt):
    string = str(inpt)
    formatter = HtmlFormatter()
    return '<style type="text/css">{}</style>{}'.format(
        formatter.get_style_defs('.highlight'),
        highlight(string, JsonLexer(), formatter))


@register_cell_magic
def stix(line, cell):
    stix_objs, var_bindings = _processor.build_graph(
        cell, return_variable_bindings=True, embed_variable_names=True)