Skip to content

Commit 5e645c1

Browse files
committed
adding the config file to spawn-agent, refactoring for common usage + typo
1 parent 09c9115 commit 5e645c1

File tree

7 files changed

+54
-58
lines changed

7 files changed

+54
-58
lines changed

.github/workflows/build-job-prep-image.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build and publish HPCS jon preparation image
1+
name: Build and publish HPCS job preparation image
22
on: [push]
33

44
env:

.github/workflows/build-server-image.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Publish server docker image
1+
name: Build and publish HPCS server image
22
on: [push]
33

44
env:

client/container_preparation/entrypoint.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,14 @@ echo -e "${YELLOW}[LUMI-SD]${NC}${BLUE}[Container preparation]${NC} Entering ent
6767
if [ -n "$encrypted" ]; then
6868
echo -e "${YELLOW}[LUMI-SD]${NC}${BLUE}[Container preparation]${NC} Encryption mode is on. Registering and running SPIRE Agent"
6969

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

7373
fi
7474

75+
76+
ps -p $spire_agent_pid > /dev/null || ( echo "spire agent died, aborting" ; end_entrypoint "$spire_agent_pid" 1)
77+
7578
#
7679
## [END] Perform node attestation
7780
#

client/data_preparation/entrypoint.sh

+6-1
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,20 @@ echo -e "${YELLOW}[LUMI-SD]${NC}${BLUE}[Data preparation]${NC} Entering entrypoi
5858

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

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

6464
until [ -e /tmp/agent.sock ]
6565
do
6666
echo -e "${RED}[LUMI-SD][Data preparation] Spire workload api socket doesn't exist, waiting 10 seconds ${NC}"
6767
sleep 10
68+
if ! ps -p $spire_agent_pid > /dev/null ; then
69+
echo "spire agent died, aborting"
70+
end_entrypoint "$spire_agent_pid" 1
71+
fi
6872
done
6973

74+
7075
#
7176
## [END] Perform node attestation
7277
#

utils/conf/client/conf.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Parse configuration file
2+
from configparser import ConfigParser, NoSectionError, NoOptionError
3+
4+
def parse_configuration(path : str):
5+
config = ConfigParser()
6+
config.read(path)
7+
8+
if not 'spire-server' in config:
9+
raise NoSectionError("hpcs-server section missing in configuration file, aborting")
10+
11+
if not 'hpcs-server' in config:
12+
raise NoSectionError("hpcs-server section missing in configuration file, aborting")
13+
14+
if not 'vault' in config:
15+
raise NoSectionError("vault section missing in configuration file, aborting")
16+
17+
if not 'address' in config['spire-server'] or not 'port' in config['spire-server'] or not 'trust-domain' in config['spire-server']:
18+
raise NoOptionError("'spire-server' section is incomplete in configuration file, aborting")
19+
20+
if not 'url' in config['hpcs-server']:
21+
raise NoOptionError("'hpcs-server' section is incomplete in configuration file, aborting")
22+
23+
if not 'url' in config['vault']:
24+
raise NoOptionError("'vault' section is incomplete in configuration file, aborting")
25+
26+
return config

utils/ship_a_key.py

+2-21
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
import yaml
1111
from hashlib import sha512
1212
from ssh_utils import ssh_connect, ssh_copy_file
13-
14-
from configparser import ConfigParser, NoSectionError, NoOptionError
13+
from conf.client.conf import parse_configuration
1514

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

9897
return parser.parse_args()
9998

100-
# Parse configuration file
101-
def parse_configuration(path : str):
102-
config = ConfigParser()
103-
config.read(path)
104-
105-
if not 'hpcs-server' in config:
106-
raise NoSectionError("hpcs-server section missing in configuration file, aborting")
107-
108-
if not 'vault' in config:
109-
raise NoSectionError("vault section missing in configuration file, aborting")
110-
111-
if not 'url' in config['hpcs-server']:
112-
raise NoOptionError("'hpcs-server' section is incomplete in configuration file, aborting")
113-
114-
if not 'url' in config['vault']:
115-
raise NoOptionError("'vault' section is incomplete in configuration file, aborting")
116-
117-
return config
118-
11999

