github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/tunnel.go (about) 1 /* 2 Copyright 2020 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 26 // ReverseTunnel is SSH reverse tunnel established between a local Proxy 27 // and a remote Proxy. It helps to bypass firewall restrictions, so local 28 // clusters don't need to have the cluster involved 29 type ReverseTunnel interface { 30 // Resource provides common methods for resource objects 31 Resource 32 // GetClusterName returns name of the cluster 33 GetClusterName() string 34 // SetClusterName sets cluster name 35 SetClusterName(name string) 36 // GetType gets the type of ReverseTunnel. 37 GetType() TunnelType 38 // SetType sets the type of ReverseTunnel. 39 SetType(TunnelType) 40 // GetDialAddrs returns list of dial addresses for this cluster 41 GetDialAddrs() []string 42 } 43 44 // NewReverseTunnel returns new version of reverse tunnel 45 func NewReverseTunnel(clusterName string, dialAddrs []string) (ReverseTunnel, error) { 46 r := &ReverseTunnelV2{ 47 Metadata: Metadata{ 48 Name: clusterName, 49 }, 50 Spec: ReverseTunnelSpecV2{ 51 ClusterName: clusterName, 52 DialAddrs: dialAddrs, 53 }, 54 } 55 if err := r.CheckAndSetDefaults(); err != nil { 56 return nil, trace.Wrap(err) 57 } 58 return r, nil 59 } 60 61 // GetVersion returns resource version 62 func (r *ReverseTunnelV2) GetVersion() string { 63 return r.Version 64 } 65 66 // GetKind returns resource kind 67 func (r *ReverseTunnelV2) GetKind() string { 68 return r.Kind 69 } 70 71 // GetSubKind returns resource sub kind 72 func (r *ReverseTunnelV2) GetSubKind() string { 73 return r.SubKind 74 } 75 76 // SetSubKind sets resource subkind 77 func (r *ReverseTunnelV2) SetSubKind(s string) { 78 r.SubKind = s 79 } 80 81 // GetResourceID returns resource ID 82 func (r *ReverseTunnelV2) GetResourceID() int64 { 83 return r.Metadata.ID 84 } 85 86 // SetResourceID sets resource ID 87 func (r *ReverseTunnelV2) SetResourceID(id int64) { 88 r.Metadata.ID = id 89 } 90 91 // GetRevision returns the revision 92 func (r *ReverseTunnelV2) GetRevision() string { 93 return r.Metadata.GetRevision() 94 } 95 96 // SetRevision sets the revision 97 func (r *ReverseTunnelV2) SetRevision(rev string) { 98 r.Metadata.SetRevision(rev) 99 } 100 101 // GetMetadata returns object metadata 102 func (r *ReverseTunnelV2) GetMetadata() Metadata { 103 return r.Metadata 104 } 105 106 // SetExpiry sets expiry time for the object 107 func (r *ReverseTunnelV2) SetExpiry(expires time.Time) { 108 r.Metadata.SetExpiry(expires) 109 } 110 111 // Expiry returns object expiry setting 112 func (r *ReverseTunnelV2) Expiry() time.Time { 113 return r.Metadata.Expiry() 114 } 115 116 // GetName returns the name of the User 117 func (r *ReverseTunnelV2) GetName() string { 118 return r.Metadata.Name 119 } 120 121 // SetName sets the name of the User 122 func (r *ReverseTunnelV2) SetName(e string) { 123 r.Metadata.Name = e 124 } 125 126 // setStaticFields sets static resource header and metadata fields. 127 func (r *ReverseTunnelV2) setStaticFields() { 128 r.Kind = KindReverseTunnel 129 r.Version = V2 130 } 131 132 // CheckAndSetDefaults checks and sets defaults 133 func (r *ReverseTunnelV2) CheckAndSetDefaults() error { 134 r.setStaticFields() 135 if err := r.Metadata.CheckAndSetDefaults(); err != nil { 136 return trace.Wrap(err) 137 } 138 139 if strings.TrimSpace(r.Spec.ClusterName) == "" { 140 return trace.BadParameter("reverse tunnel validation error: empty cluster name") 141 } 142 if len(r.Spec.DialAddrs) == 0 { 143 return trace.BadParameter("invalid dial address for reverse tunnel '%v'", r.Spec.ClusterName) 144 } 145 146 return nil 147 } 148 149 // SetClusterName sets name of a cluster 150 func (r *ReverseTunnelV2) SetClusterName(name string) { 151 r.Spec.ClusterName = name 152 } 153 154 // GetClusterName returns name of the cluster 155 func (r *ReverseTunnelV2) GetClusterName() string { 156 return r.Spec.ClusterName 157 } 158 159 // GetType gets the type of ReverseTunnel. 160 func (r *ReverseTunnelV2) GetType() TunnelType { 161 if string(r.Spec.Type) == "" { 162 return ProxyTunnel 163 } 164 return r.Spec.Type 165 } 166 167 // SetType sets the type of ReverseTunnel. 168 func (r *ReverseTunnelV2) SetType(tt TunnelType) { 169 r.Spec.Type = tt 170 } 171 172 // GetDialAddrs returns list of dial addresses for this cluster 173 func (r *ReverseTunnelV2) GetDialAddrs() []string { 174 return r.Spec.DialAddrs 175 }