Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2507,6 +2507,59 @@ func (c *Client) DeleteAllDatabaseServices(ctx context.Context) error {
return trail.FromGRPC(err)
}

// UpsertDiscoveredServer creates or updates existing DiscoveredServer resource.
func (c *Client) UpsertDiscoveredServer(ctx context.Context, service types.DiscoveredServer) (*types.KeepAlive, error) {
serverV1, ok := service.(*types.DiscoveredServerV1)
if !ok {
return nil, trace.BadParameter("unsupported DiscoveredServer type %T", serverV1)
}
keepAlive, err := c.grpc.UpsertDiscoveredServer(ctx, &types.UpsertDiscoveredServerRequest{
Server: serverV1,
}, c.callOpts...)

return keepAlive, trail.FromGRPC(err)
}

// GetDiscoveredServer gets a discovered server resource.
func (c *Client) GetDiscoveredServer(ctx context.Context, instanceID, accountID string) (*types.DiscoveredServerV1, error) {
resp, err := c.grpc.GetDiscoveredServer(ctx, &types.GetDiscoveredServerRequest{
InstanceID: instanceID,
AccountID: accountID,
})
if err != nil {
return nil, trail.FromGRPC(err)
}
return resp, nil
}

// GetDiscoveredServers gets all discovered server resources.
func (c *Client) GetDiscoveredServers(ctx context.Context) ([]*types.DiscoveredServerV1, error) {
resp, err := c.grpc.GetDiscoveredServers(ctx, &emptypb.Empty{})
if err != nil {
return nil, trail.FromGRPC(err)
}
return resp.Servers, nil
}

// DeleteDiscoveredServer deletes a discovered server resource.
func (c *Client) DeleteDiscoveredServer(ctx context.Context, instanceID, accountID string) error {
_, err := c.grpc.DeleteDiscoveredServer(ctx, &types.DeleteDiscoveredServerRequest{
InstanceID: instanceID,
AccountID: accountID,
})
if err != nil {
return trail.FromGRPC(err)
}
return nil
}

// DeleteAllDiscoveredServers deletes all DiscoveredServer resources.
// If an error occurs, a partial delete may happen.
func (c *Client) DeleteAllDiscoveredServers(ctx context.Context) error {
_, err := c.grpc.DeleteAllDiscoveredServers(ctx, &emptypb.Empty{}, c.callOpts...)
return trail.FromGRPC(err)
}

// GetWindowsDesktopServices returns all registered windows desktop services.
func (c *Client) GetWindowsDesktopServices(ctx context.Context) ([]types.WindowsDesktopService, error) {
resp, err := c.grpc.GetWindowsDesktopServices(ctx, &emptypb.Empty{}, c.callOpts...)
Expand Down
1,757 changes: 1,017 additions & 740 deletions api/client/proto/authservice.pb.go

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions api/proto/teleport/legacy/client/proto/authservice.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1631,6 +1631,8 @@ message PaginatedResource {
types.WindowsDesktopServiceV3 WindowsDesktopService = 8 [(gogoproto.jsontag) = "windows_desktop_service,omitempty"];
// DatabaseService represents a DatabaseService resource.
types.DatabaseServiceV1 DatabaseService = 9 [(gogoproto.jsontag) = "database_service,omitempty"];
// DiscoveredServer represents a DiscoveredServer resource.
types.DiscoveredServerV1 DiscoveredServer = 10 [(gogoproto.jsontag) = "discovered_server,omitempty"];
}
}

