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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/ec2 v1.142.1
github.com/aws/aws-sdk-go-v2/service/ec2instanceconnect v1.20.6
github.com/aws/aws-sdk-go-v2/service/ecs v1.36.0
github.com/aws/aws-sdk-go-v2/service/eks v1.37.1
github.com/aws/aws-sdk-go-v2/service/glue v1.73.1
github.com/aws/aws-sdk-go-v2/service/iam v1.28.7
github.com/aws/aws-sdk-go-v2/service/rds v1.66.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ github.com/aws/aws-sdk-go-v2/service/ec2instanceconnect v1.20.6 h1:Y0pqdpafA8TdG
github.com/aws/aws-sdk-go-v2/service/ec2instanceconnect v1.20.6/go.mod h1:y6fUhf01cjz+VUz+zrmJh3KfIXhefV7dS4STCxgHx7g=
github.com/aws/aws-sdk-go-v2/service/ecs v1.36.0 h1:XjN5jaDmvP0fDGEOn/Ws06wNKNXUAPGLdeBhKUetQcc=
github.com/aws/aws-sdk-go-v2/service/ecs v1.36.0/go.mod h1:kt+L4lMA2nvv9evq9S6TOH1up95/2RsQG4GXfxoPRfM=
github.com/aws/aws-sdk-go-v2/service/eks v1.37.1 h1:5eFw5vlZI2KOChY0DOWxsnuC6N01WC3ZUo5+lco9mN8=
github.com/aws/aws-sdk-go-v2/service/eks v1.37.1/go.mod h1:0R62cZb66e+iaJU7jG3GQbenxD8B7kh4UFNZ19pauTA=
github.com/aws/aws-sdk-go-v2/service/glue v1.73.1 h1:z/NBYW8RygzWrDgNWib10fuLUBl0SLj0KruGoEHxnKQ=
github.com/aws/aws-sdk-go-v2/service/glue v1.73.1/go.mod h1:F3B9DC5FsIHAxUtHZdY5KUeqN+tHoGlRPzSSYdXjC38=
github.com/aws/aws-sdk-go-v2/service/iam v1.28.7 h1:FKPRDYZOO0Eur19vWUL1B40Op0j89KQj3kARjrszMK8=
Expand Down
22 changes: 10 additions & 12 deletions lib/auth/auth_with_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ import (
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
Expand Down Expand Up @@ -2041,11 +2039,11 @@ func TestKubernetesClusterCRUD_DiscoveryService(t *testing.T) {
discoveryClt, err := srv.NewClient(TestBuiltin(types.RoleDiscovery))
require.NoError(t, err)

eksCluster, err := services.NewKubeClusterFromAWSEKS(&eks.Cluster{
Name: aws.String("eks-cluster1"),
Arn: aws.String("arn:aws:eks:eu-west-1:accountID:cluster/cluster1"),
Status: aws.String(eks.ClusterStatusActive),
})
eksCluster, err := services.NewKubeClusterFromAWSEKS(
"eks-cluster1",
"arn:aws:eks:eu-west-1:accountID:cluster/cluster1",
nil,
)
require.NoError(t, err)
eksCluster.SetOrigin(types.OriginCloud)

Expand All @@ -2061,11 +2059,11 @@ func TestKubernetesClusterCRUD_DiscoveryService(t *testing.T) {
require.NoError(t, srv.Auth().CreateKubernetesCluster(ctx, nonCloudCluster))

// Discovery service cannot create cluster with dynamic labels.
clusterWithDynamicLabels, err := services.NewKubeClusterFromAWSEKS(&eks.Cluster{
Name: aws.String("eks-cluster2"),
Arn: aws.String("arn:aws:eks:eu-west-1:accountID:cluster/cluster2"),
Status: aws.String(eks.ClusterStatusActive),
})
clusterWithDynamicLabels, err := services.NewKubeClusterFromAWSEKS(
"eks-cluster2",
"arn:aws:eks:eu-west-1:accountID:cluster/cluster2",
nil,
)
require.NoError(t, err)
clusterWithDynamicLabels.SetOrigin(types.OriginCloud)
clusterWithDynamicLabels.SetDynamicLabels(map[string]types.CommandLabel{
Expand Down
12 changes: 11 additions & 1 deletion lib/integrations/awsoidc/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2instanceconnect"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/eks"
"github.com/aws/aws-sdk-go-v2/service/rds"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/gravitational/trace"
Expand Down Expand Up @@ -95,6 +96,15 @@ func newAWSConfig(ctx context.Context, req *AWSClientRequest) (*aws.Config, erro
return &cfg, nil
}

func newEKSClient(ctx context.Context, req *AWSClientRequest) (*eks.Client, error) {
cfg, err := newAWSConfig(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}

return eks.NewFromConfig(*cfg), nil
}

// newRDSClient creates an [rds.Client] using the provided Token, RoleARN and Region.
func newRDSClient(ctx context.Context, req *AWSClientRequest) (*rds.Client, error) {
cfg, err := newAWSConfig(ctx, req)
Expand Down Expand Up @@ -145,7 +155,7 @@ func newEC2InstanceConnectClient(ctx context.Context, req *AWSClientRequest) (*e
return ec2instanceconnect.NewFromConfig(*cfg), nil
}

// newAWSCredentialsProvider creates an [aws.CredentialsRetriever] using the provided Token, RoleARN and Region.
// newAWSCredentialsProvider creates an [aws.CredentialsProvider] using the provided Token, RoleARN and Region.
func newAWSCredentialsProvider(ctx context.Context, req *AWSClientRequest) (aws.CredentialsProvider, error) {
cfg, err := newAWSConfig(ctx, req)
if err != nil {
Expand Down
185 changes: 185 additions & 0 deletions lib/integrations/awsoidc/eks_list_clusters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
/*
* 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 awsoidc

import (
"context"
"strings"
"sync"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/eks"
eksTypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/gravitational/trace"
"golang.org/x/sync/errgroup"

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

// ListEKSClustersRequest contains the required fields to list AWS EKS Clusters.
type ListEKSClustersRequest struct {
// Region is the AWS Region.
Region string

// NextToken is the token to be used to fetch the next page.
// If empty, the first page is fetched.
NextToken string
}

// CheckAndSetDefaults checks if the required fields are present.
func (req *ListEKSClustersRequest) CheckAndSetDefaults() error {
if req.Region == "" {
return trace.BadParameter("region is required")
}

return nil
}

// EKSCluster represents a cluster in AWS EKS.
type EKSCluster struct {
// Name is the name of AWS EKS cluster.
Name string

// Region is an AWS region.
Region string

// Labels are labels of a EKS cluster.
Labels map[string]string

// JoinLabels are Teleport labels that should be injected into kube agent
// if the cluster will be enrolled into Teleport (agent installed on it).
JoinLabels map[string]string

// Status is a current status of an EKS cluster in AWS.
Status string
}

// ListEKSClustersResponse contains a page of AWS EKS Clusters.
type ListEKSClustersResponse struct {
// Servers contains the page of Servers.
Clusters []EKSCluster

// NextToken is used for pagination.
// If non-empty, it can be used to request the next page.
NextToken string

// ClusterFetchingErrors contains errors for fetching detailed information about specific cluster, if any happened.
ClusterFetchingErrors map[string]error
}

// NewListEKSClustersClient creates a new ListEKSClusters client using AWSClientRequest.
func NewListEKSClustersClient(ctx context.Context, req *AWSClientRequest) (ListEKSClustersClient, error) {
clt, err := newEKSClient(ctx, req)
return clt, trace.Wrap(err)
}

// ListEKSClustersClient describes the required methods to List EKS clusters using a 3rd Party API.
type ListEKSClustersClient interface {
// ListClusters lists the EKS clusters.
ListClusters(ctx context.Context, params *eks.ListClustersInput, optFns ...func(*eks.Options)) (*eks.ListClustersOutput, error)

// DescribeCluster returns detailed information about an EKS cluster.
DescribeCluster(ctx context.Context, params *eks.DescribeClusterInput, optFns ...func(*eks.Options)) (*eks.DescribeClusterOutput, error)
}

// concurrentEKSFetchingLimit is a limit of how many clusters we are trying to describe concurrently, after receiving a list of clusters.
const concurrentEKSFetchingLimit = 5

// ListEKSClusters calls the following AWS API:
// https://docs.aws.amazon.com/eks/latest/APIReference/API_ListClusters.html - to list available EKS clusters
// https://docs.aws.amazon.com/eks/latest/APIReference/API_DescribeCluster.html - to get more detailed information about
// the each cluster in the list we received.
// It returns a list of EKS clusters with detailed information about them.
func ListEKSClusters(ctx context.Context, clt ListEKSClustersClient, req ListEKSClustersRequest) (*ListEKSClustersResponse, error) {
if err := req.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}

listEKSClusters := &eks.ListClustersInput{}
if req.NextToken != "" {
listEKSClusters.NextToken = &req.NextToken
}
eksClusters, err := clt.ListClusters(ctx, listEKSClusters)
if err != nil {
return nil, trace.Wrap(err)
}

var mu sync.Mutex
ret := &ListEKSClustersResponse{
NextToken: aws.ToString(eksClusters.NextToken),
ClusterFetchingErrors: map[string]error{},
}

group, groupCtx := errgroup.WithContext(ctx)
group.SetLimit(concurrentEKSFetchingLimit)

ret.Clusters = make([]EKSCluster, 0, len(eksClusters.Clusters))
for _, clusterName := range eksClusters.Clusters {
clusterName := clusterName
if clusterName == "" {
continue
}
group.Go(func() error {
eksClusterInfo, err := clt.DescribeCluster(groupCtx, &eks.DescribeClusterInput{
Name: aws.String(clusterName),
})

mu.Lock()
defer mu.Unlock()

if err != nil {
ret.ClusterFetchingErrors[clusterName] = err
Comment thread
marcoandredinis marked this conversation as resolved.
return nil
}

extraLabels, err := getExtraEKSLabels(eksClusterInfo.Cluster)
if err != nil {
ret.ClusterFetchingErrors[clusterName] = err
return nil
}

ret.Clusters = append(ret.Clusters, EKSCluster{
Name: aws.ToString(eksClusterInfo.Cluster.Name),
Region: req.Region,
Labels: eksClusterInfo.Cluster.Tags,
JoinLabels: extraLabels,
Status: strings.ToLower(string(eksClusterInfo.Cluster.Status)),
})
return nil
})
}

// We don't return error from individual group goroutines, they are gathered in the returned value.
_ = group.Wait()

return ret, nil
}

func getExtraEKSLabels(cluster *eksTypes.Cluster) (map[string]string, error) {
parsedARN, err := arn.Parse(aws.ToString(cluster.Arn))
if err != nil {
return nil, trace.Wrap(err)
}
return map[string]string{
types.CloudLabel: types.CloudAWS,
types.DiscoveryLabelAccountID: parsedARN.AccountID,
types.DiscoveryLabelRegion: parsedARN.Region,
}, nil
}
Loading