Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions cmd/tls-scanner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/openshift/tls-scanner/internal/k8s"
"github.com/openshift/tls-scanner/internal/output"
"github.com/openshift/tls-scanner/internal/scanner"
"github.com/openshift/tls-scanner/internal/stringutil"
"github.com/openshift/tls-scanner/internal/timing"
)

Expand Down Expand Up @@ -311,7 +312,7 @@ func run(args []string) (exitCode int) {
return 1
}

jobs := []scanner.ScanJob{{IP: normalizeHost(*host), Port: portNum}}
jobs := []scanner.ScanJob{{IP: stringutil.NormalizeHost(*host), Port: portNum}}

if *dryRun {
output.PrintDryRunTargets(jobs)
Expand Down Expand Up @@ -361,13 +362,5 @@ func parseTarget(target string) (string, int, error) {
return "", 0, err
}

return normalizeHost(host), portNum, nil
}

func normalizeHost(host string) string {
host = strings.TrimSpace(host)
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") && len(host) >= 2 {
return host[1 : len(host)-1]
}
return host
return stringutil.NormalizeHost(host), portNum, nil
}
11 changes: 11 additions & 0 deletions internal/stringutil/stringutil.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
package stringutil

import "strings"

// NormalizeHost trims whitespace and strips IPv6 square brackets from a host string.
func NormalizeHost(host string) string {
host = strings.TrimSpace(host)
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") && len(host) >= 2 {
return host[1 : len(host)-1]
}
return host
}

// RemoveDuplicates returns a new slice with duplicates and empty strings removed,
// preserving the original order.
func RemoveDuplicates(slice []string) []string {
Expand Down
29 changes: 29 additions & 0 deletions internal/stringutil/stringutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@ import (
"testing"
)

func TestNormalizeHost(t *testing.T) {
t.Parallel()

tests := []struct {
name string
in string
want string
}{
{"plain IPv4", "10.0.0.1", "10.0.0.1"},
{"bracketed IPv6", "[::1]", "::1"},
{"bracketed full IPv6", "[fd2e:6f44:5dd8::16]", "fd2e:6f44:5dd8::16"},
{"no brackets", "fd2e:6f44:5dd8::16", "fd2e:6f44:5dd8::16"},
{"whitespace trimmed", " 10.0.0.1 ", "10.0.0.1"},
{"whitespace with brackets", " [::1] ", "::1"},
{"empty string", "", ""},
{"single bracket only", "[", "["},
{"hostname", "example.com", "example.com"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := NormalizeHost(tt.in); got != tt.want {
t.Errorf("NormalizeHost(%q) = %q, want %q", tt.in, got, tt.want)
}
})
}
}

func TestRemoveDuplicates(t *testing.T) {
t.Parallel()

Expand Down