Example #1
0
def create_service_endpoint(service_name, service_json_filename):
    if get_service_id_from_name(service_name) is not None:
        service_id = get_service_id_from_name(service_name)
        api = '/services/' + service_id
    else:
        api = '/services'

    # Open JSON schema for service endpoint
    with open('json_schema/service-schema.json') as service_schema:
        schema = json.load(service_schema)

    # Open JSON file for new or existing service endpoint as defined by user
    with open(service_json_filename) as json_file:
        payload = json.load(json_file)

    # Validate user JSON file against schema
    try:
        validate(instance=payload, schema=schema)
    except (exceptions.ValidationError, exceptions.SchemaError) as err:
        print(err)
        exit(1)

    # Get service by name and diff existing service and proposed service
    existing_endpoint = get_service_endpoint(service_name)
    if existing_endpoint is not None:
        ddiff = DeepDiff(existing_endpoint,
                         payload,
                         ignore_order=True,
                         exclude_paths=[
                             "root['id']", "root['created_at']",
                             "root['updated_at']"
                         ])
        if bool(ddiff) is False:
            print('No changes, nothing to do!')
            exit(0)

    if api == '/services':
        create_service = make_request('POST', api, payload)
    else:
        create_service = make_request('PUT', api, payload)

    try:
        ddiff
    except NameError:
        ddiff = None

    if ddiff is not None:
        pprint(ddiff, indent=2)
    else:
        print(json.dumps(create_service, indent=2))
        return create_service
Example #2
0
def get_route_by_id(route_id):
    api = '/routes/'
    payload = {}

    get_route = make_request('GET', api + route_id, payload)

    return get_route
Example #3
0
def delete_service_by_id(service_id):
    api = '/services/'
    payload = {}

    delete_service = make_request('DELETE', api + service_id, payload)

    return delete_service
Example #4
0
def get_kong_version():
    api = ''
    payload = {}
    resp = make_request('GET', api, payload)
    version = resp['version']

    return version
Example #5
0
def create_route_on_service(service_name, route_json_filename):
    if get_service_id_from_name(service_name) is not None:
        service_id = get_service_id_from_name(service_name)
        api = '/services/' + service_id + '/routes'
    else:
        print("Service doesn't exist, please check name...")
        exit(1)

    # Open JSON schema for route
    with open('json_schema/route-schema.json') as route_schema:
        schema = json.load(route_schema)

    # Open JSON file for new or existing route as defined by user
    with open(route_json_filename) as json_file:
        payload = json.load(json_file)

    # Validate user JSON file against schema
    try:
        validate(instance=payload, schema=schema)
    except (exceptions.ValidationError, exceptions.SchemaError) as err:
        print(err)
        exit(1)

    create_route = make_request('POST', api, payload)

    print(json.dumps(create_route, indent=2))

    return create_route
Example #6
0
def delete_route_by_id(route_id):
    api = '/routes/'
    payload = {}

    delete_route = make_request('DELETE', api + route_id, payload)

    return delete_route
Example #7
0
def delete_plugin(plugin_id):
    api = '/plugins/' + plugin_id

    payload = {}

    delete_plugin_with_id = make_request('DELETE', api, payload)

    return delete_plugin_with_id
Example #8
0
def get_all_service_endpoints():
    api = '/services/'
    payload = {}

    service_endpoints = make_request('GET', api, payload)

    print(json.dumps(service_endpoints, indent=2))

    return service_endpoints
Example #9
0
def list_ca_certificates():
    api = '/ca_certificates'

    payload = {}

    ca_certificates = make_request('GET', api, payload)

    print(json.dumps(ca_certificates, indent=2))

    return ca_certificates
Example #10
0
def get_route_id_from_name(route_name):
    api = '/routes'
    payload = {}

    get_route_id = make_request('GET', api, payload)

    for data in get_route_id['data']:
        if data['name'] == route_name:
            route_id = data['id']
            return route_id
Example #11
0
def get_service_id_from_name(service_name):
    api = '/services'
    payload = {}

    get_service_id = make_request('GET', api, payload)

    for data in get_service_id['data']:
        if data['name'] == service_name:
            service_id = data['id']
            return service_id
Example #12
0
def get_service_endpoint(service_name, display_output=False):
    api = '/services/'
    payload = {}

    service_id = get_service_id_from_name(service_name)

    if service_id is not None:
        service_endpoint = make_request('GET', api + service_id, payload)
        if display_output:
            print(json.dumps(service_endpoint, indent=2))
        return service_endpoint
Example #13
0
def get_routes_on_service(service_name):
    payload = {}

    service_id = get_service_id_from_name(service_name)

    api = '/services/' + service_id + '/routes'

    service_routes = make_request('GET', api, payload)

    print(json.dumps(service_routes, indent=2))

    return service_routes
