-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathdata_utils.py
More file actions
69 lines (48 loc) · 1.65 KB
/
Copy pathdata_utils.py
File metadata and controls
69 lines (48 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import base64
import paramiko
def base64_encode_str(text: str) -> str:
"""Encode a string to base64 format.
Args:
text: The string to encode.
Returns:
Base64-encoded string.
Examples:
>>> base64_encode_str("hello")
'aGVsbG8='
"""
return base64.b64encode(text.encode()).decode()
def name_prefix(name: str) -> str:
"""Extract the prefix from a dotted name.
# TODO: refactor to a more huristic approach.
Args:
name: The name containing dots (e.g., "file.txt", "archive.tar.gz").
Returns:
The portion before the first dot. If no dot exists, returns the entire name.
Examples:
>>> name_prefix("file.txt")
'file'
>>> name_prefix("archive.tar.gz")
'archive'
>>> name_prefix("noextension")
'noextension'
"""
return name.split(".")[0]
def authorized_key(private_key_path: str) -> str:
"""Generate an SSH authorized_keys entry from a private key file.
Args:
private_key_path: Path to the RSA private key file.
Returns:
Formatted SSH authorized_keys entry (ssh-rsa <base64> root@exec1.rdocloud).
Examples:
>>> authorized_key("/path/to/id_rsa")
'ssh-rsa AAAAB3NzaC1yc2E... root@exec1.rdocloud'
"""
return f"ssh-rsa {private_to_public_key(key=private_key_path)} root@exec1.rdocloud"
def private_to_public_key(key: str) -> str:
"""Convert an RSA private key to its base64-encoded public key.
Args:
key: Path to the RSA private key file.
Returns:
Base64-encoded public key string.
"""
return paramiko.RSAKey.from_private_key_file(key).get_base64()