From 1cd5e02fe8339eef2926464d68bd8dc5ce3f800e Mon Sep 17 00:00:00 2001 From: Antoine Drochon Date: Tue, 12 Apr 2022 14:13:43 -0700 Subject: [PATCH] - FIX: search result were applied on the first 250, implemented pagination scan (solves issue #10) - Update of Open Source license headers (year, adding missing one) - Bump version to 0.4.7 --- VERSION | 2 +- bin/akamai-eaa | 62 +++++++++++++++++++++++++++++++++---------- cli.json | 2 +- libeaa/application.py | 2 +- libeaa/cert.py | 2 +- libeaa/common.py | 5 ++-- libeaa/connector.py | 2 +- libeaa/directory.py | 14 ++++++++++ libeaa/error.py | 2 +- libeaa/eventlog.py | 2 +- libeaa/idp.py | 14 ++++++++++ 11 files changed, 86 insertions(+), 23 deletions(-) diff --git a/VERSION b/VERSION index c0a1ac1..5546bd2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.6 \ No newline at end of file +0.4.7 \ No newline at end of file diff --git a/bin/akamai-eaa b/bin/akamai-eaa index 19545e1..5e26d1f 100755 --- a/bin/akamai-eaa +++ b/bin/akamai-eaa @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -# Copyright 2021 Akamai Technologies, Inc. All Rights Reserved +# Copyright 2022 Akamai Technologies, Inc. All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -30,6 +30,7 @@ import fnmatch import signal import http.client as http_client from json import dumps +from math import ceil # cli-eaa import _paths @@ -45,6 +46,7 @@ verbose = getattr(config, 'verbose', False) LOG_FMT = '%(asctime)s %(name)s %(levelname).1s %(message)s' + class SearchAPI(BaseAPI): """ Search EAA applications @@ -57,17 +59,55 @@ class SearchAPI(BaseAPI): super(SearchAPI, self).__init__(config, api=BaseAPI.API_Version.OpenAPI) def search_app(self, search_pattern): - url_params = {'limit': SearchAPI.SCAN_LIMIT, 'expand': 'true'} - search_app = self.get('mgmt-pop/apps', params=url_params) - self.process_response(search_app.json(), search_pattern) + """ + Search for a particular application pattern, or all if no pattern is provided. + Corresponding API documentation: https://techdocs.akamai.com/eaa-api/reference/get-apps + """ - def process_response(self, data, search_pattern): + url_params = {'limit': SearchAPI.SCAN_LIMIT, 'expand': 'true'} + first_search = self.get('mgmt-pop/apps', params=url_params) + data = first_search.json() app_found = 0 + app_scanned = 0 + + # CLI ouput header + cli.print('#app_id,type,name,host,cname,cert_id,status,reachable') + stats = self.process_page(data, search_pattern) + app_scanned += stats[0] + app_found += stats[1] + if data.get("meta"): + app_count = data.get("meta").get("total_count") - # Header - cli.print('#app_id,type,name,host,cname,cert_id,status,reachable') + page_offset = data.get("meta").get("offset") + page_limit = data.get("meta").get("limit") + page_total = ceil(app_count / page_limit) + + logging.debug("app_count: {}, scanned: {}, offset: {}, limit: {}, pages: {}".format( + app_count, app_scanned, page_offset, page_limit, page_total)) + + for page in range(1, page_total): + logging.debug("Loading application page {} of {}".format(page, page_total)) + url_params['offset'] = page * page_limit + search = self.get('mgmt-pop/apps', params=url_params) + stats = self.process_page(search.json(), search_pattern) + app_scanned += stats[0] + app_found += stats[1] + + # CLI ouput footer + if not config.batch: + if app_found != app_count: + cli.footer("Found %s app(s), total %s app(s)" % (app_found, app_count)) + else: + cli.footer("%s app(s)" % app_count) + + def process_page(self, data, search_pattern): + app_found = 0 + app_scanned = 0 + + if data.get("meta"): for a in data.get('objects', []): + app_scanned += 1 if not search_pattern or ( search_pattern and ( fnmatch.fnmatch(a.get('name') or "", "*%s*" % search_pattern) or @@ -88,14 +128,8 @@ class SearchAPI(BaseAPI): status=ApplicationAPI.Status(a.get('app_status')).name, reach=('Y' if a.get('resource_status', {}).get('host_reachable') else 'F')) ) - # cli.print(json.dumps(a)) app_found += 1 - # Footer - if not config.batch: - if app_found != app_count: - cli.footer("Found %s app(s), total %s app(s)" % (app_found, app_count)) - else: - cli.footer("%s app(s)" % app_count) + return app_scanned, app_found class ReportingAPI(BaseAPI): diff --git a/cli.json b/cli.json index 2949bf9..3433d3c 100755 --- a/cli.json +++ b/cli.json @@ -5,7 +5,7 @@ "commands": [ { "name": "eaa", - "version": "0.4.6", + "version": "0.4.7", "description": "Akamai CLI for Enterprise Application Access (EAA)" } ] diff --git a/libeaa/application.py b/libeaa/application.py index 7cb288e..bba60c3 100644 --- a/libeaa/application.py +++ b/libeaa/application.py @@ -1,4 +1,4 @@ -# Copyright 2021 Akamai Technologies, Inc. All Rights Reserved +# Copyright 2022 Akamai Technologies, Inc. All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/libeaa/cert.py b/libeaa/cert.py index 52dff7e..5b48c12 100644 --- a/libeaa/cert.py +++ b/libeaa/cert.py @@ -1,4 +1,4 @@ -# Copyright 2021 Akamai Technologies, Inc. All Rights Reserved +# Copyright 2022 Akamai Technologies, Inc. All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/libeaa/common.py b/libeaa/common.py index 2a4fbaa..4c5d987 100644 --- a/libeaa/common.py +++ b/libeaa/common.py @@ -1,4 +1,4 @@ -# Copyright 2021 Akamai Technologies, Inc. All Rights Reserved +# Copyright 2022 Akamai Technologies, Inc. All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -37,7 +37,7 @@ config = EdgeGridConfig({'verbose': False}, 'default') #: cli-eaa version -__version__ = '0.4.6' +__version__ = '0.4.7' #: HTTP Request Timeout in seconds HTTP_REQ_TIMEOUT = 300 @@ -91,6 +91,7 @@ def exit_gracefully(signum, frame): logging.info(f"Stop due to SIGTERM(15) or SIGINT(2) signal received: {signum} ") cli.stop_event.set() + class EAALegacyAuth(requests.auth.AuthBase): """ EAA legacy API authentication for Requests diff --git a/libeaa/connector.py b/libeaa/connector.py index fb2ded7..5b53af8 100644 --- a/libeaa/connector.py +++ b/libeaa/connector.py @@ -1,4 +1,4 @@ -# Copyright 2021 Akamai Technologies, Inc. All Rights Reserved +# Copyright 2022 Akamai Technologies, Inc. All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/libeaa/directory.py b/libeaa/directory.py index e342b2c..88ac220 100644 --- a/libeaa/directory.py +++ b/libeaa/directory.py @@ -1,3 +1,17 @@ +# Copyright 2022 Akamai Technologies, Inc. All Rights Reserved +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + # Python modules from enum import Enum import logging diff --git a/libeaa/error.py b/libeaa/error.py index d8f43c9..c3dd2a9 100644 --- a/libeaa/error.py +++ b/libeaa/error.py @@ -1,4 +1,4 @@ -# Copyright 2021 Akamai Technologies, Inc. All Rights Reserved +# Copyright 2022 Akamai Technologies, Inc. All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/libeaa/eventlog.py b/libeaa/eventlog.py index e5e29f8..a00901c 100644 --- a/libeaa/eventlog.py +++ b/libeaa/eventlog.py @@ -1,4 +1,4 @@ -# Copyright 2021 Akamai Technologies, Inc. All Rights Reserved +# Copyright 2022 Akamai Technologies, Inc. All Rights Reserved # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/libeaa/idp.py b/libeaa/idp.py index 5ca6998..47a7081 100644 --- a/libeaa/idp.py +++ b/libeaa/idp.py @@ -1,3 +1,17 @@ +# Copyright 2022 Akamai Technologies, Inc. All Rights Reserved +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + # cli-eaa from common import cli, BaseAPI from application import ApplicationAPI