Skip to content

Commit 99d396f

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

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

server/app.py

+54-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,40 @@
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+
]
33+
34+
if configuration.get("spire-agent") and configuration["spire-agent"].get(
35+
"spire-agent-socket"
36+
):
37+
spire_interactions.jwt_workload_api = default_jwt_source.DefaultJwtSource(
38+
workload_api_client=None,
39+
spiffe_socket_path=f"unix://{configuration['spire-agent'].get('spire-agent-socket')}",
40+
timeout_in_seconds=None,
41+
)
42+
else:
43+
spire_interactions.jwt_workload_api = default_jwt_source.DefaultJwtSource(
44+
workload_api_client=None,
45+
spiffe_socket_path="unix:///tmp/spire-agent/public/api.sock",
46+
timeout_in_seconds=None,
47+
)
3048

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'] == "\"\"":
49+
if configuration["spire-server"].get("pre-command"):
50+
spire_interactions.pre_command = configuration["spire-server"]["pre-command"]
51+
if configuration["spire-server"]["pre-command"] == '""':
3452
spire_interactions.pre_command = ""
35-
53+
3654
# Defining the trust domain (SPIRE Trust Domain)
37-
trust_domain = configuration['spire-server']['trust-domain']
55+
trust_domain = configuration["spire-server"]["trust-domain"]
3856

3957
# 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'])
58+
hvac_client = vault_login(
59+
configuration["vault"]["url"],
60+
get_server_identity_JWT(),
61+
configuration["vault"]["server-role"],
62+
)
4163

4264

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

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

108128
# Write the role bound to the workload's spiffeID
109129
write_client_role(hvac_client, f"client_{client_id}", workload_spiffeID)
@@ -128,22 +148,34 @@ async def handle_client_registration():
128148
"client_id": client_id,
129149
"token": agent_token,
130150
}
131-
132-
# Spire-Agent binary
151+
152+
# Spire-Agent binary
133153
result = entry_create(
134-
agent_spiffeID, workload_spiffeID, ["unix:sha256:5ebff0fdb3335ec0221c35dcc7d3a4433eb8a5073a15a6dcfdbbb95bb8dbfa8e"]
154+
agent_spiffeID,
155+
workload_spiffeID,
156+
[
157+
"unix:sha256:5ebff0fdb3335ec0221c35dcc7d3a4433eb8a5073a15a6dcfdbbb95bb8dbfa8e"
158+
],
135159
)
136-
137-
# Python 3.9 binary
160+
161+
# Python 3.9 binary
138162
result = entry_create(
139-
agent_spiffeID, workload_spiffeID, ["unix:sha256:956a50083eb7a58240fea28ac52ff39e9c04c5c74468895239b24bdf4760bffe"]
163+
agent_spiffeID,
164+
workload_spiffeID,
165+
[
166+
"unix:sha256:956a50083eb7a58240fea28ac52ff39e9c04c5c74468895239b24bdf4760bffe"
167+
],
140168
)
141-
169+
142170
# Qemu x86_64 (For docker mac) // Could add Rosetta binary
143171
result = entry_create(
144-
agent_spiffeID, workload_spiffeID, ["unix:sha256:3fc6c8fbd8fe429b67276854fbb5ae594118f7f0b10352a508477833b04ee9b7"]
172+
agent_spiffeID,
173+
workload_spiffeID,
174+
[
175+
"unix:sha256:3fc6c8fbd8fe429b67276854fbb5ae594118f7f0b10352a508477833b04ee9b7"
176+
],
145177
)
146-
178+
147179
# Success
148180
return {
149181
"success": True,
@@ -176,9 +208,7 @@ async def handle_workload_creation():
176208
client_id = hashlib.sha256(client_id.encode()).hexdigest()[0:9]
177209

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

183213
# Check that the SVID correspond to the client_id (Can be removed if developper is certified)
184214
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

0 commit comments

Comments
 (0)