use tlsClientConfig instead of custom dialer

This commit is contained in:
Denis Mishin 2022-12-23 22:10:02 -05:00
parent 753eeff12f
commit bfadf123d5
6 changed files with 52 additions and 7 deletions

3
.vscode/launch.json vendored
View file

@ -7,7 +7,8 @@
"request": "launch",
"mode": "debug",
"program": "${workspaceRoot}/cmd/pomerium",
"args": ["-config", "${workspaceRoot}/.config.yaml"]
"args": ["-config", "${workspaceRoot}/.config.yaml"],
"cwd": "${workspaceRoot}",
},
{
"name": "Connect to server",

View file

@ -84,9 +84,15 @@ func newAuthorizeStateFromConfig(cfg *config.Config, store *store.Store) (*autho
}
state.hpkePrivateKey = hpke.DerivePrivateKey(sharedKey)
state.authenticateKeyFetcher = hpke.NewKeyFetcher(authenticateURL.ResolveReference(&url.URL{
jwksURL := authenticateURL.ResolveReference(&url.URL{
Path: "/.well-known/pomerium/jwks.json",
}).String())
}).String()
transport, err := config.GetTLSClientTransport(cfg)
if err != nil {
return nil, fmt.Errorf("authorize: get tls client config: %w", err)
}
state.authenticateKeyFetcher = hpke.NewKeyFetcher(jwksURL, transport)
return state, nil
}

View file

@ -5,6 +5,7 @@ import (
"github.com/pomerium/pomerium/internal/hashutil"
"github.com/pomerium/pomerium/internal/telemetry/metrics"
"github.com/pomerium/pomerium/pkg/cryptutil"
)
// MetricsScrapeEndpoint defines additional metrics endpoints that would be scraped and exposed by pomerium
@ -86,3 +87,15 @@ func (cfg *Config) AllocatePorts(ports [6]string) {
cfg.DebugPort = ports[4]
cfg.ACMETLSALPNPort = ports[5]
}
// GetTLSClientConfig returns TLS configuration that accounts for additional CA entries
func (cfg *Config) GetTLSClientConfig() (*tls.Config, error) {
roots, err := cryptutil.GetCertPool(cfg.Options.CA, cfg.Options.CAFile)
if err != nil {
return nil, err
}
return &tls.Config{
RootCAs: roots,
MinVersion: tls.VersionTLS12,
}, nil
}

View file

@ -42,7 +42,12 @@ func NewHTTPTransport(src Source) *http.Transport {
Config: tlsConfig,
}
lock.Unlock()
return d.DialContext(ctx, network, addr)
log.Info(ctx).Str("network", network).Str("addr", addr).Msg("DIALING...")
conn, err := d.DialContext(ctx, network, addr)
if err != nil {
log.Error(ctx).Err(err).Str("network", network).Str("addr", addr).Msg("DIAL")
}
return conn, err
}
transport.ForceAttemptHTTP2 = true
return transport
@ -117,3 +122,15 @@ func NewPolicyHTTPTransport(options *Options, policy *Policy, disableHTTP2 bool)
}
return c.Then(transport)
}
// GetTLSClientTransport returns http transport accounting for custom CAs from config
func GetTLSClientTransport(cfg *Config) (*http.Transport, error) {
tlsConfig, err := cfg.GetTLSClientConfig()
if err != nil {
return nil, err
}
return &http.Transport{
TLSClientConfig: tlsConfig,
ForceAttemptHTTP2: true,
}, nil
}

View file

@ -81,9 +81,12 @@ func (fetcher *jwksKeyFetcher) FetchPublicKey(ctx context.Context) (*PublicKey,
}
// NewKeyFetcher returns a new KeyFetcher which fetches keys using an in-memory HTTP cache.
func NewKeyFetcher(endpoint string) KeyFetcher {
func NewKeyFetcher(endpoint string, transport http.RoundTripper) KeyFetcher {
return &jwksKeyFetcher{
client: httpcache.NewMemoryCacheTransport().Client(),
client: (&httpcache.Transport{
Transport: transport,
Cache: httpcache.NewMemoryCache(),
}).Client(),
endpoint: endpoint,
}
}

View file

@ -3,6 +3,7 @@ package proxy
import (
"context"
"crypto/cipher"
"net/http"
"net/url"
"github.com/pomerium/pomerium/config"
@ -61,9 +62,13 @@ func newProxyStateFromConfig(cfg *config.Config) (*proxyState, error) {
if err != nil {
return nil, err
}
tlsConfig, err := cfg.GetTLSClientConfig()
if err != nil {
return nil, err
}
state.authenticateKeyFetcher = hpke.NewKeyFetcher(authenticateURL.ResolveReference(&url.URL{
Path: "/.well-known/pomerium/jwks.json",
}).String())
}).String(), &http.Transport{TLSClientConfig: tlsConfig, ForceAttemptHTTP2: true})
state.sharedCipher, err = cryptutil.NewAEADCipher(state.sharedKey)
if err != nil {