-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
service_database_tunnel.go
82 lines (73 loc) · 2.64 KB
/
service_database_tunnel.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
/*
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package config
import (
"net/url"
"github.com/gravitational/trace"
"gopkg.in/yaml.v3"
)
const DatabaseTunnelServiceType = "database-tunnel"
// DatabaseTunnelService opens an authenticated tunnel for Database Access.
type DatabaseTunnelService struct {
// Listen is the address on which database tunnel should listen. Example:
// - "tcp://127.0.0.1:3306"
// - "tcp://0.0.0.0:3306
Listen string `yaml:"listen"`
// Roles is the list of roles to request for the tunnel.
// If empty, it defaults to all the bot's roles.
Roles []string `yaml:"roles,omitempty"`
// Service is the service name of the Teleport database. Generally this is
// the name of the Teleport resource. This field is required for all types
// of database.
Service string `yaml:"service"`
// Database is the name of the database to proxy to.
Database string `yaml:"database"`
// Username is the database username to proxy as.
Username string `yaml:"username"`
}
func (s *DatabaseTunnelService) Type() string {
return DatabaseTunnelServiceType
}
func (s *DatabaseTunnelService) MarshalYAML() (interface{}, error) {
type raw DatabaseTunnelService
return withTypeHeader((*raw)(s), DatabaseTunnelServiceType)
}
func (s *DatabaseTunnelService) UnmarshalYAML(node *yaml.Node) error {
// Alias type to remove UnmarshalYAML to avoid recursion
type raw DatabaseTunnelService
if err := node.Decode((*raw)(s)); err != nil {
return trace.Wrap(err)
}
return nil
}
func (s *DatabaseTunnelService) CheckAndSetDefaults() error {
switch {
case s.Listen == "":
return trace.BadParameter("listen: should not be empty")
case s.Service == "":
return trace.BadParameter("service: should not be empty")
case s.Database == "":
return trace.BadParameter("database: should not be empty")
case s.Username == "":
return trace.BadParameter("username: should not be empty")
}
if _, err := url.Parse(s.Listen); err != nil {
return trace.Wrap(err, "parsing listen")
}
return nil
}