Exemplo n.º 1
0
def _add_route(config, domains, route, enable_ssl_redirect):
    route_name = routes_manager.get_name(route)
    logs.info(f'adding route to nginx config: {route_name}')
    logs.debug_verbose(config=config,
                       domains=domains,
                       route=route,
                       enable_ssl_redirect=enable_ssl_redirect)
    backend_url = routes_manager.get_backend_url(route)
    frontend_hostname = routes_manager.get_frontend_hostname(route)
    print(f'F/B = {frontend_hostname} {backend_url}')
    root_domain, sub_domain = routes_manager.get_domain_parts(route)
    domains.setdefault(root_domain, []).append(sub_domain)
    # if route['spec'].get('extra-no-dns-subdomains'):
    #     extra_hostnames = ',' + ','.join([f'{s}.{root_domain}' for s in route['spec']['extra-no-dns-subdomains']])
    # else:
    extra_hostnames = ''
    logs.debug_verbose(route_name=route_name,
                       backend_url=backend_url,
                       frontend_hostname=frontend_hostname,
                       root_domain=root_domain,
                       sub_domain=sub_domain,
                       domains=domains,
                       extra_hostnames=extra_hostnames)
    if backend_url:
        raise NotImplementedError()
Exemplo n.º 2
0
 def get_routes(datapusher_name, deis_instance_id, backend_url_target_id, one, external_domain, edit):
     if datapusher_name:
         assert not deis_instance_id
         routes = routers_manager.get_datapusher_routes(datapusher_name, edit=edit)
     elif deis_instance_id:
         routes = routers_manager.get_deis_instance_routes(deis_instance_id, edit=edit)
     elif backend_url_target_id:
         routes = routers_manager.get_backend_url_routes(backend_url_target_id, edit=edit)
     else:
         raise Exception(f'invalid arguments')
     if routes:
         if one: assert len(routes) == 1, 'too many routes!'
         for route in routes:
             if external_domain:
                 data = routers_manager.get_route_frontend_hostname(route)
                 if one:
                     print(data)
                 else:
                     print(yaml.dump([data], default_flow_style=False))
             else:
                 data = {
                     'name': route['metadata']['name'],
                     'backend-url': routes_manager.get_backend_url(route),
                     'frontend-hostname': routes_manager.get_frontend_hostname(route),
                 }
                 if one:
                     print(yaml.dump(data, default_flow_style=False))
                 else:
                     print(yaml.dump([data], default_flow_style=False))
Exemplo n.º 3
0
def _add_route(config, domains, route, enable_ssl_redirect):
    route_name = routes_manager.get_name(route)
    logs.info(f'adding route to traefik config: {route_name}')
    logs.debug_verbose(config=config,
                       domains=domains,
                       route=route,
                       enable_ssl_redirect=enable_ssl_redirect)
    backend_url = routes_manager.get_backend_url(route)
    frontend_hostname = routes_manager.get_frontend_hostname(route)
    print(f'F/B = {frontend_hostname} {backend_url}')
    root_domain, sub_domain = routes_manager.get_domain_parts(route)
    domains.setdefault(root_domain, []).append(sub_domain)
    if route['spec'].get('extra-no-dns-subdomains'):
        extra_hostnames = ',' + ','.join([
            f'{s}.{root_domain}'
            for s in route['spec']['extra-no-dns-subdomains']
        ])
    else:
        extra_hostnames = ''
    logs.debug_verbose(route_name=route_name,
                       backend_url=backend_url,
                       frontend_hostname=frontend_hostname,
                       root_domain=root_domain,
                       sub_domain=sub_domain,
                       domains=domains,
                       extra_hostnames=extra_hostnames)
    if backend_url:
        config['backends'][route_name] = {
            'servers': {
                'server1': {
                    'url': backend_url
                }
            }
        }
        config['frontends'][route_name] = {
            'backend':
            route_name,
            'passHostHeader':
            True,
            'headers': {
                'SSLRedirect': bool(enable_ssl_redirect)
            },
            'routes': {
                'route1': {
                    'rule': f'Host:{frontend_hostname}{extra_hostnames}'
                }
            },
            **({
                'auth': {
                    'basic': {
                        'usersFile':
                        '/httpauth-' + route['spec']['httpauth-secret'] + '/.htpasswd'
                    }
                }
            } if route['spec'].get('httpauth-secret') else {}),
        }
