Example #1
0
 def setUp(self):
     super(SimplePipeLineTest, self).setUp()
     self.templates = TemplateLookup(
         directories=[os.path.join(self.datadir, 'simple-pipeline')])
     self.output = tempfile.NamedTemporaryFile('w').name
     self.signer = tempfile.NamedTemporaryFile('w').name
     self.signer_template = self.templates.get_template('signer.fd')
     self.validator = tempfile.NamedTemporaryFile('w').name
     self.validator_template = self.templates.get_template('validator.fd')
     self.md_signer = MDRepository()
     self.md_validator = MDRepository()
     with open(self.signer, "w") as fd:
         fd.write(self.signer_template.render(ctx=self))
     with open(self.validator, "w") as fd:
         fd.write(self.validator_template.render(ctx=self))
     self.signer_result = plumbing(self.signer).process(self.md_signer,
                                                        state={
                                                            'batch': True,
                                                            'stats': {}
                                                        })
     self.validator_result = plumbing(self.validator).process(
         self.md_validator, state={
             'batch': True,
             'stats': {}
         })
Example #2
0
 def setUp(self):
     self.md = MDRepository()
     self.md.store = make_store_instance()
     self.datadir = resource_filename('metadata', 'test/data')
     self.xml_source = os.path.join(self.datadir, 'test01.xml')
     self.swamid_source = os.path.join(self.datadir, 'swamid-2.0-test.xml')
     self.swamid = root(parse_xml(self.swamid_source))
     self.t = parse_xml(self.xml_source)
     self.non_metadata = parse_xml(
         resource_filename("not-metadata.xml", self.datadir))
Example #3
0
 def exec_pipeline(self, pstr):
     md = MDRepository()
     p = yaml.safe_load(six.StringIO(pstr))
     print("\n{}".format(yaml.dump(p)))
     pl = Plumbing(p, pid="test")
     res = pl.process(md, state={'batch': True, 'stats': {}})
     return res, md
Example #4
0
    def run_pipeline(self, pl_name, ctx=None, md=None):
        if ctx is None:
            ctx = dict()

        if md is None:
            md = MDRepository()

        templates = TemplateLookup(
            directories=[os.path.join(self.datadir, 'simple-pipeline')])
        pipeline = tempfile.NamedTemporaryFile('w').name
        template = templates.get_template(pl_name)
        with open(pipeline, "w") as fd:
            fd.write(template.render(ctx=ctx))
        res = plumbing(pipeline).process(md,
                                         state={
                                             'batch': True,
                                             'stats': {}
                                         })
        os.unlink(pipeline)
        return res, md, ctx
Example #5
0
def main():
    """
    The main entrypoint for the pyFF cmdline tool.
    """
    args = parse_options("pyff", __doc__)

    log_args = {'level': config.loglevel}
    if config.logfile is not None:
        log_args['filename'] = config.logfile
    logging.basicConfig(**log_args)

    config.modules.append('pyff.builtins')
    for mn in config.modules:
        importlib.import_module(mn)
    config.update_frequency = 0
    try:
        md = MDRepository()
        for p in args:
            plumbing(p).process(md, state={'batch': True, 'stats': {}})
        sys.exit(0)
    except Exception as ex:
        logging.debug(traceback.format_exc())
        logging.error(ex)
        sys.exit(-1)
Example #6
0
class TestRepo(TestCase):
    def setUp(self):
        self.md = MDRepository()
        self.md.store = make_store_instance()
        self.datadir = resource_filename('metadata', 'test/data')
        self.xml_source = os.path.join(self.datadir, 'test01.xml')
        self.swamid_source = os.path.join(self.datadir, 'swamid-2.0-test.xml')
        self.swamid = root(parse_xml(self.swamid_source))
        self.t = parse_xml(self.xml_source)
        self.non_metadata = parse_xml(
            resource_filename("not-metadata.xml", self.datadir))

    def test_md_exists(self):
        assert (self.md is not None)

    def test_clone(self):
        entity_id = root(self.t).get('entityID')
        self.md.store.update(root(self.t), entity_id)
        nstore = deepcopy(self.md.store)
        assert (nstore.size() == self.md.store.size())
        assert (nstore.lookup(entity_id) is not None)

    def test_sha1_hash(self):
        entity_id = root(self.t).get('entityID')
        self.md.store.update(root(self.t), entity_id)
        e = self.md.lookup(entity_id)
        assert (sha1_id(
            e[0]) == "{sha1}568515f6fae8c8b4d42d543853c96d08f051ef13")
        assert (hash_id(
            e[0], 'sha1',
            prefix=False) == "568515f6fae8c8b4d42d543853c96d08f051ef13")

    def test_entity_attribute(self):
        entity_id = root(self.t).get('entityID')
        set_entity_attributes(root(self.t), {"http://ns.example.org": "foo"})
        self.md.store.update(root(self.t), entity_id)
        e = self.md.lookup("{%s}%s" % ("http://ns.example.org", 'foo'))[0]
        assert (e is not None)
        assert (e.get('entityID') == entity_id)

    def test_utils(self):
        entity_id = root(self.t).get('entityID')
        self.md.store.update(root(self.t), entity_id)
        e = self.md.lookup(entity_id)[0]
        assert (is_idp(e))
        assert (not is_sp(e))
        icon = entity_icon_url(e)
        assert ('url' in icon)
        assert ('https://www.example.com/static/images/umu_logo.jpg'
                in icon['url'])
        assert ('width' in icon)
        assert ('358' == icon['width'])
        assert ('height' in icon)
        assert ('63' == icon['height'])
        assert ('62' != icon['height'])

        domains = entity_domains(e)
        assert ('example.com' in domains)
        assert ('example.net' in domains)
        assert ('idp.example.com' not in domains)
        assert ('foo.com' not in domains)

        edup = deepcopy(e)
        name, desc = entity_extended_display(e)
        assert (name == 'Example University')
        assert (desc == 'Identity Provider for Example University')

        disp = entity_display_name(e)
        assert (disp == 'Example University')
        for elt in e.findall(".//{%s}DisplayName" % NS['mdui']):
            elt.getparent().remove(elt)

        disp = entity_display_name(e)
        assert (disp == 'The Example University')
        for elt in e.findall(".//{%s}OrganizationDisplayName" % NS['md']):
            elt.getparent().remove(elt)

        disp = entity_display_name(e)
        assert (disp == 'ExampleU')
        for elt in e.findall(".//{%s}OrganizationName" % NS['md']):
            elt.getparent().remove(elt)

        disp = entity_display_name(e)
        assert (disp == entity_id)

        e = edup

        subs = entity_domains(e)
        assert ('example.com' in subs)
        assert ('example.net' in subs)
        assert ('idp.example.com' not in subs)

        summary = entity_simple_summary(e)
        assert (summary['title'] == 'Example University')
        assert (summary['descr'] == 'Identity Provider for Example University')
        assert (summary['entityID'] == entity_id)
        assert ('domains' in summary)
        assert ('id' in summary)

        empty = entity_simple_summary(None)
        assert (not empty)

    def test_display(self):
        swamid = root(self.swamid)
        self.md.store.update(swamid, swamid.get('Name'))
        funet_connect = self.md.lookup(
            'https://connect.funet.fi/shibboleth')[0]
        name, desc = entity_extended_display(funet_connect)
        assert (name == 'FUNET E-Meeting Service')
        dn = entity_extended_display(funet_connect)

    def test_missing(self):
        swamid = root(self.swamid)
        self.md.store.update(swamid, swamid.get('Name'))
        missing = self.md.lookup('https://connect.funet.fi/shibboleth+missing')
        assert (len(missing) == 0)

    def test_non_metadata(self):
        e = root(self.non_metadata)
        assert metadata_expiration(e) is None
        try:
            annotate_entity(e, "kaka", "x", "y")
            set_entity_attributes(e, dict(a=1))
            assert False
        except MetadataException:
            pass
