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

Implement docker outgoing firewall #254

Open
wants to merge 3 commits into
base: master
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
7 changes: 7 additions & 0 deletions restful_tango/tangoREST.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ def convertJobObj(self, dirName, jobObj):
if "disable_network" in jobObj and isinstance(jobObj["disable_network"], bool):
disableNetwork = jobObj["disable_network"]

allowedOutgoingIPs = None
if "allowed_outgoing_ips" in jobObj and isinstance(
jobObj["allowed_outgoing_ips"], list
):
allowedOutgoingIPs = jobObj["allowed_outgoing_ips"]

job = TangoJob(
name=name,
vm=vm,
Expand All @@ -180,6 +186,7 @@ def convertJobObj(self, dirName, jobObj):
accessKey=accessKey,
accessKeyId=accessKeyId,
disableNetwork=disableNetwork,
allowedOutgoingIPs=allowedOutgoingIPs,
)

self.log.debug("inputFiles: %s" % [file.localFile for file in input])
Expand Down
2 changes: 2 additions & 0 deletions tangoObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def __init__(
accessKeyId=None,
accessKey=None,
disableNetwork=None,
allowedOutgoingIPs=None,
):
self.assigned = False
self.retries = 0
Expand All @@ -114,6 +115,7 @@ def __init__(
self.accessKeyId = accessKeyId
self.accessKey = accessKey
self.disableNetwork = disableNetwork
self.allowedOutgoingIPs = allowedOutgoingIPs
evanyeyeye marked this conversation as resolved.
Show resolved Hide resolved

def makeAssigned(self):
self.syncRemote()
Expand Down
17 changes: 14 additions & 3 deletions vmms/localDocker.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,9 @@ def copyIn(self, vm, inputFiles):
)
return 0

def runJob(self, vm, runTimeout, maxOutputFileSize, disableNetwork):
def runJob(
self, vm, runTimeout, maxOutputFileSize, disableNetwork, allowedOutgoingIPs
):
"""runJob - Run a docker container by doing the follows:
- mount directory corresponding to this job to /home/autolab
in the container
Expand All @@ -163,6 +165,8 @@ def runJob(self, vm, runTimeout, maxOutputFileSize, disableNetwork):
args = args + ["-m", f"{vm.memory}m"]
if disableNetwork:
args = args + ["--network", "none"]
if not disableNetwork and allowedOutgoingIPs:
args = args + ["--dns", "8.8.8.8", "--cap-add=NET_ADMIN"]
args = args + [vm.image]
args = args + ["sh", "-c"]

Expand All @@ -176,10 +180,17 @@ def runJob(self, vm, runTimeout, maxOutputFileSize, disableNetwork):
)
)

iptablesCmd = ""
if not disableNetwork and allowedOutgoingIPs:
iptablesCmd += "iptables -A OUTPUT -d 8.8.8.8 -j ACCEPT; "
for IP in allowedOutgoingIPs:
iptablesCmd += f"iptables -A OUTPUT -d {IP} -j ACCEPT; "
Comment on lines +185 to +187
Copy link

Choose a reason for hiding this comment

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

Correct the static string in the f-string.

- iptablesCmd += "iptables -A OUTPUT -d 8.8.8.8 -j ACCEPT; "
+ iptablesCmd += f"iptables -A OUTPUT -d 8.8.8.8 -j ACCEPT; "

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
iptablesCmd += "iptables -A OUTPUT -d 8.8.8.8 -j ACCEPT; "
for IP in allowedOutgoingIPs:
iptablesCmd += f"iptables -A OUTPUT -d {IP} -j ACCEPT; "
iptablesCmd += f"iptables -A OUTPUT -d 8.8.8.8 -j ACCEPT; "
for IP in allowedOutgoingIPs:
iptablesCmd += f"iptables -A OUTPUT -d {IP} -j ACCEPT; "

iptablesCmd += "iptables -A OUTPUT -j DROP;"

args = args + [
'cp -r mount/* autolab/; su autolab -c "%s"; \
'%s cp -r mount/* autolab/; su autolab -c "%s"; \
cp output/feedback mount/feedback'
% autodriverCmd
% (iptablesCmd, autodriverCmd)
]

self.log.debug("Running job: %s" % str(args))
Expand Down
1 change: 1 addition & 0 deletions worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ def run(self):
self.job.timeout,
self.job.maxOutputFileSize,
self.job.disableNetwork,
self.job.allowedOutgoingIPs,
)
if ret["runjob"] != 0:
Config.runjob_errors += 1
Expand Down