Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 10 additions & 0 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (

"github.com/gravitational/teleport/api/breaker"
"github.com/gravitational/teleport/api/client/accesslist"
"github.com/gravitational/teleport/api/client/discoveryconfig"
"github.com/gravitational/teleport/api/client/okta"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/api/client/userloginstate"
Expand All @@ -52,6 +53,7 @@ import (
accesslistv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/accesslist/v1"
auditlogpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/auditlog/v1"
devicepb "github.com/gravitational/teleport/api/gen/proto/go/teleport/devicetrust/v1"
discoveryconfigv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/discoveryconfig/v1"
integrationpb "github.com/gravitational/teleport/api/gen/proto/go/teleport/integration/v1"
kubeproto "github.com/gravitational/teleport/api/gen/proto/go/teleport/kube/v1"
loginrulepb "github.com/gravitational/teleport/api/gen/proto/go/teleport/loginrule/v1"
Expand Down Expand Up @@ -4034,6 +4036,14 @@ func (c *Client) AccessListClient() *accesslist.Client {
return accesslist.NewClient(accesslistv1.NewAccessListServiceClient(c.conn))
}

// DiscoveryConfigClient returns a DiscoveryConfig client.
// Clients connecting to older Teleport versions, still get an DiscoveryConfig client
// when calling this method, but all RPCs will return "not implemented" errors
// (as per the default gRPC behavior).
func (c *Client) DiscoveryConfigClient() *discoveryconfig.Client {
return discoveryconfig.NewClient(discoveryconfigv1.NewDiscoveryConfigServiceClient(c.conn))
}

// UserLoginStateClient returns a user login state client.
// Clients connecting to older Teleport versions, still get a user login state client
// when calling this method, but all RPCs will return "not implemented" errors
Expand Down
111 changes: 111 additions & 0 deletions api/client/discoveryconfig/discoveryconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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 discoveryconfig

import (
"context"

"github.com/gravitational/trace"

discoveryconfigv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/discoveryconfig/v1"
"github.com/gravitational/teleport/api/types/discoveryconfig"
conv "github.com/gravitational/teleport/api/types/discoveryconfig/convert/v1"
)

// Client is an DiscoveryConfig client that conforms to the following lib/services interfaces:
// - services.DiscoveryConfigs
type Client struct {
grpcClient discoveryconfigv1.DiscoveryConfigServiceClient
}

// NewClient creates a new Discovery Config client.
func NewClient(grpcClient discoveryconfigv1.DiscoveryConfigServiceClient) *Client {
return &Client{
grpcClient: grpcClient,
}
}

// ListDiscoveryConfigs returns a paginated list of DiscoveryConfigs.
func (c *Client) ListDiscoveryConfigs(ctx context.Context, pageSize int, nextToken string) ([]*discoveryconfig.DiscoveryConfig, string, error) {
resp, err := c.grpcClient.ListDiscoveryConfigs(ctx, &discoveryconfigv1.ListDiscoveryConfigsRequest{
PageSize: int32(pageSize),
NextToken: nextToken,
})
if err != nil {
return nil, "", trace.Wrap(err)
}

discoveryConfigs := make([]*discoveryconfig.DiscoveryConfig, len(resp.DiscoveryConfigs))
for i, discoveryConfig := range resp.DiscoveryConfigs {
var err error
discoveryConfigs[i], err = conv.FromProto(discoveryConfig)
if err != nil {
return nil, "", trace.Wrap(err)
}
}

return discoveryConfigs, resp.GetNextKey(), nil
}

// GetDiscoveryConfig returns the specified DiscoveryConfig resource.
func (c *Client) GetDiscoveryConfig(ctx context.Context, name string) (*discoveryconfig.DiscoveryConfig, error) {
resp, err := c.grpcClient.GetDiscoveryConfig(ctx, &discoveryconfigv1.GetDiscoveryConfigRequest{
Name: name,
})
if err != nil {
return nil, trace.Wrap(err)
}

discoveryConfig, err := conv.FromProto(resp)
return discoveryConfig, trace.Wrap(err)
}

// CreateDiscoveryConfig creates the DiscoveryConfig.
func (c *Client) CreateDiscoveryConfig(ctx context.Context, discoveryConfig *discoveryconfig.DiscoveryConfig) (*discoveryconfig.DiscoveryConfig, error) {
resp, err := c.grpcClient.CreateDiscoveryConfig(ctx, &discoveryconfigv1.CreateDiscoveryConfigRequest{
DiscoveryConfig: conv.ToProto(discoveryConfig),
})
if err != nil {
return nil, trace.Wrap(err)
}
dc, err := conv.FromProto(resp)
return dc, trace.Wrap(err)
}

// UpdateDiscoveryConfig updates the DiscoveryConfig.
func (c *Client) UpdateDiscoveryConfig(ctx context.Context, discoveryConfig *discoveryconfig.DiscoveryConfig) (*discoveryconfig.DiscoveryConfig, error) {
resp, err := c.grpcClient.UpdateDiscoveryConfig(ctx, &discoveryconfigv1.UpdateDiscoveryConfigRequest{
DiscoveryConfig: conv.ToProto(discoveryConfig),
})
if err != nil {
return nil, trace.Wrap(err)
}
dc, err := conv.FromProto(resp)
return dc, trace.Wrap(err)
}

// DeleteDiscoveryConfig removes the specified DiscoveryConfig resource.
func (c *Client) DeleteDiscoveryConfig(ctx context.Context, name string) error {
_, err := c.grpcClient.DeleteDiscoveryConfig(ctx, &discoveryconfigv1.DeleteDiscoveryConfigRequest{
Name: name,
})
return trace.Wrap(err)
}

// DeleteAllDiscoveryConfigs removes all DiscoveryConfigs.
func (c *Client) DeleteAllDiscoveryConfigs(ctx context.Context) error {
_, err := c.grpcClient.DeleteAllDiscoveryConfigs(ctx, &discoveryconfigv1.DeleteAllDiscoveryConfigsRequest{})
return trace.Wrap(err)
}
4 changes: 4 additions & 0 deletions api/types/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,10 @@ const (
// KindAccessListMember is an AccessListMember resource
KindAccessListMember = "access_list_member"

// KindDiscoveryConfig is a DiscoveryConfig resource.
// Used for adding additional matchers in Discovery Service.
KindDiscoveryConfig = "discovery_config"

// V7 is the seventh version of resources.
V7 = "v7"

Expand Down
108 changes: 108 additions & 0 deletions api/types/discoveryconfig/convert/v1/discoveryconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
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 v1

import (
"github.com/gravitational/trace"

discoveryconfigv1 "github.com/gravitational/teleport/api/gen/proto/go/teleport/discoveryconfig/v1"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/api/types/discoveryconfig"
headerv1 "github.com/gravitational/teleport/api/types/header/convert/v1"
)

// FromProto converts a v1 discovery config into an internal discovery config object.
func FromProto(msg *discoveryconfigv1.DiscoveryConfig) (*discoveryconfig.DiscoveryConfig, error) {
if msg == nil {
return nil, trace.BadParameter("discovery config message is nil")
}

if msg.Spec == nil {
return nil, trace.BadParameter("spec is missing")
}
if msg.Spec.DiscoveryGroup == "" {
return nil, trace.BadParameter("discovery group is missing")
}

awsMatchers := make([]types.AWSMatcher, 0, len(msg.Spec.Aws))
for _, m := range msg.Spec.Aws {
awsMatchers = append(awsMatchers, *m)
}

azureMatchers := make([]types.AzureMatcher, 0, len(msg.Spec.Azure))
for _, m := range msg.Spec.Azure {
azureMatchers = append(azureMatchers, *m)
}

gcpMatchers := make([]types.GCPMatcher, 0, len(msg.Spec.Gcp))
for _, m := range msg.Spec.Gcp {
gcpMatchers = append(gcpMatchers, *m)
}

kubeMatchers := make([]types.KubernetesMatcher, 0, len(msg.Spec.Kube))
for _, m := range msg.Spec.Kube {
kubeMatchers = append(kubeMatchers, *m)
}

discoveryConfig, err := discoveryconfig.NewDiscoveryConfig(
headerv1.FromMetadataProto(msg.Header.Metadata),
discoveryconfig.Spec{
DiscoveryGroup: msg.Spec.DiscoveryGroup,
AWS: awsMatchers,
Azure: azureMatchers,
GCP: gcpMatchers,
Kube: kubeMatchers,
},
)

return discoveryConfig, trace.Wrap(err)
}

// ToProto converts an internal discovery config into a v1 discovery config object.
func ToProto(discoveryConfig *discoveryconfig.DiscoveryConfig) *discoveryconfigv1.DiscoveryConfig {
awsMatchers := make([]*types.AWSMatcher, 0, len(discoveryConfig.Spec.AWS))
for _, m := range discoveryConfig.Spec.AWS {
m := m
awsMatchers = append(awsMatchers, &m)
}

azureMatchers := make([]*types.AzureMatcher, 0, len(discoveryConfig.Spec.Azure))
for _, m := range discoveryConfig.Spec.Azure {
azureMatchers = append(azureMatchers, &m)
}

gcpMatchers := make([]*types.GCPMatcher, 0, len(discoveryConfig.Spec.GCP))
for _, m := range discoveryConfig.Spec.GCP {
gcpMatchers = append(gcpMatchers, &m)
}

kubeMatchers := make([]*types.KubernetesMatcher, 0, len(discoveryConfig.Spec.Kube))
for _, m := range discoveryConfig.Spec.Kube {
kubeMatchers = append(kubeMatchers, &m)
}

return &discoveryconfigv1.DiscoveryConfig{
Header: headerv1.ToResourceHeaderProto(discoveryConfig.ResourceHeader),
Spec: &discoveryconfigv1.DiscoveryConfigSpec{
DiscoveryGroup: discoveryConfig.GetDiscoveryGroup(),
Aws: awsMatchers,
Azure: azureMatchers,
Gcp: gcpMatchers,
Kube: kubeMatchers,
},
}
}
72 changes: 72 additions & 0 deletions api/types/discoveryconfig/convert/v1/discoveryconfig_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
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 v1

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"

"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/api/types/discoveryconfig"
"github.com/gravitational/teleport/api/types/header"
)

func TestRoundtrip(t *testing.T) {
discoveryConfig := newDiscoveryConfig(t, "discovery-config-01")

converted, err := FromProto(ToProto(discoveryConfig))
require.NoError(t, err)

require.Empty(t, cmp.Diff(discoveryConfig, converted))
}

// Make sure that we don't panic if any of the message fields are missing.
func TestFromProtoNils(t *testing.T) {
// Spec is nil
discoveryConfig := ToProto(newDiscoveryConfig(t, "discovery-config-01"))
discoveryConfig.Spec = nil

_, err := FromProto(discoveryConfig)
require.Error(t, err)
}

func newDiscoveryConfig(t *testing.T, name string) *discoveryconfig.DiscoveryConfig {
t.Helper()

discoveryConfig, err := discoveryconfig.NewDiscoveryConfig(
header.Metadata{
Name: name,
},
discoveryconfig.Spec{
DiscoveryGroup: "discovery-group-01",
AWS: []types.AWSMatcher{
{
Types: []string{"rds"},
Regions: []string{"us-west-2"},
},
{
Types: []string{"ec2"},
Regions: []string{"eu-west-2"},
},
},
},
)
require.NoError(t, err)
return discoveryConfig
}
Loading