Skip to content

Commit af4f6c2

Browse files
committed
adding socketpath to spire-agent socket in server
1 parent e859010 commit af4f6c2

File tree

3 files changed

+73
-41
lines changed

3 files changed

+73
-41
lines changed

server/app.py

+52-24
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
get_server_identity_JWT,
77
validate_client_JWT_SVID,
88
)
9-
from lib import spire_interactions
9+
from lib import spire_interactions
1010
from tools.docker_utils import get_build_env_image_digests
1111
from pyspiffe.spiffe_id.spiffe_id import SpiffeId
12+
from pyspiffe.workloadapi import default_jwt_source
1213

1314
from tools.config.config import parse_configuration
1415
from tools.cli.cli import parse_arguments
@@ -25,19 +26,38 @@
2526
options = parse_arguments()
2627
configuration = parse_configuration(options.config)
2728

28-
if configuration['spire-server'].get('spire-server-bin') :
29-
spire_interactions.spire_server_bin = configuration['spire-server']['spire-server-bin']
29+
if configuration["spire-server"].get("spire-server-bin"):
30+
spire_interactions.spire_server_bin = configuration["spire-server"][
31+
"spire-server-bin"
32+
]
3033

31-
if configuration['spire-server'].get('pre-command') :
32-
spire_interactions.pre_command = configuration['spire-server']['pre-command']
33-
if configuration['spire-server']['pre-command'] == "\"\"":
34+
if configuration["spire-agent"].get("spire-agent-socket"):
35+
spire_interactions.jwt_workload_api = default_jwt_source.DefaultJwtSource(
36+
workload_api_client=None,
37+
spiffe_socket_path=f"unix://{configuration['spire-agent'].get('spire-agent-socket')}",
38+
timeout_in_seconds=None,
39+
)
40+
else:
41+
spire_interactions.jwt_workload_api = default_jwt_source.DefaultJwtSource(
42+
workload_api_client=None,
43+
spiffe_socket_path="unix:///tmp/spire-agent/public/api.sock",
44+
timeout_in_seconds=None,
45+
)
46+
47+
if configuration["spire-server"].get("pre-command"):
48+
spire_interactions.pre_command = configuration["spire-server"]["pre-command"]
49+
if configuration["spire-server"]["pre-command"] == '""':
3450
spire_interactions.pre_command = ""
35-
51+
3652
# Defining the trust domain (SPIRE Trust Domain)
37-
trust_domain = configuration['spire-server']['trust-domain']
53+
trust_domain = configuration["spire-server"]["trust-domain"]
3854

3955
# Perform vault login, to be able to run later operations against vault
40-
hvac_client = vault_login(configuration['vault']['url'], get_server_identity_JWT(), configuration['vault']['server-role'])
56+
hvac_client = vault_login(
57+
configuration["vault"]["url"],
58+
get_server_identity_JWT(),
59+
configuration["vault"]["server-role"],
60+
)
4161

4262

4363
# Dummy endpoint that handles the registration of compute nodes.
@@ -101,9 +121,7 @@ async def handle_client_registration():
101121

102122
# Create a spiffeID for the workloads on the client.
103123
# Register workloads that have to run on this agent
104-
workload_spiffeID = SpiffeId(
105-
f"spiffe://{trust_domain}/c/{client_id}/workload"
106-
)
124+
workload_spiffeID = SpiffeId(f"spiffe://{trust_domain}/c/{client_id}/workload")
107125

