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

26 implement aws ssm connection type #31

Merged
merged 26 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e2bce47
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Nov 28, 2023
7b05f6d
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Dec 5, 2023
81cb271
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Dec 5, 2023
3c0d3b4
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Dec 5, 2023
7f88afd
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Dec 14, 2023
92bc821
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Dec 15, 2023
6c418eb
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 11, 2024
681999e
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 18, 2024
9de5484
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 19, 2024
6b05fe7
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 23, 2024
b4d8d3b
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 24, 2024
84a7b50
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 24, 2024
091eae3
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 24, 2024
0fdc56c
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 24, 2024
c5ea9a4
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 24, 2024
8669a9f
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 25, 2024
e41d5d6
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech Jan 25, 2024
96d905a
Default region + error handling
krystian-panek-vmltech Jan 26, 2024
c5dcc23
Defaults upgrade
krystian-panek-vmltech Jan 26, 2024
62500c7
Private fields
krystian-panek-vmltech Jan 26, 2024
8df5428
Minor imprs
krystian-panek-vmltech Jan 26, 2024
4340256
Deps upgrade
krystian-panek-vmltech Jan 26, 2024
2d1fbf5
Performance improvement
dominik-przybyl-wttech Jan 26, 2024
d4043d4
SSM works like a charm ;)
krystian-panek-vmltech Jan 29, 2024
9f3b271
Timeouts configurable
krystian-panek-vmltech Jan 29, 2024
5f60ea8
Done threshold
krystian-panek-vmltech Jan 29, 2024
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
4 changes: 2 additions & 2 deletions examples/aws_ssh/aws.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ resource "aws_iam_instance_profile" "aem_ec2" {
}

