Skip to content

Commit f8da988

Browse files
committed
Add validation to cw url
1 parent 3b92cfc commit f8da988

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

src/hyperpod_cli/service/get_logs.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
from hyperpod_cli.utils import (
2424
get_eks_cluster_name,
2525
get_hyperpod_cluster_region,
26+
validate_region_and_cluster_name,
2627
)
2728
from kubernetes.client.rest import ApiException
2829
from kubernetes.client import V1ResourceAttributes
30+
import re
2931

3032
AMAZON_ClOUDWATCH_OBSERVABILITY = "amazon-cloudwatch-observability"
3133

@@ -103,18 +105,29 @@ def generate_cloudwatch_link(
103105
container_id = None
104106

105107
# Cloudwatch container insight log groups should have the same pod log as API response
106-
if node_name and pod_name and namespace and container_name and container_id:
107-
region = get_hyperpod_cluster_region()
108+
region = get_hyperpod_cluster_region()
109+
cloudwatch_url = self.get_log_url(eks_cluster_name, region, node_name, pod_name, namespace, container_name, container_id)
108110

109-
cloudwatch_url = self.get_log_url(eks_cluster_name, region, node_name, pod_name, namespace, container_name, container_id)
110-
cloudwatch_link = f'The pod cloudwatch log stream link is {cloudwatch_url}'
111-
else:
112-
cloudwatch_link = 'Failed to load container insights CloudWatch Link!'
111+
if not validate_region_and_cluster_name(region, eks_cluster_name):
112+
return 'Failed to validate Eks cluster name and region'
113+
114+
if not self._validate_log_url(cloudwatch_url):
115+
return 'Failed to validate cloudwatch log url. Please verify node name, pod container name and container id are valid'
116+
117+
cloudwatch_link = f'The pod cloudwatch log stream link is {cloudwatch_url}'
113118
else:
114119
cloudwatch_link = None
115120

116121
return cloudwatch_link
117122

123+
def _validate_log_url(self, log_url):
124+
pattern = "https:\/\/([a-z0-9-]+).console.aws.amazon.com\/cloudwatch\/home\?region=([a-z0-9-]+)#logsV2:log-groups\/log-group\/\$252Faws\$252Fcontainerinsights\$252F([a-zA-Z0-9-]+)\$252Fapplication\/log-events\/([a-z0-9-]+)-application.var.log.containers.([a-z0-9-]+)_([a-z0-9-]+)_([a-z0-9-]+)-([a-z0-9-]+).log"
125+
match = re.match(pattern, log_url)
126+
if match:
127+
return True
128+
else:
129+
return False
130+
118131
def get_log_url(self, eks_cluster_name, region, node_name, pod_name, namespace, container_name, container_id):
119132
console_prefix = f'https://{region}.console.aws.amazon.com/cloudwatch/home?region={region}#'
120133
log_group_prefix = f'logsV2:log-groups/log-group/$252Faws$252Fcontainerinsights$252F{eks_cluster_name}$252Fapplication/log-events/'

src/hyperpod_cli/utils.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def _validate_link(console_url):
121121
return False
122122

123123

124-
def _validate_placeholders(region, cluster_name):
124+
def validate_region_and_cluster_name(region, cluster_name):
125125
output = False
126126
region_char_list = region.split("-")
127127

@@ -135,6 +135,9 @@ def _validate_placeholders(region, cluster_name):
135135
region_prefix_length = len(region_char_list[0])
136136
region_length = len(region_char_list[1])
137137
region_suffix_length = len(region_char_list[2])
138+
139+
cluster_name_match = re.match("[a-zA-Z0-9-]+", cluster_name)
140+
138141
if (
139142
region_prefix_match
140143
and region_match
@@ -143,6 +146,7 @@ def _validate_placeholders(region, cluster_name):
143146
and region_suffix_length == 1
144147
and region_length >= 4
145148
and region_length < 10
149+
and cluster_name_match
146150
and len(cluster_name) >= 1
147151
and len(cluster_name) <= 63
148152
):
@@ -165,7 +169,7 @@ def get_cluster_console_url():
165169
f"https://{region}.console.aws.amazon.com/sagemaker/"
166170
f"home?region={region}#/cluster-management/{cluster_name}"
167171
)
168-
if _validate_link(console_url) and _validate_placeholders(region, cluster_name):
172+
if _validate_link(console_url) and validate_region_and_cluster_name(region, cluster_name):
169173
return console_url
170174
return None
171175

0 commit comments

Comments
 (0)