Skip to content

Commit

Permalink
updating with example, and fixing bug
Browse files Browse the repository at this point in the history
Signed-off-by: Vanessa Sochat <vsochat@stanford.edu>
  • Loading branch information
vsoch committed Jun 26, 2019
1 parent da1e2a3 commit 5d0d659
Show file tree
Hide file tree
Showing 6 changed files with 918 additions and 24 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -54,3 +54,6 @@ Scan a local image in $PWD mapped to /code in the container. If you didn't clone
$ singularity pull shub://vsoch/singularity-images
$ docker exec -it clair-scanner sclair singularity-images_latest.sif
```

For a full example (using a container with a known vulnerability) see
the [example test](test) folder.
7 changes: 3 additions & 4 deletions stools/clair/__init__.py
Expand Up @@ -22,6 +22,7 @@
from stools.version import __version__
from stools.clair.image import export_to_targz
from stools.clair.api import Clair
from stools.clair.server import start
from multiprocessing import Process
import argparse
import os
Expand Down Expand Up @@ -103,14 +104,12 @@ def help(retval=0):

# Local Server
webroot = '/var/www/images'
server = 'http://%s:%s/' %(args.host, args.port)

# Start the server and serve static files from root

if args.server is True:
from stools.clair.server import start
print('\n1. Starting server...')
webroot = tempfile.mkdtemp()
server = 'http://%s:%s/' %(args.host, args.port)
process = Process(target=start, args=(args.port, args.host, webroot))
# start(port=args.port, host=args.host, static_folder=webroot)
process.daemon = True
Expand All @@ -128,7 +127,7 @@ def help(retval=0):
for image in args.images:

# 1. decompress to sandbox --> tar.gz
targz = export_to_targz(image, via_build=True)
targz = export_to_targz(image)
print("...exported %s to %s" %(image, targz))

# 2. Move to webroot
Expand Down
2 changes: 1 addition & 1 deletion stools/clair/api.py
Expand Up @@ -47,7 +47,7 @@ def scan(self, targz_url, name):
'Parentname': '',
'Format': 'Docker' }

response = requests.post(url, json={'Layer': data })
response = requests.post(url, json={'Layer': data})

if response.status_code != 201:
print('Error creating %s at %s' %(data['Path'], url))
Expand Down
30 changes: 11 additions & 19 deletions stools/clair/image.py
Expand Up @@ -22,11 +22,12 @@
from stools.utils import get_temporary_name
import hashlib
import tempfile
import tarfile
import shutil
import os


def export_to_targz(image, tmpdir=None, via_build=True):
def export_to_targz(image, tmpdir=None):
'''export a Singularity image to a .tar.gz file. If run within a docker
image, you should set via_build to false (as sudo will work under
priviledged). Outside of Docker as regular user, via_build works
Expand All @@ -38,33 +39,24 @@ def export_to_targz(image, tmpdir=None, via_build=True):
tmpdir: a temporary directory to export to.
'''
print("Exporting %s to targz..." %image)
print("Exporting %s to targz..." % image)

if tmpdir == None:
tmpdir = tempfile.mkdtemp()

# We will build into this directory (sandbox) to export without sudo
export_dir = get_temporary_name(tmpdir, 'singularity-clair')
tar = "%s.tar" %export_dir
targz = "%s.gz" %tar
targz = "%s.gz" % export_dir

if via_build is True:

sandbox = Client.build(image, export_dir, sandbox=True, sudo=False)
sandbox = Client.build(image, export_dir,
sandbox=True,
sudo=False)

# Create the .tar, then .tar.gz

cmd = ["tar", "-cf", tar, sandbox]
Client._run_command(cmd)
shutil.rmtree(sandbox)

else:

# Requires sudo
Client.image.export(image, tar)
# Write the tarfile
with tarfile.open(targz, "w:gz") as tar:
tar.add(sandbox, arcname='/')

cmd = ["gzip", tar]
Client._run_command(cmd)
shutil.rmtree(sandbox)

if os.path.exists(targz):
return targz
Expand Down

0 comments on commit 5d0d659

Please sign in to comment.