1/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8    http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package util
18
19import (
20	"fmt"
21	"net"
22	"net/url"
23	"strconv"
24
25	kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
26
27	"k8s.io/apimachinery/pkg/util/validation"
28	utilsnet "k8s.io/utils/net"
29
30	"github.com/pkg/errors"
31)
32
33// GetControlPlaneEndpoint returns a properly formatted endpoint for the control plane built according following rules:
34// - If the controlPlaneEndpoint is defined, use it.
35// - if the controlPlaneEndpoint is defined but without a port number, use the controlPlaneEndpoint + localEndpoint.BindPort is used.
36// - Otherwise, in case the controlPlaneEndpoint is not defined, use the localEndpoint.AdvertiseAddress + the localEndpoint.BindPort.
37func GetControlPlaneEndpoint(controlPlaneEndpoint string, localEndpoint *kubeadmapi.APIEndpoint) (string, error) {
38	// get the URL of the local endpoint
39	localAPIEndpoint, err := GetLocalAPIEndpoint(localEndpoint)
40	if err != nil {
41		return "", err
42	}
43
44	// if the controlplane endpoint is defined
45	if len(controlPlaneEndpoint) > 0 {
46		// parse the controlplane endpoint
47		var host, port string
48		var err error
49		if host, port, err = ParseHostPort(controlPlaneEndpoint); err != nil {
50			return "", errors.Wrapf(err, "invalid value %q given for controlPlaneEndpoint", controlPlaneEndpoint)
51		}
52
53		// if a port is provided within the controlPlaneAddress warn the users we are using it, else use the bindport
54		localEndpointPort := strconv.Itoa(int(localEndpoint.BindPort))
55		if port != "" {
56			if port != localEndpointPort {
57				fmt.Println("[endpoint] WARNING: port specified in controlPlaneEndpoint overrides bindPort in the controlplane address")
58			}
59		} else {
60			port = localEndpointPort
61		}
62
63		// overrides the control-plane url using the controlPlaneAddress (and eventually the bindport)
64		return formatURL(host, port).String(), nil
65	}
66
67	return localAPIEndpoint, nil
68}
69
70// GetLocalAPIEndpoint parses an APIEndpoint and returns it as a string,
71// or returns and error in case it cannot be parsed.
72func GetLocalAPIEndpoint(localEndpoint *kubeadmapi.APIEndpoint) (string, error) {
73	// get the URL of the local endpoint
74	localEndpointIP, localEndpointPort, err := parseAPIEndpoint(localEndpoint)
75	if err != nil {
76		return "", err
77	}
78	url := formatURL(localEndpointIP.String(), localEndpointPort)
79	return url.String(), nil
80}
81
82// ParseHostPort parses a network address of the form "host:port", "ipv4:port", "[ipv6]:port" into host and port;
83// ":port" can be eventually omitted.
84// If the string is not a valid representation of network address, ParseHostPort returns an error.
85func ParseHostPort(hostport string) (string, string, error) {
86	var host, port string
87	var err error
88
89	// try to split host and port
90	if host, port, err = net.SplitHostPort(hostport); err != nil {
91		// if SplitHostPort returns an error, the entire hostport is considered as host
92		host = hostport
93	}
94
95	// if port is defined, parse and validate it
96	if port != "" {
97		if _, err := ParsePort(port); err != nil {
98			return "", "", errors.Errorf("hostport %s: port %s must be a valid number between 1 and 65535, inclusive", hostport, port)
99		}
100	}
101
102	// if host is a valid IP, returns it
103	if ip := net.ParseIP(host); ip != nil {
104		return host, port, nil
105	}
106
107	// if host is a validate RFC-1123 subdomain, returns it
108	if errs := validation.IsDNS1123Subdomain(host); len(errs) == 0 {
109		return host, port, nil
110	}
111
112	return "", "", errors.Errorf("hostport %s: host '%s' must be a valid IP address or a valid RFC-1123 DNS subdomain", hostport, host)
113}
114
115// ParsePort parses a string representing a TCP port.
116// If the string is not a valid representation of a TCP port, ParsePort returns an error.
117func ParsePort(port string) (int, error) {
118	portInt, err := utilsnet.ParsePort(port, true)
119	if err == nil && (1 <= portInt && portInt <= 65535) {
120		return portInt, nil
121	}
122
123	return 0, errors.New("port must be a valid number between 1 and 65535, inclusive")
124}
125
126// parseAPIEndpoint parses an APIEndpoint and returns the AdvertiseAddress as net.IP and the BindPort as string.
127// If the BindPort or AdvertiseAddress are invalid it returns an error.
128func parseAPIEndpoint(localEndpoint *kubeadmapi.APIEndpoint) (net.IP, string, error) {
129	// parse the bind port
130	bindPortString := strconv.Itoa(int(localEndpoint.BindPort))
131	if _, err := ParsePort(bindPortString); err != nil {
132		return nil, "", errors.Wrapf(err, "invalid value %q given for api.bindPort", localEndpoint.BindPort)
133	}
134
135	// parse the AdvertiseAddress
136	var ip = net.ParseIP(localEndpoint.AdvertiseAddress)
137	if ip == nil {
138		return nil, "", errors.Errorf("invalid value `%s` given for api.advertiseAddress", localEndpoint.AdvertiseAddress)
139	}
140
141	return ip, bindPortString, nil
142}
143
144// formatURL takes a host and a port string and creates a net.URL using https scheme
145func formatURL(host, port string) *url.URL {
146	return &url.URL{
147		Scheme: "https",
148		Host:   net.JoinHostPort(host, port),
149	}
150}
151