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

Feature: File attachments to items #164

Closed
wants to merge 2 commits into from
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: 15 additions & 0 deletions onepassword/cli/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ func (op *OP) delete(ctx context.Context, item *onepassword.Item, vaultUuid stri
return nil, op.execJson(ctx, nil, nil, p("item"), p("delete"), p(item.ID), f("vault", vaultUuid))
}

func (op *OP) GetFileContent(ctx context.Context, file *onepassword.File) ([]byte, error) {
versionErr := op.checkCliVersion(ctx)
if versionErr != nil {
return nil, versionErr
}

path := file.ContentPath
path = strings.ReplaceAll(path, "/v1/vaults/", "op://")
path = strings.ReplaceAll(path, "/items/", "/")
path = strings.ReplaceAll(path, "/files/", "/")
path = strings.ReplaceAll(path, "/content", "")

return op.execRaw(ctx, nil, p("read"), p(path))
}

func (op *OP) execJson(ctx context.Context, dst any, stdin []byte, args ...opArg) error {
result, err := op.execRaw(ctx, stdin, args...)
if err != nil {
Expand Down
43 changes: 43 additions & 0 deletions onepassword/data_source_onepassword_item.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,32 @@ func dataSourceOnepasswordItem() *schema.Resource {
Optional: true,
Sensitive: true,
},
"files": {
Description: "Files",
Type: schema.TypeList,
Computed: true,
MinItems: 0,
Elem: &schema.Resource{
Description: "File",
Schema: map[string]*schema.Schema{
"id": {
Description: fieldIDDescription,
Type: schema.TypeString,
Computed: true,
},
"content": {
Description: "File contents",
Type: schema.TypeString,
Computed: true,
},
"name": {
Description: "File name",
Type: schema.TypeString,
Computed: true,
},
},
},
},
"section": {
Description: sectionsDescription,
Type: schema.TypeList,
Expand Down Expand Up @@ -223,6 +249,23 @@ func dataSourceOnepasswordItemRead(ctx context.Context, data *schema.ResourceDat
}
}

files := []interface{}{}

for _, f := range item.Files {
bytes, err := client.GetFileContent(ctx, f)
if err != nil {
return diag.FromErr(err)
}
fileItem := map[string]interface{}{}
fileItem["id"] = f.ID
fileItem["name"] = f.Name
fileItem["content"] = string(bytes)

files = append(files, fileItem)
}

data.Set("files", files)

return nil
}

Expand Down
5 changes: 5 additions & 0 deletions onepassword/mock_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type testClient struct {
CreateItemFunc func(item *onepassword.Item, vaultUUID string) (*onepassword.Item, error)
UpdateItemFunc func(item *onepassword.Item, vaultUUID string) (*onepassword.Item, error)
DeleteItemFunc func(item *onepassword.Item, vaultUUID string) error
GetFileContentFunc func(file *onepassword.File) ([]byte, error)
}

var _ Client = (*testClient)(nil)
Expand Down Expand Up @@ -45,3 +46,7 @@ func (m *testClient) DeleteItem(_ context.Context, item *onepassword.Item, vault
func (m *testClient) UpdateItem(_ context.Context, item *onepassword.Item, vaultUUID string) (*onepassword.Item, error) {
return m.UpdateItemFunc(item, vaultUUID)
}

func (m *testClient) GetFileContent(_ context.Context, file *onepassword.File) ([]byte, error) {
return m.GetFileContentFunc(file)
}
1 change: 1 addition & 0 deletions onepassword/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,5 @@ type Client interface {
CreateItem(ctx context.Context, item *onepassword.Item, vaultUuid string) (*onepassword.Item, error)
UpdateItem(ctx context.Context, item *onepassword.Item, vaultUuid string) (*onepassword.Item, error)
DeleteItem(ctx context.Context, item *onepassword.Item, vaultUuid string) error
GetFileContent(ctx context.Context, file *onepassword.File) ([]byte, error)
}