Skip to content

Commit

Permalink
Merge pull request #5 from laszlojau/main
Browse files Browse the repository at this point in the history
Return values from multiple secret fields
  • Loading branch information
breed808 authored Sep 30, 2021
2 parents 0271e57 + e70a998 commit 127397c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 44 deletions.
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ packer {
required_plugins {
tss = {
version = ">= 0.1.0"
source = "github.com/breed808/packer-plugin-tss"
source = "github.com/breed808/tss"
}
}
}
Expand Down Expand Up @@ -54,7 +54,8 @@ data "tss" "mock-data" {
password = "test123" # TSS password
server_url = "https://my-thycotic-server.example.com/SecretServer"
secret_id = "500" # ID of TSS secret to retrieve
secret_id = 500 # ID of TSS secret to retrieve
secret_fields = ["username", "password"] # Fields to retrieve from the TSS secret
}
```

Expand All @@ -67,7 +68,8 @@ data "tss" "mock-data" {
server_url = "https://my-thycotic-server.example.com/SecretServer"
domain = "example.com" # Domain of user. I.E. testing@example.com
secret_id = "500" # ID of TSS secret to retrieve
secret_id = 500 # ID of TSS secret to retrieve
secret_fields = ["username", "password"] # Fields to retrieve from the TSS secret
}
```

Expand All @@ -84,7 +86,8 @@ data "tss" "mock-data" {
password = "test123" # TSS password
server_url = "https://my-thycotic-server.example.com/SecretServer"
secret_id = "500" # ID of TSS secret to retrieve
secret_id = 500 # ID of TSS secret to retrieve
secret_fields = ["username", "password"] # Fields to retrieve from the TSS secret
}
build {
Expand All @@ -105,12 +108,21 @@ build {
datacenter = "PackerDatacenter"
datastore = "datastore1"
host = "123.45.678.9"
password = data.mock-data.password
username = data.mock-data.username
password = data.mock-data.fields.username
username = data.mock-data.fields.password
}
}
}
```

**NOTE:** Packer does not seem to support sensitive values from custom data sources yet. If you are passing the variables to provisioners and wish to keep them sensitive, you can create a sensitive local.

```hcl
local "my_secret_password" {
expression = "${data.mock-data.fields.password}"
sensitive = true
}
```

## Packer Compatibility
This template is compatible with Packer >= v1.7.0
37 changes: 17 additions & 20 deletions datasource/tss/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package tss

import (
"fmt"

"packer-plugin-tss/common"

"github.com/hashicorp/hcl/v2/hcldec"
Expand All @@ -14,19 +13,19 @@ import (

type Config struct {
common.AuthConfig `mapstructure:",squash"`
SecretID int `mapstructure:"secret_id" required:"true"`
SecretID int `mapstructure:"secret_id" required:"true"`
SecretFields []string `mapstructure:"secret_fields" required:"true"`
}

type Datasource struct {
config Config
}

type DatasourceOutput struct {
// Secret ID in TSS.
ID int `mapstructure:"id"`

// Though TSS stores other fields, retrieve only credential details (username & password) for now.
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
// Values of the requested Secret Fields.
Fields map[string]string `mapstructure:"fields"`
}

func (d *Datasource) ConfigSpec() hcldec.ObjectSpec {
Expand All @@ -50,32 +49,30 @@ func (d *Datasource) OutputSpec() hcldec.ObjectSpec {
}

func (d *Datasource) Execute() (cty.Value, error) {
output := DatasourceOutput{}

emptyOutput := hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec())

client, err := d.config.CreateClient()
if err != nil {
return emptyOutput, err
return cty.NullVal(cty.EmptyObject), err
}

// TSS SDK only supports retrieving secrets by ID
secret, err := client.Secret(d.config.SecretID)
if err != nil {
return emptyOutput, err
return cty.NullVal(cty.EmptyObject), err
}

output.ID = secret.ID
secretFields := make(map[string]string, len(d.config.SecretFields))

var success bool
output.Username, success = secret.Field("username")
if !success {
output.Username = ""
for _, field := range d.config.SecretFields {
var success bool
secretFields[field], success = secret.Field(field)
if !success {
secretFields[field] = ""
}
}

output.Password, success = secret.Field("password")
if !success {
output.Password = ""
output := DatasourceOutput{
ID: secret.ID,
Fields: secretFields,
}

return hcl2helper.HCL2ValueFromConfig(output, d.OutputSpec()), nil
Expand Down
32 changes: 16 additions & 16 deletions datasource/tss/data.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions example/data.pkr.hcl
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
data "tss" "mock-data" {
username = "testing"
password = "test123"
username = "testing"
password = "test123"
server_url = "https://my-thycotic-server.example.com/SecretServer"

secret_id = "500"
secret_fields = [
"password",
"username",
]
}

0 comments on commit 127397c

Please sign in to comment.