Expand Down Expand Up @@ -2257,6 +2259,17 @@ service AuthService {
// A subset of resources might be deleted while others still exist.
rpc DeleteAllDatabaseServices(DeleteAllDatabaseServicesRequest) returns (google.protobuf.Empty);

// UpsertDiscoveredServer creates a new DiscoveredServer resource.
rpc UpsertDiscoveredServer(types.UpsertDiscoveredServerRequest) returns (types.KeepAlive);
// GetDiscoveredServer returns a DiscoveredServer.
rpc GetDiscoveredServer(types.GetDiscoveredServerRequest) returns (types.DiscoveredServerV1);
// GetDiscoveredServers returns all DiscoveredServers.
rpc GetDiscoveredServers(google.protobuf.Empty) returns (types.GetDiscoveredServersResponse);
// DeleteDiscoveredServer deletes a DiscoveredServer resource.
rpc DeleteDiscoveredServer(types.DeleteDiscoveredServerRequest) returns (google.protobuf.Empty);
// DeleteAllDiscoveredServers deletes all discoveredServer resources.
rpc DeleteAllDiscoveredServers(google.protobuf.Empty) returns (google.protobuf.Empty);

// SignDatabaseCSR generates client certificate used by proxy to
// authenticate with a remote database service.
rpc SignDatabaseCSR(DatabaseCSRRequest) returns (DatabaseCSRResponse);
Expand Down
58 changes: 58 additions & 0 deletions api/proto/teleport/legacy/types/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ message KeepAlive {
DATABASE = 3;
WINDOWS_DESKTOP = 4;
KUBERNETES = 5;
DISCOVERED_SERVER = 6;
}
KeepAliveType Type = 9 [(gogoproto.jsontag) = "type"];
// HostID is an optional UUID of the host the resource belongs to.
Expand Down Expand Up @@ -4618,6 +4619,63 @@ message DatabaseResourceMatcher {
];
}

// DiscoveredServerV1 is the representation of a DiscoveredServer.
message DiscoveredServerV1 {
ResourceHeader Header = 1 [
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "",
(gogoproto.embed) = true
];
// Spec is the resource spec.
DiscoveredServerSpecV1 Spec = 2 [
(gogoproto.nullable) = false,
(gogoproto.jsontag) = "spec"
];
}

// DiscoveredServerSpecV1 is the DiscoveredServer Spec.
message DiscoveredServerSpecV1 {
// ResourceMatchers is the configured match for DiscoveredServer resources.
repeated DiscoveredServerResourceMatcher ResourceMatchers = 1 [(gogoproto.jsontag) = "resources"];

string InstanceID = 2 [(gogoproto.jsontag) = "instance_id"];
string AccountID = 3 [(gogoproto.jsontag) = "account_id"];

// Labels is an optional label selector.
map<string, string> Labels = 4 [(gogoproto.jsontag) = "labels"];
}

// DiscoveredServerMatcher is a set of properties that is used to match on resources.
message DiscoveredServerResourceMatcher {
wrappers.LabelValues Labels = 1 [
(gogoproto.jsontag) = "labels",
(gogoproto.customtype) = "Labels"
];
}

// UpsertDiscoveredServerRequest is a request to register a discovered server.
message UpsertDiscoveredServerRequest {
// Server is the discovered server to register.
DiscoveredServerV1 Server = 1 [(gogoproto.jsontag) = "server"];
}

// DeleteDiscoveredServerRequest is a request to delete a discovered server.
message DeleteDiscoveredServerRequest {
string InstanceID = 1 [(gogoproto.jsontag) = "instance_id"];
string AccountID = 2 [(gogoproto.jsontag) = "account_id"];
}

// GetDiscoveredserverRequest is a request to retrieve a discovered server.
message GetDiscoveredServerRequest {
string InstanceID = 1 [(gogoproto.jsontag) = "instance_id"];
string AccountID = 2 [(gogoproto.jsontag) = "account_id"];
}

// GetDiscoveredServersResponse is a request to retrieve all discovered servers.
message GetDiscoveredServersResponse {
repeated DiscoveredServerV1 Servers = 1 [(gogoproto.jsontag) = "servers"];
}

