fix: correctly handle IPv6 address in apid

This validates IPs by simple parsing and ensures `host:part` is correct
by using `net.FormatAddress`.

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
This commit is contained in:
Andrey Smirnov
2020-06-01 22:01:08 +03:00
committed by talos-bot
parent 77150f51cf
commit 67b31029a7

View File

@@ -7,9 +7,10 @@ package backend
import ( import (
"context" "context"
"fmt" "fmt"
"strings"
"sync" "sync"
stdlibnet "net"
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/talos-systems/grpc-proxy/proxy" "github.com/talos-systems/grpc-proxy/proxy"
"google.golang.org/grpc" "google.golang.org/grpc"
@@ -18,6 +19,7 @@ import (
"github.com/talos-systems/talos/api/common" "github.com/talos-systems/talos/api/common"
"github.com/talos-systems/talos/pkg/constants" "github.com/talos-systems/talos/pkg/constants"
"github.com/talos-systems/talos/pkg/net"
) )
// APID backend performs proxying to another apid instance. // APID backend performs proxying to another apid instance.
@@ -34,8 +36,8 @@ type APID struct {
// NewAPID creates new instance of APID backend // NewAPID creates new instance of APID backend
func NewAPID(target string, creds credentials.TransportCredentials) (*APID, error) { func NewAPID(target string, creds credentials.TransportCredentials) (*APID, error) {
// perform very basic validation on target // perform very basic validation on target
if target == "" || strings.Contains(target, ":") { if stdlibnet.ParseIP(target) == nil {
return nil, fmt.Errorf("invalid target %q", target) return nil, fmt.Errorf("invalid target IP %q", target)
} }
return &APID{ return &APID{
@@ -74,7 +76,7 @@ func (a *APID) GetConnection(ctx context.Context) (context.Context, *grpc.Client
var err error var err error
a.conn, err = grpc.DialContext( a.conn, err = grpc.DialContext(
ctx, ctx,
fmt.Sprintf("%s:%d", a.target, constants.ApidPort), fmt.Sprintf("%s:%d", net.FormatAddress(a.target), constants.ApidPort),
grpc.WithTransportCredentials(a.creds), grpc.WithTransportCredentials(a.creds),
grpc.WithCodec(proxy.Codec()), //nolint: staticcheck grpc.WithCodec(proxy.Codec()), //nolint: staticcheck
) )