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 all commits
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
2 changes: 2 additions & 0 deletions docs/data-sources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Required:

Optional:

- **agent** (Boolean) Use a local SSH agent to login to the remote host. Defaults to `false`.
- **password** (String, Sensitive) The pasword for the user on the remote host.
- **port** (Number) The ssh port on the remote host. Defaults to `22`.
- **private_key** (String, Sensitive) The private key used to login to the remote host.
Expand All @@ -77,6 +78,7 @@ Optional:

Read-Only:

- **agent** (Boolean)
- **host** (String)
- **password** (String)
- **port** (Number)
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Required:

Optional:

- **agent** (Boolean) Use a local SSH agent to login to the remote host. Defaults to `false`.
- **password** (String, Sensitive) The pasword for the user on the remote host.
- **port** (Number) The ssh port on the remote host. Defaults to `22`.
- **private_key** (String, Sensitive) The private key used to login to the remote host.
Expand Down
2 changes: 2 additions & 0 deletions docs/resources/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Required:

Optional:

- **agent** (Boolean) Use a local SSH agent to login to the remote host. Defaults to `false`.
- **password** (String, Sensitive) The pasword for the user on the remote host.
- **port** (Number) The ssh port on the remote host. Defaults to `22`.
- **private_key** (String, Sensitive) The private key used to login to the remote host.
Expand All @@ -84,6 +85,7 @@ Optional:

Read-Only:

- **agent** (Boolean)
- **host** (String)
- **password** (String)
- **port** (Number)
Expand Down
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))
}

enableAgent, ok := d.GetOk("result_conn.0.agent")
if ok && enableAgent.(bool) {
connection, err := net.Dial("unix", os.Getenv("SSH_AUTH_SOCK"))
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(connection).Signers))
}

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