Skip to content

Commit b5c42e9

Browse files
committed
modifying vault utils, separating the client + introducing cli and config for server
1 parent 427341d commit b5c42e9

File tree

5 files changed

+61
-19
lines changed

5 files changed

+61
-19
lines changed

server/app.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from tools.docker_utils import get_build_env_image_digests
1010
from pyspiffe.spiffe_id.spiffe_id import SpiffeId
1111

12-
sys.path.append(os.path.expanduser("../"))
12+
from tools.config.config import parse_configuration
13+
from tools.cli.cli import parse_arguments
1314
from utils.vault.vault_utils import (
1415
vault_login,
1516
write_client_policy,
@@ -20,11 +21,14 @@
2021

2122
app = Quart(__name__)
2223

24+
options = parse_arguments()
25+
configuration = parse_configuration(options.config)
26+
2327
# Defining the trust domain (SPIRE Trust Domain)
24-
trust_domain = "lumi-sd-dev"
28+
trust_domain = configuration['spire-server']['trust-domain']
2529

2630
# Perform vault login, to be able to run later operations against vault
27-
vault_login(get_server_identity_JWT(), "lumi-sd-server")
31+
hvac_client = vault_login(configuration['vault']['url'], get_server_identity_JWT(), configuration['vault']['server-role'])
2832

2933

3034
# Dummy endpoint that handles the registration of compute nodes.
@@ -73,7 +77,7 @@ async def handle_client_registration():
7377
client_id = hashlib.sha256(client_id.encode()).hexdigest()[0:9]
7478

7579
# Write a policy to the vault to authorize the client to write secrets
76-
write_client_policy(f"client_{client_id}")
80+
write_client_policy(hvac_client, f"client_{client_id}")
7781

7882
# Create spiffeID out of this client id
7983
agent_spiffeID = SpiffeId.parse(f"spiffe://{trust_domain}/c/{client_id}")
@@ -93,7 +97,7 @@ async def handle_client_registration():
9397
)
9498

9599
# Write the role bound to the workload's spiffeID
96-
write_client_role(f"client_{client_id}", workload_spiffeID)
100+
write_client_role(hvac_client, f"client_{client_id}", workload_spiffeID)
97101

98102
# For each authorized container preparation process (Here, a list of docker container_preaparation image names)
99103
for digest in get_build_env_image_digests():
@@ -223,10 +227,10 @@ async def handle_workload_creation():
223227
compute_nodes_added[compute_node]["groups"] = groups_added
224228

225229
# Generate and create a policy that gives read-only access to the application's secret
226-
write_user_policy(f"client_{client_id}", data["secret"])
230+
write_user_policy(hvac_client, f"client_{client_id}", data["secret"])
227231

228232
# Generate and create a role bound to the policy and to the spiffeID
229-
write_user_role(f"client_{client_id}", data["secret"], spiffeID)
233+
write_user_role(hvac_client, f"client_{client_id}", data["secret"], spiffeID)
230234

231235
# Success
232236
return {

server/tools/cli/cli.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import argparse
2+
3+
# Parse arguments from the cli
4+
def parse_arguments():
5+
"""Parse arguments from cli
6+
7+
Returns:
8+
ArgumentParser: the ArgumentParser produced
9+
"""
10+
parser = argparse.ArgumentParser(description="CLI Optinons")
11+
12+
parser.add_argument(
13+
"--config",
14+
"-c",
15+
type=str,
16+
default="/tmp/hpcs-server.conf",
17+
help="Configuration file (INI Format) (default: /tmp/hpcs-server.conf)",
18+
)
19+
20+
return parser.parse_args()

server/tools/config/config.py

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

utils/ship_a_key.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,15 @@ def create_authorized_workloads(
285285
)
286286

287287
# Login to the vault using client's certificate
288-
vault_login(SVID, f"client_{client_id}")
288+
hvac_client = vault_login(SVID, f"client_{client_id}")
289289

290290
# Prepare secret
291291
secret = {}
292292
with open(pem_path, "r") as pem:
293293
secret["key"] = pem.read()
294294

295295
# Write secret to the vault
296-
write_secret(secrets_path, secret)
296+
write_secret(hvac_client, secrets_path, secret)
297297

298298
print(
299299
f"Key successfully written to the vault. Users needs the role {user_role} to access the secret stored at {secrets_path}"

utils/vault/vault_utils.py

+9-10
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22
from pyspiffe.svid.jwt_svid import JwtSvid
33
from pyspiffe.spiffe_id.spiffe_id import SpiffeId
44

5-
client = hvac.Client(url="")
6-
7-
8-
def vault_login(SVID: JwtSvid, client_id):
5+
def vault_login(url : str, SVID: JwtSvid, client_id) -> hvac.Client :
96
"""Login to vault
107
118
Args:
129
SVID (JwtSvid): The client's certificate to perform mTLS via OIDC
1310
client_id (str): client's id, which happens to be the name of the role bound to the client
1411
"""
15-
return client.auth.jwt.jwt_login(role=client_id, jwt=SVID.token)
12+
client = hvac.Client(url=url)
13+
client.auth.jwt.jwt_login(role=client_id, jwt=SVID.token)
14+
return client
1615

1716

18-
def write_client_policy(client_id: str):
17+
def write_client_policy(client : hvac.Client, client_id: str):
1918
"""Write a client write-only policy to vault
2019
2120
Args:
@@ -30,7 +29,7 @@ def write_client_policy(client_id: str):
3029
return client.sys.create_or_update_acl_policy(name=f"{client_id}", policy=policy)
3130

3231

33-
def write_client_role(client_id: str, spiffeID: SpiffeId):
32+
def write_client_role(client : hvac.Client, client_id: str, spiffeID: SpiffeId):
3433
"""Write a client role, mapping a "clientID" named role to a spiffeID
3534
3635
Args:
@@ -48,7 +47,7 @@ def write_client_role(client_id: str, spiffeID: SpiffeId):
4847
)
4948

5049

51-
def write_user_policy(client_id: str, application: str):
50+
def write_user_policy(client : hvac.Client, client_id: str, application: str):
5251
"""Write a user read-only policy to vault
5352
5453
Args:
@@ -66,7 +65,7 @@ def write_user_policy(client_id: str, application: str):
6665
)
6766

6867

69-
def write_user_role(client_id: str, application: str, spiffeID: SpiffeId):
68+
def write_user_role(client : hvac.Client, client_id: str, application: str, spiffeID: SpiffeId):
7069
"""Write a user role bounding a spiffeID to the read-only policy accessing the client's secret
7170
7271
Args:
@@ -85,7 +84,7 @@ def write_user_role(client_id: str, application: str, spiffeID: SpiffeId):
8584
)
8685

8786

88-
def write_secret(secrets_path: str, secret: any):
87+
def write_secret(client : hvac.Client, secrets_path: str, secret: any):
8988
"""Write a secret to the vault
9089
9190
Args:

0 commit comments

Comments
 (0)