forked from hashicorp/terraform-provider-tls
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
From hashicorp#97 (hashicorp#95 would also work instead)
- Loading branch information
Showing
3 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package provider | ||
|
||
import ( | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"strings" | ||
"time" | ||
|
||
"golang.org/x/crypto/ssh" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceSshKeyScan() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceSshKeyScanRead, | ||
Schema: map[string]*schema.Schema{ | ||
"host": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Host to ssh key scan.", | ||
}, | ||
"port": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Default: 22, | ||
Description: "Port to key scan", | ||
}, | ||
"public_host_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "Result of ssh key scan.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceSshKeyScanRead(d *schema.ResourceData, meta interface{}) error { | ||
host := d.Get("host").(string) | ||
port := d.Get("port").(int) | ||
|
||
hostKeyCh := make(chan string, 1) | ||
hostKeyError := errors.New("ignoring host key verification") | ||
hostKeyCallback := func(hostname string, remote net.Addr, key ssh.PublicKey) error { | ||
keyStr := base64.StdEncoding.EncodeToString([]byte(key.Marshal())) | ||
hostKeyCh <- fmt.Sprintf("%s %s", key.Type(), keyStr) | ||
return hostKeyError | ||
} | ||
|
||
config := &ssh.ClientConfig{ | ||
HostKeyCallback: hostKeyCallback, | ||
Timeout: 5 * time.Second, | ||
} | ||
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%v", host, port), config) | ||
if err != nil && !strings.Contains(err.Error(), hostKeyError.Error()) { | ||
return err | ||
} | ||
|
||
// Authentication errors will cause client to be nil | ||
if client != nil { | ||
client.Close() | ||
} | ||
hostKey := <-hostKeyCh | ||
|
||
d.Set("public_host_key", fmt.Sprintf("%s %s", host, hostKey)) | ||
d.SetId(time.Now().UTC().String()) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package provider | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"testing" | ||
) | ||
|
||
func TestAccSshKeyScan_dataSource(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
Providers: testProviders, | ||
|
||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
data "tls_ssh_key_scan" "test" { | ||
host = "github.com" | ||
} | ||
`, | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.tls_ssh_key_scan.test", "public_host_key", "github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ=="), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters