Skip to content

Commit

Permalink
move and rename models
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse committed Sep 29, 2024
1 parent 7b0a86b commit f6d7453
Show file tree
Hide file tree
Showing 40 changed files with 367 additions and 253 deletions.
34 changes: 0 additions & 34 deletions internal/bitwarden/bw/client_options.go

This file was deleted.

15 changes: 0 additions & 15 deletions internal/bitwarden/bw/filter.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package bw
package bwcli

import (
"context"
Expand All @@ -7,31 +7,33 @@ import (
"fmt"
"os"

"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/command"
)

type Client interface {
CreateAttachment(ctx context.Context, itemId, filePath string) (*Object, error)
CreateObject(context.Context, Object) (*Object, error)
EditObject(context.Context, Object) (*Object, error)
type CLIClient interface {
CreateAttachment(ctx context.Context, itemId, filePath string) (*models.Object, error)
CreateObject(context.Context, models.Object) (*models.Object, error)
EditObject(context.Context, models.Object) (*models.Object, error)
GetAttachment(ctx context.Context, itemId, attachmentId string) ([]byte, error)
GetObject(context.Context, Object) (*Object, error)
GetObject(context.Context, models.Object) (*models.Object, error)
GetSessionKey() string
HasSessionKey() bool
ListObjects(ctx context.Context, objType string, options ...ListObjectsOption) ([]Object, error)
ListObjects(ctx context.Context, objType models.ObjectType, options ...bitwarden.ListObjectsOption) ([]models.Object, error)
LoginWithAPIKey(ctx context.Context, password, clientId, clientSecret string) error
LoginWithPassword(ctx context.Context, username, password string) error
Logout(context.Context) error
DeleteAttachment(ctx context.Context, itemId, attachmentId string) error
DeleteObject(context.Context, Object) error
DeleteObject(context.Context, models.Object) error
SetServer(context.Context, string) error
SetSessionKey(string)
Status(context.Context) (*Status, error)
Sync(context.Context) error
Unlock(ctx context.Context, password string) error
}

func NewClient(execPath string, opts ...Options) Client {
func NewClient(execPath string, opts ...Options) CLIClient {
c := &client{
execPath: execPath,
}
Expand All @@ -55,33 +57,33 @@ type client struct {
sessionKey string
}

type Options func(c Client)
type Options func(c bitwarden.Client)

func WithAppDataDir(appDataDir string) Options {
return func(c Client) {
return func(c bitwarden.Client) {
c.(*client).appDataDir = appDataDir
}
}

func WithExtraCACertsPath(extraCACertsPath string) Options {
return func(c Client) {
return func(c bitwarden.Client) {
c.(*client).extraCACertsPath = extraCACertsPath
}
}

func DisableSync() Options {
return func(c Client) {
return func(c bitwarden.Client) {
c.(*client).disableSync = true
}
}

func DisableRetryBackoff() Options {
return func(c Client) {
return func(c bitwarden.Client) {
c.(*client).disableRetryBackoff = true
}
}

func (c *client) CreateObject(ctx context.Context, obj Object) (*Object, error) {
func (c *client) CreateObject(ctx context.Context, obj models.Object) (*models.Object, error) {
objEncoded, err := c.encode(obj)
if err != nil {
return nil, err
Expand All @@ -93,7 +95,7 @@ func (c *client) CreateObject(ctx context.Context, obj Object) (*Object, error)
objEncoded,
}

if obj.Object == ObjectTypeOrgCollection {
if obj.Object == models.ObjectTypeOrgCollection {
args = append(args, "--organizationid", obj.OrganizationID)
}

Expand All @@ -111,13 +113,13 @@ func (c *client) CreateObject(ctx context.Context, obj Object) (*Object, error)
return &obj, nil
}

func (c *client) CreateAttachment(ctx context.Context, itemId string, filePath string) (*Object, error) {
out, err := c.cmdWithSession("create", string(ObjectTypeAttachment), "--itemid", itemId, "--file", filePath).Run(ctx)
func (c *client) CreateAttachment(ctx context.Context, itemId string, filePath string) (*models.Object, error) {
out, err := c.cmdWithSession("create", string(models.ObjectTypeAttachment), "--itemid", itemId, "--file", filePath).Run(ctx)
if err != nil {
return nil, err
}

var obj Object
var obj models.Object
err = json.Unmarshal(out, &obj)
if err != nil {
return nil, err
Expand All @@ -128,7 +130,7 @@ func (c *client) CreateAttachment(ctx context.Context, itemId string, filePath s
return &obj, nil
}

func (c *client) EditObject(ctx context.Context, obj Object) (*Object, error) {
func (c *client) EditObject(ctx context.Context, obj models.Object) (*models.Object, error) {
objEncoded, err := c.encode(obj)
if err != nil {
return nil, err
Expand Down Expand Up @@ -157,14 +159,14 @@ func (c *client) EditObject(ctx context.Context, obj Object) (*Object, error) {
return &obj, nil
}

func (c *client) GetObject(ctx context.Context, obj Object) (*Object, error) {
func (c *client) GetObject(ctx context.Context, obj models.Object) (*models.Object, error) {
args := []string{
"get",
string(obj.Object),
obj.ID,
}

if obj.Object == ObjectTypeOrgCollection {
if obj.Object == models.ObjectTypeOrgCollection {
args = append(args, "--organizationid", obj.OrganizationID)
}

Expand All @@ -182,7 +184,7 @@ func (c *client) GetObject(ctx context.Context, obj Object) (*Object, error) {
}

func (c *client) GetAttachment(ctx context.Context, itemId, attachmentId string) ([]byte, error) {
out, err := c.cmdWithSession("get", string(ObjectTypeAttachment), attachmentId, "--itemid", itemId, "--raw").Run(ctx)
out, err := c.cmdWithSession("get", string(models.ObjectTypeAttachment), attachmentId, "--itemid", itemId, "--raw").Run(ctx)
if err != nil {
return nil, remapError(err)
}
Expand All @@ -195,22 +197,20 @@ func (c *client) GetSessionKey() string {
}

// ListObjects returns objects of a given type matching given filters.
func (c *client) ListObjects(ctx context.Context, objType string, options ...ListObjectsOption) ([]Object, error) {
func (c *client) ListObjects(ctx context.Context, objType models.ObjectType, options ...bitwarden.ListObjectsOption) ([]models.Object, error) {
args := []string{
"list",
objType,
string(objType),
}

for _, applyOption := range options {
applyOption(&args)
}
applyFiltersToArgs(&args, options...)

out, err := c.cmdWithSession(args...).Run(ctx)
if err != nil {
return nil, remapError(err)
}

var obj []Object
var obj []models.Object
err = json.Unmarshal(out, &obj)
if err != nil {
return nil, newUnmarshallError(err, args[0:2], out)
Expand Down Expand Up @@ -245,14 +245,14 @@ func (c *client) Logout(ctx context.Context) error {
return err
}

func (c *client) DeleteObject(ctx context.Context, obj Object) error {
func (c *client) DeleteObject(ctx context.Context, obj models.Object) error {
args := []string{
"delete",
string(obj.Object),
obj.ID,
}

if obj.Object == ObjectTypeOrgCollection {
if obj.Object == models.ObjectTypeOrgCollection {
args = append(args, "--organizationid", obj.OrganizationID)
}

Expand All @@ -261,7 +261,7 @@ func (c *client) DeleteObject(ctx context.Context, obj Object) error {
}

func (c *client) DeleteAttachment(ctx context.Context, itemId, attachmentId string) error {
_, err := c.cmdWithSession("delete", string(ObjectTypeAttachment), attachmentId, "--itemid", itemId).Run(ctx)
_, err := c.cmdWithSession("delete", string(models.ObjectTypeAttachment), attachmentId, "--itemid", itemId).Run(ctx)
return err
}

Expand Down Expand Up @@ -331,10 +331,29 @@ func (c *client) env() []string {
return defaultEnv
}

func (c *client) encode(item Object) (string, error) {
func (c *client) encode(item models.Object) (string, error) {
newOut, err := json.Marshal(item)
if err != nil {
return "", fmt.Errorf("marshalling error: %v, %v", err, string(newOut))
}
return base64.RawStdEncoding.EncodeToString(newOut), nil
}

func applyFiltersToArgs(args *[]string, options ...bitwarden.ListObjectsOption) {
filters := bitwarden.ListObjectsOptionsToFilterOptions(options...)
if filters.OrganizationFilter != "" {
*args = append(*args, "--organizationid", filters.OrganizationFilter)
}
if filters.FolderFilter != "" {
*args = append(*args, "--folderid", filters.FolderFilter)
}
if filters.CollectionFilter != "" {
*args = append(*args, "--collectionid", filters.CollectionFilter)
}
if filters.SearchFilter != "" {
*args = append(*args, "--search", filters.SearchFilter)
}
if filters.UrlFilter != "" {
*args = append(*args, "--url", filters.UrlFilter)
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package bw
package bwcli

import (
"context"
"testing"

"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models"
test_command "github.com/maxlaverse/terraform-provider-bitwarden/internal/command/test"
"github.com/stretchr/testify/assert"
)
Expand All @@ -15,10 +17,10 @@ func TestCreateObjectEncoding(t *testing.T) {
defer removeMocks(t)

b := NewClient("dummy")
_, err := b.CreateObject(context.Background(), Object{
Type: ItemTypeLogin,
Object: ObjectTypeItem,
Fields: []Field{
_, err := b.CreateObject(context.Background(), models.Object{
Type: models.ItemTypeLogin,
Object: models.ObjectTypeItem,
Fields: []models.Field{
{
Name: "test",
Value: "passed",
Expand All @@ -40,7 +42,7 @@ func TestListObjects(t *testing.T) {
defer removeMocks(t)

b := NewClient("dummy")
_, err := b.ListObjects(context.Background(), "item", WithFolderID("folder-id"), WithCollectionID("collection-id"), WithSearch("search"))
_, err := b.ListObjects(context.Background(), "item", bitwarden.WithFolderID("folder-id"), bitwarden.WithCollectionID("collection-id"), bitwarden.WithSearch("search"))

assert.NoError(t, err)
if assert.Len(t, commandsExecuted(), 1) {
Expand All @@ -55,7 +57,7 @@ func TestGetItem(t *testing.T) {
defer removeMocks(t)

b := NewClient("dummy")
_, err := b.GetObject(context.Background(), Object{ID: "object-id", Object: ObjectTypeItem, Type: ItemTypeLogin})
_, err := b.GetObject(context.Background(), models.Object{ID: "object-id", Object: models.ObjectTypeItem, Type: models.ItemTypeLogin})

assert.NoError(t, err)
if assert.Len(t, commandsExecuted(), 1) {
Expand All @@ -70,7 +72,7 @@ func TestGetOrgCollection(t *testing.T) {
defer removeMocks(t)

b := NewClient("dummy")
_, err := b.GetObject(context.Background(), Object{ID: "object-id", Object: ObjectTypeOrgCollection, OrganizationID: "org-id"})
_, err := b.GetObject(context.Background(), models.Object{ID: "object-id", Object: models.ObjectTypeOrgCollection, OrganizationID: "org-id"})

assert.NoError(t, err)
if assert.Len(t, commandsExecuted(), 1) {
Expand All @@ -85,7 +87,7 @@ func TestErrorContainsCommand(t *testing.T) {
defer removeMocks(t)

b := NewClient("dummy")
_, err := b.ListObjects(context.Background(), "org-collection", WithSearch("search"))
_, err := b.ListObjects(context.Background(), "org-collection", bitwarden.WithSearch("search"))

if assert.Error(t, err) {
assert.ErrorContains(t, err, "unable to parse result of 'list org-collection', error: 'unexpected end of JSON input', output: ''")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package bw
package bwcli

import (
"errors"
"fmt"
"regexp"
"strings"

"github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models"
"github.com/maxlaverse/terraform-provider-bitwarden/internal/command"
)

var (
ErrObjectNotFound = errors.New("object not found")
ErrAttachmentNotFound = errors.New("attachment not found")

attachmentNotFoundRegexp = regexp.MustCompile(`^Attachment .* was not found.$`)
)

Expand All @@ -25,9 +22,9 @@ func remapError(err error) error {
if ok {
switch {
case isObjectNotFoundError(v):
return ErrObjectNotFound
return models.ErrObjectNotFound
case isAttachmentNotFoundError(v):
return ErrAttachmentNotFound
return models.ErrAttachmentNotFound
}
}
return err
Expand Down
17 changes: 17 additions & 0 deletions internal/bitwarden/bwcli/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package bwcli

import "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models"

func FilterObjectsByType(objs []models.Object, itemType models.ItemType) []models.Object {
if itemType == 0 {
return objs
}

filtered := make([]models.Object, 0, len(objs))
for _, obj := range objs {
if obj.Type == itemType {
filtered = append(filtered, obj)
}
}
return filtered
}
Loading

0 comments on commit f6d7453

Please sign in to comment.