1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
package workloadapi
import (
"errors"
"net"
"net/url"
"os"
)
const (
// SocketEnv is the environment variable holding the default Workload API
// address.
SocketEnv = "SPIFFE_ENDPOINT_SOCKET"
)
func GetDefaultAddress() (string, bool) {
return os.LookupEnv(SocketEnv)
}
// ValidateAddress validates that the provided address
// can be parsed to a gRPC target string for dialing
// a Workload API endpoint exposed as either a Unix
// Domain Socket or TCP socket.
func ValidateAddress(addr string) error {
_, err := TargetFromAddress(addr)
return err
}
// TargetFromAddress parses the endpoint address and returns a gRPC target
// string for dialing.
func TargetFromAddress(addr string) (string, error) {
u, err := url.Parse(addr)
if err != nil {
return "", errors.New("workload endpoint socket is not a valid URI: " + err.Error())
}
return parseTargetFromURLAddr(u)
}
func parseTargetFromURLAddr(u *url.URL) (string, error) {
if u.Scheme == "tcp" {
switch {
case u.Opaque != "":
return "", errors.New("workload endpoint tcp socket URI must not be opaque")
case u.User != nil:
return "", errors.New("workload endpoint tcp socket URI must not include user info")
case u.Host == "":
return "", errors.New("workload endpoint tcp socket URI must include a host")
case u.Path != "":
return "", errors.New("workload endpoint tcp socket URI must not include a path")
case u.RawQuery != "":
return "", errors.New("workload endpoint tcp socket URI must not include query values")
case u.Fragment != "":
return "", errors.New("workload endpoint tcp socket URI must not include a fragment")
}
ip := net.ParseIP(u.Hostname())
if ip == nil {
return "", errors.New("workload endpoint tcp socket URI host component must be an IP:port")
}
port := u.Port()
if port == "" {
return "", errors.New("workload endpoint tcp socket URI host component must include a port")
}
return net.JoinHostPort(ip.String(), port), nil
}
return parseTargetFromURLAddrOS(u)
}
|