Skip to content

Commit

Permalink
SSH connected
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Aug 29, 2023
1 parent 43fdb5f commit 17c9074
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
11 changes: 0 additions & 11 deletions examples/aws_ssm/aws.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,6 @@ resource "aws_instance" "aem_single" {
tags = local.tags
}

data "tls_public_key" "main" {
private_key_pem = file("ec2-key.cer")
}

resource "aws_key_pair" "main" {
key_name = local.workspace
public_key = data.tls_public_key.main.public_key_openssh
tags = local.tags
}


resource "aws_iam_instance_profile" "ssm" {
name = "${local.workspace}_ssm_ec2"
role = aws_iam_role.ssm.name
Expand Down
11 changes: 11 additions & 0 deletions examples/ssh/aws.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ resource "aws_instance" "aem_single" {
instance_type = "m5.xlarge"
associate_public_ip_address = true
tags = local.tags
key_name = aws_key_pair.main.key_name
}

data "tls_public_key" "main" {
private_key_pem = file("ec2-key.cer")
}

resource "aws_key_pair" "main" {
key_name = "${local.workspace}-example-tf"
public_key = data.tls_public_key.main.public_key_openssh
tags = local.tags
}

output "instance_ip" {
Expand Down
14 changes: 12 additions & 2 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,28 @@ TF_RC_FILE="$(pwd)/dev_overrides.tfrc"
if [ ! -f "$GO_BIN_DIR/terraform-provider-aws" ]
then
echo "Setting up Terraform AWS provider as dev-override: $GO_BIN_DIR/terraform-provider-aws"
wget https://releases.hashicorp.com/terraform-provider-aws/5.14.0/terraform-provider-aws_5.14.0_darwin_arm64.zip -O /tmp/terraform-provider-aws.zip
unzip /tmp/terraform-provider-aws.zip -d "$GO_BIN_DIR"
wget https://releases.hashicorp.com/terraform-provider-aws/5.14.0/terraform-provider-aws_5.14.0_darwin_arm64.zip -c -O /tmp/terraform-provider-aws.zip
unzip -o /tmp/terraform-provider-aws.zip -d "$GO_BIN_DIR"
cp /tmp/terraform-provider-aws_v5.14.0_x5 "$GO_BIN_DIR/terraform-provider-aws"
fi

# TLS provider
if [ ! -f "$GO_BIN_DIR/terraform-provider-tls" ]
then
echo "Setting up Terraform TLS provider as dev-override: $GO_BIN_DIR/terraform-provider-tls"
wget https://releases.hashicorp.com/terraform-provider-tls/4.0.4/terraform-provider-tls_4.0.4_darwin_arm64.zip -c -O /tmp/terraform-provider-tls.zip
unzip -o /tmp/terraform-provider-tls.zip -d "$GO_BIN_DIR"
cp /tmp/terraform-provider-tls_v4.0.4_x5 "$GO_BIN_DIR/terraform-provider-tls"
fi

echo "Setting up dev-overrides in custom Terraform CLI configuration file: $TF_RC_FILE"
cat <<EOT > "$TF_RC_FILE"
provider_installation {
dev_overrides {
"registry.terraform.io/wttech/aem" = "$GO_BIN_DIR"
"registry.terraform.io/hashicorp/aws" = "$GO_BIN_DIR"
"registry.terraform.io/hashicorp/tls" = "$GO_BIN_DIR"
}
# For all other providers, install them directly from their origin provider
Expand Down
15 changes: 14 additions & 1 deletion internal/client/client_ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package client
import (
"fmt"
"github.com/melbahja/goph"
"github.com/spf13/cast"
"golang.org/x/crypto/ssh"
)

type SSHClient struct {
Expand All @@ -20,7 +22,15 @@ func (s *SSHClient) Connect() error {
if err != nil {
return fmt.Errorf("SSH: cannot get auth using private key '%s': %w", s.privateKeyFile, err)
}
client, err := goph.New(s.user, s.host, auth)
// TODO loop until establishment of connection
client, err := goph.NewConn(&goph.Config{
User: s.user,
Addr: s.host,
Port: cast.ToUint(s.port),
Auth: auth,
Timeout: goph.DefaultTimeout,
Callback: ssh.InsecureIgnoreHostKey(), // TODO make it secure by default
})
if err != nil {
return fmt.Errorf("SSH: cannot connect to host '%s': %w", s.host, err)
}
Expand All @@ -29,6 +39,9 @@ func (s *SSHClient) Connect() error {
}

func (s *SSHClient) Disconnect() error {
if s.client == nil {
return nil
}
if err := s.client.Close(); err != nil {
return fmt.Errorf("SSH: cannot disconnect from host '%s': %w", s.host, err)
}
Expand Down
7 changes: 6 additions & 1 deletion internal/provider/instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ func (r *InstanceResource) Create(ctx context.Context, req resource.CreateReques
tflog.Trace(ctx, "creating AEM instance resource")

tflog.Trace(ctx, "connecting to AEM instance machine")
cl, err := r.clientManager.Make(data.Client.Type.ValueString(), map[string]string{})

typeName := data.Client.Type.ValueString()
var settings map[string]string
data.Client.Settings.ElementsAs(ctx, &settings, true)

cl, err := r.clientManager.Make(typeName, settings)
if err != nil {
resp.Diagnostics.AddError("AEM instance error", fmt.Sprintf("Unable to determine AEM instance client, got error: %s", err))
return
Expand Down

0 comments on commit 17c9074

Please sign in to comment.