You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
consul/testing/deployer/util/consul.go

115 lines
3.5 KiB

[COMPLIANCE] License changes (#18443) * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
1 year ago
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package util
import (
"context"
"crypto/tls"
"fmt"
"net/http"
"net/url"
"strconv"
"github.com/hashicorp/consul-server-connection-manager/discovery"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-hclog"
"google.golang.org/grpc"
)
func DialExposedGRPCConn(
ctx context.Context, logger hclog.Logger,
exposedServerGRPCPort int, token string,
tlsConfig *tls.Config,
) (*grpc.ClientConn, func(), error) {
if exposedServerGRPCPort <= 0 {
return nil, nil, fmt.Errorf("cannot dial server grpc on port %d", exposedServerGRPCPort)
}
cfg := discovery.Config{
Addresses: "127.0.0.1",
GRPCPort: exposedServerGRPCPort,
// Disable server watch because we only need to get server IPs once.
ServerWatchDisabled: true,
TLS: tlsConfig,
Credentials: discovery.Credentials{
Type: discovery.CredentialsTypeStatic,
Static: discovery.StaticTokenCredential{
Token: token,
},
},
}
watcher, err := discovery.NewWatcher(ctx, cfg, logger.Named("consul-server-connection-manager"))
if err != nil {
return nil, nil, err
}
go watcher.Run()
// We recycle the GRPC connection from the discovery client because it
// should have all the necessary dial options, including the resolver that
// continuously updates Consul server addresses. Otherwise, a lot of code from consul-server-connection-manager
// would need to be duplicated
state, err := watcher.State()
if err != nil {
watcher.Stop()
return nil, nil, fmt.Errorf("unable to get connection manager state: %w", err)
}
return state.GRPCConn, func() { watcher.Stop() }, nil
}
func ProxyNotPooledAPIClient(proxyPort int, containerIP string, containerPort int, token string) (*api.Client, error) {
return proxyAPIClient(cleanhttp.DefaultTransport(), proxyPort, containerIP, containerPort, token)
}
func ProxyAPIClient(proxyPort int, containerIP string, containerPort int, token string) (*api.Client, error) {
return proxyAPIClient(cleanhttp.DefaultPooledTransport(), proxyPort, containerIP, containerPort, token)
}
func proxyAPIClient(baseTransport *http.Transport, proxyPort int, containerIP string, containerPort int, token string) (*api.Client, error) {
if proxyPort <= 0 {
return nil, fmt.Errorf("cannot use an http proxy on port %d", proxyPort)
}
if containerIP == "" {
return nil, fmt.Errorf("container IP is required")
}
if containerPort <= 0 {
return nil, fmt.Errorf("cannot dial api client on port %d", containerPort)
}
proxyURL, err := url.Parse("http://127.0.0.1:" + strconv.Itoa(proxyPort))
if err != nil {
return nil, err
}
cfg := api.DefaultConfig()
cfg.Transport = baseTransport
cfg.Transport.Proxy = http.ProxyURL(proxyURL)
cfg.Address = fmt.Sprintf("http://%s:%d", containerIP, containerPort)
cfg.Token = token
return api.NewClient(cfg)
}
func ProxyNotPooledHTTPTransport(proxyPort int) (*http.Transport, error) {
return proxyHTTPTransport(cleanhttp.DefaultTransport(), proxyPort)
}
func ProxyHTTPTransport(proxyPort int) (*http.Transport, error) {
return proxyHTTPTransport(cleanhttp.DefaultPooledTransport(), proxyPort)
}
func proxyHTTPTransport(baseTransport *http.Transport, proxyPort int) (*http.Transport, error) {
if proxyPort <= 0 {
return nil, fmt.Errorf("cannot use an http proxy on port %d", proxyPort)
}
proxyURL, err := url.Parse("http://127.0.0.1:" + strconv.Itoa(proxyPort))
if err != nil {
return nil, err
}
baseTransport.Proxy = http.ProxyURL(proxyURL)
return baseTransport, nil
}