Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sca #100

Merged
merged 5 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion dongtai_agent_python/api/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import socket
import threading
from dongtai_agent_python.common.logger import logger_config
from dongtai_agent_python.utils import scope
from dongtai_agent_python.utils import scope, utils
from dongtai_agent_python.utils import Singleton, SystemInfo

logger = logger_config('openapi')
Expand Down Expand Up @@ -200,6 +200,9 @@ def agent_register(self):
# check manual pause
t3 = threading.Timer(self.interval_check_manual_pause, self.thread_check_manual_pause)
t3.start()
# packages
t4 = threading.Timer(2, self.packages_report)
t4.start()

return resp

Expand All @@ -214,6 +217,25 @@ def async_report_upload(self, executor, upload_report):
self.report_queue = self.report_queue + 1
executor.submit(self.report_upload, upload_report)

@scope.with_scope(scope.SCOPE_AGENT)
def packages_report(self):
packages = utils.get_packages()
if not packages:
return
detail = {
'detail': {
'agentId': self.agent_id,
'packages': packages,
},
'type': 18,
}
url = '/api/v1/report/upload'
heart_resp = self.report(url, detail)
if heart_resp.get('status', 0) == 201:
logger.debug("packages report success")
else:
logger.error("packages report error")

# check agent should pause when use high system resource
def check_enable(self):
url = '/api/v1/agent/limit'
Expand Down
14 changes: 14 additions & 0 deletions dongtai_agent_python/tests/utils/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import unittest

from dongtai_agent_python.utils import utils


class TestUtils(unittest.TestCase):
def test_get_packages(self):
packages = utils.get_packages()
for package in packages:
print(package)


if __name__ == '__main__':
unittest.main()
43 changes: 43 additions & 0 deletions dongtai_agent_python/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import base64
import hashlib
import os

import pkg_resources

from dongtai_agent_python.setting import const
from dongtai_agent_python.utils import scope
Expand Down Expand Up @@ -64,3 +67,43 @@ def get_hash(item):
except Exception:
h = id(item)
return h


@scope.with_scope(scope.SCOPE_AGENT)
def get_packages():
packages = pkg_resources.working_set
sca_packages = []
for package in packages:
module_path = package.location + os.sep + package.project_name.lower()
found = False
if os.path.exists(module_path):
found = True

if not found:
module_path = package.location + os.sep + package.project_name.replace('-', '_')
if os.path.exists(module_path):
found = True

if not found:
module_path = package.location + os.sep + package.project_name
if os.path.exists(module_path):
found = True

if not found and package.has_metadata('top_level.txt'):
top_level = package.get_metadata('top_level.txt').splitlines()
if top_level:
for lvl in top_level:
if os.path.exists(package.location + os.sep + lvl):
module_path = package.location + os.sep + lvl

sha_1 = hashlib.sha1()
sha_1.update(bytes(package.project_name.lower() + '-' + package.version, encoding='utf-8'))
digest = sha_1.hexdigest()

sca_packages.append({
'packageName': package.project_name + '-' + package.version,
'packagePath': module_path,
'packageAlgorithm': 'SHA-1',
'packageSignature': digest
})
return sca_packages