Skip to content

Commit 3323136

Browse files
committed
add ssh_key_scan data source
From hashicorp#97 (hashicorp#95 would also work instead)
1 parent 9d83d6f commit 3323136

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package provider
2+
3+
import (
4+
"encoding/base64"
5+
"errors"
6+
"fmt"
7+
"net"
8+
"strings"
9+
"time"
10+
11+
"golang.org/x/crypto/ssh"
12+
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
14+
)
15+
16+
func dataSourceSshKeyScan() *schema.Resource {
17+
return &schema.Resource{
18+
Read: dataSourceSshKeyScanRead,
19+
Schema: map[string]*schema.Schema{
20+
"host": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
Description: "Host to ssh key scan.",
24+
},
25+
"port": {
26+
Type: schema.TypeInt,
27+
Optional: true,
28+
Default: 22,
29+
Description: "Port to key scan",
30+
},
31+
"public_host_key": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
Description: "Result of ssh key scan.",
35+
},
36+
},
37+
}
38+
}
39+
40+
func dataSourceSshKeyScanRead(d *schema.ResourceData, meta interface{}) error {
41+
host := d.Get("host").(string)
42+
port := d.Get("port").(int)
43+
44+
hostKeyCh := make(chan string, 1)
45+
hostKeyError := errors.New("ignoring host key verification")
46+
hostKeyCallback := func(hostname string, remote net.Addr, key ssh.PublicKey) error {
47+
keyStr := base64.StdEncoding.EncodeToString([]byte(key.Marshal()))
48+
hostKeyCh <- fmt.Sprintf("%s %s", key.Type(), keyStr)
49+
return hostKeyError
50+
}
51+
52+
config := &ssh.ClientConfig{
53+
HostKeyCallback: hostKeyCallback,
54+
Timeout: 5 * time.Second,
55+
}
56+
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%v", host, port), config)
57+
if err != nil && !strings.Contains(err.Error(), hostKeyError.Error()) {
58+
return err
59+
}
60+
61+
// Authentication errors will cause client to be nil
62+
if client != nil {
63+
client.Close()
64+
}
65+
hostKey := <-hostKeyCh
66+
67+
d.Set("public_host_key", fmt.Sprintf("%s %s", host, hostKey))
68+
d.SetId(time.Now().UTC().String())
69+
70+
return nil
71+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package provider
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
5+
"testing"
6+
)
7+
8+
func TestAccSshKeyScan_dataSource(t *testing.T) {
9+
resource.UnitTest(t, resource.TestCase{
10+
Providers: testProviders,
11+
12+
Steps: []resource.TestStep{
13+
{
14+
Config: `
15+
data "tls_ssh_key_scan" "test" {
16+
host = "github.com"
17+
}
18+
`,
19+
Check: resource.ComposeAggregateTestCheckFunc(
20+
resource.TestCheckResourceAttr("data.tls_ssh_key_scan.test", "public_host_key", "github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ=="),
21+
),
22+
},
23+
},
24+
})
25+
}

internal/provider/provider.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ func New() *schema.Provider {
1818
"tls_cert_request": resourceCertRequest(),
1919
},
2020
DataSourcesMap: map[string]*schema.Resource{
21-
"tls_public_key": dataSourcePublicKey(),
22-
"tls_certificate": dataSourceTlsCertificate(),
21+
"tls_public_key": dataSourcePublicKey(),
22+
"tls_certificate": dataSourceTlsCertificate(),
23+
"tls_ssh_key_scan": dataSourceSshKeyScan(),
2324
},
2425
}
2526
}

0 commit comments

Comments
 (0)