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

Allow connections via SSH agent #31

Merged
merged 5 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion docs/data-sources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Optional:
- **private_key_env_var** (String) The name of the local environment variable containing the private key used to login to the remote host.
- **private_key_path** (String) The local path to the private key used to login to the remote host.
- **sudo** (Boolean) Use sudo to gain access to file. Defaults to `false`.
- **agent** (Booean) Use local SSH agent to login to the remote host. Default to `false`.


<a id="nestedatt--result_conn"></a>
Expand All @@ -85,5 +86,5 @@ Read-Only:
- **private_key_path** (String)
- **sudo** (Boolean)
- **user** (String)

- **agent** (Booean)
phaer marked this conversation as resolved.
Show resolved Hide resolved

1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ Optional:
- **private_key_env_var** (String) The name of the local environment variable containing the private key used to login to the remote host.
- **private_key_path** (String) The local path to the private key used to login to the remote host.
- **sudo** (Boolean) Use sudo to gain access to file. Defaults to `false`.
- **agent** (Booean) Use local SSH agent to login to the remote host. Default to `false`.
18 changes: 18 additions & 0 deletions internal/provider/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package provider
import (
"fmt"
"io/ioutil"
"net"
"os"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/agent"
)

var connectionSchemaResource = &schema.Resource{
Expand Down Expand Up @@ -56,6 +58,13 @@ var connectionSchemaResource = &schema.Resource{
Optional: true,
Description: "The name of the local environment variable containing the private key used to login to the remote host.",
},
"agent": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Use a local SSH agent to login to the remote host.",
},

},
}

Expand Down Expand Up @@ -107,6 +116,15 @@ func ConnectionFromResourceData(d *schema.ResourceData) (string, *ssh.ClientConf
clientConfig.Auth = append(clientConfig.Auth, ssh.PublicKeys(signer))
}

enable_agent, ok := d.GetOk("result_conn.0.agent")
phaer marked this conversation as resolved.
Show resolved Hide resolved
if ok && enable_agent.(bool) {
phaer marked this conversation as resolved.
Show resolved Hide resolved
sshAgent, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
tenstad marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return "", nil, fmt.Errorf("couldn't connect to SSH agent: %s", err.Error())
}
clientConfig.Auth = append(clientConfig.Auth, ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers))
tenstad marked this conversation as resolved.
Show resolved Hide resolved
}

host := fmt.Sprintf("%s:%d", d.Get("result_conn.0.host").(string), d.Get("result_conn.0.port").(int))
return host, &clientConfig, nil
}