// AlertSeverity represents how problematic/urgent an alert is, and is used to assist
// in sorting alerts for display.
enum AlertSeverity {
Expand Down
9 changes: 9 additions & 0 deletions api/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ const (
// KindLoginRule is a login rule resource.
KindLoginRule = "login_rule"

// KindDisciveredServer is a discovered server resource.
KindDiscoveredServer = "discovered_server"

// MetaNameDiscoveredServer is the metadata name for discovered servers
MetaNameDiscoveredServer = "discovered-server"

// V6 is the sixth version of resources.
V6 = "v6"

Expand Down Expand Up @@ -526,6 +532,9 @@ const (
// the Node that used that token to join the cluster
InternalResourceIDLabel = TeleportInternalLabelPrefix + "resource-id"

// Label prefix used for labels imported from auto discovered ec2 isntances
AWSLabelPrefix = "aws/"

// AlertOnLogin is an internal label that indicates an alert should be displayed to users on login
AlertOnLogin = TeleportInternalLabelPrefix + "alert-on-login"

Expand Down
127 changes: 127 additions & 0 deletions api/types/discovered_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
Copyright 2023 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package types

import (
"github.com/gravitational/trace"

"github.com/gravitational/teleport/api/utils"
)

// DiscoveredServer represents a DiscoveredServer.
type DiscoveredServer interface {
// ResourceWithLabels provides common resource methods.
ResourceWithLabels

// GetNamespace returns the resource namespace.
GetNamespace() string

// GetInstanceID returns the instance id of the server.
GetInstanceID() string

// GetAccountID returns the account id of the server.
GetAccountID() string

// GetDiscoveredLabels returns the labels of the discovered server.
GetDiscoveredLabels() map[string]string

// GetResourceMatchers returns the resource matchers of the DiscoveredServer.
GetResourceMatchers() []*DiscoveredServerResourceMatcher
}

// NewDiscoveredServerV1 creates a new DiscoveredServer instance.
func NewDiscoveredServerV1(meta Metadata, spec DiscoveredServerSpecV1) (*DiscoveredServerV1, error) {
s := &DiscoveredServerV1{
ResourceHeader: ResourceHeader{
Metadata: meta,
},
Spec: spec,
}

if err := s.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return s, nil
}

func (s *DiscoveredServerV1) setStaticFields() {
s.Kind = KindDiscoveredServer
s.Version = V1
}

// CheckAndSetDefaults checks and sets default values for any missing fields.
func (s *DiscoveredServerV1) CheckAndSetDefaults() error {
s.setStaticFields()
return trace.Wrap(s.ResourceHeader.CheckAndSetDefaults())
}

// GetResourceMatchers returns the resource matchers of the DiscoveredServer.
func (s *DiscoveredServerV1) GetResourceMatchers() []*DiscoveredServerResourceMatcher {
return s.Spec.ResourceMatchers
}

// GetNamespace returns the resource namespace.
func (s *DiscoveredServerV1) GetNamespace() string {
return s.Metadata.Namespace
}

// GetInstanceID returns the instance ID.
func (s *DiscoveredServerV1) GetInstanceID() string {
return s.Spec.InstanceID
}

// GetAccountID returns the resource namespace.
func (s *DiscoveredServerV1) GetAccountID() string {
return s.Spec.AccountID
}

// GetDiscoveredLabels returns the labels of the discovered server.
func (s *DiscoveredServerV1) GetDiscoveredLabels() map[string]string {
return s.Spec.Labels
}

// GetAllLabels returns combined static and dynamic labels.
func (s *DiscoveredServerV1) GetAllLabels() map[string]string {
return s.Metadata.Labels
}

// GetStaticLabels returns the static labels.
func (s *DiscoveredServerV1) GetStaticLabels() map[string]string {
return s.Metadata.Labels
}

// SetStaticLabels sets the static labels.
func (s *DiscoveredServerV1) SetStaticLabels(sl map[string]string) {
s.Metadata.Labels = sl
}

// MatchSearch goes through select field values and tries to
// match against the list of search values.
func (s *DiscoveredServerV1) MatchSearch(values []string) bool {
fieldVals := append(utils.MapToStrings(s.GetAllLabels()), s.GetName())
return MatchSearch(fieldVals, values, nil)
}

// Origin returns the origin value of the resource.
func (s *DiscoveredServerV1) Origin() string {
return s.Metadata.Origin()
}

// SetOrigin sets the origin value of the resource.
func (s *DiscoveredServerV1) SetOrigin(origin string) {
s.Metadata.SetOrigin(origin)
}
13 changes: 13 additions & 0 deletions api/types/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,19 @@ func (r ResourcesWithLabels) AsKubeServers() ([]KubeServer, error) {
return servers, nil
}

// AsDiscoveredServer converts each resource into type DiscoveredServer.
func (r ResourcesWithLabels) AsDiscoveredServer() ([]DiscoveredServer, error) {
servers := make([]DiscoveredServer, len(r))
for i, resource := range r {
server, ok := resource.(DiscoveredServer)
if !ok {
return nil, trace.BadParameter("expected types.DiscoveredServer, got: %T", resource)
}
servers[i] = server
}
return servers, nil
}

// GetVersion returns resource version
func (h *ResourceHeader) GetVersion() string {
return h.Version
Expand Down
Loading