resource "aws_iam_role" "aem_ec2" {
name = "${local.workspace}_aem_ec2"
name = "${local.workspace}_aem_ec2"
assume_role_policy = trimspace(<<EOF
{
"Version": "2012-10-17",
Expand All @@ -49,7 +49,7 @@ resource "aws_iam_role" "aem_ec2" {
}
EOF
)
tags = local.tags
tags = local.tags
}

resource "aws_iam_role_policy_attachment" "s3" {
Expand Down
4 changes: 2 additions & 2 deletions examples/aws_ssm/aws.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ resource "aws_iam_instance_profile" "aem_ec2" {
}

resource "aws_iam_role" "aem_ec2" {
name = "${local.workspace}_aem_ec2"
name = "${local.workspace}_aem_ec2"
assume_role_policy = trimspace(<<EOF
{
"Version": "2012-10-17",
Expand All @@ -37,7 +37,7 @@ resource "aws_iam_role" "aem_ec2" {
}
EOF
)
tags = local.tags
tags = local.tags
}

resource "aws_iam_role_policy_attachment" "ssm" {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/aws/aws-sdk-go v1.48.12 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/cloudflare/circl v1.3.3 // indirect
github.com/fatih/color v1.16.0 // indirect
Expand All @@ -42,6 +43,7 @@ require (
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ github.com/apparentlymart/go-textseg/v13 v13.0.0/go.mod h1:ZK2fH7c4NqDTLtiYLvIkE
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aws/aws-sdk-go v1.48.12 h1:n+eGzflzzvYubu2cOjqpVll7lF+Ci0ThyCpg5kzfzbo=
github.com/aws/aws-sdk-go v1.48.12/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/bgentry/speakeasy v0.1.0 h1:ByYyxL9InA1OWqxJqqp2A5pYHUrCiAL6K3J+LKSsQkY=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
Expand Down Expand Up @@ -91,6 +93,9 @@ github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jhump/protoreflect v1.15.1 h1:HUMERORf3I3ZdX05WaQ6MIpd/NJ434hTp5YiKgfCL6c=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
Expand Down Expand Up @@ -237,6 +242,7 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
4 changes: 2 additions & 2 deletions internal/client/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (c ClientManager) connection(typeName string, settings map[string]string) (
}, nil
case "aws-ssm":
return &AWSSSMConnection{
InstanceID: settings["instance_id"],
Region: settings["region"],
instanceId: settings["instance_id"],
region: settings["region"],
}, nil
}
return nil, fmt.Errorf("unknown AEM client type: %s", typeName)
Expand Down
140 changes: 119 additions & 21 deletions internal/client/connection_aws_ssm.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,136 @@
package client

import "github.com/melbahja/goph"
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/melbahja/goph"
"os"
"strings"
)

type AWSSSMConnection struct {
InstanceID string
Region string
instanceId string
region string
ssmClient *ssm.SSM
sessionId *string
}

func (A AWSSSMConnection) Info() string {
//TODO implement me
panic("implement me")
func (a *AWSSSMConnection) Info() string {
return fmt.Sprintf("ssm: instance='%s', region='%s'", a.instanceId, a.region)
}

func (A AWSSSMConnection) User() string {
//TODO implement me
panic("implement me")
func (a *AWSSSMConnection) User() string {
return "aem" // does not impact the connection, used as default user for systemd only
}
func (a *AWSSSMConnection) Connect() error {
// Create an AWS session
sess, err := session.NewSession(&aws.Config{
Region: aws.String(a.region),
})
if err != nil {
return fmt.Errorf("ssm: error creating AWS session: %v", err)
}

func (A AWSSSMConnection) Connect() error {
//TODO implement me
panic("implement me")
// Connect to AWS instance using SSM
ssmClient := ssm.New(sess)
startSessionInput := &ssm.StartSessionInput{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 lines to 1; fmt

Target: aws.String(a.instanceId),
}

startSessionOutput, err := ssmClient.StartSession(startSessionInput)
if err != nil {
return fmt.Errorf("ssm: error starting session: %v", err)
}

a.ssmClient = ssmClient
a.sessionId = startSessionOutput.SessionId

return nil
}

func (A AWSSSMConnection) Disconnect() error {
//TODO implement me
panic("implement me")
func (a *AWSSSMConnection) Disconnect() error {
// Disconnect from the session
terminateSessionInput := &ssm.TerminateSessionInput{
SessionId: a.sessionId,
}

_, err := a.ssmClient.TerminateSession(terminateSessionInput)
if err != nil {
return fmt.Errorf("ssm: error terminating session: %v", err)
}

return nil
}

func (A AWSSSMConnection) Command(cmdLine []string) (*goph.Cmd, error) {
//TODO implement me
panic("implement me")
func (a *AWSSSMConnection) Command(cmdLine []string) (*goph.Cmd, error) {
// Execute command on the remote instance
runCommandInput := &ssm.SendCommandInput{
DocumentName: aws.String("AWS-RunShellScript"),
InstanceIds: []*string{aws.String(a.instanceId)},
Parameters: map[string][]*string{
"commands": aws.StringSlice(cmdLine),
},
}

runCommandOutput, err := a.ssmClient.SendCommand(runCommandInput)
if err != nil {
return nil, fmt.Errorf("ssm: error executing command: %v", err)
}

commandId := runCommandOutput.Command.CommandId

// Wait for command completion
err = a.ssmClient.WaitUntilCommandExecuted(&ssm.GetCommandInvocationInput{
CommandId: commandId,
InstanceId: aws.String(a.instanceId),
})
if err != nil {
return nil, fmt.Errorf("ssm: error executing command: %v", err)
}

getCommandOutput, err := a.ssmClient.GetCommandInvocation(&ssm.GetCommandInvocationInput{
CommandId: commandId,
InstanceId: aws.String(a.instanceId),
})
if err != nil {
return nil, fmt.Errorf("ssm: error executing command: %v", err)
}

// Transform the SSM command output into a goph.Cmd structure
parts := strings.Fields(*getCommandOutput.StandardOutputContent)
if len(parts) < 2 {
return nil, fmt.Errorf("ssm: unexpected command output format")
}

gophCommand := goph.Cmd{
Path: parts[0],
Args: parts[1:],
Env: os.Environ(),
}

return &gophCommand, nil
}

func (A AWSSSMConnection) CopyFile(localPath string, remotePath string) error {
//TODO implement me
panic("implement me")
func (a *AWSSSMConnection) CopyFile(localPath string, remotePath string) error {
// Upload file to the remote instance using SSM Parameter Store
fileContent, err := os.ReadFile(localPath)
if err != nil {
return fmt.Errorf("ssm: error reading local file: %v", err)
}

putParameterInput := &ssm.PutParameterInput{
Name: aws.String(remotePath),
Value: aws.String(string(fileContent)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about 500 mb string ?:D

Type: aws.String("SecureString"),
Overwrite: aws.Bool(true),
}

_, err = a.ssmClient.PutParameter(putParameterInput)
if err != nil {
return fmt.Errorf("ssm: error uploading file to the instance: %v", err)
}

return nil
}
Loading