github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/networking.go (about) 1 /* 2 Copyright 2021 Gravitational, Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package types 18 19 import ( 20 "strings" 21 "time" 22 23 "github.com/gravitational/trace" 24 25 "github.com/gravitational/teleport/api/defaults" 26 "github.com/gravitational/teleport/api/utils" 27 ) 28 29 // ClusterNetworkingConfig defines cluster networking configuration. This is 30 // a configuration resource, never create more than one instance of it. 31 type ClusterNetworkingConfig interface { 32 ResourceWithOrigin 33 34 // GetClientIdleTimeout returns client idle timeout setting 35 GetClientIdleTimeout() time.Duration 36 37 // SetClientIdleTimeout sets client idle timeout setting 38 SetClientIdleTimeout(t time.Duration) 39 40 // GetKeepAliveInterval gets the keep-alive interval for server to client 41 // connections. 42 GetKeepAliveInterval() time.Duration 43 44 // SetKeepAliveInterval sets the keep-alive interval for server to client 45 // connections. 46 SetKeepAliveInterval(t time.Duration) 47 48 // GetKeepAliveCountMax gets the number of missed keep-alive messages before 49 // the server disconnects the client. 50 GetKeepAliveCountMax() int64 51 52 // SetKeepAliveCountMax sets the number of missed keep-alive messages before 53 // the server disconnects the client. 54 SetKeepAliveCountMax(c int64) 55 56 // GetSessionControlTimeout gets the session control timeout. 57 GetSessionControlTimeout() time.Duration 58 59 // SetSessionControlTimeout sets the session control timeout. 60 SetSessionControlTimeout(t time.Duration) 61 62 // GetClientIdleTimeoutMessage fetches the message to be sent to the client in 63 // the event of an idle timeout. An empty string implies no message should 64 // be sent. 65 GetClientIdleTimeoutMessage() string 66 67 // SetClientIdleTimeoutMessage sets the inactivity timeout disconnection message 68 // to be sent to the user. 69 SetClientIdleTimeoutMessage(string) 70 71 // GetWebIdleTimeout gets web idle timeout duration. 72 GetWebIdleTimeout() time.Duration 73 74 // SetWebIdleTimeout sets the web idle timeout duration. 75 SetWebIdleTimeout(time.Duration) 76 77 // GetProxyListenerMode gets the proxy listener mode. 78 GetProxyListenerMode() ProxyListenerMode 79 80 // SetProxyListenerMode sets the proxy listener mode. 81 SetProxyListenerMode(ProxyListenerMode) 82 83 // Clone performs a deep copy. 84 Clone() ClusterNetworkingConfig 85 86 // GetRoutingStrategy gets the routing strategy setting. 87 GetRoutingStrategy() RoutingStrategy 88 89 // SetRoutingStrategy sets the routing strategy setting. 90 SetRoutingStrategy(strategy RoutingStrategy) 91 92 // GetTunnelStrategy gets the tunnel strategy. 93 GetTunnelStrategyType() (TunnelStrategyType, error) 94 95 // GetAgentMeshTunnelStrategy gets the agent mesh tunnel strategy. 96 GetAgentMeshTunnelStrategy() *AgentMeshTunnelStrategy 97 98 // GetProxyPeeringTunnelStrategy gets the proxy peering tunnel strategy. 99 GetProxyPeeringTunnelStrategy() *ProxyPeeringTunnelStrategy 100 101 // SetTunnelStrategy sets the tunnel strategy. 102 SetTunnelStrategy(*TunnelStrategyV1) 103 104 // GetProxyPingInterval gets the proxy ping interval. 105 GetProxyPingInterval() time.Duration 106 107 // SetProxyPingInterval sets the proxy ping interval. 108 SetProxyPingInterval(time.Duration) 109 110 // GetAssistCommandExecutionWorkers gets the number of parallel command execution workers for Assist 111 GetAssistCommandExecutionWorkers() int32 112 113 // SetAssistCommandExecutionWorkers sets the number of parallel command execution workers for Assist 114 SetAssistCommandExecutionWorkers(n int32) 115 116 // GetCaseInsensitiveRouting gets the case-insensitive routing option. 117 GetCaseInsensitiveRouting() bool 118 119 // SetCaseInsensitiveRouting sets the case-insenstivie routing option. 120 SetCaseInsensitiveRouting(cir bool) 121 } 122 123 // NewClusterNetworkingConfigFromConfigFile is a convenience method to create 124 // ClusterNetworkingConfigV2 labeled as originating from config file. 125 func NewClusterNetworkingConfigFromConfigFile(spec ClusterNetworkingConfigSpecV2) (ClusterNetworkingConfig, error) { 126 return newClusterNetworkingConfigWithLabels(spec, map[string]string{ 127 OriginLabel: OriginConfigFile, 128 }) 129 } 130 131 // DefaultClusterNetworkingConfig returns the default cluster networking config. 132 func DefaultClusterNetworkingConfig() ClusterNetworkingConfig { 133 config, _ := newClusterNetworkingConfigWithLabels(ClusterNetworkingConfigSpecV2{}, map[string]string{ 134 OriginLabel: OriginDefaults, 135 }) 136 return config 137 } 138 139 // newClusterNetworkingConfigWithLabels is a convenience method to create 140 // ClusterNetworkingConfigV2 with a specific map of labels. 141 func newClusterNetworkingConfigWithLabels(spec ClusterNetworkingConfigSpecV2, labels map[string]string) (ClusterNetworkingConfig, error) { 142 c := &ClusterNetworkingConfigV2{ 143 Metadata: Metadata{ 144 Labels: labels, 145 }, 146 Spec: spec, 147 } 148 if err := c.CheckAndSetDefaults(); err != nil { 149 return nil, trace.Wrap(err) 150 } 151 return c, nil 152 } 153 154 // GetVersion returns resource version. 155 func (c *ClusterNetworkingConfigV2) GetVersion() string { 156 return c.Version 157 } 158 159 // GetName returns the name of the resource. 160 func (c *ClusterNetworkingConfigV2) GetName() string { 161 return c.Metadata.Name 162 } 163 164 // SetName sets the name of the resource. 165 func (c *ClusterNetworkingConfigV2) SetName(name string) { 166 c.Metadata.Name = name 167 } 168 169 // SetExpiry sets expiry time for the object. 170 func (c *ClusterNetworkingConfigV2) SetExpiry(expires time.Time) { 171 c.Metadata.SetExpiry(expires) 172 } 173 174 // Expiry returns object expiry setting. 175 func (c *ClusterNetworkingConfigV2) Expiry() time.Time { 176 return c.Metadata.Expiry() 177 } 178 179 // GetMetadata returns object metadata. 180 func (c *ClusterNetworkingConfigV2) GetMetadata() Metadata { 181 return c.Metadata 182 } 183 184 // GetResourceID returns resource ID. 185 func (c *ClusterNetworkingConfigV2) GetResourceID() int64 { 186 return c.Metadata.ID 187 } 188 189 // SetResourceID sets resource ID. 190 func (c *ClusterNetworkingConfigV2) SetResourceID(id int64) { 191 c.Metadata.ID = id 192 } 193 194 // GetRevision returns the revision 195 func (c *ClusterNetworkingConfigV2) GetRevision() string { 196 return c.Metadata.GetRevision() 197 } 198 199 // SetRevision sets the revision 200 func (c *ClusterNetworkingConfigV2) SetRevision(rev string) { 201 c.Metadata.SetRevision(rev) 202 } 203 204 // Origin returns the origin value of the resource. 205 func (c *ClusterNetworkingConfigV2) Origin() string { 206 return c.Metadata.Origin() 207 } 208 209 // SetOrigin sets the origin value of the resource. 210 func (c *ClusterNetworkingConfigV2) SetOrigin(origin string) { 211 c.Metadata.SetOrigin(origin) 212 } 213 214 // GetKind returns resource kind. 215 func (c *ClusterNetworkingConfigV2) GetKind() string { 216 return c.Kind 217 } 218 219 // GetSubKind returns resource subkind. 220 func (c *ClusterNetworkingConfigV2) GetSubKind() string { 221 return c.SubKind 222 } 223 224 // SetSubKind sets resource subkind. 225 func (c *ClusterNetworkingConfigV2) SetSubKind(sk string) { 226 c.SubKind = sk 227 } 228 229 // GetClientIdleTimeout returns client idle timeout setting. 230 func (c *ClusterNetworkingConfigV2) GetClientIdleTimeout() time.Duration { 231 return c.Spec.ClientIdleTimeout.Duration() 232 } 233 234 // SetClientIdleTimeout sets client idle timeout setting. 235 func (c *ClusterNetworkingConfigV2) SetClientIdleTimeout(d time.Duration) { 236 c.Spec.ClientIdleTimeout = Duration(d) 237 } 238 239 // GetKeepAliveInterval gets the keep-alive interval. 240 func (c *ClusterNetworkingConfigV2) GetKeepAliveInterval() time.Duration { 241 return c.Spec.KeepAliveInterval.Duration() 242 } 243 244 // SetKeepAliveInterval sets the keep-alive interval. 245 func (c *ClusterNetworkingConfigV2) SetKeepAliveInterval(t time.Duration) { 246 c.Spec.KeepAliveInterval = Duration(t) 247 } 248 249 // GetKeepAliveCountMax gets the number of missed keep-alive messages before 250 // the server disconnects the client. 251 func (c *ClusterNetworkingConfigV2) GetKeepAliveCountMax() int64 { 252 return c.Spec.KeepAliveCountMax 253 } 254 255 // SetKeepAliveCountMax sets the number of missed keep-alive messages before 256 // the server disconnects the client. 257 func (c *ClusterNetworkingConfigV2) SetKeepAliveCountMax(m int64) { 258 c.Spec.KeepAliveCountMax = m 259 } 260 261 // GetSessionControlTimeout gets the session control timeout. 262 func (c *ClusterNetworkingConfigV2) GetSessionControlTimeout() time.Duration { 263 return c.Spec.SessionControlTimeout.Duration() 264 } 265 266 // SetSessionControlTimeout sets the session control timeout. 267 func (c *ClusterNetworkingConfigV2) SetSessionControlTimeout(d time.Duration) { 268 c.Spec.SessionControlTimeout = Duration(d) 269 } 270 271 func (c *ClusterNetworkingConfigV2) GetClientIdleTimeoutMessage() string { 272 return c.Spec.ClientIdleTimeoutMessage 273 } 274 275 func (c *ClusterNetworkingConfigV2) SetClientIdleTimeoutMessage(msg string) { 276 c.Spec.ClientIdleTimeoutMessage = msg 277 } 278 279 // GetWebIdleTimeout gets the web idle timeout. 280 func (c *ClusterNetworkingConfigV2) GetWebIdleTimeout() time.Duration { 281 return c.Spec.WebIdleTimeout.Duration() 282 } 283 284 // SetWebIdleTimeout sets the web idle timeout. 285 func (c *ClusterNetworkingConfigV2) SetWebIdleTimeout(ttl time.Duration) { 286 c.Spec.WebIdleTimeout = Duration(ttl) 287 } 288 289 // GetProxyListenerMode gets the proxy listener mode. 290 func (c *ClusterNetworkingConfigV2) GetProxyListenerMode() ProxyListenerMode { 291 return c.Spec.ProxyListenerMode 292 } 293 294 // SetProxyListenerMode sets the proxy listener mode. 295 func (c *ClusterNetworkingConfigV2) SetProxyListenerMode(mode ProxyListenerMode) { 296 c.Spec.ProxyListenerMode = mode 297 } 298 299 // Clone performs a deep copy. 300 func (c *ClusterNetworkingConfigV2) Clone() ClusterNetworkingConfig { 301 return utils.CloneProtoMsg(c) 302 } 303 304 // setStaticFields sets static resource header and metadata fields. 305 func (c *ClusterNetworkingConfigV2) setStaticFields() { 306 c.Kind = KindClusterNetworkingConfig 307 c.Version = V2 308 c.Metadata.Name = MetaNameClusterNetworkingConfig 309 } 310 311 // GetRoutingStrategy gets the routing strategy setting. 312 func (c *ClusterNetworkingConfigV2) GetRoutingStrategy() RoutingStrategy { 313 return c.Spec.RoutingStrategy 314 } 315 316 // SetRoutingStrategy sets the routing strategy setting. 317 func (c *ClusterNetworkingConfigV2) SetRoutingStrategy(strategy RoutingStrategy) { 318 c.Spec.RoutingStrategy = strategy 319 } 320 321 // GetTunnelStrategy gets the tunnel strategy type. 322 func (c *ClusterNetworkingConfigV2) GetTunnelStrategyType() (TunnelStrategyType, error) { 323 if c.Spec.TunnelStrategy == nil { 324 return "", trace.BadParameter("tunnel strategy is nil") 325 } 326 327 switch c.Spec.TunnelStrategy.Strategy.(type) { 328 case *TunnelStrategyV1_AgentMesh: 329 return AgentMesh, nil 330 case *TunnelStrategyV1_ProxyPeering: 331 return ProxyPeering, nil 332 } 333 334 return "", trace.BadParameter("unknown tunnel strategy type: %T", c.Spec.TunnelStrategy.Strategy) 335 } 336 337 // GetAgentMeshTunnelStrategy gets the agent mesh tunnel strategy. 338 func (c *ClusterNetworkingConfigV2) GetAgentMeshTunnelStrategy() *AgentMeshTunnelStrategy { 339 return c.Spec.TunnelStrategy.GetAgentMesh() 340 } 341 342 // GetProxyPeeringTunnelStrategy gets the proxy peering tunnel strategy. 343 func (c *ClusterNetworkingConfigV2) GetProxyPeeringTunnelStrategy() *ProxyPeeringTunnelStrategy { 344 return c.Spec.TunnelStrategy.GetProxyPeering() 345 } 346 347 // SetTunnelStrategy sets the tunnel strategy. 348 func (c *ClusterNetworkingConfigV2) SetTunnelStrategy(strategy *TunnelStrategyV1) { 349 c.Spec.TunnelStrategy = strategy 350 } 351 352 // CheckAndSetDefaults verifies the constraints for ClusterNetworkingConfig. 353 func (c *ClusterNetworkingConfigV2) CheckAndSetDefaults() error { 354 c.setStaticFields() 355 if err := c.Metadata.CheckAndSetDefaults(); err != nil { 356 return trace.Wrap(err) 357 } 358 359 // Make sure origin value is always set. 360 if c.Origin() == "" { 361 c.SetOrigin(OriginDynamic) 362 } 363 364 // Set the keep-alive interval and max missed keep-alives. 365 if c.Spec.KeepAliveInterval.Duration() == 0 { 366 c.Spec.KeepAliveInterval = NewDuration(defaults.KeepAliveInterval()) 367 } 368 if c.Spec.KeepAliveCountMax == 0 { 369 c.Spec.KeepAliveCountMax = int64(defaults.KeepAliveCountMax) 370 } 371 372 if c.Spec.TunnelStrategy == nil { 373 c.Spec.TunnelStrategy = &TunnelStrategyV1{ 374 Strategy: DefaultTunnelStrategy(), 375 } 376 } 377 if err := c.Spec.TunnelStrategy.CheckAndSetDefaults(); err != nil { 378 return trace.Wrap(err) 379 } 380 381 if c.Spec.AssistCommandExecutionWorkers < 0 { 382 return trace.BadParameter("command_execution_workers must be non-negative") 383 } else if c.Spec.AssistCommandExecutionWorkers == 0 { 384 c.Spec.AssistCommandExecutionWorkers = defaults.AssistCommandExecutionWorkers 385 } 386 387 return nil 388 } 389 390 // GetProxyPingInterval gets the proxy ping interval. 391 func (c *ClusterNetworkingConfigV2) GetProxyPingInterval() time.Duration { 392 return c.Spec.ProxyPingInterval.Duration() 393 } 394 395 // SetProxyPingInterval sets the proxy ping interval. 396 func (c *ClusterNetworkingConfigV2) SetProxyPingInterval(interval time.Duration) { 397 c.Spec.ProxyPingInterval = Duration(interval) 398 } 399 400 // GetAssistCommandExecutionWorkers gets the number of parallel command execution workers for Assist 401 func (c *ClusterNetworkingConfigV2) GetAssistCommandExecutionWorkers() int32 { 402 return c.Spec.AssistCommandExecutionWorkers 403 } 404 405 // SetAssistCommandExecutionWorkers sets the number of parallel command execution workers for Assist 406 func (c *ClusterNetworkingConfigV2) SetAssistCommandExecutionWorkers(n int32) { 407 c.Spec.AssistCommandExecutionWorkers = n 408 } 409 410 // GetCaseInsensitiveRouting gets the case-insensitive routing option. 411 func (c *ClusterNetworkingConfigV2) GetCaseInsensitiveRouting() bool { 412 return c.Spec.CaseInsensitiveRouting 413 } 414 415 // SetCaseInsensitiveRouting sets the case-insensitive routing option. 416 func (c *ClusterNetworkingConfigV2) SetCaseInsensitiveRouting(cir bool) { 417 c.Spec.CaseInsensitiveRouting = cir 418 } 419 420 // MarshalYAML defines how a proxy listener mode should be marshaled to a string 421 func (p ProxyListenerMode) MarshalYAML() (interface{}, error) { 422 return strings.ToLower(p.String()), nil 423 } 424 425 // UnmarshalYAML unmarshalls proxy listener mode from YAML value. 426 func (p *ProxyListenerMode) UnmarshalYAML(unmarshal func(interface{}) error) error { 427 var stringVar string 428 if err := unmarshal(&stringVar); err != nil { 429 return trace.Wrap(err) 430 } 431 for k, v := range ProxyListenerMode_value { 432 if strings.EqualFold(k, stringVar) { 433 *p = ProxyListenerMode(v) 434 return nil 435 } 436 } 437 438 available := make([]string, 0, len(ProxyListenerMode_value)) 439 for k := range ProxyListenerMode_value { 440 available = append(available, strings.ToLower(k)) 441 } 442 return trace.BadParameter( 443 "proxy listener mode must be one of %s; got %q", strings.Join(available, ","), stringVar) 444 } 445 446 // MarshalYAML defines how a routing strategy should be marshaled to a string 447 func (s RoutingStrategy) MarshalYAML() (interface{}, error) { 448 return strings.ToLower(s.String()), nil 449 } 450 451 // UnmarshalYAML unmarshalls routing strategy from YAML value. 452 func (s *RoutingStrategy) UnmarshalYAML(unmarshal func(interface{}) error) error { 453 var stringVar string 454 if err := unmarshal(&stringVar); err != nil { 455 return trace.Wrap(err) 456 } 457 458 for k, v := range RoutingStrategy_value { 459 if strings.EqualFold(k, stringVar) { 460 *s = RoutingStrategy(v) 461 return nil 462 } 463 } 464 465 available := make([]string, 0, len(RoutingStrategy_value)) 466 for k := range RoutingStrategy_value { 467 available = append(available, strings.ToLower(k)) 468 } 469 return trace.BadParameter( 470 "routing strategy must be one of %s; got %q", strings.Join(available, ","), stringVar) 471 }