fix(swarm): fixed issue parsing url with no scheme [EE-4017] (#7502)

pull/7575/head
Matt Hook 2022-08-26 11:55:55 +12:00 committed by GitHub
parent 27095ede22
commit a54c54ef24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 67 additions and 50 deletions

View File

@ -5,18 +5,17 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
netUrl "net/url"
"strconv" "strconv"
"strings"
"time" "time"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/internal/url"
) )
// GetAgentVersionAndPlatform returns the agent version and platform // GetAgentVersionAndPlatform returns the agent version and platform
// //
// it sends a ping to the agent and parses the version and platform from the headers // it sends a ping to the agent and parses the version and platform from the headers
func GetAgentVersionAndPlatform(url string, tlsConfig *tls.Config) (portainer.AgentPlatform, string, error) { func GetAgentVersionAndPlatform(endpointUrl string, tlsConfig *tls.Config) (portainer.AgentPlatform, string, error) {
httpCli := &http.Client{ httpCli := &http.Client{
Timeout: 3 * time.Second, Timeout: 3 * time.Second,
} }
@ -27,11 +26,7 @@ func GetAgentVersionAndPlatform(url string, tlsConfig *tls.Config) (portainer.Ag
} }
} }
if !strings.Contains(url, "://") { parsedURL, err := url.ParseURL(endpointUrl + "/ping")
url = "https://" + url
}
parsedURL, err := netUrl.Parse(fmt.Sprintf("%s/ping", url))
if err != nil { if err != nil {
return 0, "", err return 0, "", err
} }

View File

@ -259,9 +259,6 @@ func (handler *Handler) createEndpoint(payload *endpointCreatePayload) (*portain
endpointType := portainer.DockerEnvironment endpointType := portainer.DockerEnvironment
var agentVersion string var agentVersion string
if payload.EndpointCreationType == agentEnvironment { if payload.EndpointCreationType == agentEnvironment {
payload.URL = "tcp://" + normalizeAgentAddress(payload.URL)
var tlsConfig *tls.Config var tlsConfig *tls.Config
if payload.TLS { if payload.TLS {
tlsConfig, err = crypto.CreateTLSConfigurationFromBytes(payload.TLSCACertFile, payload.TLSCertFile, payload.TLSKeyFile, payload.TLSSkipVerify, payload.TLSSkipClientVerify) tlsConfig, err = crypto.CreateTLSConfigurationFromBytes(payload.TLSCACertFile, payload.TLSCertFile, payload.TLSKeyFile, payload.TLSSkipVerify, payload.TLSSkipClientVerify)

View File

@ -105,12 +105,7 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) *
} }
if payload.URL != nil { if payload.URL != nil {
if endpoint.Type == portainer.AgentOnDockerEnvironment || endpoint.URL = *payload.URL
endpoint.Type == portainer.AgentOnKubernetesEnvironment {
endpoint.URL = normalizeAgentAddress(*payload.URL)
} else {
endpoint.URL = *payload.URL
}
} }
if payload.PublicURL != nil { if payload.PublicURL != nil {

View File

@ -1,18 +1,6 @@
package endpoints package endpoints
import "strings"
func BoolAddr(b bool) *bool { func BoolAddr(b bool) *bool {
boolVar := b boolVar := b
return &boolVar return &boolVar
} }
func normalizeAgentAddress(url string) string {
// Case insensitive strip http or https scheme if URL entered
index := strings.Index(url, "://")
if index >= 0 {
return url[index+3:]
}
return url
}

View File

@ -5,14 +5,13 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"net/url"
"strings"
"github.com/pkg/errors" "github.com/pkg/errors"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/crypto" "github.com/portainer/portainer/api/crypto"
"github.com/portainer/portainer/api/http/proxy/factory/agent" "github.com/portainer/portainer/api/http/proxy/factory/agent"
"github.com/portainer/portainer/api/internal/endpointutils" "github.com/portainer/portainer/api/internal/endpointutils"
"github.com/portainer/portainer/api/internal/url"
) )
// ProxyServer provide an extended proxy with a local server to forward requests // ProxyServer provide an extended proxy with a local server to forward requests
@ -34,7 +33,7 @@ func (factory *ProxyFactory) NewAgentProxy(endpoint *portainer.Endpoint) (*Proxy
urlString = fmt.Sprintf("http://127.0.0.1:%d", tunnel.Port) urlString = fmt.Sprintf("http://127.0.0.1:%d", tunnel.Port)
} }
endpointURL, err := parseURL(urlString) endpointURL, err := url.ParseURL(urlString)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed parsing url %s", endpoint.URL) return nil, errors.Wrapf(err, "failed parsing url %s", endpoint.URL)
} }
@ -99,15 +98,3 @@ func (proxy *ProxyServer) Close() {
proxy.server.Close() proxy.server.Close()
} }
} }
// parseURL parses the endpointURL using url.Parse.
//
// to prevent an error when url has port but no protocol prefix
// we add `//` prefix if needed
func parseURL(endpointURL string) (*url.URL, error) {
if !strings.HasPrefix(endpointURL, "http") && !strings.HasPrefix(endpointURL, "tcp") && !strings.HasPrefix(endpointURL, "//") {
endpointURL = fmt.Sprintf("//%s", endpointURL)
}
return url.Parse(endpointURL)
}

View File