Example #14
0
def retrieve_ca_certificate_by_id(ca_certificate_id):
    api = '/ca_certificates/'
    payload = {}

    if ca_certificate_id is None:
        print('No CA Certificate found with this ID...')
        exit(1)

    ca_certificate_id_request = make_request('GET', api + ca_certificate_id, payload)

    print(json.dumps(ca_certificate_id_request, indent=2))

    return ca_certificate_id_request
Example #15
0
def amend_route(route_name, route_json_filename):
    if get_route_id_from_name(route_name) is not None:
        route_id = get_route_id_from_name(route_name)
        api = '/routes/' + route_id
    else:
        print("Route doesn't exist, please check name...")
        exit(1)

    # Open JSON schema for route
    with open('json_schema/route-schema.json') as route_schema:
        schema = json.load(route_schema)

    # Open JSON file for new or existing route as defined by user
    with open(route_json_filename) as json_file:
        payload = json.load(json_file)

    # Validate user JSON file against schema
    try:
        validate(instance=payload, schema=schema)
    except (exceptions.ValidationError, exceptions.SchemaError) as err:
        print(err)
        exit(1)

    # Get route by ID and diff existing route and proposed route
    existing_route = get_route_by_id(route_id)
    ddiff = DeepDiff(existing_route,
                     payload,
                     ignore_order=True,
                     exclude_paths=[
                         "root['id']", "root['created_at']",
                         "root['updated_at']", "root['service']"
                     ])
    if bool(ddiff) is False:
        print('No changes, nothing to do!')
        exit(0)

    create_route = make_request('PATCH', api, payload)

    try:
        ddiff
    except NameError:
        ddiff = None

    if ddiff is not None:
        pprint(ddiff, indent=2)
    else:
        print(json.dumps(create_route, indent=2))
        return create_route
Example #16
0
def get_plugins(service_name, display_output=False):
    service_id = get_service_id_from_name(service_name)

    if service_id is not None:
        api = '/services/' + service_id + '/plugins'
    else:
        print('No plugins to display...')
        exit(1)

    payload = {}

    plugins = make_request('GET', api, payload)

    if display_output:
        print(json.dumps(plugins, indent=2))

    return json.dumps(plugins)
Example #17
0
def add_ca_certificate(ca_cert_json_filename):
    api = '/ca_certificates'

    # Open JSON schema for service endpoint
    with open('json_schema/ca-cert-schema.json') as ca_cert_schema:
        schema = json.load(ca_cert_schema)

    # Open JSON file for new or existing service endpoint as defined by user
    with open(ca_cert_json_filename) as json_file:
        payload = json.load(json_file)

    # Validate user JSON file against schema
    validate(instance=payload, schema=schema)

    create_ca_cert = make_request('POST', api, payload)

    print(json.dumps(create_ca_cert, indent=2))

    return create_ca_cert
Example #18
0
def add_plugins(service_name,
                plugin_json_filename,
                ip_whitelist=None,
                ip_whitelist_2=None,
                ip_whitelist_3=None):
    service_id = get_service_id_from_name(service_name)

    if service_id is not None:
        api = '/services/' + service_id + '/plugins'
    else:
        print('Service not found...')
        exit(1)

    if ip_whitelist is None:
        # Open JSON schema for service endpoint
        with open('json_schema/plugin-schema.json') as plugin_schema:
            schema = json.load(plugin_schema)

        # Open JSON file for new or existing service endpoint as defined by user
        with open(plugin_json_filename) as json_file:
            payload = json.load(json_file)

        # Validate user JSON file against schema
        validate(instance=payload, schema=schema)
    else:
        with open('json_schema/plugin-schema.json') as plugin_schema:
            schema = json.load(plugin_schema)

        with open(plugin_json_filename) as json_file1:
            json1_file = json.load(json_file1)

        with open(ip_whitelist) as json_file2:
            json2_file = json.load(json_file2)

        if ip_whitelist_2 is not None:
            with open(ip_whitelist_2) as json_file3:
                json3_file = json.load(json_file3)

        if ip_whitelist_3 is not None:
            with open(ip_whitelist_3) as json_file4:
                json4_file = json.load(json_file4)

        iprestriction_config_key = kong_version_check()

        if iprestriction_config_key in json1_file[
                "config"] and ip_whitelist_2 is None:
            json1_file["config"][iprestriction_config_key].extend(
                json2_file["config"][iprestriction_config_key])
        elif iprestriction_config_key in json1_file[
                "config"] and ip_whitelist_3 is None:
            json1_file["config"][iprestriction_config_key].extend(
                json2_file["config"][iprestriction_config_key])
            json1_file["config"][iprestriction_config_key].extend(
                json3_file["config"][iprestriction_config_key])
        elif iprestriction_config_key in json1_file[
                "config"] and ip_whitelist_3 is not None:
            json1_file["config"][iprestriction_config_key].extend(
                json2_file["config"][iprestriction_config_key])
            json1_file["config"][iprestriction_config_key].extend(
                json3_file["config"][iprestriction_config_key])
            json1_file["config"][iprestriction_config_key].extend(
                json4_file["config"][iprestriction_config_key])
        else:
            print(
                "Merging of JSON plugin files only supports the IP restriction plugin..."
            )
            exit(1)

        payload = json1_file

        # Validate user JSON file against schema
        try:
            validate(instance=payload, schema=schema)
        except (exceptions.ValidationError, exceptions.SchemaError) as err:
            print(err)
            exit(1)

    add_request = make_request('POST', api, payload)

    print(json.dumps(add_request, indent=2))

    return add_request
