-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
proxy_settings.go
169 lines (146 loc) · 6.37 KB
/
proxy_settings.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Copyright 2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package service
import (
"context"
"fmt"
"github.com/gravitational/trace"
"github.com/gravitational/teleport/api/client/webclient"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
)
// networkConfigGetter is a helper interface that allows to fetch the current proxy configuration.
type networkConfigGetter interface {
GetClusterNetworkingConfig(ctx context.Context, opts ...services.MarshalOption) (types.ClusterNetworkingConfig, error)
}
// proxySettings is a helper type that allows to fetch the current proxy configuration.
type proxySettings struct {
// cfg is the Teleport service configuration.
cfg *servicecfg.Config
// proxySSHAddr is the address of the proxy ssh service. It can be assigned during runtime when a user set the
// proxy listener address to a random port (e.g. `127.0.0.1:0`).
proxySSHAddr utils.NetAddr
// accessPoint is the caching client connected to the auth server.
accessPoint networkConfigGetter
}
// GetOpenAIAPIKey returns the OpenAI API key.
// TODO(jakule): Remove once plugin support is added to OSS.
func (p *proxySettings) GetOpenAIAPIKey() string {
return p.cfg.Proxy.AssistAPIKey
}
// GetProxySettings allows returns current proxy configuration.
func (p *proxySettings) GetProxySettings(ctx context.Context) (*webclient.ProxySettings, error) {
resp, err := p.accessPoint.GetClusterNetworkingConfig(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
switch p.cfg.Version {
case defaults.TeleportConfigVersionV2, defaults.TeleportConfigVersionV3:
return p.buildProxySettingsV2(resp.GetProxyListenerMode()), nil
default:
return p.buildProxySettings(resp.GetProxyListenerMode()), nil
}
}
// buildProxySettings builds standard proxy configuration where proxy services are
// configured on different listeners. If the TLSRoutingEnabled flag is set and a proxy
// client support the TLSRouting dialer then the client will connect to the Teleport Proxy WebPort
// where incoming connections are routed to the proper proxy service based on TLS SNI ALPN routing information.
func (p *proxySettings) buildProxySettings(proxyListenerMode types.ProxyListenerMode) *webclient.ProxySettings {
proxySettings := webclient.ProxySettings{
TLSRoutingEnabled: proxyListenerMode == types.ProxyListenerMode_Multiplex,
AssistEnabled: p.cfg.Proxy.AssistAPIKey != "",
AccessGraphEnabled: p.cfg.AccessGraph.Enabled,
Kube: webclient.KubeProxySettings{
Enabled: p.cfg.Proxy.Kube.Enabled,
},
SSH: webclient.SSHProxySettings{
ListenAddr: p.proxySSHAddr.String(),
TunnelListenAddr: p.cfg.Proxy.ReverseTunnelListenAddr.String(),
WebListenAddr: p.cfg.Proxy.WebAddr.String(),
},
}
p.setProxyPublicAddressesSettings(&proxySettings)
if !p.cfg.Proxy.MySQLAddr.IsEmpty() {
proxySettings.DB.MySQLListenAddr = p.cfg.Proxy.MySQLAddr.String()
}
if !p.cfg.Proxy.PostgresAddr.IsEmpty() {
proxySettings.DB.PostgresListenAddr = p.cfg.Proxy.PostgresAddr.String()
}
if !p.cfg.Proxy.MongoAddr.IsEmpty() {
proxySettings.DB.MongoListenAddr = p.cfg.Proxy.MongoAddr.String()
}
if p.cfg.Proxy.Kube.Enabled {
proxySettings.Kube.ListenAddr = p.cfg.Proxy.Kube.ListenAddr.String()
}
return &proxySettings
}
// buildProxySettingsV2 builds the v2 proxy settings where teleport proxies can start only on a single listener.
func (p *proxySettings) buildProxySettingsV2(proxyListenerMode types.ProxyListenerMode) *webclient.ProxySettings {
multiplexAddr := p.cfg.Proxy.WebAddr.String()
settings := p.buildProxySettings(proxyListenerMode)
if proxyListenerMode == types.ProxyListenerMode_Multiplex {
settings.SSH.ListenAddr = multiplexAddr
settings.SSH.TunnelListenAddr = multiplexAddr
settings.SSH.WebListenAddr = multiplexAddr
settings.Kube.ListenAddr = multiplexAddr
settings.DB.MySQLListenAddr = multiplexAddr
settings.DB.PostgresListenAddr = multiplexAddr
}
return settings
}
func (p *proxySettings) setProxyPublicAddressesSettings(settings *webclient.ProxySettings) {
if len(p.cfg.Proxy.PublicAddrs) > 0 {
settings.SSH.PublicAddr = p.cfg.Proxy.PublicAddrs[0].String()
}
if len(p.cfg.Proxy.SSHPublicAddrs) > 0 {
settings.SSH.SSHPublicAddr = p.cfg.Proxy.SSHPublicAddrs[0].String()
}
if len(p.cfg.Proxy.TunnelPublicAddrs) > 0 {
settings.SSH.TunnelPublicAddr = p.cfg.Proxy.TunnelPublicAddrs[0].String()
}
if len(p.cfg.Proxy.Kube.PublicAddrs) > 0 {
settings.Kube.PublicAddr = p.cfg.Proxy.Kube.PublicAddrs[0].String()
}
if len(p.cfg.Proxy.MySQLPublicAddrs) > 0 {
settings.DB.MySQLPublicAddr = p.cfg.Proxy.MySQLPublicAddrs[0].String()
}
if len(p.cfg.Proxy.MongoPublicAddrs) > 0 {
settings.DB.MongoPublicAddr = p.cfg.Proxy.MongoPublicAddrs[0].String()
}
settings.DB.PostgresPublicAddr = p.getPostgresPublicAddr()
}
// getPostgresPublicAddr returns the proxy PostgresPublicAddrs based on whether the Postgres proxy service
// was configured on separate listener. For backward compatibility if PostgresPublicAddrs was not provided.
// Proxy will reuse the PostgresPublicAddrs field to propagate postgres service address to legacy tsh clients.
func (p *proxySettings) getPostgresPublicAddr() string {
if len(p.cfg.Proxy.PostgresPublicAddrs) > 0 {
return p.cfg.Proxy.PostgresPublicAddrs[0].String()
}
if p.cfg.Proxy.PostgresAddr.IsEmpty() {
return ""
}
// DELETE IN 9.0.0
// If the PostgresPublicAddrs address was not set propagate separate postgres service listener address
// to legacy tsh clients reusing PostgresPublicAddrs field.
var host string
if len(p.cfg.Proxy.PublicAddrs) > 0 {
// Get proxy host address from public address.
host = p.cfg.Proxy.PublicAddrs[0].Host()
} else {
host = p.cfg.Proxy.WebAddr.Host()
}
return fmt.Sprintf("%s:%d", host, p.cfg.Proxy.PostgresAddr.Port(defaults.PostgresListenPort))
}