Skip to content

Commit

Permalink
EC2 Instance Connect Endpoint: List EC2 Instances (gravitational#29366)
Browse files Browse the repository at this point in the history
* EC2 Instance Connect Endpoint: List EC2 Instances

We must list the EC2 instances to the user, so they can pick one and
then add them as a Node.

This PR adds EC2 Instance Listing.
To do this, we convert the EC2 Instance into Teleport Server with a
special subkind: SubKindOpenSSHEC2InstanceConnectEndpointNode

This is similar to the ListDatabases endpoint.

* review pt1

* fix const and add test

* use any instead of interface{}
  • Loading branch information
marcoandredinis authored Aug 10, 2023
1 parent 49ce61d commit e32248c
Show file tree
Hide file tree
Showing 13 changed files with 796 additions and 13 deletions.
6 changes: 3 additions & 3 deletions api/types/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,13 @@ func (s *ServerV2) IsOpenSSHNode() bool {
// They include SubKindOpenSSHNode and SubKindOpenSSHEICENode.
func (s *ServerV2) openSSHNodeCheckAndSetDefaults() error {
if s.Spec.Addr == "" {
return trace.BadParameter(`addr must be set when server SubKind is "openssh"`)
return trace.BadParameter("addr must be set when server SubKind is %q", s.GetSubKind())
}
if len(s.GetPublicAddrs()) != 0 {
return trace.BadParameter(`publicAddrs must not be set when server SubKind is "openssh"`)
return trace.BadParameter("publicAddrs must not be set when server SubKind is %q", s.GetSubKind())
}
if s.Spec.Hostname == "" {
return trace.BadParameter(`hostname must be set when server SubKind is "openssh"`)
return trace.BadParameter("hostname must be set when server SubKind is %q", s.GetSubKind())
}

_, _, err := net.SplitHostPort(s.Spec.Addr)
Expand Down
2 changes: 1 addition & 1 deletion lib/automaticupgrades/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
func TestVersion(t *testing.T) {
ctx := context.Background()

isBadParameterErr := func(tt require.TestingT, err error, i ...interface{}) {
isBadParameterErr := func(tt require.TestingT, err error, i ...any) {
require.True(tt, trace.IsBadParameter(err), "expected bad parameter, got %v", err)
}

Expand Down
25 changes: 25 additions & 0 deletions lib/cloud/aws/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
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 aws

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

// AddMetadataLabels adds the AccountID and Region as labels.
func AddMetadataLabels(labels map[string]string, accountID, region string) {
labels[types.DiscoveryLabelAccountID] = accountID
labels[types.DiscoveryLabelRegion] = region
}
4 changes: 4 additions & 0 deletions lib/cloud/aws/tags_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package aws

import (
ec2TypesV2 "github.com/aws/aws-sdk-go-v2/service/ec2/types"
rdsTypesV2 "github.com/aws/aws-sdk-go-v2/service/rds/types"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/elasticache"
Expand All @@ -37,6 +38,7 @@ type ResourceTag interface {
// TODO Go generic does not allow access common fields yet. List all types
// here and use a type switch for now.
rdsTypesV2.Tag |
ec2TypesV2.Tag |
*rds.Tag |
*redshift.Tag |
*elasticache.Tag |
Expand Down Expand Up @@ -78,6 +80,8 @@ func resourceTagToKeyValue[Tag ResourceTag](tag Tag) (string, string) {
return aws.StringValue(v.Key), aws.StringValue(v.Value)
case rdsTypesV2.Tag:
return aws.StringValue(v.Key), aws.StringValue(v.Value)
case ec2TypesV2.Tag:
return aws.StringValue(v.Key), aws.StringValue(v.Value)
case *opensearchservice.Tag:
return aws.StringValue(v.Key), aws.StringValue(v.Value)
default:
Expand Down
11 changes: 11 additions & 0 deletions lib/integrations/awsoidc/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ecs"
"github.com/aws/aws-sdk-go-v2/service/rds"
"github.com/aws/aws-sdk-go-v2/service/sts"
Expand Down Expand Up @@ -121,6 +122,16 @@ func newSTSClient(ctx context.Context, req *AWSClientRequest) (*sts.Client, erro
return sts.NewFromConfig(*cfg), nil
}

// newEC2Client creates an [ec2.Client] using the provided Token, RoleARN and Region.
func newEC2Client(ctx context.Context, req *AWSClientRequest) (*ec2.Client, error) {
cfg, err := newAWSConfig(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}

return ec2.NewFromConfig(*cfg), nil
}

// IdentityToken is an implementation of [stscreds.IdentityTokenRetriever] for returning a static token.
type IdentityToken string

Expand Down
12 changes: 3 additions & 9 deletions lib/integrations/awsoidc/listdatabases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,9 +338,7 @@ func TestListDatabases(t *testing.T) {
Engines: []string{"postgres"},
NextToken: "",
},
errCheck: func(err error) bool {
return trace.IsBadParameter(err)
},
errCheck: trace.IsBadParameter,
},
{
name: "invalid rds type",
Expand All @@ -350,9 +348,7 @@ func TestListDatabases(t *testing.T) {
Engines: []string{"postgres"},
NextToken: "",
},
errCheck: func(err error) bool {
return trace.IsBadParameter(err)
},
errCheck: trace.IsBadParameter,
},
{
name: "empty engines list",
Expand All @@ -362,9 +358,7 @@ func TestListDatabases(t *testing.T) {
Engines: []string{},
NextToken: "",
},
errCheck: func(err error) bool {
return trace.IsBadParameter(err)
},
errCheck: trace.IsBadParameter,
},
} {
t.Run(tt.name, func(t *testing.T) {
Expand Down
178 changes: 178 additions & 0 deletions lib/integrations/awsoidc/listec2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
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 awsoidc

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2Types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/gravitational/trace"

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

const (
// awsInstanceStateName represents the state of the AWS EC2
// instance - (pending | running | shutting-down | terminated | stopping | stopped )
// https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html
// Used for filtering instances.
awsInstanceStateName = "instance-state-name"
)

var (
// filterRunningEC2Instance is an EC2 DescribeInstances Filter to filter running instances.
filterRunningEC2Instance = ec2Types.Filter{
Name: aws.String(awsInstanceStateName),
Values: []string{string(ec2Types.InstanceStateNameRunning)},
}
)

// ListEC2Request contains the required fields to list AWS EC2 Instances.
type ListEC2Request struct {
// Integration is the AWS OIDC Integration name.
// This is used to populate the Server resource.
// When connecting to the Node, this is the integration that is going to be used.
Integration string

// 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 *ListEC2Request) CheckAndSetDefaults() error {
if req.Integration == "" {
return trace.BadParameter("integration is required")
}
if req.Region == "" {
return trace.BadParameter("region is required")
}

return nil
}

// ListEC2Response contains a page of AWS EC2 Instances as Teleport Servers.
type ListEC2Response struct {
// Servers contains the page of Servers.
Servers []types.Server

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

// ListEC2Client describes the required methods to List EC2 Instances using a 3rd Party API.
type ListEC2Client interface {
// DescribeInstances describes the specified instances or all instances.
DescribeInstances(ctx context.Context, params *ec2.DescribeInstancesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeInstancesOutput, error)

// GetCallerIdentity returns details about the IAM user or role whose credentials are used to call the operation.
GetCallerIdentity(ctx context.Context, params *sts.GetCallerIdentityInput, optFns ...func(*sts.Options)) (*sts.GetCallerIdentityOutput, error)
}

type defaultListEC2Client struct {
*ec2.Client
stsClient *sts.Client
}

// GetCallerIdentity returns details about the IAM user or role whose credentials are used to call the operation.
func (d defaultListEC2Client) GetCallerIdentity(ctx context.Context, params *sts.GetCallerIdentityInput, optFns ...func(*sts.Options)) (*sts.GetCallerIdentityOutput, error) {
return d.stsClient.GetCallerIdentity(ctx, params, optFns...)
}

// NewListEC2Client creates a new ListEC2Client using a AWSClientRequest.
func NewListEC2Client(ctx context.Context, req *AWSClientRequest) (ListEC2Client, error) {
ec2Client, err := newEC2Client(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}

stsClient, err := newSTSClient(ctx, req)
if err != nil {
return nil, trace.Wrap(err)
}

return &defaultListEC2Client{
Client: ec2Client,
stsClient: stsClient,
}, nil
}

// ListEC2 calls the following AWS API:
// https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeInstances.html
// It returns a list of EC2 Instances and an optional NextToken that can be used to fetch the next page
func ListEC2(ctx context.Context, clt ListEC2Client, req ListEC2Request) (*ListEC2Response, error) {
if err := req.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}

callerIdentity, err := clt.GetCallerIdentity(ctx, nil)
if err != nil {
return nil, trace.Wrap(err)
}
accountID := aws.ToString(callerIdentity.Account)
if accountID == "" {
return nil, trace.BadParameter("failed to get AWS AccountID using GetCallerIdentity")
}

describeEC2Instances := &ec2.DescribeInstancesInput{
Filters: []ec2Types.Filter{
filterRunningEC2Instance,
},
}
if req.NextToken != "" {
describeEC2Instances.NextToken = &req.NextToken
}

ec2Instances, err := clt.DescribeInstances(ctx, describeEC2Instances)
if err != nil {
return nil, trace.Wrap(err)
}

ret := &ListEC2Response{}

if aws.ToString(ec2Instances.NextToken) != "" {
ret.NextToken = *ec2Instances.NextToken
}

ret.Servers = make([]types.Server, 0, len(ec2Instances.Reservations))
for _, reservation := range ec2Instances.Reservations {
for _, instance := range reservation.Instances {
awsInfo := &types.AWSInfo{
AccountID: accountID,
Region: req.Region,
Integration: req.Integration,
}

server, err := services.NewAWSNodeFromEC2Instance(instance, awsInfo)
if err != nil {
return nil, trace.Wrap(err)
}

ret.Servers = append(ret.Servers, server)
}
}

return ret, nil
}
Loading

0 comments on commit e32248c

Please sign in to comment.