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

prevent mixing of resource types #131

Merged
merged 1 commit into from
Apr 20, 2024
Merged
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
6 changes: 3 additions & 3 deletions internal/bitwarden/bw/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Client interface {
CreateObject(Object) (*Object, error)
EditObject(Object) (*Object, error)
GetAttachment(itemId, attachmentId string) ([]byte, error)
GetObject(objType, itemId string) (*Object, error)
GetObject(objType, itemOrSearch string) (*Object, error)
GetSessionKey() string
HasSessionKey() bool
LoginWithAPIKey(password, clientId, clientSecret string) error
Expand Down Expand Up @@ -137,8 +137,8 @@ func (c *client) EditObject(obj Object) (*Object, error) {
return &obj, nil
}

func (c *client) GetObject(objType, itemId string) (*Object, error) {
out, err := c.cmdWithSession("get", objType, itemId).Run()
func (c *client) GetObject(objType, itemOrSearch string) (*Object, error) {
out, err := c.cmdWithSession("get", objType, itemOrSearch).Run()
if err != nil {
return nil, remapError(err)
}
Expand Down
29 changes: 10 additions & 19 deletions internal/provider/data_source_item_login_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package provider

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/stretchr/testify/assert"
)

func TestAccDataSourceItemLoginAttributes(t *testing.T) {
Expand Down Expand Up @@ -40,46 +38,39 @@ func TestAccDataSourceItemLoginFailsOnInexistentItem(t *testing.T) {
})
}

func TestAccDataSourceItemLoginDeleted(t *testing.T) {
var objectID string

func TestAccDataSourceItemLoginFailsOnWrongResourceType(t *testing.T) {
ensureVaultwardenConfigured(t)

resource.UnitTest(t, resource.TestCase{
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: tfConfigProvider() + tfConfigResourceItemLoginSmall(),
Check: getObjectID("bitwarden_item_login.foo", &objectID),
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemSecureNote(),
},
{
Config: tfConfigProvider() + tfConfigResourceItemLoginSmall() + tfConfigDataItemLoginWithId(objectID),
PreConfig: func() {
err := bwTestClient(t).DeleteObject("item", objectID)
assert.NoError(t, err)
},
ExpectError: regexp.MustCompile("Error: object not found"),
Config: tfConfigProvider() + tfConfigResourceFolder() + tfConfigResourceItemSecureNote() + tfConfigDataItemLoginCrossReference(),
ExpectError: regexp.MustCompile("Error: returned object type does not match requested object type"),
},
},
})
}

func tfConfigDataItemLoginWithId(id string) string {
return fmt.Sprintf(`
func tfConfigDataItemLogin() string {
return `
data "bitwarden_item_login" "foo_data" {
provider = bitwarden

id = "%s"
id = bitwarden_item_login.foo.id
}
`, id)
`
}

func tfConfigDataItemLogin() string {
func tfConfigDataItemLoginCrossReference() string {
return `
data "bitwarden_item_login" "foo_data" {
provider = bitwarden

id = bitwarden_item_login.foo.id
id = bitwarden_item_secure_note.foo.id
}
`
}
Expand Down
20 changes: 15 additions & 5 deletions internal/provider/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,22 @@ func objectCreate(ctx context.Context, d *schema.ResourceData, meta interface{})
func objectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return diag.FromErr(objectOperation(ctx, d, func(secret bw.Object) (*bw.Object, error) {
obj, err := meta.(bw.Client).GetObject(string(secret.Object), secret.ID)
if obj != nil {
// If the object exists but is marked as soft deleted, we return an error, because relying
// on an object in the 'trash' sounds like a bad idea.
if obj.DeletedDate != nil {
return nil, errors.New("object is soft deleted")
}

if obj.ID != secret.ID {
return nil, errors.New("returned object ID does not match requested object ID")
}

// If the object exists but is marked as soft deleted, we return an error, because relying
// on an object in the 'trash' sounds like a bad idea.
if obj != nil && obj.DeletedDate != nil {
return nil, errors.New("object is soft deleted")
if obj.Type != secret.Type {
return nil, errors.New("returned object type does not match requested object type")
}
}

return obj, err
}))
}
Expand Down Expand Up @@ -57,7 +67,7 @@ func objectDelete(ctx context.Context, d *schema.ResourceData, meta interface{})
}))
}

func objectOperation(ctx context.Context, d *schema.ResourceData, operation func(secret bw.Object) (*bw.Object, error)) error {
func objectOperation(_ context.Context, d *schema.ResourceData, operation func(secret bw.Object) (*bw.Object, error)) error {
obj, err := operation(objectStructFromData(d))
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func New(version string) func() *schema.Provider {
}
}

func providerConfigure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
func providerConfigure(version string, _ *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
return func(_ context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {

bwClient, err := newBitwardenClient(d, version)
Expand Down
Loading