Example #7
0
from wsgiref.simple_server import make_server

from pyff.api import mkapp
from pyff.repo import MDRepository

md = MDRepository()
app = mkapp(md=md)


def app_factory(global_config, **local_config):
    local_config['md'] = md
    return mkapp(global_config, **local_config)


def server_runner(*args):
    _app = args[0]
    local_config = args[1]
    port = int(local_config.get('bind_address', 8080))
    host = local_config.get('port', '0.0.0.0')
    s = make_server(host, port, _app)
    s.serve_forever()


def main():
    server_runner(mkapp(md=md), bind_address='0.0.0.0', port=8080)


if __name__ == '__main__':
    main()
Example #8
0
def mkapp(*args: Any, **kwargs: Any) -> Any:
    md = kwargs.pop('md', None)
    if md is None:
        md = MDRepository()

    if config.devel_memory_profile:
        launch_memory_usage_server()

    with Configurator(debug_logger=log) as ctx:
        ctx.add_subscriber(add_cors_headers_response_callback, NewRequest)

        if config.aliases is None:
            config.aliases = dict()

        if config.modules is None:
            config.modules = []

        ctx.registry.config = config
        config.modules.append('pyff.builtins')
        for mn in config.modules:
            importlib.import_module(mn)

        pipeline = None
        if args:
            pipeline = list(args)
        if pipeline is None and config.pipeline:
            pipeline = [config.pipeline]

        ctx.registry.scheduler = md.scheduler
        if pipeline is not None:
            ctx.registry.pipeline = pipeline
            ctx.registry.plumbings = [plumbing(v) for v in pipeline]
        ctx.registry.aliases = config.aliases
        ctx.registry.md = md
        if config.caching_enabled:
            ctx.registry.cache = TTLCache(config.cache_size, config.cache_ttl)
        else:
            ctx.registry.cache = NoCache()

        ctx.add_route('robots', '/robots.txt')
        ctx.add_view(robots_handler, route_name='robots')

        ctx.add_route('webfinger',
                      '/.well-known/webfinger',
                      request_method='GET')
        ctx.add_view(webfinger_handler, route_name='webfinger')

        ctx.add_route('search', '/api/search', request_method='GET')
        ctx.add_view(search_handler, route_name='search')

        ctx.add_route('status', '/api/status', request_method='GET')
        ctx.add_view(status_handler, route_name='status')

        ctx.add_route('resources', '/api/resources', request_method='GET')
        ctx.add_view(resources_handler, route_name='resources')

        ctx.add_route('pipeline', '/api/pipeline', request_method='GET')
        ctx.add_view(pipeline_handler, route_name='pipeline')

        ctx.add_route('call',
                      '/api/call/{entry}',
                      request_method=['POST', 'PUT'])
        ctx.add_view(process_handler, route_name='call')

        ctx.add_route('request', '/*path', request_method='GET')
        ctx.add_view(request_handler, route_name='request')

        start = utc_now() + timedelta(seconds=1)
        if config.update_frequency > 0:
            ctx.registry.scheduler.add_job(
                call,
                'interval',
                id="call/update",
                args=['update'],
                start_date=start,
                misfire_grace_time=10,
                seconds=config.update_frequency,
                replace_existing=True,
                max_instances=1,
                timezone=pytz.utc,
            )

        return ctx.make_wsgi_app()
Example #9
0
 def load(self):
     return mkapp(config.pipeline, md=MDRepository())