Skip to content

Commit 648ec17

Browse files
committed
Store information about remote SSH user
This also causes the API reply to include the information when a (contextualized) node is queried. Fixes: #608 Signed-off-by: Nils Philippsen <nils@redhat.com>
1 parent a6104c5 commit 648ec17

5 files changed

Lines changed: 20 additions & 4 deletions

File tree

‎duffy/app/controllers/session.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
)
2929
from ...database.model import Node, Session, SessionNode, Tenant
3030
from ...database.types import NodeState
31-
from ...nodes.context import contextualize, decontextualize
31+
from ...nodes.context import SSH_REMOTE_USER, contextualize, decontextualize
3232
from ...tasks import deprovision_nodes, fill_pools
3333
from ...util import aclosing
3434
from ..auth import req_tenant, req_tenant_optional
@@ -226,6 +226,8 @@ async def create_session(
226226
# Mark the nodes as deployed but only commit when they’re contextualized.
227227
for node in nodes_in_transaction:
228228
node.state = NodeState.deployed
229+
# This is static but users have asked for this info to be present
230+
node.data["context"] = {"ssh_remote_user": SSH_REMOTE_USER}
229231
await db_async_session.flush()
230232

231233
contextualized_ipaddrs = await contextualize(

‎duffy/nodes/context.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,10 @@
3030
]
3131
SSH_CMD_FLAGS = SSH_CMD_FLAGS_BASE + [item for option in SSH_CMD_OPTIONS for item in ("-o", option)]
3232
TENANT_CRED_SEPARATOR = "### DUFFY tenant credentials"
33-
SSH_REMOTE_CONTEXTUALIZE_CMD = "cat - >> ~root/.ssh/authorized_keys"
33+
SSH_REMOTE_USER = "root"
34+
SSH_REMOTE_CONTEXTUALIZE_CMD = f"cat - >> ~{SSH_REMOTE_USER}/.ssh/authorized_keys"
3435
SSH_REMOTE_DECONTEXTUALIZE_CMD = (
35-
f"sed -i -n '/{TENANT_CRED_SEPARATOR}/q;p' ~root/.ssh/authorized_keys"
36+
f"sed -i -n '/{TENANT_CRED_SEPARATOR}/q;p' ~{SSH_REMOTE_USER}/.ssh/authorized_keys"
3637
)
3738

3839

‎duffy/tasks/deprovision.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ def deprovision_pool_nodes(self, pool_name: str, node_ids: List[int]):
5555

5656
for node in nodes:
5757
node.state = NodeState.deprovisioning
58+
try:
59+
del node.data["context"]
60+
except KeyError: # pragma: no cover
61+
pass
5862
node.pool = None
5963

6064
log.debug("Decontextualizing nodes.")

‎tests/app/controllers/test_session.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from duffy.app.controllers import session as session_module
2626
from duffy.database.model import Node, Session, Tenant
2727
from duffy.database.setup import _gen_test_api_key
28+
from duffy.nodes.context import SSH_REMOTE_USER
2829

2930
from . import BaseTestController
3031

@@ -272,6 +273,9 @@ async def test_request_session(
272273
# validate that the result lists the nodes
273274
session = result["session"]
274275
nodes = session["nodes"]
276+
assert all(
277+
node["data"]["context"]["ssh_remote_user"] == SSH_REMOTE_USER for node in nodes
278+
)
275279
for nodes_spec in self.nodes_specs:
276280
nodes_spec = nodes_spec.copy()
277281
quantity = nodes_spec.pop("quantity")

‎tests/tasks/test_deprovision.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def test_deprovision_pool_nodes(testcase, test_mechanism, db_sync_session, caplo
7373
state="deployed",
7474
pool="foo",
7575
reusable=reusable,
76-
data={"provision": {"mech_id": id + 20, "some_more_data": "fortytwo"}},
76+
data={
77+
"provision": {"mech_id": id + 20, "some_more_data": "fortytwo"},
78+
"context": {"ssh_remote_user": "root"},
79+
},
7780
)
7881
if reusable:
7982
node.data["somefield"] = "somevalue"
@@ -131,6 +134,8 @@ def test_deprovision_pool_nodes(testcase, test_mechanism, db_sync_session, caplo
131134
node_ids = {node.id for node in nodes}
132135

133136
if "normal" in testcase or "mechanism-failure" in testcase:
137+
# After deprovisioning, the `context` block should be removed
138+
assert not any("context" in node.data for node in nodes)
134139
decontextualize.assert_awaited_once()
135140
(ipaddrs,), kwargs = decontextualize.await_args
136141
assert not kwargs

0 commit comments

Comments
 (0)