Last active
June 22, 2018 09:46
-
-
Save NicolasGeraud/849eb5b031d8d14bcdea62fd447c4110 to your computer and use it in GitHub Desktop.
[Gravitee.io - APIM] find apis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
mode debug des logs : $.proxy.loggingMode=CLIENT_PROXY | |
health present : $.services.health-check | |
presence de policy : $.paths.*[*].transform-headers | |
presence du failover : $.proxy.failover | |
""" | |
import sys | |
import requests | |
from jsonpath_rw import parse | |
import getopt | |
import urllib3 | |
urllib3.disable_warnings() | |
filters = [] | |
baseURL = "" | |
headers = { | |
"Content-Type": "application/json", | |
"Authorization": "" | |
} | |
def get_authorization_cookies(): | |
url = baseURL + "/user/login" | |
response = requests.post(url, params=None, headers=headers, verify=False) | |
return response.cookies | |
def get_apis(api_id=None, cookies=None): | |
url = baseURL + "/apis" | |
if api_id is not None: | |
url += ("/%s" % api_id) | |
response = requests.get(url, cookies=cookies, verify=False) | |
return response.json() | |
def print_api(api): | |
print( | |
"{\n" + | |
" 'id': '" + api["id"] + "'\n" + | |
" 'name': '" + api["name"] + "'\n" + | |
" 'version': '" + api["version"] + "'\n" + | |
" 'owner': '" + api["owner"]["displayName"] + "'" | |
) | |
if "proxy" in api: | |
print(" 'proxy': '" + str(api["proxy"]) + "'") | |
if "services" in api: | |
print(" 'services': '" + str(api["services"]) + "'") | |
if "paths" in api: | |
print(" 'paths': '" + str(api["paths"]) + "'") | |
print("}") | |
def is_filtered(api): | |
for f in filters: | |
split = f.split("=") | |
jsonpath_expr = parse(split[0]) | |
expected_value = None | |
if len(split) > 1: | |
expected_value = split[1] | |
matched_values = [match.value for match in jsonpath_expr.find(api)] | |
# attribute not found | |
if len(matched_values) == 0: | |
return True | |
# expected value is wrong | |
if expected_value is not None and str(matched_values[0]) != expected_value: | |
return True | |
return False | |
def main(): | |
global filters | |
global baseURL | |
global headers | |
try: | |
opts, args = getopt.getopt(sys.argv[1:], "", ["auth=", "url=", "q="]) | |
except getopt.GetoptError: | |
print("ERROR") | |
sys.exit(-1) | |
for opt, arg in opts: | |
if opt == '--auth': | |
headers["Authorization"] = arg | |
elif opt == "--url": | |
baseURL = arg | |
elif opt == "--q": | |
filters = [arg] | |
cookies = get_authorization_cookies() | |
apis = get_apis(cookies=cookies) | |
print("Found %s apis to analyze." % len(apis)) | |
for api in apis: | |
api = get_apis(api['id'], cookies=cookies) | |
if not is_filtered(api): | |
print_api(api) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment