Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate backend #1908

Merged
merged 8 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
49 changes: 34 additions & 15 deletions pkg/gridtypes/zos/gw_fqdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package zos
import (
"fmt"
"io"
"math"
"net"
"net/url"
"regexp"
"strconv"

"github.com/pkg/errors"
"github.com/threefoldtech/zos/pkg/gridtypes"
Expand All @@ -17,26 +19,43 @@ var (

type Backend string

// check if valid http://ip:port, http://ip or ip:port
func (b Backend) Valid(tls bool) error {
u, err := url.Parse(string(b))
if err != nil {
return errors.Wrap(err, "failed to parse backend")
}
if tls {
if u.Scheme != "" {
return fmt.Errorf("scheme expected to be empty")
// Valid accepts http://ip:port, http://ip or ip:port
// checks if backend string is a valid string based on the tlsPassthrough parameter
// ip:port is only valid in case of tlsPassthrough is true
// http://ip:port or http://ip is valid in case of tlsPassthrough is false
func (b Backend) Valid(tlsPassthrough bool) error {
var hostName string
if tlsPassthrough {
host, port, err := net.SplitHostPort(string(b))
if err != nil {
return fmt.Errorf("failed to parse backend %s with error: %w", b, err)
}

parsedPort, err := strconv.ParseUint(port, 10, 64)
if err != nil {
return fmt.Errorf("invalid port in backend: %s", port)
}

if parsedPort > math.MaxUint16 {
return fmt.Errorf("port '%s' must be <= 65535", port)
}

hostName = host
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is wrong because you need to return ip:port in case of tls, or http://ip:port in case of no tls

} else {
u, err := url.Parse(string(b))
if err != nil {
return fmt.Errorf("failed to parse backend with error: %w", err)
}
if u.Port() == "" {
return fmt.Errorf("missing port in backend address")

if u.Scheme != "http" {
return fmt.Errorf("scheme expected to be http")
}
} else if u.Scheme != "http" {
return fmt.Errorf("scheme expected to be http")
hostName = u.Hostname()
}

ip := net.ParseIP(u.Hostname())
ip := net.ParseIP(hostName)
if len(ip) == 0 || ip.IsLoopback() {
return fmt.Errorf("invalid ip address in backend: %s", u.Hostname())
return fmt.Errorf("invalid ip address in backend: %s", hostName)
}
return nil
}
Expand Down
115 changes: 115 additions & 0 deletions pkg/gridtypes/zos/gw_fqdn_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package zos

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestValidBackend(t *testing.T) {
require := require.New(t)

t.Run("test ip:port cases", func(t *testing.T) {
rawdaGastan marked this conversation as resolved.
Show resolved Hide resolved
backend := Backend("1.1.1.1:10")
err := backend.Valid(true)
require.NoError(err)

backend = Backend("1.1.1.1:10")
err = backend.Valid(false)
require.Error(err)

backend = Backend("1.1.1.1")
err = backend.Valid(true)
require.Error(err)

backend = Backend("1.1.1.1:port")
err = backend.Valid(true)
require.Error(err)

backend = Backend("ip:10")
err = backend.Valid(true)
require.Error(err)

backend = Backend("1.1.1.1:1000000")
err = backend.Valid(true)
require.Error(err)
})

t.Run("test http://ip:port cases", func(t *testing.T) {
backend := Backend("http://1.1.1.1:10")
err := backend.Valid(false)
require.NoError(err)

backend = Backend("http://1.1.1.1:10")
err = backend.Valid(true)
require.Error(err)

backend = Backend("http://1.1.1.1:port")
err = backend.Valid(false)
require.Error(err)

backend = Backend("http://ip:10")
err = backend.Valid(false)
require.Error(err)
})

t.Run("test http://ip cases", func(t *testing.T) {
backend := Backend("http://1.1.1.1")
err := backend.Valid(false)
require.NoError(err)

backend = Backend("http://1.1.1.1")
err = backend.Valid(true)
require.Error(err)

backend = Backend("http://ip")
err = backend.Valid(false)
require.Error(err)
})
}

func TestValidBackendIP6(t *testing.T) {
require := require.New(t)

t.Run("test ip:port cases", func(t *testing.T) {
backend := Backend("[2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF]:10")
err := backend.Valid(true)
require.NoError(err)

backend = Backend("[2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF]:10")
err = backend.Valid(false)
require.Error(err)

backend = Backend("[2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF]:port")
err = backend.Valid(true)
require.Error(err)

backend = Backend("[2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF]:1000000")
err = backend.Valid(true)
require.Error(err)
})

t.Run("test http://ip:port cases", func(t *testing.T) {
backend := Backend("http://[2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF]:10")
err := backend.Valid(false)
require.NoError(err)

backend = Backend("http://[2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF]:10")
err = backend.Valid(true)
require.Error(err)

backend = Backend("http://[2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF]:port")
err = backend.Valid(false)
require.Error(err)
})

t.Run("test http://ip cases", func(t *testing.T) {
backend := Backend("http://[2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF]")
err := backend.Valid(false)
require.NoError(err)

backend = Backend("http://[2001:db8:3333:4444:CCCC:DDDD:EEEE:FFFF]")
err = backend.Valid(true)
require.Error(err)
})
}