120100
def validate_options(options: argparse.ArgumentParser):
121101
"""Check for the cli-provided options
@@ -262,6 +242,7 @@ def create_authorized_workloads(
262242
if __name__ == "__main__":
263243
# Parse arguments from CLI
264244
options = parse_arguments()
245+
265246
# Parse configuration file
266247
configuration = parse_configuration(options.config)
267248

utils/spawn_agent.py

+14-33
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import platform, argparse, subprocess, requests
2+
from conf.client.conf import parse_configuration
23

34

45
# Parse arguments from the cli
@@ -11,31 +12,9 @@ def parse_arguments():
1112
parser = argparse.ArgumentParser(description="CLI Optinons")
1213

1314
parser.add_argument(
14-
"--spire-trust-domain",
15-
"-t",
16-
type=str,
17-
default="lumi-sd-dev",
18-
help="Server address (default: lumi-sd-dev)",
19-
)
20-
parser.add_argument(
21-
"--sd-server-address",
22-
"-a",
23-
type=str,
24-
help="Server address",
25-
)
26-
parser.add_argument(
27-
"--spire-server-port",
28-
"-sp",
29-
type=int,
30-
default=10081,
31-
help="Spire server port (default: 10081)",
32-
)
33-
parser.add_argument(
34-
"--sd-server-port",
35-
"-ap",
36-
type=int,
37-
default=10080,
38-
help="SD API server port (default: 10080)",
15+
"--config",
16+
required=True,
17+
help="Path to the client configuration file",
3918
)
4019
parser.add_argument(
4120
"--socketpath",
@@ -54,8 +33,7 @@ def parse_arguments():
5433

5534
return parser.parse_args()
5635

57-
58-
def get_token(server, port, compute_node_token: bool):
36+
def get_token(url, compute_node_token: bool):
5937
"""Get joinToken to perform node registration from server
6038
6139
Args:
@@ -73,9 +51,9 @@ def get_token(server, port, compute_node_token: bool):
7351
# Check wether we are performing compute node attestation or client attestation, create url
7452
if compute_node_token:
7553
hostname = platform.node()
76-
url = f"http://{server}:{port}/api/agents/token?hostname={hostname}"
54+
url = f"{url}/api/agents/token?hostname={hostname}"
7755
else:
78-
url = f"http://{server}:{port}/api/client/register"
56+
url = f"{url}/api/client/register"
7957

8058
# Perform POST request to SD server
8159
response = requests.post(url)
@@ -89,22 +67,25 @@ def get_token(server, port, compute_node_token: bool):
8967
if __name__ == "__main__":
9068
# Get arguments
9169
options = parse_arguments()
70+
71+
# Parse configuration file
72+
configuration = parse_configuration(options.config)
9273

9374
# Get token from API
9475
token = get_token(
95-
options.sd_server_address, options.sd_server_port, options.compute_node
76+
configuration['hpcs-server']['url'], options.compute_node
9677
)
9778

9879
# Overwrite configuration template
9980
agent_configuration_template = open("./utils/agent-on-the-fly.conf").read()
10081
agent_configuration_template = agent_configuration_template.replace(
101-
"SPIRE_TRUST_DOMAIN", options.spire_trust_domain
82+
"SPIRE_TRUST_DOMAIN", configuration['spire-server']['trust-domain']
10283
)
10384
agent_configuration_template = agent_configuration_template.replace(
104-
"SPIRE_SERVER_ADDRESS", options.sd_server_address
85+
"SPIRE_SERVER_ADDRESS", configuration['spire-server']['address']
10586
)
10687
agent_configuration_template = agent_configuration_template.replace(
107-
"SPIRE_SERVER_PORT", str(options.spire_server_port)
88+
"SPIRE_SERVER_PORT", configuration['spire-server']['port']
10889
)
10990
agent_configuration_template = agent_configuration_template.replace(
11091
"SOCKETPATH", options.socketpath

0 commit comments

Comments
 (0)