Exemplo n.º 4
0
 def get_routes(datapusher_name, deis_instance_id, ckan_instance_id,
                backend_url_target_id, one, external_domain, edit):
     if datapusher_name:
         assert not deis_instance_id and not ckan_instance_id and not backend_url_target_id
         routes = routers_manager.get_datapusher_routes(datapusher_name,
                                                        edit=edit)
     elif deis_instance_id:
         assert not datapusher_name and not ckan_instance_id and not backend_url_target_id
         routes = routers_manager.get_deis_instance_routes(deis_instance_id,
                                                           edit=edit)
     elif ckan_instance_id:
         assert not datapusher_name and not deis_instance_id and not backend_url_target_id
         routes = routers_manager.get_ckan_instance_routes(ckan_instance_id,
                                                           edit=edit)
     elif backend_url_target_id:
         assert not datapusher_name and not deis_instance_id and not ckan_instance_id
         routes = routers_manager.get_backend_url_routes(
             backend_url_target_id, edit=edit)
     else:
         routes = routers_manager.get_all_routes()
     if routes:
         if one: assert len(routes) == 1, 'too many routes!'
         for route in routes:
             if external_domain:
                 data = routers_manager.get_route_frontend_hostname(route)
                 if one:
                     print(data)
                 else:
                     print(yaml.dump([data], default_flow_style=False))
             else:
                 try:
                     data = {
                         'name':
                         route['metadata']['name'],
                         'backend-url':
                         routes_manager.get_backend_url(route),
                         'frontend-hostname':
                         routes_manager.get_frontend_hostname(route),
                         'router-name':
                         route['spec']['router_name']
                     }
                 except Exception as e:
                     print('Warning: %s' % repr(e))
                     continue
                 if one:
                     print(yaml.dump(data, default_flow_style=False))
                 else:
                     print(yaml.dump([data], default_flow_style=False))
Exemplo n.º 5
0
def _add_route(dynamic_config, domains, route, enable_ssl_redirect,
               external_domains, wildcard_ssl_domain):
    route_name = routes_manager.get_name(route)
    logs.info(f'adding route to traefik v2 dynamic config: {route_name}')
    logs.debug_verbose(dynamic_config=dynamic_config,
                       domains=domains,
                       route=route,
                       enable_ssl_redirect=enable_ssl_redirect)
    backend_url = routes_manager.get_backend_url(route)
    frontend_hostname = routes_manager.get_frontend_hostname(route)
    print(f'F/B = {frontend_hostname} {backend_url}')
    root_domain, sub_domain = routes_manager.get_domain_parts(route)
    domains.setdefault(root_domain, []).append(sub_domain)
    if route['spec'].get('extra-no-dns-subdomains'):
        extra_hostnames = ',' + ','.join([
            f'{s}.{root_domain}'
            for s in route['spec']['extra-no-dns-subdomains']
        ])
    else:
        extra_hostnames = ''
    logs.debug_verbose(route_name=route_name,
                       backend_url=backend_url,
                       frontend_hostname=frontend_hostname,
                       root_domain=root_domain,
                       sub_domain=sub_domain,
                       domains=domains,
                       extra_hostnames=extra_hostnames)
    if backend_url:
        # config['backends'][route_name] = {
        #     'servers': {
        #         'server1': {
        #             'url': backend_url
        #         }
        #     }
        # }
        dynamic_config['http']['services'][route_name] = {
            'loadBalancer': {
                'servers': [{
                    'url': backend_url
                }]
            }
        }
        # config['frontends'][route_name] = {
        #     'backend': route_name,
        #     'passHostHeader': True,
        #     'headers': {
        #         'SSLRedirect': bool(enable_ssl_redirect)
        #     },
        #     'routes': {
        #         'route1': {
        #             'rule': f'Host:{frontend_hostname}{extra_hostnames}'
        #         }
        #     },
        #     **({
        #         'auth': {
        #             'basic': {
        #                 'usersFile': '/httpauth-' + route['spec']['httpauth-secret'] + '/.htpasswd'
        #             }
        #         }
        #     } if route['spec'].get('httpauth-secret') else {}),
        # }
        assert not extra_hostnames, "extra_hostnames not supported yet for traefik v2: %s" % extra_hostnames
        assert not route['spec'].get(
            'httpauth-secret'
        ), "httpauth-secret not supported yet for traefik v2: %s" % route[
            'spec']['httpauth-secret']
        # passHostHeader is true by default
        dynamic_config['http']['routers']['http-%s' % route_name] = {
            'rule': f'Host(`{frontend_hostname}`)',
            'service': route_name,
            'middlewares': ['SSLRedirect'],
            'entrypoints': ['http'],
        }
        domain_confs = []
        assert not external_domains, "external_domains not yet supported for traefik v2"
        if root_domain == wildcard_ssl_domain:
            domain_confs.append({"main": f'*.{root_domain}'})
        else:
            domain_confs.append({
                "main": root_domain,
                'sans': [f'{sub_domain}.{root_domain}']
            })
        dynamic_config['http']['routers']['https-%s' % route_name] = {
            'rule': f'Host(`{frontend_hostname}`)',
            'service': route_name,
            'middlewares': [],
            'entrypoints': ['https'],
            'tls': {
                'certResolver': 'myresolver',
                'domains': domain_confs
            }
        }
        for i, domain in enumerate(route['spec'].get('extra-external-domains',
                                                     [])):
            dynamic_config['http']['routers']['http-%s-eed%s' %
                                              (route_name, i)] = {
                                                  'rule': f'Host(`{domain}`)',
                                                  'service': route_name,
                                                  'middlewares':
                                                  ['SSLRedirect'],
                                                  'entrypoints': ['http'],
                                              }
            dynamic_config['http']['routers']['https-%s-eed%s' %
                                              (route_name, i)] = {
                                                  'rule': f'Host(`{domain}`)',
                                                  'service': route_name,
                                                  'middlewares': [],
                                                  'entrypoints': ['https'],
                                                  'tls': {
                                                      'certResolver':
                                                      'tlsresolver',
                                                      'domains': [{
                                                          "main":
                                                          domain
                                                      }]
                                                  }
                                              }