Skip to content

Commit 9a5ed0d

Browse files
committed
Save ssh-keys at bootstrap time
Signed-off-by: Anton Kremenetsky <anton.kremenetsky@gmail.com>
1 parent bf46389 commit 9a5ed0d

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

genesis_core/bootstrap/defaults.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2025 Genesis Corporation.
1+
# Copyright 2025-2026 Genesis Corporation.
22
#
33
# All Rights Reserved.
44
#
@@ -13,9 +13,12 @@
1313
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
16-
16+
import grp
17+
import os
18+
import pwd
1719
import ipaddress
1820
import logging
21+
import pathlib
1922
import typing as tp
2023
import uuid as sys_uuid
2124

@@ -32,6 +35,7 @@
3235
from genesis_core.vs.dm import models as vs_models
3336

3437
LOG = logging.getLogger(__name__)
38+
USER = "ubuntu"
3539

3640

3741
def add_core_set(
@@ -128,6 +132,31 @@ def add_core_set(
128132
return node_set
129133

130134

135+
def save_developer_keys(keys: str) -> None:
136+
"""Save developer keys from the spec."""
137+
if not keys:
138+
LOG.debug("No developer keys provided.")
139+
return
140+
141+
pw = pwd.getpwnam(USER)
142+
uid, gid = pw.pw_uid, grp.getgrnam(USER).gr_gid
143+
ssh_dir = pathlib.Path(pw.pw_dir) / ".ssh"
144+
145+
ssh_dir.mkdir(parents=True, exist_ok=True)
146+
os.chown(ssh_dir, uid, gid)
147+
ssh_dir.chmod(0o700)
148+
149+
keys_path = ssh_dir / "authorized_keys"
150+
content = keys_path.read_text() if keys_path.exists() else ""
151+
if keys not in content:
152+
with keys_path.open("a") as f:
153+
if content and not content.endswith("\n"):
154+
f.write("\n")
155+
f.write(keys + "\n")
156+
os.chown(keys_path, uid, gid)
157+
keys_path.chmod(0o600)
158+
159+
131160
def activate_profile(profile_name: str) -> bool:
132161
# Check if there is already an active profile
133162
active_profile = vs_models.Profile.objects.get_one_or_none(

genesis_core/cmd/bootstrap.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ def main() -> None:
347347
with open(SPEC_PATH, "r", encoding="utf-8") as f:
348348
spec = json.load(f)
349349

350+
bootstrap_defaults.save_developer_keys(spec.get("developer_keys", ""))
351+
350352
while True:
351353
try:
352354
LOG.info("GC Bootstrap script")

0 commit comments

Comments
 (0)