2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 19:12:30 +00:00
|
|
|
|
2020-03-26 14:19:21 +00:00
|
|
|
package envoy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/hashicorp/go-sockaddr/template"
|
|
|
|
)
|
|
|
|
|
2020-11-07 01:47:29 +00:00
|
|
|
const defaultGatewayPort int = 8443
|
2020-03-26 14:19:21 +00:00
|
|
|
|
|
|
|
// ServiceAddressValue implements a flag.Value that may be used to parse an
|
|
|
|
// addr:port string into an api.ServiceAddress.
|
|
|
|
type ServiceAddressValue struct {
|
|
|
|
value api.ServiceAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServiceAddressValue) String() string {
|
2020-04-14 14:48:02 +00:00
|
|
|
if s == nil || (s.value.Port == 0 && s.value.Address == "") {
|
2020-03-26 16:20:56 +00:00
|
|
|
return fmt.Sprintf(":%d", defaultGatewayPort)
|
2020-03-26 14:19:21 +00:00
|
|
|
}
|
|
|
|
return fmt.Sprintf("%v:%d", s.value.Address, s.value.Port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServiceAddressValue) Value() api.ServiceAddress {
|
|
|
|
if s == nil || s.value.Port == 0 && s.value.Address == "" {
|
2020-03-26 16:20:56 +00:00
|
|
|
return api.ServiceAddress{Port: defaultGatewayPort}
|
2020-03-26 14:19:21 +00:00
|
|
|
}
|
|
|
|
return s.value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServiceAddressValue) Set(raw string) error {
|
|
|
|
var err error
|
|
|
|
s.value, err = parseAddress(raw)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseAddress(raw string) (api.ServiceAddress, error) {
|
|
|
|
result := api.ServiceAddress{}
|
|
|
|
x, err := template.Parse(raw)
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("Error parsing address %q: %v", raw, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, portStr, err := net.SplitHostPort(x)
|
2020-05-21 14:08:12 +00:00
|
|
|
// Error message from Go's net/ipsock.go
|
2020-03-26 14:19:21 +00:00
|
|
|
if err != nil {
|
2020-05-21 14:08:12 +00:00
|
|
|
if !strings.Contains(err.Error(), "missing port in address") {
|
|
|
|
return result, fmt.Errorf("Error parsing address %q: %v", x, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the whole input as the address if there wasn't a port.
|
|
|
|
if ip := net.ParseIP(x); ip == nil {
|
|
|
|
return result, fmt.Errorf("Error parsing address %q: not an IP address", x)
|
|
|
|
}
|
|
|
|
addr = x
|
2020-03-26 14:19:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 16:20:56 +00:00
|
|
|
port := defaultGatewayPort
|
2020-03-26 14:19:21 +00:00
|
|
|
if portStr != "" {
|
|
|
|
port, err = strconv.Atoi(portStr)
|
|
|
|
if err != nil {
|
|
|
|
return result, fmt.Errorf("Error parsing port %q: %v", portStr, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Address = addr
|
|
|
|
result.Port = port
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ flag.Value = (*ServiceAddressValue)(nil)
|
|
|
|
|
|
|
|
type ServiceAddressMapValue struct {
|
|
|
|
value map[string]api.ServiceAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServiceAddressMapValue) String() string {
|
|
|
|
buf := new(strings.Builder)
|
|
|
|
for k, v := range s.value {
|
|
|
|
buf.WriteString(fmt.Sprintf("%v=%v:%d,", k, v.Address, v.Port))
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServiceAddressMapValue) Set(raw string) error {
|
|
|
|
if s.value == nil {
|
|
|
|
s.value = make(map[string]api.ServiceAddress)
|
|
|
|
}
|
|
|
|
idx := strings.Index(raw, "=")
|
|
|
|
if idx == -1 {
|
|
|
|
return fmt.Errorf(`Missing "=" in argument: %s`, raw)
|
|
|
|
}
|
|
|
|
key, value := raw[0:idx], raw[idx+1:]
|
|
|
|
var err error
|
|
|
|
s.value[key], err = parseAddress(value)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ flag.Value = (*ServiceAddressMapValue)(nil)
|