From 14bc6a4453cad0d055256fdda299ca491ff06a24 Mon Sep 17 00:00:00 2001 From: Louis Ruch Date: Tue, 13 Sep 2022 08:21:24 -0700 Subject: [PATCH] feat(target): Support ssh target --- docs/resources/target.md | 15 +++++ .../resources/boundary_target/resource.tf | 15 +++++ internal/provider/resource_target.go | 57 ++++++++++++++----- 3 files changed, 74 insertions(+), 13 deletions(-) diff --git a/docs/resources/target.md b/docs/resources/target.md index 81fe4a60..a32aacfd 100644 --- a/docs/resources/target.md +++ b/docs/resources/target.md @@ -47,6 +47,7 @@ resource "boundary_credential_library_vault" "foo" { credential_store_id = boundary_credential_store_vault.foo.id path = "my/secret/foo" # change to Vault backend path http_method = "GET" + credential_type = "username_password" } resource "boundary_host_catalog" "foo" { @@ -96,6 +97,20 @@ resource "boundary_target" "foo" { boundary_credential_library_vault.foo.id ] } + +resource "boundary_target" "ssh_foo" { + name = "ssh_foo" + description = "Ssh target" + type = "ssh" + default_port = "22" + scope_id = boundary_scope.project.id + host_source_ids = [ + boundary_host_set.foo.id + ] + injected_application_credential_source_ids = [ + boundary_credential_library_vault.foo.id + ] +} ``` diff --git a/examples/resources/boundary_target/resource.tf b/examples/resources/boundary_target/resource.tf index 3e55a54a..e22bd7f9 100644 --- a/examples/resources/boundary_target/resource.tf +++ b/examples/resources/boundary_target/resource.tf @@ -32,6 +32,7 @@ resource "boundary_credential_library_vault" "foo" { credential_store_id = boundary_credential_store_vault.foo.id path = "my/secret/foo" # change to Vault backend path http_method = "GET" + credential_type = "username_password" } resource "boundary_host_catalog" "foo" { @@ -81,3 +82,17 @@ resource "boundary_target" "foo" { boundary_credential_library_vault.foo.id ] } + +resource "boundary_target" "ssh_foo" { + name = "ssh_foo" + description = "Ssh target" + type = "ssh" + default_port = "22" + scope_id = boundary_scope.project.id + host_source_ids = [ + boundary_host_set.foo.id + ] + injected_application_credential_source_ids = [ + boundary_credential_library_vault.foo.id + ] +} diff --git a/internal/provider/resource_target.go b/internal/provider/resource_target.go index 9723eb50..4c17f8d2 100644 --- a/internal/provider/resource_target.go +++ b/internal/provider/resource_target.go @@ -21,6 +21,7 @@ const ( targetWorkerFilterKey = "worker_filter" targetTypeTcp = "tcp" + targetTypeSsh = "ssh" ) func resourceTarget() *schema.Resource { @@ -154,7 +155,7 @@ func setFromTargetResponseMap(d *schema.ResourceData, raw map[string]interface{} } switch raw["type"].(string) { - case targetTypeTcp: + case targetTypeTcp, targetTypeSsh: if attrsVal, ok := raw["attributes"]; ok { attrs := attrsVal.(map[string]interface{}) if defPort, ok := attrs["default_port"].(json.Number); ok { @@ -187,7 +188,7 @@ func resourceTargetCreate(ctx context.Context, d *schema.ResourceData, meta inte return diag.Errorf("no type provided") } switch typeStr { - case targetTypeTcp: + case targetTypeTcp, targetTypeSsh: default: return diag.Errorf("invalid type provided") } @@ -208,10 +209,15 @@ func resourceTargetCreate(ctx context.Context, d *schema.ResourceData, meta inte defaultPortVal, ok := d.GetOk(targetDefaultPortKey) if ok { defaultPortInt := defaultPortVal.(int) - if defaultPortInt < 0 { - return diag.Errorf(`"default_port" cannot be less than zero`) + if defaultPortInt < 0 || defaultPortInt > 65535 { + return diag.Errorf(`"default_port" must be a valid tcp port`) + } + switch typeStr { + case targetTypeTcp: + opts = append(opts, targets.WithTcpTargetDefaultPort(uint32(defaultPortInt))) + case targetTypeSsh: + opts = append(opts, targets.WithSshTargetDefaultPort(uint32(defaultPortInt))) } - opts = append(opts, targets.WithTcpTargetDefaultPort(uint32(defaultPortInt))) } sessionMaxSecondsVal, ok := d.GetOk(targetSessionMaxSecondsKey) @@ -344,6 +350,16 @@ func resourceTargetUpdate(ctx context.Context, d *schema.ResourceData, meta inte var opts []targets.Option + var typeStr string + if typeVal, ok := d.GetOk(TypeKey); ok { + typeStr = typeVal.(string) + } + switch typeStr { + case targetTypeTcp, targetTypeSsh: + default: + return diag.Errorf("invalid type provided") + } + var name *string if d.HasChange(NameKey) { opts = append(opts, targets.DefaultName()) @@ -368,15 +384,30 @@ func resourceTargetUpdate(ctx context.Context, d *schema.ResourceData, meta inte var defaultPort *int if d.HasChange(targetDefaultPortKey) { - opts = append(opts, targets.DefaultTcpTargetDefaultPort()) - defaultPortVal, ok := d.GetOk(targetDefaultPortKey) - if ok { - defaultPortInt := defaultPortVal.(int) - if defaultPortInt < 0 { - return diag.Errorf(`"default_port" cannot be less than zero`) + switch typeStr { + case targetTypeTcp: + opts = append(opts, targets.DefaultTcpTargetDefaultPort()) + defaultPortVal, ok := d.GetOk(targetDefaultPortKey) + if ok { + defaultPortInt := defaultPortVal.(int) + if defaultPortInt < 0 || defaultPortInt > 65535 { + return diag.Errorf(`"default_port" must be a valid tcp port`) + } + defaultPort = &defaultPortInt + opts = append(opts, targets.WithTcpTargetDefaultPort(uint32(defaultPortInt))) + } + + case targetTypeSsh: + opts = append(opts, targets.DefaultSshTargetDefaultPort()) + defaultPortVal, ok := d.GetOk(targetDefaultPortKey) + if ok { + defaultPortInt := defaultPortVal.(int) + if defaultPortInt < 0 || defaultPortInt > 65535 { + return diag.Errorf(`"default_port" must be a valid tcp port`) + } + defaultPort = &defaultPortInt + opts = append(opts, targets.WithSshTargetDefaultPort(uint32(defaultPortInt))) } - defaultPort = &defaultPortInt - opts = append(opts, targets.WithTcpTargetDefaultPort(uint32(defaultPortInt))) } }