Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ws-proxy/pkg/proxy/config.go
2500 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package proxy
6
7
import (
8
"os"
9
"path/filepath"
10
11
validation "github.com/go-ozzo/ozzo-validation"
12
"golang.org/x/xerrors"
13
14
"github.com/gitpod-io/gitpod/common-go/log"
15
"github.com/gitpod-io/gitpod/common-go/util"
16
)
17
18
// Config is the configuration for a WorkspaceProxy.
19
type Config struct {
20
HTTPS struct {
21
Key string `json:"key"`
22
Certificate string `json:"crt"`
23
} `json:"https,omitempty"`
24
25
TransportConfig *TransportConfig `json:"transportConfig"`
26
BlobServer *BlobServerConfig `json:"blobServer"`
27
GitpodInstallation *GitpodInstallation `json:"gitpodInstallation"`
28
WorkspacePodConfig *WorkspacePodConfig `json:"workspacePodConfig"`
29
30
BuiltinPages BuiltinPagesConfig `json:"builtinPages"`
31
SSHGatewayCAKeyFile string `json:"sshCAKeyFile"`
32
}
33
34
// Validate validates the configuration to catch issues during startup and not at runtime.
35
func (c *Config) Validate() error {
36
type validatable interface {
37
Validate() error
38
}
39
for _, v := range []validatable{
40
c.TransportConfig,
41
c.BlobServer,
42
c.GitpodInstallation,
43
c.WorkspacePodConfig,
44
} {
45
err := v.Validate()
46
if err != nil {
47
return err
48
}
49
}
50
51
return nil
52
}
53
54
// HostBasedIngressConfig configures the host-based ingress.
55
type HostBasedIngressConfig struct {
56
HTTPAddress string `json:"httpAddress"`
57
HTTPSAddress string `json:"httpsAddress"`
58
Header string `json:"header"`
59
}
60
61
// Validate validates this config.
62
func (c *HostBasedIngressConfig) Validate() error {
63
if c == nil {
64
return xerrors.Errorf("host based ingress config is mandatory")
65
}
66
return validation.ValidateStruct(c,
67
validation.Field(&c.HTTPAddress, validation.Required),
68
validation.Field(&c.HTTPSAddress, validation.Required),
69
validation.Field(&c.Header, validation.Required),
70
)
71
}
72
73
// WorkspacePodConfig contains config around the workspace pod.
74
type WorkspacePodConfig struct {
75
TheiaPort uint16 `json:"theiaPort"`
76
IDEDebugPort uint16 `json:"ideDebugPort"`
77
SupervisorPort uint16 `json:"supervisorPort"`
78
SupervisorDebugPort uint16 `json:"supervisorDebugPort"`
79
DebugWorkspaceProxyPort uint16 `json:"debugWorkspaceProxyPort"`
80
// SupervisorImage is deprecated
81
SupervisorImage string `json:"supervisorImage"`
82
}
83
84
// Validate validates the configuration to catch issues during startup and not at runtime.
85
func (c *WorkspacePodConfig) Validate() error {
86
if c == nil {
87
return xerrors.Errorf("WorkspacePodConfig not configured")
88
}
89
90
err := validation.ValidateStruct(c,
91
validation.Field(&c.TheiaPort, validation.Required),
92
validation.Field(&c.IDEDebugPort, validation.Required),
93
validation.Field(&c.SupervisorPort, validation.Required),
94
validation.Field(&c.SupervisorDebugPort, validation.Required),
95
validation.Field(&c.DebugWorkspaceProxyPort, validation.Required),
96
)
97
if len(c.SupervisorImage) > 0 {
98
log.Warn("config value 'workspacePodConfig.supervisorImage' is deprected, use it only to be backwards compatible")
99
}
100
if err != nil {
101
return err
102
}
103
104
return nil
105
}
106
107
// GitpodInstallation contains config regarding the Gitpod installation.
108
type GitpodInstallation struct {
109
Scheme string `json:"scheme"`
110
HostName string `json:"hostName"`
111
WorkspaceHostSuffix string `json:"workspaceHostSuffix"`
112
WorkspaceHostSuffixRegex string `json:"workspaceHostSuffixRegex"`
113
}
114
115
// Validate validates the configuration to catch issues during startup and not at runtime.
116
func (c *GitpodInstallation) Validate() error {
117
if c == nil {
118
return xerrors.Errorf("GitpodInstallation not configured")
119
}
120
121
return validation.ValidateStruct(c,
122
validation.Field(&c.Scheme, validation.Required),
123
validation.Field(&c.HostName, validation.Required), // TODO IP ONLY: Check if there is any dependency. If yes, remove it.
124
validation.Field(&c.WorkspaceHostSuffix, validation.Required),
125
)
126
}
127
128
// BlobServerConfig configures where to serve the IDE from.
129
type BlobServerConfig struct {
130
Scheme string `json:"scheme"`
131
Host string `json:"host"`
132
PathPrefix string `json:"pathPrefix"`
133
}
134
135
// Validate validates the configuration to catch issues during startup and not at runtime.
136
func (c *BlobServerConfig) Validate() error {
137
if c == nil {
138
return xerrors.Errorf("BlobServer not configured")
139
}
140
141
err := validation.ValidateStruct(c,
142
validation.Field(&c.Scheme, validation.Required, validation.In("http", "https")),
143
validation.Field(&c.Host, validation.Required),
144
)
145
if err != nil {
146
return xerrors.Errorf("invalid blobserver config: %w", err)
147
}
148
return nil
149
}
150
151
// TransportConfig configures the way how ws-proxy connects to it's backend services.
152
type TransportConfig struct {
153
ConnectTimeout util.Duration `json:"connectTimeout"`
154
IdleConnTimeout util.Duration `json:"idleConnTimeout"`
155
MaxIdleConns int `json:"maxIdleConns"`
156
MaxIdleConnsPerHost int `json:"maxIdleConnsPerHost"`
157
}
158
159
// Validate validates the configuration to catch issues during startup and not at runtime.
160
func (c *TransportConfig) Validate() error {
161
if c == nil {
162
return xerrors.Errorf("TransportConfig not configured")
163
}
164
165
return validation.ValidateStruct(c,
166
validation.Field(&c.ConnectTimeout, validation.Required),
167
validation.Field(&c.IdleConnTimeout, validation.Required),
168
validation.Field(&c.MaxIdleConns, validation.Min(0)),
169
validation.Field(&c.MaxIdleConnsPerHost, validation.Required, validation.Min(1)),
170
)
171
}
172
173
// BuiltinPagesConfig configures pages served directly by ws-proxy.
174
type BuiltinPagesConfig struct {
175
Location string `json:"location"`
176
}
177
178
// Validate validates the configuration to catch issues during startup and not at runtime.
179
func (c *BuiltinPagesConfig) Validate() error {
180
if c == nil {
181
return xerrors.Errorf("BuiltinPagesConfig not configured")
182
}
183
184
return validation.ValidateStruct(c,
185
validation.Field(&c.Location,
186
validation.Required,
187
validation.By(validateFileExists("")),
188
validation.By(validateFileExists(builtinPagePortNotFound)),
189
),
190
)
191
}
192
193
func validateFileExists(addition string) validation.RuleFunc {
194
tpRoot := os.Getenv("TELEPRESENCE_ROOT")
195
196
return func(value interface{}) error {
197
pth, ok := value.(string)
198
if !ok {
199
return xerrors.Errorf("validateFileExists: value must be a string")
200
}
201
202
fn := filepath.Join(pth, addition)
203
if tpRoot != "" {
204
fn = filepath.Join(tpRoot, fn)
205
}
206
207
_, err := os.Stat(fn)
208
if err != nil {
209
return err
210
}
211
212
return nil
213
}
214
}
215
216