-
Notifications
You must be signed in to change notification settings - Fork 2
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
krystian-panek-vmltech
merged 26 commits into
main
from
26-implement-aws_ssm-connection-type
Jan 29, 2024
Merged
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 7b05f6d
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 81cb271
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 3c0d3b4
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 7f88afd
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 92bc821
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 6c418eb
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 681999e
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 9de5484
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 6b05fe7
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech b4d8d3b
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 84a7b50
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 091eae3
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 0fdc56c
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech c5ea9a4
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 8669a9f
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech e41d5d6
Implement 'aws_ssm' connection type #26
dominik-przybyl-wttech 96d905a
Default region + error handling
krystian-panek-vmltech c5dcc23
Defaults upgrade
krystian-panek-vmltech 62500c7
Private fields
krystian-panek-vmltech 8df5428
Minor imprs
krystian-panek-vmltech 4340256
Deps upgrade
krystian-panek-vmltech 2d1fbf5
Performance improvement
dominik-przybyl-wttech d4043d4
SSM works like a charm ;)
krystian-panek-vmltech 9f3b271
Timeouts configurable
krystian-panek-vmltech 5f60ea8
Done threshold
krystian-panek-vmltech File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{ | ||
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)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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