Skip to content

Commit

Permalink
Move validation for port into util library (#135)
Browse files Browse the repository at this point in the history
since this way it can be reused by other implementors.
  • Loading branch information
sfc-gh-jchacon authored May 31, 2022
1 parent 0e06c79 commit 295fb8c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
26 changes: 2 additions & 24 deletions cmd/sanssh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,28 +109,6 @@ func init() {
subcommands.ImportantFlag("v")
}

func hasPort(s string) bool {
// Returns true if the provided address does not include a port number.
return strings.LastIndex(s, "]") < strings.LastIndex(s, ":")
}

func validateAndAddPort(s string, port int) string {
// See if there's a duration appended and pull it off
p := strings.Split(s, ";")
if len(p) == 0 || len(p) > 2 || p[0] == "" {
log.Fatalf("Invalid address %q - should be of the form host[:port][;<duration>]", s)
}
new := s
if !hasPort(p[0]) {
new = fmt.Sprintf("%s:%d", p[0], port)
if len(p) == 2 {
// Add duration back if we pulled it off.
new = fmt.Sprintf("%s;%s", new, p[1])
}
}
return new
}

func main() {
// If this is blank it'll remain blank which is fine
// as that means just talk to --targets[0] instead.
Expand Down Expand Up @@ -161,11 +139,11 @@ func main() {

// Validate and add the default proxy port (if needed).
if *proxyAddr != "" {
*proxyAddr = validateAndAddPort(*proxyAddr, defaultProxyPort)
*proxyAddr = cmdUtil.ValidateAndAddPort(*proxyAddr, defaultProxyPort)
}
// Validate and add the default target port (if needed) for each target.
for i, t := range *targetsFlag.Target {
(*targetsFlag.Target)[i] = validateAndAddPort(t, defaultTargetPort)
(*targetsFlag.Target)[i] = cmdUtil.ValidateAndAddPort(t, defaultTargetPort)
}

clientPolicy := cmdUtil.ChoosePolicy(logr.Discard(), "", *clientPolicyFlag, *clientPolicyFile)
Expand Down
28 changes: 28 additions & 0 deletions cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package util

import (
"errors"
"fmt"
"log"
"os"
"strings"

"github.com/go-logr/logr"
)
Expand Down Expand Up @@ -51,3 +54,28 @@ func ChoosePolicy(logger logr.Logger, defaultPolicy string, policyFlag string, p
}
return policy
}

// hasPort returns true if the provided address does not include a port number.
func hasPort(s string) bool {
return strings.LastIndex(s, "]") < strings.LastIndex(s, ":")
}

// ValidateAndAddPort will take a given target address and optionally add a default port
// onto it if a port is missing. This will also take in account optional dial timeout
// suffix and make sure the returned value has that if it exists.
func ValidateAndAddPort(s string, port int) string {
// See if there's a duration appended and pull it off
p := strings.Split(s, ";")
if len(p) == 0 || len(p) > 2 || p[0] == "" {
log.Fatalf("Invalid address %q - should be of the form host[:port][;<duration>]", s)
}
new := s
if !hasPort(p[0]) {
new = fmt.Sprintf("%s:%d", p[0], port)
if len(p) == 2 {
// Add duration back if we pulled it off.
new = fmt.Sprintf("%s;%s", new, p[1])
}
}
return new
}

0 comments on commit 295fb8c

Please sign in to comment.