Skip to content

Commit

Permalink
rename to DiscoveryConfigName and GetDiscoveryConfigName
Browse files Browse the repository at this point in the history
  • Loading branch information
marcoandredinis committed Dec 5, 2024
1 parent 5d4aa50 commit 26f8d4f
Show file tree
Hide file tree
Showing 22 changed files with 283 additions and 279 deletions.
4 changes: 2 additions & 2 deletions lib/srv/discovery/common/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ type Fetcher interface {
// IntegrationName identifies the integration name whose credentials were used to fetch the resources.
// Might be empty when the fetcher is using ambient credentials.
IntegrationName() string
// DiscoveryConfigName is the name of the discovery config which originated the resource.
// GetDiscoveryConfigName is the name of the discovery config which originated the resource.
// Might be empty when the fetcher is using static matchers:
// ie teleport.yaml/discovery_service.<cloud>.<matcher>
DiscoveryConfigName() string
GetDiscoveryConfigName() string
// Cloud returns the cloud the fetcher is operating.
Cloud() string
}
4 changes: 2 additions & 2 deletions lib/srv/discovery/common/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ func (w *Watcher) fetchAndSend() {
// Add the integration name to the static labels for each resource.
fetcherLabels[types.TeleportInternalDiscoveryIntegrationName] = lFetcher.IntegrationName()
}
if lFetcher.DiscoveryConfigName() != "" {
if lFetcher.GetDiscoveryConfigName() != "" {
// Add the discovery config name to the static labels of each resource.
fetcherLabels[types.TeleportInternalDiscoveryConfigName] = lFetcher.DiscoveryConfigName()
fetcherLabels[types.TeleportInternalDiscoveryConfigName] = lFetcher.GetDiscoveryConfigName()
}

if w.cfg.DiscoveryGroup != "" {
Expand Down
2 changes: 1 addition & 1 deletion lib/srv/discovery/common/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (m *mockFetcher) IntegrationName() string {
return ""
}

func (m *mockFetcher) DiscoveryConfigName() string {
func (m *mockFetcher) GetDiscoveryConfigName() string {
return ""
}
func (m *mockFetcher) Cloud() string {
Expand Down
4 changes: 2 additions & 2 deletions lib/srv/discovery/database_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *Server) startDatabaseWatchers() error {
discoveryConfigs := slices.FilterMapUnique(
s.getAllDatabaseFetchers(),
func(f common.Fetcher) (s string, skip bool) {
return f.DiscoveryConfigName(), f.DiscoveryConfigName() != ""
return f.GetDiscoveryConfigName(), f.GetDiscoveryConfigName() != ""
},
)
s.updateDiscoveryConfigStatus(discoveryConfigs...)
Expand Down Expand Up @@ -108,7 +108,7 @@ func (s *Server) startDatabaseWatchers() error {

resourceGroup := awsResourceGroupFromLabels(db.GetStaticLabels())
resourcesFoundByGroup[resourceGroup] += 1
discoveryConfigsChanged[resourceGroup.discoveryConfig] = struct{}{}
discoveryConfigsChanged[resourceGroup.discoveryConfigName] = struct{}{}

dbs = append(dbs, db)
}
Expand Down
91 changes: 46 additions & 45 deletions lib/srv/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ func (s *Server) initAWSWatchers(matchers []types.AWSMatcher) error {
discoveryConfigs := libslices.FilterMapUnique(
s.getAllAWSServerFetchers(),
func(f server.Fetcher) (s string, skip bool) {
return f.GetDiscoveryConfig(), f.GetDiscoveryConfig() != ""
return f.GetDiscoveryConfigName(), f.GetDiscoveryConfigName() != ""
},
)
s.updateDiscoveryConfigStatus(discoveryConfigs...)
Expand Down Expand Up @@ -613,12 +613,12 @@ func (s *Server) initKubeAppWatchers(matchers []types.KubernetesMatcher) error {
}

// awsServerFetchersFromMatchers converts Matchers into a set of AWS EC2 Fetchers.
func (s *Server) awsServerFetchersFromMatchers(ctx context.Context, matchers []types.AWSMatcher, discoveryConfig string) ([]server.Fetcher, error) {
func (s *Server) awsServerFetchersFromMatchers(ctx context.Context, matchers []types.AWSMatcher, discoveryConfigName string) ([]server.Fetcher, error) {
serverMatchers, _ := splitMatchers(matchers, func(matcherType string) bool {
return matcherType == types.AWSMatcherEC2
})

fetchers, err := server.MatchersToEC2InstanceFetchers(ctx, serverMatchers, s.GetEC2Client, discoveryConfig)
fetchers, err := server.MatchersToEC2InstanceFetchers(ctx, serverMatchers, s.GetEC2Client, discoveryConfigName)
if err != nil {
return nil, trace.Wrap(err)
}
Expand All @@ -627,16 +627,16 @@ func (s *Server) awsServerFetchersFromMatchers(ctx context.Context, matchers []t
}

// azureServerFetchersFromMatchers converts Matchers into a set of Azure Servers Fetchers.
func (s *Server) azureServerFetchersFromMatchers(matchers []types.AzureMatcher, discoveryConfig string) []server.Fetcher {
func (s *Server) azureServerFetchersFromMatchers(matchers []types.AzureMatcher, discoveryConfigName string) []server.Fetcher {
serverMatchers, _ := splitMatchers(matchers, func(matcherType string) bool {
return matcherType == types.AzureMatcherVM
})

return server.MatchersToAzureInstanceFetchers(serverMatchers, s.CloudClients, discoveryConfig)
return server.MatchersToAzureInstanceFetchers(serverMatchers, s.CloudClients, discoveryConfigName)
}

// gcpServerFetchersFromMatchers converts Matchers into a set of GCP Servers Fetchers.
func (s *Server) gcpServerFetchersFromMatchers(ctx context.Context, matchers []types.GCPMatcher, discoveryConfig string) ([]server.Fetcher, error) {
func (s *Server) gcpServerFetchersFromMatchers(ctx context.Context, matchers []types.GCPMatcher, discoveryConfigName string) ([]server.Fetcher, error) {
serverMatchers, _ := splitMatchers(matchers, func(matcherType string) bool {
return matcherType == types.GCPMatcherCompute
})
Expand All @@ -657,17 +657,17 @@ func (s *Server) gcpServerFetchersFromMatchers(ctx context.Context, matchers []t
return nil, trace.Wrap(err)
}

return server.MatchersToGCPInstanceFetchers(serverMatchers, client, projectsClient, discoveryConfig), nil
return server.MatchersToGCPInstanceFetchers(serverMatchers, client, projectsClient, discoveryConfigName), nil
}

// databaseFetchersFromMatchers converts Matchers into a set of Database Fetchers.
func (s *Server) databaseFetchersFromMatchers(matchers Matchers, discoveryConfig string) ([]common.Fetcher, error) {
func (s *Server) databaseFetchersFromMatchers(matchers Matchers, discoveryConfigName string) ([]common.Fetcher, error) {
var fetchers []common.Fetcher

// AWS
awsDatabaseMatchers, _ := splitMatchers(matchers.AWS, db.IsAWSMatcherType)
if len(awsDatabaseMatchers) > 0 {
databaseFetchers, err := db.MakeAWSFetchers(s.ctx, s.CloudClients, awsDatabaseMatchers, discoveryConfig)
databaseFetchers, err := db.MakeAWSFetchers(s.ctx, s.CloudClients, awsDatabaseMatchers, discoveryConfigName)
if err != nil {
return nil, trace.Wrap(err)
}
Expand All @@ -677,7 +677,7 @@ func (s *Server) databaseFetchersFromMatchers(matchers Matchers, discoveryConfig
// Azure
azureDatabaseMatchers, _ := splitMatchers(matchers.Azure, db.IsAzureMatcherType)
if len(azureDatabaseMatchers) > 0 {
databaseFetchers, err := db.MakeAzureFetchers(s.CloudClients, azureDatabaseMatchers, discoveryConfig)
databaseFetchers, err := db.MakeAzureFetchers(s.CloudClients, azureDatabaseMatchers, discoveryConfigName)
if err != nil {
return nil, trace.Wrap(err)
}
Expand All @@ -690,15 +690,15 @@ func (s *Server) databaseFetchersFromMatchers(matchers Matchers, discoveryConfig
return fetchers, nil
}

func (s *Server) kubeFetchersFromMatchers(matchers Matchers, discoveryConfig string) ([]common.Fetcher, error) {
func (s *Server) kubeFetchersFromMatchers(matchers Matchers, discoveryConfigName string) ([]common.Fetcher, error) {
var result []common.Fetcher

// AWS
awsKubeMatchers, _ := splitMatchers(matchers.AWS, func(matcherType string) bool {
return matcherType == types.AWSMatcherEKS
})
if len(awsKubeMatchers) > 0 {
eksFetchers, err := fetchers.MakeEKSFetchersFromAWSMatchers(s.LegacyLogger, s.CloudClients, awsKubeMatchers, discoveryConfig)
eksFetchers, err := fetchers.MakeEKSFetchersFromAWSMatchers(s.LegacyLogger, s.CloudClients, awsKubeMatchers, discoveryConfigName)
if err != nil {
return nil, trace.Wrap(err)
}
Expand All @@ -711,12 +711,12 @@ func (s *Server) kubeFetchersFromMatchers(matchers Matchers, discoveryConfig str
}

// initAzureWatchers starts Azure resource watchers based on types provided.
func (s *Server) initAzureWatchers(ctx context.Context, matchers []types.AzureMatcher, discoveryConfig string) error {
func (s *Server) initAzureWatchers(ctx context.Context, matchers []types.AzureMatcher, discoveryConfigName string) error {
vmMatchers, otherMatchers := splitMatchers(matchers, func(matcherType string) bool {
return matcherType == types.AzureMatcherVM
})

s.staticServerAzureFetchers = server.MatchersToAzureInstanceFetchers(vmMatchers, s.CloudClients, discoveryConfig)
s.staticServerAzureFetchers = server.MatchersToAzureInstanceFetchers(vmMatchers, s.CloudClients, discoveryConfigName)

// VM watcher.
var err error
Expand All @@ -728,7 +728,7 @@ func (s *Server) initAzureWatchers(ctx context.Context, matchers []types.AzureMa
discoveryConfigs := libslices.FilterMapUnique(
s.getAllAzureServerFetchers(),
func(f server.Fetcher) (s string, skip bool) {
return f.GetDiscoveryConfig(), f.GetDiscoveryConfig() != ""
return f.GetDiscoveryConfigName(), f.GetDiscoveryConfigName() != ""
},
)
s.updateDiscoveryConfigStatus(discoveryConfigs...)
Expand Down Expand Up @@ -761,11 +761,12 @@ func (s *Server) initAzureWatchers(ctx context.Context, matchers []types.AzureMa
return trace.Wrap(err)
}
fetcher, err := fetchers.NewAKSFetcher(fetchers.AKSFetcherConfig{
Client: kubeClient,
Regions: matcher.Regions,
FilterLabels: matcher.ResourceTags,
ResourceGroups: matcher.ResourceGroups,
Log: s.LegacyLogger,
Client: kubeClient,
Regions: matcher.Regions,
FilterLabels: matcher.ResourceTags,
ResourceGroups: matcher.ResourceGroups,
Log: s.LegacyLogger,
DiscoveryConfigName: discoveryConfigName,
})
if err != nil {
return trace.Wrap(err)
Expand All @@ -778,10 +779,10 @@ func (s *Server) initAzureWatchers(ctx context.Context, matchers []types.AzureMa
return nil
}

func (s *Server) initGCPServerWatcher(ctx context.Context, vmMatchers []types.GCPMatcher, discoveryConfig string) error {
func (s *Server) initGCPServerWatcher(ctx context.Context, vmMatchers []types.GCPMatcher, discoveryConfigName string) error {
var err error

s.staticServerGCPFetchers, err = s.gcpServerFetchersFromMatchers(ctx, vmMatchers, discoveryConfig)
s.staticServerGCPFetchers, err = s.gcpServerFetchersFromMatchers(ctx, vmMatchers, discoveryConfigName)
if err != nil {
return trace.Wrap(err)
}
Expand All @@ -794,7 +795,7 @@ func (s *Server) initGCPServerWatcher(ctx context.Context, vmMatchers []types.GC
discoveryConfigs := libslices.FilterMapUnique(
s.getAllGCPServerFetchers(),
func(f server.Fetcher) (s string, skip bool) {
return f.GetDiscoveryConfig(), f.GetDiscoveryConfig() != ""
return f.GetDiscoveryConfigName(), f.GetDiscoveryConfigName() != ""
},
)
s.updateDiscoveryConfigStatus(discoveryConfigs...)
Expand All @@ -814,15 +815,15 @@ func (s *Server) initGCPServerWatcher(ctx context.Context, vmMatchers []types.GC
}

// initGCPWatchers starts GCP resource watchers based on types provided.
func (s *Server) initGCPWatchers(ctx context.Context, matchers []types.GCPMatcher, discoveryConfig string) error {
func (s *Server) initGCPWatchers(ctx context.Context, matchers []types.GCPMatcher, discoveryConfigName string) error {
// return early if there are no matchers as GetGCPGKEClient causes
// an error if there are no credentials present

vmMatchers, otherMatchers := splitMatchers(matchers, func(matcherType string) bool {
return matcherType == types.GCPMatcherCompute
})

if err := s.initGCPServerWatcher(ctx, vmMatchers, discoveryConfig); err != nil {
if err := s.initGCPServerWatcher(ctx, vmMatchers, discoveryConfigName); err != nil {
return trace.Wrap(err)
}

Expand Down Expand Up @@ -953,8 +954,8 @@ func (s *Server) handleEC2Instances(instances *server.EC2Instances) error {

instancesAlreadyEnrolled := totalInstancesFound - len(instances.Instances)
s.awsEC2ResourcesStatus.incrementEnrolled(awsResourceGroup{
discoveryConfig: instances.DiscoveryConfig,
integration: instances.Integration,
discoveryConfigName: instances.DiscoveryConfigName,
integration: instances.Integration,
}, instancesAlreadyEnrolled)

if len(instances.Instances) == 0 {
Expand Down Expand Up @@ -996,8 +997,8 @@ func (s *Server) heartbeatEICEInstance(instances *server.EC2Instances) {
s.Log.WarnContext(s.ctx, "Error converting to Teleport EICE Node", "error", err, "instance_id", ec2Instance.InstanceID)

s.awsEC2ResourcesStatus.incrementFailed(awsResourceGroup{
discoveryConfig: instances.DiscoveryConfig,
integration: instances.Integration,
discoveryConfigName: instances.DiscoveryConfigName,
integration: instances.Integration,
}, 1)
continue
}
Expand Down Expand Up @@ -1050,8 +1051,8 @@ func (s *Server) heartbeatEICEInstance(instances *server.EC2Instances) {
instanceID := eiceNode.GetAWSInstanceID()
s.Log.WarnContext(s.ctx, "Error upserting EC2 instance", "instance_id", instanceID, "error", err)
s.awsEC2ResourcesStatus.incrementFailed(awsResourceGroup{
discoveryConfig: instances.DiscoveryConfig,
integration: instances.Integration,
discoveryConfigName: instances.DiscoveryConfigName,
integration: instances.Integration,
}, 1)
}
})
Expand All @@ -1073,19 +1074,19 @@ func (s *Server) handleEC2RemoteInstallation(instances *server.EC2Instances) err
s.Log.DebugContext(s.ctx, "Running Teleport installation on instances", "account_id", instances.AccountID, "instances", genEC2InstancesLogStr(instances.Instances))

req := server.SSMRunRequest{
DocumentName: instances.DocumentName,
SSM: ssmClient,
Instances: instances.Instances,
Params: instances.Parameters,
Region: instances.Region,
AccountID: instances.AccountID,
IntegrationName: instances.Integration,
DiscoveryConfig: instances.DiscoveryConfig,
DocumentName: instances.DocumentName,
SSM: ssmClient,
Instances: instances.Instances,
Params: instances.Parameters,
Region: instances.Region,
AccountID: instances.AccountID,
IntegrationName: instances.Integration,
DiscoveryConfigName: instances.DiscoveryConfigName,
}
if err := s.ec2Installer.Run(s.ctx, req); err != nil {
s.awsEC2ResourcesStatus.incrementFailed(awsResourceGroup{
discoveryConfig: instances.DiscoveryConfig,
integration: instances.Integration,
discoveryConfigName: instances.DiscoveryConfigName,
integration: instances.Integration,
}, len(req.Instances))

for _, instance := range req.Instances {
Expand All @@ -1099,7 +1100,7 @@ func (s *Server) handleEC2RemoteInstallation(instances *server.EC2Instances) err
installerScript: req.InstallerScriptName(),
},
&usertasksv1.DiscoverEC2Instance{
DiscoveryConfig: instances.DiscoveryConfig,
DiscoveryConfig: instances.DiscoveryConfigName,
DiscoveryGroup: s.DiscoveryGroup,
InstanceId: instance.InstanceID,
Name: instance.InstanceName,
Expand Down Expand Up @@ -1218,15 +1219,15 @@ func (s *Server) handleEC2Discovery() {
s.Log.DebugContext(s.ctx, "EC2 instances discovered, starting installation", "account_id", ec2Instances.AccountID, "instances", genEC2InstancesLogStr(ec2Instances.Instances))

s.awsEC2ResourcesStatus.incrementFound(awsResourceGroup{
discoveryConfig: instances.EC2.DiscoveryConfig,
integration: instances.EC2.Integration,
discoveryConfigName: instances.EC2.DiscoveryConfigName,
integration: instances.EC2.Integration,
}, len(instances.EC2.Instances))

if err := s.handleEC2Instances(ec2Instances); err != nil {
s.logHandleInstancesErr(err)
}

s.updateDiscoveryConfigStatus(instances.EC2.DiscoveryConfig)
s.updateDiscoveryConfigStatus(instances.EC2.DiscoveryConfigName)
s.upsertTasksForAWSEC2FailedEnrollments()
case <-s.ctx.Done():
s.ec2Watcher.Stop()
Expand Down
22 changes: 11 additions & 11 deletions lib/srv/discovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1852,18 +1852,18 @@ func TestDiscoveryDatabase(t *testing.T) {
)
awsRedshiftResource, awsRedshiftDB := makeRedshiftCluster(t, "aws-redshift", "us-east-1", rewriteDiscoveryLabelsParams{discoveryGroup: mainDiscoveryGroup})
_, awsRedshiftDBWithIntegration := makeRedshiftCluster(t, "aws-redshift", "us-east-1", rewriteDiscoveryLabelsParams{discoveryGroup: mainDiscoveryGroup, integration: integrationName})
_, awsRedshiftDBWithIntegrationAndDiscoveryConfig := makeRedshiftCluster(t, "aws-redshift", "us-east-1", rewriteDiscoveryLabelsParams{discoveryGroup: mainDiscoveryGroup, integration: integrationName, discoveryConfig: discoveryConfigName})
_, awsRedshiftDBWithDiscoveryConfig := makeRedshiftCluster(t, "aws-redshift", "us-east-1", rewriteDiscoveryLabelsParams{discoveryGroup: mainDiscoveryGroup, discoveryConfig: discoveryConfigName})
_, awsRedshiftDBWithIntegrationAndDiscoveryConfig := makeRedshiftCluster(t, "aws-redshift", "us-east-1", rewriteDiscoveryLabelsParams{discoveryGroup: mainDiscoveryGroup, integration: integrationName, discoveryConfigName: discoveryConfigName})
_, awsRedshiftDBWithDiscoveryConfig := makeRedshiftCluster(t, "aws-redshift", "us-east-1", rewriteDiscoveryLabelsParams{discoveryGroup: mainDiscoveryGroup, discoveryConfigName: discoveryConfigName})
awsRDSInstance, awsRDSDB := makeRDSInstance(t, "aws-rds", "us-west-1", rewriteDiscoveryLabelsParams{discoveryGroup: mainDiscoveryGroup})
azRedisResource, azRedisDB := makeAzureRedisServer(t, "az-redis", "sub1", "group1", "East US", rewriteDiscoveryLabelsParams{discoveryGroup: mainDiscoveryGroup})
_, azRedisDBWithDiscoveryConfig := makeAzureRedisServer(t, "az-redis", "sub1", "group1", "East US", rewriteDiscoveryLabelsParams{discoveryGroup: mainDiscoveryGroup, discoveryConfig: discoveryConfigName})
_, azRedisDBWithDiscoveryConfig := makeAzureRedisServer(t, "az-redis", "sub1", "group1", "East US", rewriteDiscoveryLabelsParams{discoveryGroup: mainDiscoveryGroup, discoveryConfigName: discoveryConfigName})

role := types.AssumeRole{RoleARN: "arn:aws:iam::123456789012:role/test-role", ExternalID: "test123"}
awsRDSDBWithRole := awsRDSDB.Copy()
awsRDSDBWithRole.SetAWSAssumeRole("arn:aws:iam::123456789012:role/test-role")
awsRDSDBWithRole.SetAWSExternalID("test123")

eksAWSResource, _ := makeEKSCluster(t, "aws-eks", "us-east-1", rewriteDiscoveryLabelsParams{discoveryGroup: mainDiscoveryGroup, integration: integrationName, discoveryConfig: discoveryConfigName})
eksAWSResource, _ := makeEKSCluster(t, "aws-eks", "us-east-1", rewriteDiscoveryLabelsParams{discoveryGroup: mainDiscoveryGroup, integration: integrationName, discoveryConfigName: discoveryConfigName})

matcherForDiscoveryConfigFn := func(t *testing.T, discoveryGroup string, m Matchers) *discoveryconfig.DiscoveryConfig {
dc, err := discoveryconfig.NewDiscoveryConfig(
Expand Down Expand Up @@ -2291,7 +2291,7 @@ func TestDiscoveryDatabaseRemovingDiscoveryConfigs(t *testing.T) {
dc1Name := uuid.NewString()
dc2Name := uuid.NewString()

awsRDSInstance, awsRDSDB := makeRDSInstance(t, "aws-rds", "us-west-1", rewriteDiscoveryLabelsParams{discoveryConfig: dc2Name, discoveryGroup: mainDiscoveryGroup})
awsRDSInstance, awsRDSDB := makeRDSInstance(t, "aws-rds", "us-west-1", rewriteDiscoveryLabelsParams{discoveryConfigName: dc2Name, discoveryGroup: mainDiscoveryGroup})

testCloudClients := &cloud.TestCloudClients{
STS: &mocks.STSMock{},
Expand Down Expand Up @@ -3449,10 +3449,10 @@ func (m fakeWatcher) Error() error {
}

type rewriteDiscoveryLabelsParams struct {
matcherType string
discoveryConfig string
discoveryGroup string
integration string
matcherType string
discoveryConfigName string
discoveryGroup string
integration string
}

// rewriteCloudResource is a test helper func that rewrites an expected cloud
Expand All @@ -3463,8 +3463,8 @@ func rewriteCloudResource(t *testing.T, r types.ResourceWithLabels, discoveryPar
if discoveryParams.matcherType != "" {
staticLabels[types.DiscoveryTypeLabel] = discoveryParams.matcherType
}
if discoveryParams.discoveryConfig != "" {
staticLabels[types.TeleportInternalDiscoveryConfigName] = discoveryParams.discoveryConfig
if discoveryParams.discoveryConfigName != "" {
staticLabels[types.TeleportInternalDiscoveryConfigName] = discoveryParams.discoveryConfigName
}
if discoveryParams.discoveryGroup != "" {
staticLabels[types.TeleportInternalDiscoveryGroupName] = discoveryParams.discoveryGroup
Expand Down
Loading

0 comments on commit 26f8d4f

Please sign in to comment.