Skip to content

Commit

Permalink
Allow EKG_HOST to use IPv4 and Host/Domain names (#1834)
Browse files Browse the repository at this point in the history
## Description
* Splits the **isValidIPv4** function so that it returns 1 on invalid
IPv4 addresses.
* Adds new **isValidHostnameOrDomain** function to provide the extra
check that **isValidIPv4** was performing to allow cntools.sh to
validate the `relays_ip_enter`.
* Updates the EKG_HOST IP check to work for IPv4 or valid hostname /
domainnames

## Motivation and context
1. Allowing EKG_HOST to be a non IPv4 address (for decoupled &
containerized/k8s environments).
2. Ensure that isValidIPv4 correctly invalidates IPv4 addresses.

## Which issue it fixes?
Closes #1832 
Closes #1833 

## How has this been tested?
* Building locally into a test container and verifying that cncli.sh
`[sync|leaderlog|validatre]` no longer errors on valid hostnames or IPv6
and can connect via EKG.
* Resolvable names like `EKG_HOST=cardano-node` pass. The cncli.sh sync,
leaderlog, & validate subcommands all work as expected.
* EKG_HOST does not permit IPv6 as enabling IPv6 on the node does not
appear to enable it for EKG_PORT.
  • Loading branch information
TrevorBenson authored Oct 27, 2024
1 parent 8706317 commit 97e37fa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
6 changes: 3 additions & 3 deletions scripts/cnode-helper-scripts/cntools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2195,14 +2195,14 @@ function main {
;;
1) getAnswerAnyCust relay_ip_enter "Enter relays's IPv4/v6 address"
if [[ -n "${relay_ip_enter}" ]]; then
if ! isValidIPv4 "${relay_ip_enter}" && ! isValidIPv6 "${relay_ip_enter}"; then
println ERROR "${FG_RED}ERROR${NC}: invalid IPv4/v6 address format!"
if ! isValidIPv4 "${relay_ip_enter}" && ! isValidIPv6 "${relay_ip_enter}" && ! isValidHostnameOrDomain "${relay_ip_enter}"; then
println ERROR "${FG_RED}ERROR${NC}: Invalid IPv4/v6 address format or hostname/domain name format!"
else
getAnswerAnyCust relay_port_enter "Enter relays's port"
if [[ -n "${relay_port_enter}" ]]; then
if ! isNumber ${relay_port_enter} || [[ ${relay_port_enter} -lt 1 || ${relay_port_enter} -gt 65535 ]]; then
println ERROR "${FG_RED}ERROR${NC}: invalid port number!"
elif isValidIPv4 "${relay_ip_enter}"; then
elif isValidIPv4 "${relay_ip_enter}" || isValidHostnameOrDomain "${relay_ip_enter}"; then
relay_array+=( "type" "IPv4" "address" "${relay_ip_enter}" "port" "${relay_port_enter}" )
relay_output+="--pool-relay-port ${relay_port_enter} --pool-relay-ipv4 ${relay_ip_enter} "
else
Expand Down
29 changes: 20 additions & 9 deletions scripts/cnode-helper-scripts/env
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,24 @@ fractionToPCT() {
isValidIPv4() {
local ip=$1
[[ -z ${ip} ]] && return 1
if [[ ${ip} =~ ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ || ${ip} =~ ^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9_\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9_\-]*[A-Za-z0-9])$ ]]; then
ipv4_regex="^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
if [[ ${ip} =~ ${ipv4_regex} ]]; then
return 0
fi
return 1
}

# Description : Helper function to validate hostname or domain name
# : $1 = hostname or domain name
isValidHostnameOrDomain() {
local name=$1
[[ -z ${name} ]] && return 1

# Regular expression for valid hostnames and domain names
hostname_regex="^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9_\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9_\-]*[A-Za-z0-9])$"

# Check if the input matches the hostname/domain name format
if [[ ${name} =~ ${hostname_regex} ]]; then
return 0
fi
return 1
Expand Down Expand Up @@ -1163,14 +1180,8 @@ fi

[[ -z ${EKG_TIMEOUT} ]] && EKG_TIMEOUT=3
[[ -z ${EKG_HOST} ]] && EKG_HOST=127.0.0.1
if [[ ${EKG_HOST} =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
IFS='.' read -ra EKG_OCTETS <<< ${EKG_HOST}
if ! [[ ${EKG_OCTETS[0]} -le 255 && ${EKG_OCTETS[1]} -le 255 && ${EKG_OCTETS[2]} -le 255 && ${EKG_OCTETS[3]} -le 255 ]]; then
echo "Not a valid IP range set for EKG host, please check env file for value of EKG_HOST (currently it is ${EKG_HOST} )!"
return 1
fi
else
echo "Not a valid IP format set for EKG host, please check env file!"
if ! isValidIPv4 "${EKG_HOST}" && ! isValidHostnameOrDomain "${EKG_HOST}"; then
echo "Not a valid IP or hostname set for EKG host, please check the env file (currently it is ${EKG_HOST})!"
return 1
fi

Expand Down
5 changes: 3 additions & 2 deletions scripts/cnode-helper-scripts/topologyUpdater.sh
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ if [[ ${TU_FETCH} = "Y" ]]; then
*) echo "ERROR: Invalid Custom Peer definition '${cpeer}'. Please double check CUSTOM_PEERS definition"
exit 1 ;;
esac
if [[ ${addr} = *.* ]]; then
! isValidIPv4 "${addr}" && echo "ERROR: Invalid IPv4 address or hostname '${addr}'. Please check CUSTOM_PEERS definition" && continue
if ! isValidIPv4 "${addr}" && ! isValidHostnameOrDomain "${addr}"; then
echo "ERROR: Invalid IPv4 address or hostname '${addr}'. Please check CUSTOM_PEERS definition"
continue
elif [[ ${addr} = *:* ]]; then
! isValidIPv6 "${addr}" && echo "ERROR: Invalid IPv6 address '${addr}'. Please check CUSTOM_PEERS definition" && continue
fi
Expand Down

0 comments on commit 97e37fa

Please sign in to comment.