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

CSN-212-Security--SSH-Keys #201

Open
wants to merge 2 commits into
base: dev
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
36 changes: 31 additions & 5 deletions neurons/Miner/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
ssh_port = 4444 # Port to map SSH service on the host


def get_stored_ssh_key():
try:
file_path = 'allocation_key'
with open(file_path, 'r') as file:
allocation_key = file.read()
allocation_key = base64.b64decode(allocation_key).decode("utf-8")
return allocation_key
except Exception as e:
bt.logging.info(f"Error getting stored SSH key {e}")
return None
# Initialize Docker client
def get_docker():
client = docker.from_env()
Expand Down Expand Up @@ -280,8 +290,12 @@ def build_sample_container():
return {"status": False}


def restart_container():
def restart_container(public_key):
try:
stored_ssh_key = get_stored_ssh_key()
if stored_ssh_key.strip() != public_key.strip():
bt.logging.info("Error restart the container: Permission denied.")
return {"status": False}
client, containers = get_docker()
running_container = None
for container in containers:
Expand All @@ -302,8 +316,12 @@ def restart_container():
bt.logging.info(f"Error restart container {e}")
return {"status": False}

def pause_container():
def pause_container(public_key):
try:
stored_ssh_key = get_stored_ssh_key()
if stored_ssh_key.strip() != public_key.strip():
bt.logging.info("Error pausing the container: Permission denied.")
return {"status": False}
client, containers = get_docker()
running_container = None
for container in containers:
Expand All @@ -320,8 +338,12 @@ def pause_container():
bt.logging.info(f"Error pausing container {e}")
return {"status": False}

def unpause_container():
def unpause_container(public_key):
try:
stored_ssh_key = get_stored_ssh_key()
if stored_ssh_key.strip() != public_key.strip():
bt.logging.info("Error unpausing the container: Permission denied.")
return {"status": False}
client, containers = get_docker()
running_container = None
for container in containers:
Expand All @@ -338,8 +360,12 @@ def unpause_container():
bt.logging.info(f"Error unpausing container {e}")
return {"status": False}

def exchange_key_container(new_ssh_key: str):
def exchange_key_container(public_key,new_ssh_key: str):
try:
stored_ssh_key = get_stored_ssh_key()
if stored_ssh_key.strip() != public_key.strip():
bt.logging.info("Error exchanging the container key: Permission denied.")
return {"status": False}
client, containers = get_docker()
running_container = None
for container in containers:
Expand All @@ -359,4 +385,4 @@ def exchange_key_container(new_ssh_key: str):
return {"status": False}
except Exception as e:
bt.logging.info(f"Error changing SSH key on container {e}")
return {"status": False}
return {"status": False}
8 changes: 4 additions & 4 deletions neurons/miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,22 +447,22 @@ def allocate(self, synapse: Allocate) -> Allocate:
if docker_action["action"] == "exchange_key":
public_key = synapse.public_key
new_ssh_key = docker_action["ssh_key"]
result = exchange_key_container(new_ssh_key)
result = exchange_key_container(public_key=public_key, new_ssh_key=new_ssh_key)
synapse.output = result
elif docker_action["action"] == "restart":
public_key = synapse.public_key
result = restart_container()
result = restart_container(public_key=public_key)
synapse.output = result
elif docker_action["action"] == "pause":
public_key = synapse.public_key
result = pause_container()
result = pause_container(public_key=public_key)
synapse.output = result
elif (
docker_action["action"] == "unpause"
or docker_action["action"] == "resume"
):
public_key = synapse.public_key
result = unpause_container()
result = unpause_container(public_key=public_key)
synapse.output = result
else:
bt.logging.info(f"Unknown action: {docker_action['action']}")
Expand Down