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

Add fields to each items default section #80

Closed
Closed
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
15 changes: 14 additions & 1 deletion docs/data-sources/item.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ data "onepassword_item" "example" {

### Read-only

- **category** (String, Read-only) The category of the item. One of ["login" "password" "database"]
- **category** (String, Read-only) The category of the item. One of ["login" "password" "database" "secure_note"]
- **database** (String, Read-only) (Only applies to the database category) The name of the database.
- **field** (Block List) A list of custom fields in an item (see [below for nested schema](#nestedblock--field))
- **hostname** (String, Read-only) (Only applies to the database category) The address where the database can be found
- **id** (String, Read-only) The Terraform resource identifier for this item in the format `vaults/<vault_id>/items/<item_id>`
- **password** (String, Read-only) Password for this item.
Expand All @@ -44,6 +45,18 @@ data "onepassword_item" "example" {
- **url** (String, Read-only) The primary URL for the item.
- **username** (String, Read-only) Username for this item.

<a id="nestedblock--field"></a>
### Nested Schema for `field`

Read-only:

- **id** (String, Read-only) A unique identifier for the field.
- **label** (String, Read-only) The label for the field.
- **purpose** (String, Read-only) Purpose indicates this is a special field: a username, password, or notes field. One of ["USERNAME" "PASSWORD" "NOTES"]
- **type** (String, Read-only) The type of value stored in the field. One of ["STRING" "EMAIL" "CONCEALED" "URL" "OTP" "DATE" "MONTH_YEAR" "MENU"]
- **value** (String, Read-only) The value of the field.


<a id="nestedatt--section"></a>
### Nested Schema for `section`

Expand Down
39 changes: 36 additions & 3 deletions docs/resources/item.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ resource "onepassword_item" "demo_db" {

### Optional

- **category** (String, Optional) The category of the item. One of ["login" "password" "database"]
- **category** (String, Optional) The category of the item. One of ["login" "password" "database" "secure_note"]
- **database** (String, Optional) (Only applies to the database category) The name of the database.
- **field** (Block List) A list of custom fields in an item (see [below for nested schema](#nestedblock--field))
- **hostname** (String, Optional) (Only applies to the database category) The address where the database can be found
- **note_value** (String, Optional) Secure Note value.
- **password** (String, Optional) Password for this item.
- **password_recipe** (Block List, Max: 1) Password for this item. (see [below for nested schema](#nestedblock--password_recipe))
- **port** (String, Optional) (Only applies to the database category) The port the database is listening on.
Expand All @@ -72,6 +74,35 @@ resource "onepassword_item" "demo_db" {
- **id** (String, Read-only) The Terraform resource identifier for this item in the format `vaults/<vault_id>/items/<item_id>`.
- **uuid** (String, Read-only) The UUID of the item. Item identifiers are unique within a specific vault.

<a id="nestedblock--field"></a>
### Nested Schema for `field`

Required:

- **label** (String, Required) The label for the field.

Optional:

- **password_recipe** (Block List, Max: 1) Password for this item. (see [below for nested schema](#nestedblock--field--password_recipe))
- **type** (String, Optional) The type of value stored in the field. One of ["STRING" "EMAIL" "CONCEALED" "URL" "OTP" "DATE" "MONTH_YEAR" "MENU"]
- **value** (String, Optional) The value of the field.

Read-only:

- **id** (String, Read-only) A unique identifier for the field.

<a id="nestedblock--field--password_recipe"></a>
### Nested Schema for `field.password_recipe`

Optional:

- **digits** (Boolean, Optional) Use digits [0-9] when generating the password.
- **length** (Number, Optional) The length of the password to be generated.
- **letters** (Boolean, Optional) Use letters [a-zA-Z] when generating the password.
- **symbols** (Boolean, Optional) Use symbols [!@.-_*] when generating the password.



<a id="nestedblock--password_recipe"></a>
### Nested Schema for `password_recipe`

Expand Down Expand Up @@ -107,12 +138,14 @@ Required:

Optional:

- **id** (String, Optional) A unique identifier for the field.
- **password_recipe** (Block List, Max: 1) Password for this item. (see [below for nested schema](#nestedblock--section--field--password_recipe))
- **purpose** (String, Optional) Purpose indicates this is a special field: a username, password, or notes field. One of ["USERNAME" "PASSWORD" "NOTES"]
- **type** (String, Optional) The type of value stored in the field. One of ["STRING" "EMAIL" "CONCEALED" "URL" "OTP" "DATE" "MONTH_YEAR" "MENU"]
- **value** (String, Optional) The value of the field.

Read-only:

- **id** (String, Read-only) A unique identifier for the field.

<a id="nestedblock--section--field--password_recipe"></a>
### Nested Schema for `section.field.password_recipe`

Expand Down
96 changes: 62 additions & 34 deletions onepassword/data_source_onepassword_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,35 @@ import (
func dataSourceOnepasswordItem() *schema.Resource {
exactlyOneOfUUIDAndTitle := []string{"uuid", "title"}

fieldSchema := map[string]*schema.Schema{
"id": {
Description: fieldIDDescription,
Type: schema.TypeString,
Computed: true,
},
"label": {
Description: fieldLabelDescription,
Type: schema.TypeString,
Computed: true,
},
"purpose": {
Description: fmt.Sprintf(enumDescription, fieldPurposeDescription, fieldPurposes),
Type: schema.TypeString,
Computed: true,
},
"type": {
Description: fmt.Sprintf(enumDescription, fieldTypeDescription, fieldTypes),
Type: schema.TypeString,
Computed: true,
},
"value": {
Description: fieldValueDescription,
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
}

return &schema.Resource{
Description: "Use this data source to get details of an item by its vault uuid and either the title or the uuid of the item.",
Read: dataSourceOnepasswordItemRead,
Expand Down Expand Up @@ -95,6 +124,17 @@ func dataSourceOnepasswordItem() *schema.Resource {
Optional: true,
Sensitive: true,
},
"field": {
Description: fieldsDescription,
Type: schema.TypeList,
Computed: true,
Optional: true,
MinItems: 0,
Elem: &schema.Resource{
Description: fieldDescription,
Schema: fieldSchema,
},
},
"section": {
Description: sectionsDescription,
Type: schema.TypeList,
Expand All @@ -120,34 +160,7 @@ func dataSourceOnepasswordItem() *schema.Resource {
MinItems: 0,
Elem: &schema.Resource{
Description: fieldDescription,
Schema: map[string]*schema.Schema{
"id": {
Description: fieldIDDescription,
Type: schema.TypeString,
Computed: true,
},
"label": {
Description: fieldLabelDescription,
Type: schema.TypeString,
Computed: true,
},
"purpose": {
Description: fmt.Sprintf(enumDescription, fieldPurposeDescription, fieldPurposes),
Type: schema.TypeString,
Computed: true,
},
"type": {
Description: fmt.Sprintf(enumDescription, fieldTypeDescription, fieldTypes),
Type: schema.TypeString,
Computed: true,
},
"value": {
Description: fieldValueDescription,
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
},
Schema: fieldSchema,
},
},
},
Expand Down Expand Up @@ -193,7 +206,6 @@ func dataSourceOnepasswordItemRead(data *schema.ResourceData, meta interface{})
dataField := map[string]interface{}{}
dataField["id"] = f.ID
dataField["label"] = f.Label
dataField["purpose"] = f.Purpose
dataField["type"] = f.Type
dataField["value"] = f.Value

Expand All @@ -207,20 +219,36 @@ func dataSourceOnepasswordItemRead(data *schema.ResourceData, meta interface{})

data.Set("section", dataSections)

fields := []interface{}{}
for _, f := range item.Fields {
switch f.Purpose {
case "USERNAME":
switch f.Label {
case "username":
data.Set("username", f.Value)
case "PASSWORD":
case "password":
data.Set("password", f.Value)
case "NOTES":
case "hostname":
data.Set("hostname", f.Value)
case "database":
data.Set("database", f.Value)
case "port":
data.Set("port", f.Value)
case "type":
data.Set("type", f.Value)
case "notesPlain":
data.Set("note_value", f.Value)
default:
if f.Section == nil {
data.Set(strings.ToLower(f.Label), f.Value)
field := make(map[string]interface{})
field["id"] = f.ID
field["label"] = f.Label
field["purpose"] = f.Purpose
field["type"] = f.Type
field["value"] = f.Value
fields = append(fields, field)
}
}
}
data.Set("field", fields)

return nil
}
Expand Down
97 changes: 78 additions & 19 deletions onepassword/data_source_onepassword_item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ func TestDataSourceOnePasswordItemReadWithSections(t *testing.T) {
compareItemToSource(t, dataSourceData, expectedItem)
}

func TestDataSourceOnePasswordItemReadWithFields(t *testing.T) {
expectedItem := generateItem()
meta := &testClient{
GetItemFunc: func(uuid string, vaultUUID string) (*onepassword.Item, error) {
return expectedItem, nil
},
}
expectedItem.Fields = append(expectedItem.Fields, &onepassword.ItemField{
ID: "98765",
Type: "CONCEALED",
Label: "Secret Field",
Value: "Very secret",
})

dataSourceData := generateDataSource(t, expectedItem)
dataSourceData.Set("uuid", expectedItem.ID)

err := dataSourceOnepasswordItemRead(dataSourceData, meta)
if err != nil {
t.Errorf("Unexpected error occured")
}
compareItemToSource(t, dataSourceData, expectedItem)
}

func compareItemToSource(t *testing.T, dataSourceData *schema.ResourceData, item *onepassword.Item) {
if dataSourceData.Get("uuid") != item.ID {
t.Errorf("Expected uuid to be %v got %v", item.ID, dataSourceData.Get("uuid"))
Expand All @@ -94,33 +118,53 @@ func compareItemToSource(t *testing.T, dataSourceData *schema.ResourceData, item
}
compareStringSlice(t, getTags(dataSourceData), item.Tags)

predefinedFields := []string{"username", "password", "hostname", "database", "port", "type"}
for _, f := range item.Fields {
path := f.Label
if f.Section != nil {
sectionIndex := 0
fieldIndex := 0
sections := dataSourceData.Get("section").([]interface{})

for i, section := range sections {
s := section.(map[string]interface{})
if s["label"] == f.Section.Label ||
(f.Section.ID != "" && s["id"] == f.Section.ID) {
sectionIndex = i
sectionFields := dataSourceData.Get(fmt.Sprintf("section.%d.field", i)).([]interface{})

for j, field := range sectionFields {
df := field.(map[string]interface{})
if df["label"] == f.Label {
fieldIndex = j
if !contains(t, predefinedFields, f.Label) {
if f.Section != nil {
sectionIndex := 0
fieldIndex := 0
sections := dataSourceData.Get("section").([]interface{})

for i, section := range sections {
s := section.(map[string]interface{})
if s["label"] == f.Section.Label ||
(f.Section.ID != "" && s["id"] == f.Section.ID) {
sectionIndex = i
sectionFields := dataSourceData.Get(fmt.Sprintf("section.%d.field", i)).([]interface{})

for j, field := range sectionFields {
df := field.(map[string]interface{})
if df["label"] == f.Label {
fieldIndex = j
}
}
}
}
}

if len(sections) > 0 {
path = fmt.Sprintf("section.%d.field.%d.value", sectionIndex, fieldIndex)
if len(sections) > 0 {
path = fmt.Sprintf("section.%d.field.%d.value", sectionIndex, fieldIndex)
}
} else {
fieldIndex := 0
fields := dataSourceData.Get("field").([]interface{})

for i, field := range fields {
df := field.(map[string]interface{})
if df["label"] == f.Label {
fieldIndex = i
}
}

if len(fields) > 0 {
path = fmt.Sprintf("field.%d.value", fieldIndex)
}
}
}
if f.Label == "notesPlain" {
path = "note_value"
}
if dataSourceData.Get(path) != f.Value {
t.Errorf("Expected field %v to be %v got %v", f.Label, f.Value, dataSourceData.Get(path))
}
Expand Down Expand Up @@ -176,6 +220,10 @@ func generateFields() []*onepassword.ItemField {
Label: "type",
Value: "test_type",
},
{
Label: "notesPlain",
Value: "test_note",
},
}
return fields
}
Expand All @@ -192,3 +240,14 @@ func compareStringSlice(t *testing.T, actual, expected []string) {
}
}
}

func contains(t *testing.T, s []string, e string) bool {
t.Helper()

for _, a := range s {
if a == e {
return true
}
}
return false
}
Loading