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

Added bashrc_extesions extension #296

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ You can get full details on the extensions from the main `rocker --help` command
- home -- Mount the user's home directory into the container
- pulse -- Mount pulse audio into the container
- ssh -- Pass through ssh access to the container.
- bashrc_extensions -- Pass a list of files/URLs of bash scripts that will extend the container's native .bashrc

As well as access to many of the docker arguments as well such as `device`, `env`, `volume`, `name`, `network`, `ipc`, and `privileged`.

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
'detect_docker_image_os = rocker.cli:detect_image_os',
],
'rocker.extensions': [
'bashrc_extensions = rocker.bashrc_extension:BashrcExtensions',
'cuda = rocker.nvidia_extension:Cuda',
'devices = rocker.extensions:Devices',
'dev_helpers = rocker.extensions:DevHelpers',
Expand Down
79 changes: 79 additions & 0 deletions src/rocker/bashrc_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import em
import pkgutil
import os
import urllib3
from rocker.core import get_user_name, ExtensionError
from rocker.extensions import RockerExtension


class BashrcExtensions(RockerExtension):

name = 'bashrc_extensions'

@classmethod
def get_name(cls):
return cls.name

def __init__(self):
self._env_subs = None
self.name = BashrcExtensions.get_name()

def precondition_environment(self, cli_args):
pass

def validate_environment(self, cli_args):
pass

def get_preamble(self, cli_args):
return ''

def get_filename(self, bashrc_extension_file):
if os.path.isfile(bashrc_extension_file):
return os.path.join(self.name, os.path.basename(bashrc_extension_file))
elif bashrc_extension_file.startswith(('http://', 'https://')):
filename = os.path.basename(urllib3.util.url.parse_url(bashrc_extension_file).path)
if not filename:
raise ExtensionError('Bashrc extension file does not appear to have a filename: {}'.format(bashrc_extension_file))
return os.path.join(self.name, filename)
else:
raise ExtensionError('Bashrc extension files is not a file or URL: {}'.format(bashrc_extension_file))

def get_files(self, cli_args):
files = {}
for bashrc_extension in cli_args[self.name]:
if os.path.isfile(bashrc_extension):
with open(bashrc_extension, 'r') as f:
files[self.get_filename(bashrc_extension)] = f.read()
elif bashrc_extension.startswith(('http://', 'https://')):
try:
response = urllib3.PoolManager().request('GET', bashrc_extension)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a big change in scope. Fetching remote content opens up a whole extra can of worms. You can easily wget it to file just before running with barely more characters. Lets not add this level of complexity. Especially as this is a path to full remote code execution in the environment when you enter the container as we're not validating the remote content.

if response.status != 200:
raise ExtensionError(f'Failed to fetch bashrc extension from URL {bashrc_extension}, status code: {response.status}')
files[self.get_filename(bashrc_extension)] = response.data.decode('utf-8')
except urllib3.exceptions.HTTPError as e:
raise ExtensionError(f'Failed to fetch bashrc extension from URL {bashrc_extension}: {str(e)}')

return files

@staticmethod
def get_home_dir(cli_args):
if cli_args["user"]:
return os.path.join(os.path.sep, "home", get_user_name())
else:
return os.path.join(os.path.sep, "root")

def get_user_snippet(self, cli_args):
args = {}
args['bashrc_extension_files'] = {self.get_filename(bashrc_extension): os.path.basename(self.get_filename(bashrc_extension)) for bashrc_extension in cli_args[self.name]}
args['home_dir'] = self.get_home_dir(cli_args)

snippet = pkgutil.get_data(
'rocker', 'templates/{}_user_snippet.Dockerfile.em'.format(self.name)).decode('utf-8')

return em.expand(snippet, args)

@staticmethod
def register_arguments(parser, defaults={}):
parser.add_argument('--bashrc-extensions',
nargs='+',
help="Sources custom bashrc extensions from the container's default bashrc. An extension can be a local file or a URL.")
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
WORKDIR @home_dir
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to be careful about setting this WORKDIR as it sideeffects downstream snippets.

RUN echo "\n# Source custom bashrc extensions" >> @home_dir/.bashrc
@[for path, filename in bashrc_extension_files.items()]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is potentially adding a bunch of layers. It can at least collapse the RUN command into only one instance not one per file.

COPY @path @filename
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is effectively a temporary file, it should probably go into somewhere like /tmp and not potentially override content in their home directory. (For example if someone tries to append their own local .bashrc it might get messy.

RUN echo ". ~/@filename" >> @home_dir/.bashrc
@[end for]
Loading