Skip to content

Commit 024f496

Browse files
authored
Merge pull request #100 from lostsnow/feature/sca
Feature/sca
2 parents f4ba43a + cb614c8 commit 024f496

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

dongtai_agent_python/api/openapi.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import socket
1111
import threading
1212
from dongtai_agent_python.common.logger import logger_config
13-
from dongtai_agent_python.utils import scope
13+
from dongtai_agent_python.utils import scope, utils
1414
from dongtai_agent_python.utils import Singleton, SystemInfo
1515

1616
logger = logger_config('openapi')
@@ -202,6 +202,9 @@ def agent_register(self):
202202
# check manual pause
203203
t3 = threading.Timer(self.interval_check_manual_pause, self.thread_check_manual_pause)
204204
t3.start()
205+
# packages
206+
t4 = threading.Timer(2, self.packages_report)
207+
t4.start()
205208

206209
return resp
207210

@@ -216,6 +219,25 @@ def async_report_upload(self, executor, upload_report):
216219
self.report_queue = self.report_queue + 1
217220
executor.submit(self.report_upload, upload_report)
218221

222+
@scope.with_scope(scope.SCOPE_AGENT)
223+
def packages_report(self):
224+
packages = utils.get_packages()
225+
if not packages:
226+
return
227+
detail = {
228+
'detail': {
229+
'agentId': self.agent_id,
230+
'packages': packages,
231+
},
232+
'type': 18,
233+
}
234+
url = '/api/v1/report/upload'
235+
heart_resp = self.report(url, detail)
236+
if heart_resp.get('status', 0) == 201:
237+
logger.debug("packages report success")
238+
else:
239+
logger.error("packages report error")
240+
219241
# check agent should pause when use high system resource
220242
def check_enable(self):
221243
url = '/api/v1/agent/limit'
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import unittest
2+
3+
from dongtai_agent_python.utils import utils
4+
5+
6+
class TestUtils(unittest.TestCase):
7+
def test_get_packages(self):
8+
packages = utils.get_packages()
9+
for package in packages:
10+
print(package)
11+
12+
13+
if __name__ == '__main__':
14+
unittest.main()

dongtai_agent_python/utils/utils.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import base64
22
import hashlib
3+
import os
4+
5+
import pkg_resources
36

47
from dongtai_agent_python.setting import const
58
from dongtai_agent_python.utils import scope
@@ -64,3 +67,43 @@ def get_hash(item):
6467
except Exception:
6568
h = id(item)
6669
return h
70+
71+
72+
@scope.with_scope(scope.SCOPE_AGENT)
73+
def get_packages():
74+
packages = pkg_resources.working_set
75+
sca_packages = []
76+
for package in packages:
77+
module_path = package.location + os.sep + package.project_name.lower()
78+
found = False
79+
if os.path.exists(module_path):
80+
found = True
81+
82+
if not found:
83+
module_path = package.location + os.sep + package.project_name.replace('-', '_')
84+
if os.path.exists(module_path):
85+
found = True
86+
87+
if not found:
88+
module_path = package.location + os.sep + package.project_name
89+
if os.path.exists(module_path):
90+
found = True
91+
92+
if not found and package.has_metadata('top_level.txt'):
93+
top_level = package.get_metadata('top_level.txt').splitlines()
94+
if top_level:
95+
for lvl in top_level:
96+
if os.path.exists(package.location + os.sep + lvl):
97+
module_path = package.location + os.sep + lvl
98+
99+
sha_1 = hashlib.sha1()
100+
sha_1.update(bytes(package.project_name.lower() + '-' + package.version, encoding='utf-8'))
101+
digest = sha_1.hexdigest()
102+
103+
sca_packages.append({
104+
'packageName': package.project_name + '-' + package.version,
105+
'packagePath': module_path,
106+
'packageAlgorithm': 'SHA-1',
107+
'packageSignature': digest
108+
})
109+
return sca_packages

0 commit comments

Comments
 (0)