Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#584 Added Support for SSM based Instance #1159

Merged
merged 18 commits into from
Jan 24, 2024
Merged
Changes from 15 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
97 changes: 97 additions & 0 deletions providers/aws/systemsmanager/managedinstances.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package systemsmanager

import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
)

func getManagedEc2(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {

Check failure on line 16 in providers/aws/systemsmanager/managedinstances.go

View workflow job for this annotation

GitHub Actions / golangci-lint

func `getManagedEc2` is unused (unused)
Horiodino marked this conversation as resolved.
Show resolved Hide resolved
resources := make([]models.Resource, 0)

ssmClient := ssm.NewFromConfig(*client.AWSClient)
ec2Client := ec2.NewFromConfig(*client.AWSClient)

var nexttoken *string

for {
ssmOutput, err := ssmClient.DescribeInstanceInformation(ctx, &ssm.DescribeInstanceInformationInput{
NextToken: nexttoken,
})
if err != nil {
return resources, err
}

instanceIds := make([]string, 0, len(ssmOutput.InstanceInformationList))
for _, ec2instance := range ssmOutput.InstanceInformationList {
instanceIds = append(instanceIds, *ec2instance.InstanceId)
}
ec2Output, err := ec2Client.DescribeInstances(ctx, &ec2.DescribeInstancesInput{
InstanceIds: instanceIds,
})
if err != nil {
return resources, err
}

account, accountID, err := fetchID(ctx, client)
if err != nil {
return resources, err
}

for _, ec2instance := range ec2Output.Reservations {
for _, instance := range ec2instance.Instances {

if instance.State.Name == types.InstanceStateNameRunning {
tags := make([]models.Tag, 0)
for _, tag := range instance.Tags {
tags = append(tags, models.Tag{
Key: *tag.Key,
Value: *tag.Value,
})
}

resources = append(resources, models.Resource{
Provider: "AWS",
Account: account,
AccountId: accountID,
Service: "SSM Instance",
Region: client.AWSClient.Region,
ResourceId: *instance.InstanceId,
Name: string(instance.InstanceType),
CreatedAt: *instance.LaunchTime,
FetchedAt: time.Now(),
Tags: tags,
Link: fmt.Sprintf("https://%s.console.aws.amazon.com/ec2/home?region=%s#InstanceDetails:instanceId=%s",
client.AWSClient.Region, client.AWSClient.Region, *instance.InstanceId),
})
}

}
}
if ssmOutput.NextToken == nil {
break
}
nexttoken = ssmOutput.NextToken
}

return resources, nil
}

func fetchID(ctx context.Context, client providers.ProviderClient) (accountID string, userID string, err error) {

Check failure on line 87 in providers/aws/systemsmanager/managedinstances.go

View workflow job for this annotation

GitHub Actions / golangci-lint

func `fetchID` is unused (unused)
svc := sts.NewFromConfig(*client.AWSClient)
result, err := svc.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
if err != nil {
fmt.Println("Got error retrieving account ID:")
return "", "", err
}

// if the accountID and userID are the same, then the account is a root account
return *result.Account, *result.UserId, nil
}
Loading