Skip to content

Commit

Permalink
adding the config file to spawn-agent, refactoring for common usage
Browse files Browse the repository at this point in the history
  • Loading branch information
telliere committed Mar 21, 2024
1 parent 09c9115 commit 58fdf0b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 58 deletions.
2 changes: 1 addition & 1 deletion client/container_preparation/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ echo -e "${YELLOW}[LUMI-SD]${NC}${BLUE}[Container preparation]${NC} Entering ent
if [ -n "$encrypted" ]; then
echo -e "${YELLOW}[LUMI-SD]${NC}${BLUE}[Container preparation]${NC} Encryption mode is on. Registering and running SPIRE Agent"

python3 ./utils/spawn_agent.py > /dev/null 2> /dev/null || exit 1 &
python3 ./utils/spawn_agent.py --config $config > /dev/null 2> /dev/null || exit 1 &
spire_agent_pid=$!

fi
Expand Down
2 changes: 1 addition & 1 deletion client/data_preparation/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ echo -e "${YELLOW}[LUMI-SD]${NC}${BLUE}[Data preparation]${NC} Entering entrypoi

echo -e "${YELLOW}[LUMI-SD]${NC}${BLUE}[Data preparation]${NC} Registering and running SPIRE Agent"

python3 ./utils/spawn_agent.py > /dev/null 2> /dev/null || exit 1 &
python3 ./utils/spawn_agent.py --config $config > /dev/null 2> /dev/null || exit 1 &
spire_agent_pid=$!

until [ -e /tmp/agent.sock ]
Expand Down
26 changes: 26 additions & 0 deletions utils/conf/client/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Parse configuration file
from configparser import ConfigParser, NoSectionError, NoOptionError

def parse_configuration(path : str):
config = ConfigParser()
config.read(path)

if not 'spire-server' in config:
raise NoSectionError("hpcs-server section missing in configuration file, aborting")

if not 'hpcs-server' in config:
raise NoSectionError("hpcs-server section missing in configuration file, aborting")

if not 'vault' in config:
raise NoSectionError("vault section missing in configuration file, aborting")

if not 'address' in config['spire-server'] or not 'port' in config['spire-server'] or not 'trust-domain' in config['spire-server']:
raise NoOptionError("'spire-server' section is incomplete in configuration file, aborting")

if not 'url' in config['hpcs-server']:
raise NoOptionError("'hpcs-server' section is incomplete in configuration file, aborting")

if not 'url' in config['vault']:
raise NoOptionError("'vault' section is incomplete in configuration file, aborting")

return config
23 changes: 2 additions & 21 deletions utils/ship_a_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import yaml
from hashlib import sha512
from ssh_utils import ssh_connect, ssh_copy_file

from configparser import ConfigParser, NoSectionError, NoOptionError
from conf.client.conf import parse_configuration

# Provide client_id from cli$
# Same for trust domain
Expand Down Expand Up @@ -97,25 +96,6 @@ def parse_arguments() -> argparse.ArgumentParser:

return parser.parse_args()

# Parse configuration file
def parse_configuration(path : str):
config = ConfigParser()
config.read(path)

if not 'hpcs-server' in config:
raise NoSectionError("hpcs-server section missing in configuration file, aborting")

if not 'vault' in config:
raise NoSectionError("vault section missing in configuration file, aborting")

if not 'url' in config['hpcs-server']:
raise NoOptionError("'hpcs-server' section is incomplete in configuration file, aborting")

if not 'url' in config['vault']:
raise NoOptionError("'vault' section is incomplete in configuration file, aborting")

return config


def validate_options(options: argparse.ArgumentParser):
"""Check for the cli-provided options
Expand Down Expand Up @@ -262,6 +242,7 @@ def create_authorized_workloads(
if __name__ == "__main__":
# Parse arguments from CLI
options = parse_arguments()

# Parse configuration file
configuration = parse_configuration(options.config)

Expand Down
46 changes: 11 additions & 35 deletions utils/spawn_agent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import platform, argparse, subprocess, requests
from conf.client.conf import parse_configuration


# Parse arguments from the cli
Expand All @@ -10,33 +11,6 @@ def parse_arguments():
"""
parser = argparse.ArgumentParser(description="CLI Optinons")

parser.add_argument(
"--spire-trust-domain",
"-t",
type=str,
default="lumi-sd-dev",
help="Server address (default: lumi-sd-dev)",
)
parser.add_argument(
"--sd-server-address",
"-a",
type=str,
help="Server address",
)
parser.add_argument(
"--spire-server-port",
"-sp",
type=int,
default=10081,
help="Spire server port (default: 10081)",
)
parser.add_argument(
"--sd-server-port",
"-ap",
type=int,
default=10080,
help="SD API server port (default: 10080)",
)
parser.add_argument(
"--socketpath",
"-s",
Expand All @@ -54,8 +28,7 @@ def parse_arguments():

return parser.parse_args()


def get_token(server, port, compute_node_token: bool):
def get_token(url, compute_node_token: bool):
"""Get joinToken to perform node registration from server
Args:
Expand All @@ -73,9 +46,9 @@ def get_token(server, port, compute_node_token: bool):
# Check wether we are performing compute node attestation or client attestation, create url
if compute_node_token:
hostname = platform.node()
url = f"http://{server}:{port}/api/agents/token?hostname={hostname}"
url = f"{url}/api/agents/token?hostname={hostname}"
else:
url = f"http://{server}:{port}/api/client/register"
url = f"{url}/api/client/register"

# Perform POST request to SD server
response = requests.post(url)
Expand All @@ -89,22 +62,25 @@ def get_token(server, port, compute_node_token: bool):
if __name__ == "__main__":
# Get arguments
options = parse_arguments()

# Parse configuration file
configuration = parse_configuration(options.config)

# Get token from API
token = get_token(
options.sd_server_address, options.sd_server_port, options.compute_node
configuration['hpcs-server']['url'], options.compute_node
)

# Overwrite configuration template
agent_configuration_template = open("./utils/agent-on-the-fly.conf").read()
agent_configuration_template = agent_configuration_template.replace(
"SPIRE_TRUST_DOMAIN", options.spire_trust_domain
"SPIRE_TRUST_DOMAIN", configuration['spire-server']['trust-domain']
)
agent_configuration_template = agent_configuration_template.replace(
"SPIRE_SERVER_ADDRESS", options.sd_server_address
"SPIRE_SERVER_ADDRESS", configuration['spire-server']['address']
)
agent_configuration_template = agent_configuration_template.replace(
"SPIRE_SERVER_PORT", str(options.spire_server_port)
"SPIRE_SERVER_ADDRESS", configuration['spire-server']['port']
)
agent_configuration_template = agent_configuration_template.replace(
"SOCKETPATH", options.socketpath
Expand Down

0 comments on commit 58fdf0b

Please sign in to comment.