-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added jar plugin Signed-off-by: Sastry Duri <[email protected]>
- Loading branch information
1 parent
7f10a45
commit 824a898
Showing
18 changed files
with
398 additions
and
11 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[Core] | ||
Name = jar_container | ||
Module = jar_container_crawler | ||
|
||
[Documentation] | ||
Author = IBM | ||
Version = 0.1 | ||
Description = Produces md5 hashes for jar files and class files in jars |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import logging | ||
|
||
import utils.dockerutils | ||
import utils.misc | ||
from icrawl_plugin import IContainerCrawler | ||
from utils.jar_utils import crawl_jar_files | ||
from utils.namespace import run_as_another_namespace | ||
|
||
logger = logging.getLogger('crawlutils') | ||
|
||
|
||
class JarContainerCrawler(IContainerCrawler): | ||
|
||
def get_feature(self): | ||
return 'jar' | ||
|
||
def crawl( | ||
self, | ||
container_id=None, | ||
avoid_setns=False, | ||
root_dir='/', | ||
exclude_dirs=[ | ||
'/boot', | ||
'/dev', | ||
'/proc', | ||
'/sys', | ||
'/mnt', | ||
'/tmp', | ||
'/var/cache', | ||
'/usr/share/man', | ||
'/usr/share/doc', | ||
'/usr/share/mime'], | ||
**kwargs): | ||
inspect = utils.dockerutils.exec_dockerinspect(container_id) | ||
state = inspect['State'] | ||
pid = str(state['Pid']) | ||
logger.debug('Crawling jars for container %s' % container_id) | ||
|
||
if avoid_setns: | ||
rootfs_dir = utils.dockerutils.get_docker_container_rootfs_path( | ||
container_id) | ||
exclude_dirs = [utils.misc.join_abs_paths(rootfs_dir, d) | ||
for d in exclude_dirs] | ||
return crawl_jar_files( | ||
root_dir=utils.misc.join_abs_paths(rootfs_dir, root_dir), | ||
exclude_dirs=exclude_dirs, | ||
root_dir_alias=root_dir) | ||
else: # in all other cases, including wrong mode set | ||
return run_as_another_namespace(pid, | ||
['mnt'], | ||
crawl_jar_files, | ||
root_dir, | ||
exclude_dirs, | ||
None) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[Core] | ||
Name = jar_host | ||
Module = jar_host_crawler | ||
|
||
[Documentation] | ||
Author = IBM | ||
Version = 0.1 | ||
Description = Produces md5 hashes for jar files and class files in jars |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from icrawl_plugin import IHostCrawler | ||
from utils.jar_utils import crawl_jar_files | ||
|
||
|
||
class JarHostCrawler(IHostCrawler): | ||
|
||
def get_feature(self): | ||
return 'jar' | ||
|
||
def crawl( | ||
self, | ||
root_dir='/', | ||
exclude_dirs=[ | ||
'/boot', | ||
'/dev', | ||
'/proc', | ||
'/sys', | ||
'/mnt', | ||
'/tmp', | ||
'/var/cache', | ||
'/usr/share/man', | ||
'/usr/share/doc', | ||
'/usr/share/mime'], | ||
**kwargs): | ||
return crawl_jar_files(root_dir=root_dir, | ||
exclude_dirs=exclude_dirs) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import fnmatch | ||
import logging | ||
import os | ||
import re | ||
import hashlib | ||
import zipfile | ||
|
||
from utils.features import JarFeature | ||
|
||
logger = logging.getLogger('crawlutils') | ||
|
||
|
||
def crawl_jar_files( | ||
root_dir='/', | ||
exclude_dirs=[], | ||
root_dir_alias=None, | ||
accessed_since=0): | ||
|
||
if not os.path.isdir(root_dir): | ||
return | ||
|
||
saved_args = locals() | ||
logger.debug('crawl_jar_files: %s' % (saved_args)) | ||
|
||
assert os.path.isdir(root_dir) | ||
if root_dir_alias is None: | ||
root_dir_alias = root_dir | ||
exclude_dirs = [os.path.join(root_dir, d) for d in | ||
exclude_dirs] | ||
exclude_regex = r'|'.join([fnmatch.translate(d) | ||
for d in exclude_dirs]) or r'$.' | ||
|
||
# walk the directory hierarchy starting at 'root_dir' in BFS | ||
# order | ||
|
||
for (root_dirpath, dirs, files) in os.walk(root_dir): | ||
dirs[:] = [os.path.join(root_dirpath, d) for d in | ||
dirs] | ||
dirs[:] = [d for d in dirs | ||
if not re.match(exclude_regex, d)] | ||
files = [os.path.join(root_dirpath, f) for f in | ||
files] | ||
files = [f for f in files | ||
if not re.match(exclude_regex, f)] | ||
|
||
for fpath in files: | ||
if not fpath.endswith('.jar'): | ||
continue | ||
feature = _crawl_jar_file(root_dir, fpath, root_dir_alias) | ||
if feature: | ||
yield (feature.path, feature, 'jar') | ||
|
||
|
||
# crawl a single file | ||
def _crawl_jar_file( | ||
root_dir, | ||
fpath, | ||
root_dir_alias, | ||
): | ||
if not fpath.endswith('.jar'): | ||
return | ||
|
||
hashes = [] | ||
with zipfile.ZipFile(fpath, 'r') as zf: | ||
for info in zf.infolist(): | ||
if not info.filename.endswith('.class'): | ||
continue | ||
data = zf.read(info.filename) | ||
md = hashlib.md5() | ||
md.update(data) | ||
hashes.append(md.hexdigest()) | ||
|
||
# compute hash of jar file | ||
with open(fpath, 'rb') as jarin: | ||
md = hashlib.md5() | ||
md.update(jarin.read()) | ||
jarhash = md.hexdigest() | ||
# This replaces `/<root_dir>/a/b/c` with `/<root_dir_alias>/a/b/c` | ||
frelpath = os.path.join(root_dir_alias, | ||
os.path.relpath(fpath, root_dir)) | ||
|
||
# This converts something like `/.` to `/` | ||
|
||
frelpath = os.path.normpath(frelpath) | ||
|
||
(_, fname) = os.path.split(frelpath) | ||
return JarFeature( | ||
os.path.basename(fpath), | ||
fpath, | ||
jarhash, | ||
hashes | ||
) |
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
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
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import unittest | ||
|
||
import os | ||
import sys | ||
import tempfile | ||
from zipfile import ZipFile, ZipInfo | ||
|
||
from utils import jar_utils | ||
from utils.features import JarFeature | ||
|
||
# | ||
# https://security.openstack.org/guidelines/dg_using-temporary-files-securely.html | ||
# | ||
|
||
sys.path.append('tests/unit/') | ||
from plugins.systems.jar_host_crawler import JarHostCrawler | ||
|
||
|
||
class JarHashesPluginTests(unittest.TestCase): | ||
|
||
def setUp(self): | ||
pass | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
def test_jar_host_crawler_plugin(self, *args): | ||
tmpdir = tempfile.mkdtemp() | ||
jar_file_name = 'myfile.jar' | ||
|
||
# Ensure the file is read/write by the creator only | ||
saved_umask = os.umask(0077) | ||
|
||
path = os.path.join(tmpdir, jar_file_name) | ||
try: | ||
with ZipFile(path, "w") as myjar: | ||
myjar.writestr(ZipInfo('first.class',(1980,1,1,1,1,1)), "first secrets!") | ||
myjar.writestr(ZipInfo('second.class',(1980,1,1,1,1,1)), "second secrets!") | ||
myjar.writestr(ZipInfo('second.txt',(1980,1,1,1,1,1)), "second secrets!") | ||
|
||
fc = JarHostCrawler() | ||
jars = list(fc.crawl(root_dir=tmpdir)) | ||
#jars = list(jar_utils.crawl_jar_files(root_dir=tmpdir)) | ||
print jars | ||
jar_feature = jars[0][1] | ||
assert 'myfile.jar' == jar_feature.name | ||
assert '48ac85a26ffa7ff5cefdd5c73a9fb888' == jar_feature.jarhash | ||
assert ['ddc6eff37020aa858e26b1ba8a49ee0e', | ||
'cbe2a13eb99c1c8ac5f30d0a04f8c492'] == jar_feature.hashes | ||
assert 'jar' == jars[0][2] | ||
|
||
except IOError as e: | ||
print 'IOError' | ||
finally: | ||
os.remove(path) | ||
os.umask(saved_umask) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import unittest | ||
|
||
import os | ||
import tempfile | ||
from zipfile import ZipFile, ZipInfo | ||
|
||
from utils import jar_utils | ||
from utils.features import JarFeature | ||
|
||
# | ||
# https://security.openstack.org/guidelines/dg_using-temporary-files-securely.html | ||
# | ||
|
||
class JarUtilsTests(unittest.TestCase): | ||
|
||
def setUp(self): | ||
pass | ||
|
||
def tearDown(self): | ||
pass | ||
|
||
def test_get_jar_features(self): | ||
tmpdir = tempfile.mkdtemp() | ||
jar_file_name = 'myfile.jar' | ||
|
||
# Ensure the file is read/write by the creator only | ||
saved_umask = os.umask(0077) | ||
|
||
path = os.path.join(tmpdir, jar_file_name) | ||
try: | ||
with ZipFile(path, "w") as myjar: | ||
myjar.writestr(ZipInfo('first.class',(1980,1,1,1,1,1)), "first secrets!") | ||
myjar.writestr(ZipInfo('second.class',(1980,1,1,1,1,1)), "second secrets!") | ||
myjar.writestr(ZipInfo('second.txt',(1980,1,1,1,1,1)), "second secrets!") | ||
|
||
jars = list(jar_utils.crawl_jar_files(root_dir=tmpdir)) | ||
print jars | ||
jar_feature = jars[0][1] | ||
assert 'myfile.jar' == jar_feature.name | ||
assert '48ac85a26ffa7ff5cefdd5c73a9fb888' == jar_feature.jarhash | ||
assert ['ddc6eff37020aa858e26b1ba8a49ee0e', | ||
'cbe2a13eb99c1c8ac5f30d0a04f8c492'] == jar_feature.hashes | ||
assert 'jar' == jars[0][2] | ||
|
||
except IOError as e: | ||
print 'IOError' | ||
finally: | ||
os.remove(path) | ||
os.umask(saved_umask) | ||
os.rmdir(tmpdir) |
Oops, something went wrong.