Skip to content

Commit 3488ddd

Browse files
authored
Fix erroneous microk8s join invocations by adding validation (#4397)
1 parent 1e4e8c2 commit 3488ddd

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ tests/__pycache__
1515
/installer/**/__pycache__
1616
/installer/dist/
1717
/installer/build/
18+
.tox/
1819
.tox_env/
1920
__pycache__/
2021
microk8s_*.txt #Remote build log

scripts/wrappers/add_token.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import argparse
77
import subprocess
88

9-
from common.cluster.utils import is_node_running_dqlite
9+
from common.cluster.utils import is_node_running_dqlite, TOKEN_ΜΙΝ_LEN
1010

1111
try:
1212
from secrets import token_hex
@@ -165,7 +165,7 @@ def print_short(token, check):
165165
else:
166166
token = token_hex(16)
167167

168-
if len(token) < 32:
168+
if len(token) < TOKEN_ΜΙΝ_LEN:
169169
print("Invalid token size. It must be 32 characters long.")
170170
exit(1)
171171

scripts/wrappers/common/cluster/utils.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import base64
22
import datetime
3+
import ipaddress
34
import json
45
import os
56
import random
@@ -14,6 +15,13 @@
1415

1516
import yaml
1617

18+
FINGERPRINT_MIN_LEN = 12
19+
TOKEN_ΜΙΝ_LEN = 32
20+
21+
22+
class InvalidConnectionError(Exception):
23+
pass
24+
1725

1826
def is_strict():
1927
snap_yaml = snap() / "meta/snap.yaml"
@@ -544,3 +552,45 @@ def rebuild_x509_auth_client_configs():
544552
stdout=subprocess.DEVNULL,
545553
stderr=subprocess.DEVNULL,
546554
)
555+
556+
557+
def get_valid_connection_parts(connection):
558+
"""
559+
Ensure that connection has a valid format <master_ip>:<master_port>/<token>[/<fingerprint>]
560+
561+
:param connection: the connection string
562+
:return: connection parts
563+
:raise:
564+
InvalidConnectionError: if connection string is not valid
565+
"""
566+
connection_parts = connection.split("/")
567+
568+
if len(connection_parts) not in range(2, 3):
569+
raise InvalidConnectionError(
570+
"Expected format: <master_IP>:<master_PORT>/<token>[/<fingerprint>]"
571+
)
572+
573+
master_ep = connection_parts[0].split(":")
574+
if len(master_ep) != 2:
575+
raise InvalidConnectionError(
576+
"Expected format: <master_IP>:<master_PORT>/<token>[/<fingerprint>]"
577+
)
578+
579+
try:
580+
ipaddress.ip(master_ep[0])
581+
except ValueError:
582+
raise InvalidConnectionError("Invalid master IP")
583+
584+
try:
585+
if int(master_ep[1]) not in range(1, 65535):
586+
raise InvalidConnectionError("Master PORT not in range 1:65535")
587+
except ValueError:
588+
raise InvalidConnectionError("Master PORT not a number")
589+
590+
if len(connection_parts[1]) < TOKEN_ΜΙΝ_LEN:
591+
raise InvalidConnectionError(f"Cluster token size should be at least {TOKEN_ΜΙΝ_LEN} bytes")
592+
593+
if len(connection_parts) == 3 and len(connection_parts[2]) < FINGERPRINT_MIN_LEN:
594+
raise InvalidConnectionError(f"Fingerprint should be at least {FINGERPRINT_MIN_LEN} bytes")
595+
596+
return connection_parts

scripts/wrappers/join.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
get_cluster_agent_port,
2424
get_cluster_cidr,
2525
get_token,
26+
get_valid_connection_parts,
2627
is_low_memory_guard_enabled,
2728
is_node_running_dqlite,
2829
is_token_auth_enabled,
@@ -34,6 +35,8 @@
3435
try_set_file_permissions,
3536
snap,
3637
snap_data,
38+
FINGERPRINT_MIN_LEN,
39+
InvalidConnectionError,
3740
)
3841

3942
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
@@ -53,8 +56,6 @@
5356
cluster_cert_file = "{}/cluster.crt".format(cluster_dir)
5457
cluster_key_file = "{}/cluster.key".format(cluster_dir)
5558

56-
FINGERPRINT_MIN_LEN = 12
57-
5859

5960
def get_traefik_port():
6061
"""
@@ -969,7 +970,12 @@ def join(connection, worker, skip_verify, disable_low_memory_guard):
969970
970971
CONNECTION: the cluster connection endpoint in format <master>:<port>/<token>
971972
"""
972-
connection_parts = connection.split("/")
973+
try:
974+
connection_parts = get_valid_connection_parts(connection)
975+
except InvalidConnectionError as err:
976+
print("Invalid connection:", err)
977+
sys.exit(1)
978+
973979
verify = not skip_verify
974980

975981
if is_low_memory_guard_enabled() and disable_low_memory_guard:

0 commit comments

Comments
 (0)