Example #19
0
def amend_plugin(plugin_id,
                 plugin_json_filename,
                 ip_whitelist=None,
                 ip_whitelist_2=None,
                 ip_whitelist_3=None):
    api = '/plugins/' + plugin_id

    if ip_whitelist is None:
        # Open JSON schema for service endpoint
        with open('json_schema/plugin-schema.json') as plugin_schema:
            schema = json.load(plugin_schema)

        # Open JSON file for new or existing service endpoint as defined by user
        with open(plugin_json_filename) as json_file:
            payload = json.load(json_file)

        # Validate user JSON file against schema
        try:
            validate(instance=payload, schema=schema)
        except (exceptions.ValidationError, exceptions.SchemaError) as err:
            print(err)
            exit(1)

        existing_plugin = make_request('GET', api, params=None)
        ddiff = DeepDiff(existing_plugin,
                         payload,
                         ignore_order=True,
                         report_repetition=True,
                         exclude_paths=[
                             "root['id']", "root['created_at']",
                             "root['service']", "root['route']",
                             "root['enabled']", "root['consumer']",
                             "root['protocols']", "root['run_on']",
                             "root['config']['blacklist']",
                             "root['config']['deny']"
                         ])
        if bool(ddiff) is False:
            print('No changes, nothing to do!')
            exit(0)
    else:
        with open('json_schema/plugin-schema.json') as plugin_schema:
            schema = json.load(plugin_schema)

        with open(plugin_json_filename) as json_file1:
            json1_file = json.load(json_file1)

        with open(ip_whitelist) as json_file2:
            json2_file = json.load(json_file2)

        if ip_whitelist_2 is not None:
            with open(ip_whitelist_2) as json_file3:
                json3_file = json.load(json_file3)

        if ip_whitelist_3 is not None:
            with open(ip_whitelist_3) as json_file4:
                json4_file = json.load(json_file4)

        iprestriction_config_key = kong_version_check()

        if iprestriction_config_key in json1_file[
                "config"] and ip_whitelist_2 is None:
            json1_file["config"][iprestriction_config_key].extend(
                json2_file["config"][iprestriction_config_key])
        elif iprestriction_config_key in json1_file[
                "config"] and ip_whitelist_3 is None:
            json1_file["config"][iprestriction_config_key].extend(
                json2_file["config"][iprestriction_config_key])
            json1_file["config"][iprestriction_config_key].extend(
                json3_file["config"][iprestriction_config_key])
        elif iprestriction_config_key in json1_file[
                "config"] and ip_whitelist_3 is not None:
            json1_file["config"][iprestriction_config_key].extend(
                json2_file["config"][iprestriction_config_key])
            json1_file["config"][iprestriction_config_key].extend(
                json3_file["config"][iprestriction_config_key])
            json1_file["config"][iprestriction_config_key].extend(
                json4_file["config"][iprestriction_config_key])
        else:
            print(
                "Merging of JSON plugin files only supports the IP restriction plugin..."
            )
            exit(1)

        payload = json1_file

        # Validate user JSON file against schema
        try:
            validate(instance=payload, schema=schema)
        except (exceptions.ValidationError, exceptions.SchemaError) as err:
            print(err)
            exit(1)

        if ip_whitelist is not None:
            existing_whitelist = make_request('GET', api, params=None)
            existing_whitelist_json = json.dumps(existing_whitelist, indent=2)
            if "ip-restriction" in existing_whitelist_json:
                ddiff = DeepDiff(existing_whitelist,
                                 payload,
                                 ignore_order=True,
                                 report_repetition=True,
                                 exclude_paths=[
                                     "root['id']", "root['created_at']",
                                     "root['service']", "root['route']",
                                     "root['enabled']", "root['consumer']",
                                     "root['protocols']", "root['run_on']",
                                     "root['config']['blacklist']",
                                     "root['config']['deny']"
                                 ])
                if bool(ddiff) is False:
                    print('No changes, nothing to do!')
                    exit(0)

    amend_request = make_request('PATCH', api, payload)

    try:
        ddiff
    except NameError:
        ddiff = None

    if ddiff is not None:
        pprint(ddiff, indent=2)
    else:
        print(json.dumps(amend_request, indent=2))
        return amend_request