Skip to content

Commit

Permalink
Implement 'aws_ssm' connection type #26
Browse files Browse the repository at this point in the history
  • Loading branch information
dominik-przybyl-wttech committed Dec 14, 2023
1 parent 3c0d3b4 commit 7f88afd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 47 deletions.
12 changes: 2 additions & 10 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"context"
"fmt"
"github.com/melbahja/goph"
"github.com/wttech/terraform-provider-aem/internal/utils"
"os"
"path/filepath"
Expand Down Expand Up @@ -68,7 +67,7 @@ func (c Client) Connection() Connection {
return c.connection
}

func (c Client) Command(cmdLine []string) (*goph.Cmd, error) {
func (c Client) Command(cmdLine []string) ([]byte, error) {
return c.connection.Command(cmdLine)
}

Expand Down Expand Up @@ -110,17 +109,10 @@ func (c Client) RunShellPurely(cmd string) ([]byte, error) {
} else {
cmdLine = []string{"sh", "-c", "\"" + cmd + "\""}
}
cmdObj, err := c.connection.Command(cmdLine)
out, err := c.connection.Command(cmdLine)
if err != nil {
return nil, fmt.Errorf("cannot create command '%s': %w", cmd, err)
}
out, err := cmdObj.CombinedOutput()
if err != nil {
if len(out) > 0 {
return nil, fmt.Errorf("cannot run command '%s': %w\n\n%s", cmdObj, err, string(out))
}
return nil, err
}
return out, nil
}

Expand Down
6 changes: 1 addition & 5 deletions internal/client/connection.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package client

import (
"github.com/melbahja/goph"
)

type Connection interface {
Info() string
User() string
Connect() error
Disconnect() error
Command(cmdLine []string) (*goph.Cmd, error)
Command(cmdLine []string) ([]byte, error)
CopyFile(localPath string, remotePath string) error
}
38 changes: 8 additions & 30 deletions internal/client/connection_aws_ssm.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package client

import (
"encoding/base64"
"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"
)
Expand Down Expand Up @@ -64,13 +64,13 @@ func (a *AWSSSMConnection) Disconnect() error {
return nil
}

func (a *AWSSSMConnection) Command(cmdLine []string) (*goph.Cmd, error) {
func (a *AWSSSMConnection) Command(cmdLine []string) ([]byte, 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),
"commands": {aws.String(strings.Join(cmdLine, " "))},
},
}

Expand Down Expand Up @@ -98,39 +98,17 @@ func (a *AWSSSMConnection) Command(cmdLine []string) (*goph.Cmd, error) {
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
return []byte(*getCommandOutput.StandardOutputContent), nil
}

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)
}
encodedContent := base64.StdEncoding.EncodeToString(fileContent)

putParameterInput := &ssm.PutParameterInput{
Name: aws.String(remotePath),
Value: aws.String(string(fileContent)),
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
command := fmt.Sprintf("echo -n %s | base64 -d > %s", encodedContent, remotePath)
_, err = a.Command(strings.Split(command, " "))
return err
}
11 changes: 9 additions & 2 deletions internal/client/connection_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,20 @@ func (s *SSHConnection) Disconnect() error {
return nil
}

func (s *SSHConnection) Command(cmdLine []string) (*goph.Cmd, error) {
func (s *SSHConnection) Command(cmdLine []string) ([]byte, error) {
name, args := s.splitCommandLine(cmdLine)
cmd, err := s.client.Command(name, args...)
if err != nil {
return nil, fmt.Errorf("ssh: cannot create command '%s' for host '%s': %w", strings.Join(cmdLine, " "), s.host, err)
}
return cmd, nil
out, err := cmd.CombinedOutput()
if err != nil {
if len(out) > 0 {
return nil, fmt.Errorf("ssh: cannot run command '%s': %w\n\n%s", cmd, err, string(out))
}
return nil, err
}
return out, nil
}

func (s *SSHConnection) splitCommandLine(cmdLine []string) (string, []string) {
Expand Down

0 comments on commit 7f88afd

Please sign in to comment.