From f35c7be3bd486226cfd746f2d9c762013489f1d9 Mon Sep 17 00:00:00 2001 From: Fabrice Daugan Date: Wed, 27 Apr 2022 16:13:35 +0200 Subject: [PATCH 1/2] Add custom endpoint management --- src/sessionmanagerplugin/session/session.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/sessionmanagerplugin/session/session.go b/src/sessionmanagerplugin/session/session.go index 5f708ecf..77873fbe 100644 --- a/src/sessionmanagerplugin/session/session.go +++ b/src/sessionmanagerplugin/session/session.go @@ -20,6 +20,8 @@ import ( "fmt" "io" "os" + "os/exec" + "strings" "time" "github.com/aws/SSMCLI/src/config" @@ -191,6 +193,22 @@ func ValidateInputAndStartSession(args []string, out io.Writer) { session.SessionId = *startSessionOutput.SessionId session.StreamUrl = *startSessionOutput.StreamUrl + + if len(profile) > 0 { + // Lookup for a custom endpoint inside the profile + ssmmsgEndpointRes, err := exec.Command("aws", "configure", "get", "aws_ssmmessages_endpoint", "--profile", profile).Output() + if err == nil && ssmmsgEndpointRes != nil { + ssmmsgEndpoint := string(ssmmsgEndpointRes) + streamUrl := string(*startSessionOutput.StreamUrl) + // Cleanup Windows/Linux line feeds + ssmmsgEndpoint = strings.TrimSuffix(strings.TrimSuffix(strings.TrimSuffix(ssmmsgEndpoint, "\n"), "\r"), "\n") + if strings.HasSuffix(ssmmsgEndpoint, ".vpce.amazonaws.com") { + // Looks like a legit endpoint, patch the WSS url + fmt.Printf("Custom endpoint detected %s\n", ssmmsgEndpoint) + session.StreamUrl = strings.Replace(streamUrl, "ssmmessages.eu-west-1.amazonaws.com", ssmmsgEndpoint, -1) + } + } + } session.TokenValue = *startSessionOutput.TokenValue session.Endpoint = ssmEndpoint session.ClientId = clientId From 831994e039c3839e18edc2f13f1b8392c2cba8ad Mon Sep 17 00:00:00 2001 From: Fabrice Daugan Date: Wed, 27 Apr 2022 16:47:08 +0200 Subject: [PATCH 2/2] Remove hard region reference --- src/sessionmanagerplugin/session/session.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/sessionmanagerplugin/session/session.go b/src/sessionmanagerplugin/session/session.go index 77873fbe..6b1300e2 100644 --- a/src/sessionmanagerplugin/session/session.go +++ b/src/sessionmanagerplugin/session/session.go @@ -21,6 +21,7 @@ import ( "io" "os" "os/exec" + "regexp" "strings" "time" @@ -205,7 +206,8 @@ func ValidateInputAndStartSession(args []string, out io.Writer) { if strings.HasSuffix(ssmmsgEndpoint, ".vpce.amazonaws.com") { // Looks like a legit endpoint, patch the WSS url fmt.Printf("Custom endpoint detected %s\n", ssmmsgEndpoint) - session.StreamUrl = strings.Replace(streamUrl, "ssmmessages.eu-west-1.amazonaws.com", ssmmsgEndpoint, -1) + m := regexp.MustCompile(`ssmmessages.*amazonaws.com`) + session.StreamUrl = m.ReplaceAllString(streamUrl, ssmmsgEndpoint) } } }