108126
# Write the role bound to the workload's spiffeID
109127
write_client_role(hvac_client, f"client_{client_id}", workload_spiffeID)
@@ -128,22 +146,34 @@ async def handle_client_registration():
128146
"client_id": client_id,
129147
"token": agent_token,
130148
}
131-
132-
# Spire-Agent binary
149+
150+
# Spire-Agent binary
133151
result = entry_create(
134-
agent_spiffeID, workload_spiffeID, ["unix:sha256:5ebff0fdb3335ec0221c35dcc7d3a4433eb8a5073a15a6dcfdbbb95bb8dbfa8e"]
152+
agent_spiffeID,
153+
workload_spiffeID,
154+
[
155+
"unix:sha256:5ebff0fdb3335ec0221c35dcc7d3a4433eb8a5073a15a6dcfdbbb95bb8dbfa8e"
156+
],
135157
)
136-
137-
# Python 3.9 binary
158+
159+
# Python 3.9 binary
138160
result = entry_create(
139-
agent_spiffeID, workload_spiffeID, ["unix:sha256:956a50083eb7a58240fea28ac52ff39e9c04c5c74468895239b24bdf4760bffe"]
161+
agent_spiffeID,
162+
workload_spiffeID,
163+
[
164+
"unix:sha256:956a50083eb7a58240fea28ac52ff39e9c04c5c74468895239b24bdf4760bffe"
165+
],
140166
)
141-
167+
142168
# Qemu x86_64 (For docker mac) // Could add Rosetta binary
143169
result = entry_create(
144-
agent_spiffeID, workload_spiffeID, ["unix:sha256:3fc6c8fbd8fe429b67276854fbb5ae594118f7f0b10352a508477833b04ee9b7"]
170+
agent_spiffeID,
171+
workload_spiffeID,
172+
[
173+
"unix:sha256:3fc6c8fbd8fe429b67276854fbb5ae594118f7f0b10352a508477833b04ee9b7"
174+
],
145175
)
146-
176+
147177
# Success
148178
return {
149179
"success": True,
@@ -176,9 +206,7 @@ async def handle_workload_creation():
176206
client_id = hashlib.sha256(client_id.encode()).hexdigest()[0:9]
177207

178208
# Parse the spiffeID that will access the application
179-
spiffeID = SpiffeId(
180-
f"spiffe://{trust_domain}/c/{client_id}/s/{data['secret']}"
181-
)
209+
spiffeID = SpiffeId(f"spiffe://{trust_domain}/c/{client_id}/s/{data['secret']}")
182210

183211
# Check that the SVID correspond to the client_id (Can be removed if developper is certified)
184212
if validate_client_JWT_SVID(data["jwt"], client_id):

server/lib/spire_interactions.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88
pre_command = "microk8s.kubectl exec -n spire spire-server-0 --"
99

1010

11-
jwt_workload_api = default_jwt_source.DefaultJwtSource(
12-
workload_api_client=None,
13-
spiffe_socket_path="unix:///tmp/spire-agent/public/api.sock",
14-
timeout_in_seconds=None
15-
)
11+
jwt_workload_api = None
1612

1713

1814
def token_generate(spiffeID: SpiffeId) -> subprocess.CompletedProcess:
@@ -33,7 +29,7 @@ def token_generate(spiffeID: SpiffeId) -> subprocess.CompletedProcess:
3329
command = f"{spire_server_bin} token generate -spiffeID {str(spiffeID)}".split(
3430
" "
3531
)
36-
32+
3733
return subprocess.run(command, capture_output=True)
3834

3935

server/tools/config/config.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
from configparser import ConfigParser, NoSectionError, NoOptionError
22

3-
def parse_configuration(path : str):
3+
4+
def parse_configuration(path: str):
45
config = ConfigParser()
56
config.read(path)
6-
7-
if not 'spire-server' in config:
7+
8+
if not "spire-agent" in config:
9+
raise NoSectionError("spire-agent section missing, aborting")
10+
11+
if not "spire-server" in config:
812
raise NoSectionError("spire-server section missing, aborting")
9-
10-
if not 'vault' in config:
13+
14+
if not "vault" in config:
1115
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']:
16+
17+
if (
18+
not "address" in config["spire-server"]
19+
or not "port" in config["spire-server"]
20+
or not "trust-domain" in config["spire-server"]
21+
):
1422
raise NoOptionError("'spire-server' section is incomplete, aborting")
15-
16-
if not 'url' in config['vault'] or not 'server-role' in config['vault']:
23+
24+
if not "url" in config["vault"] or not "server-role" in config["vault"]:
1725
raise NoOptionError("'vault' section is incomplete, aborting")
18-
19-
return config
26+
27+
return config

0 commit comments

Comments
 (0)