forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_databases.go
95 lines (80 loc) · 2.77 KB
/
handler_databases.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
/*
* Teleport
* Copyright (C) 2023 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 handler
import (
"context"
"sort"
"github.com/gravitational/trace"
api "github.com/gravitational/teleport/gen/proto/go/teleport/lib/teleterm/v1"
"github.com/gravitational/teleport/lib/teleterm/clusters"
)
// GetDatabases gets databses with filters and returns paginated results
func (s *Handler) GetDatabases(ctx context.Context, req *api.GetDatabasesRequest) (*api.GetDatabasesResponse, error) {
cluster, _, err := s.DaemonService.ResolveCluster(req.ClusterUri)
if err != nil {
return nil, trace.Wrap(err)
}
resp, err := cluster.GetDatabases(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}
response := &api.GetDatabasesResponse{
StartKey: resp.StartKey,
TotalCount: int32(resp.TotalCount),
}
for _, database := range resp.Databases {
response.Agents = append(response.Agents, newAPIDatabase(database))
}
return response, nil
}
// ListDatabaseUsers is used to list database user suggestions when the user is attempting to
// establish a connection to a database through Teleterm.
//
// The list is based on whatever we can deduce from the role set, so it's similar to the behavior of
// `tsh db ls -v`, with the exception that Teleterm is interested only in the allowed usernames.
func (s *Handler) ListDatabaseUsers(ctx context.Context, req *api.ListDatabaseUsersRequest) (*api.ListDatabaseUsersResponse, error) {
cluster, _, err := s.DaemonService.ResolveCluster(req.DbUri)
if err != nil {
return nil, trace.Wrap(err)
}
dbUsers, err := cluster.GetAllowedDatabaseUsers(ctx, req.DbUri)
if err != nil {
return nil, trace.Wrap(err)
}
return &api.ListDatabaseUsersResponse{
Users: dbUsers,
}, nil
}
func newAPIDatabase(db clusters.Database) *api.Database {
apiLabels := APILabels{}
for name, value := range db.GetAllLabels() {
apiLabels = append(apiLabels, &api.Label{
Name: name,
Value: value,
})
}
sort.Sort(apiLabels)
return &api.Database{
Uri: db.URI.String(),
Name: db.GetName(),
Desc: db.GetDescription(),
Protocol: db.GetProtocol(),
Type: db.GetType(),
Labels: apiLabels,
}
}