@ -5,13 +5,13 @@ import (
"io" "io"
"log" "log"
"net/http" "net/http"
"net/url"
"strings" "strings"
httperror "github.com/portainer/libhttp/error" httperror "github.com/portainer/libhttp/error"
portainer "github.com/portainer/portainer/api" portainer "github.com/portainer/portainer/api"
"github.com/portainer/portainer/api/crypto" "github.com/portainer/portainer/api/crypto"
"github.com/portainer/portainer/api/http/proxy/factory/docker" "github.com/portainer/portainer/api/http/proxy/factory/docker"
"github.com/portainer/portainer/api/internal/url"
) )
func (factory *ProxyFactory) newDockerProxy(endpoint *portainer.Endpoint) (http.Handler, error) { func (factory *ProxyFactory) newDockerProxy(endpoint *portainer.Endpoint) (http.Handler, error) {
@ -23,7 +23,7 @@ func (factory *ProxyFactory) newDockerProxy(endpoint *portainer.Endpoint) (http.
} }
func (factory *ProxyFactory) newDockerLocalProxy(endpoint *portainer.Endpoint) (http.Handler, error) { func (factory *ProxyFactory) newDockerLocalProxy(endpoint *portainer.Endpoint) (http.Handler, error) {
endpointURL, err := url.Parse(endpoint.URL) endpointURL, err := url.ParseURL(endpoint.URL)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -38,7 +38,7 @@ func (factory *ProxyFactory) newDockerHTTPProxy(endpoint *portainer.Endpoint) (h
rawURL = fmt.Sprintf("http://127.0.0.1:%d", tunnel.Port) rawURL = fmt.Sprintf("http://127.0.0.1:%d", tunnel.Port)
} }
endpointURL, err := url.Parse(rawURL) endpointURL, err := url.ParseURL(rawURL)
if err != nil { if err != nil {
return nil, err return nil, err
} }

19
api/internal/url/url.go Normal file
View File

@ -0,0 +1,19 @@
package url
import (
"fmt"
"net/url"
"strings"
)
// ParseURL parses the endpointURL using url.Parse.
//
// to prevent an error when url has port but no protocol prefix
// we add `//` prefix if needed
func ParseURL(endpointURL string) (*url.URL, error) {
if !strings.HasPrefix(endpointURL, "http") && !strings.HasPrefix(endpointURL, "tcp") && !strings.HasPrefix(endpointURL, "//") {
endpointURL = fmt.Sprintf("//%s", endpointURL)
}
return url.Parse(endpointURL)
}

View File

@ -130,7 +130,7 @@ export async function createRemoteEnvironment({
}: CreateRemoteEnvironment) { }: CreateRemoteEnvironment) {
return createEnvironment(name, creationType, { return createEnvironment(name, creationType, {
...options, ...options,
url: `${url}`, url: `tcp://${url}`,
}); });
} }

View File

@ -110,9 +110,11 @@
<span ng-if="!state.agentEndpoint">Environment URL</span> <span ng-if="!state.agentEndpoint">Environment URL</span>
<span ng-if="state.agentEndpoint">Environment address</span> <span ng-if="state.agentEndpoint">Environment address</span>
<portainer-tooltip <portainer-tooltip
ng-if="!state.agentEndpoint"
message="'URL or IP address of a Docker host. The Docker API must be exposed over a TCP port. Please refer to the Docker documentation to configure it.'" message="'URL or IP address of a Docker host. The Docker API must be exposed over a TCP port. Please refer to the Docker documentation to configure it.'"
> >
</portainer-tooltip> </portainer-tooltip>
<portainer-tooltip ng-if="state.agentEndpoint" message="'The address for the Portainer agent in the format <HOST>:<PORT> or <IP>:<PORT>'"> </portainer-tooltip>
</label> </label>
<div class="col-sm-9 col-lg-10"> <div class="col-sm-9 col-lg-10">
<input <input

View File

@ -9,8 +9,42 @@ import { nameValidation } from '../NameField';
export function validation(): SchemaOf<CreateAgentEnvironmentValues> { export function validation(): SchemaOf<CreateAgentEnvironmentValues> {
return object({ return object({
name: nameValidation(), name: nameValidation(),
environmentUrl: string().required('This field is required.'), environmentUrl: environmentValidation(),
meta: metadataValidation(), meta: metadataValidation(),
gpus: gpusListValidation(), gpus: gpusListValidation(),
}); });
} }
function environmentValidation() {
return string()
.required('This field is required')
.test(
'address',
'Environment address must be of the form <IP>:<PORT> or <HOST>:<PORT>.',
(environmentUrl) => validateAddress(environmentUrl)
);
}
export function validateAddress(address: string | undefined) {
if (typeof address === 'undefined') {
return false;
}
if (address.indexOf('://') > -1) {
return false;
}
const [host, port] = address.split(':');
if (
host.length === 0 ||
Number.isNaN(parseInt(port, 10)) ||
port.match(/^[0-9]+$/) == null ||
parseInt(port, 10) < 1 ||
parseInt(port, 10) > 65535
) {
return false;
}
return true;
}

View File

@ -12,7 +12,7 @@ export function EnvironmentUrlField() {
errors={meta.error} errors={meta.error}
required required
inputId="environment-url-field" inputId="environment-url-field"
tooltip="A host:port combination. The host can be either an IP address or a host name." tooltip="<HOST>:<PORT> or <IP>:<PORT>"
> >
<Field <Field
id="environment-url-field" id="environment-url-field"