From 75f4e936f1e23bb434cfa348741548a3eab0c02d Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 29 Jan 2024 09:50:28 -0500 Subject: [PATCH 1/2] feat: add support for ipv6 addresses This change updates the remote url parsing to be done in python, allowing us to properly parse the url without using regex. Closes #99 --- Dockerfile | 2 +- bin/parse-ssh-host | 41 ++++++++++++++++++++++++++++++++--------- bin/parse-ssh-port | 38 +++++++++++++++++++++++++------------- 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/Dockerfile b/Dockerfile index dec35c4..445126e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,6 @@ FROM alpine:3.19.0 -RUN apk --no-cache add git==2.43.0-r0 openssh==9.6_p1-r0 && \ +RUN apk --no-cache add git==2.43.0-r0 python3==3.11.6-r1 openssh==9.6_p1-r0 && \ mkdir -p ~/.ssh COPY bin /bin diff --git a/bin/parse-ssh-host b/bin/parse-ssh-host index 47c8f74..ff3654a 100755 --- a/bin/parse-ssh-host +++ b/bin/parse-ssh-host @@ -1,12 +1,35 @@ -#!/bin/sh -l -set -e +#!/usr/bin/python3 -if [ -n "$TRACE" ]; then - set -x -fi +import os +import sys +import urllib.parse -if [ -n "$PLUGIN_GIT_REMOTE_URL" ]; then - export GIT_REMOTE_URL="$PLUGIN_GIT_REMOTE_URL" -fi -echo "$GIT_REMOTE_URL" | sed -e 's/.*@//' -e 's/[:/].*//' +def main(): + """ + Prints out the host of the git remote url + """ + git_remote_url = os.getenv("GIT_REMOTE_URL") + if os.getenv("PLUGIN_GIT_REMOTE_URL"): + git_remote_url = os.getenv("PLUGIN_GIT_REMOTE_URL") + + if not git_remote_url: + print("GIT_REMOTE_URL is empty", file=sys.stderr) + sys.exit(1) + + u = urllib.parse.urlparse(git_remote_url) + print(u.hostname) + + +if __name__ == "__main__": + main() + +# if [ -n "$TRACE" ]; then +# set -x +# fi + +# if [ -n "$PLUGIN_GIT_REMOTE_URL" ]; then +# export GIT_REMOTE_URL="$PLUGIN_GIT_REMOTE_URL" +# fi + +# echo "$GIT_REMOTE_URL" | sed -e 's/.*@//' -e 's/[:/].*//' diff --git a/bin/parse-ssh-port b/bin/parse-ssh-port index b268070..82cad5c 100755 --- a/bin/parse-ssh-port +++ b/bin/parse-ssh-port @@ -1,17 +1,29 @@ -#!/bin/sh -l -set -e +#!/usr/bin/python3 -if [ -n "$TRACE" ]; then - set -x -fi +import os +import sys +import urllib.parse -if [ -n "$PLUGIN_GIT_REMOTE_URL" ]; then - export GIT_REMOTE_URL="$PLUGIN_GIT_REMOTE_URL" -fi -ssh_port="$(echo "$GIT_REMOTE_URL" | sed -e 's/.*@//' -e 's/\/.*//' -ne 's/.*:\([0-9]*\)/\1/p')" -if [ -z "$ssh_port" ]; then - ssh_port=22 -fi +def main(): + """ + Prints out the port of the git remote url + """ + git_remote_url = os.getenv("GIT_REMOTE_URL") + if os.getenv("PLUGIN_GIT_REMOTE_URL"): + git_remote_url = os.getenv("PLUGIN_GIT_REMOTE_URL") -echo "$ssh_port" + if not git_remote_url: + print("GIT_REMOTE_URL is empty", file=sys.stderr) + sys.exit(1) + + u = urllib.parse.urlparse(git_remote_url) + port = "22" + if u.port: + port = u.port + + print(port) + + +if __name__ == "__main__": + main() From 6d2f500ee8ab31885ce5f01af6e8da8d4f1d080f Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Mon, 29 Jan 2024 09:56:05 -0500 Subject: [PATCH 2/2] fix: wrap ipv6 addresses in brackets --- bin/parse-ssh-host | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/bin/parse-ssh-host b/bin/parse-ssh-host index ff3654a..8d1386a 100755 --- a/bin/parse-ssh-host +++ b/bin/parse-ssh-host @@ -1,10 +1,22 @@ #!/usr/bin/python3 +import ipaddress import os import sys import urllib.parse +def is_ipv6(ip): + """ + Returns True if the ip is an IPv6 address + """ + try: + return isinstance(ipaddress.ip_address(ip), ipaddress.IPv6Address) + except ValueError: + print(f"Invalid IP address: {ip}", file=sys.stderr) + return False + + def main(): """ Prints out the host of the git remote url @@ -18,7 +30,10 @@ def main(): sys.exit(1) u = urllib.parse.urlparse(git_remote_url) - print(u.hostname) + host = u.hostname + if is_ipv6(host): + host = f"[{host}]" + print(host) if __name__ == "__main__":