Skip to content

Commit

Permalink
rename client in password manager
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlaverse committed Oct 6, 2024
1 parent b63ff64 commit 23ba13c
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
10 changes: 5 additions & 5 deletions internal/bitwarden/bwcli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,28 @@ type client struct {
sessionKey string
}

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

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

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

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

func DisableRetryBackoff() Options {
return func(c bitwarden.Client) {
return func(c bitwarden.PasswordManager) {
c.(*client).disableRetryBackoff = true
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/bitwarden/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const (
DefaultBitwardenServerURL = "https://vault.bitwarden.com"
)

type Client interface {
type PasswordManager interface {
CreateAttachment(ctx context.Context, itemId, filePath string) (*models.Object, error)
CreateObject(context.Context, models.Object) (*models.Object, error)
DeleteAttachment(ctx context.Context, itemId, attachmentId string) error
Expand Down
12 changes: 6 additions & 6 deletions internal/bitwarden/embedded/vault_webapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ type WebAPIVault interface {
Unlock(ctx context.Context, password string) error
}

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

// DisableCryptoSafeMode disables the safe mode for crypto operations, which reverses
// crypto.Encrypt() to make sure it can decrypt the result.
func DisableCryptoSafeMode() Options {
return func(c bitwarden.Client) {
return func(c bitwarden.PasswordManager) {
crypto.SafeMode = false
}
}
Expand All @@ -48,7 +48,7 @@ func DisableCryptoSafeMode() Options {
// (items, folders, collections) after they have been created or edited, to verify that the
// encryption can be reverse.
func DisableObjectEncryptionVerification() Options {
return func(c bitwarden.Client) {
return func(c bitwarden.PasswordManager) {
c.(*webAPIVault).baseVault.verifyObjectEncryption = false
}
}
Expand All @@ -57,21 +57,21 @@ func DisableObjectEncryptionVerification() Options {
// delete) to the vault. Write operations already return the object that was created or edited, so
// Sync() is not strictly necessary.
func DisableSyncAfterWrite() Options {
return func(c bitwarden.Client) {
return func(c bitwarden.PasswordManager) {
c.(*webAPIVault).syncAfterWrite = false
}
}

// DisableRetryBackoff disables the retry backoff mechanism for API calls.
func WithHttpOptions(opts ...webapi.Options) Options {
return func(c bitwarden.Client) {
return func(c bitwarden.PasswordManager) {
c.(*webAPIVault).client = webapi.NewClient(c.(*webAPIVault).serverURL, opts...)
}
}

// Panic on error is useful for debugging, but should not be used in production.
func EnablePanicOnEncryptionError() Options {
return func(c bitwarden.Client) {
return func(c bitwarden.PasswordManager) {
panicOnEncryptionErrors = true
}
}
Expand Down
12 changes: 6 additions & 6 deletions internal/provider/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
func attachmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
itemId := d.Get(attributeAttachmentItemID).(string)

existingAttachments, err := listExistingAttachments(ctx, meta.(bitwarden.Client), itemId)
existingAttachments, err := listExistingAttachments(ctx, meta.(bitwarden.PasswordManager), itemId)
if err != nil {
return diag.FromErr(err)
}

filePath := d.Get(attributeAttachmentFile).(string)
obj, err := meta.(bitwarden.Client).CreateAttachment(ctx, itemId, filePath)
obj, err := meta.(bitwarden.PasswordManager).CreateAttachment(ctx, itemId, filePath)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -43,7 +43,7 @@ func attachmentCreate(ctx context.Context, d *schema.ResourceData, meta interfac
func attachmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
itemId := d.Get(attributeAttachmentItemID).(string)

obj, err := meta.(bitwarden.Client).GetObject(ctx, models.Object{ID: itemId, Object: models.ObjectTypeItem})
obj, err := meta.(bitwarden.PasswordManager).GetObject(ctx, models.Object{ID: itemId, Object: models.ObjectTypeItem})
if err != nil {
// If the item is not found, we can't simply consider the attachment as
// deleted, because we won't have an item to attach it to.
Expand All @@ -67,7 +67,7 @@ func attachmentRead(ctx context.Context, d *schema.ResourceData, meta interface{
func attachmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
itemId := d.Get(attributeAttachmentItemID).(string)

return diag.FromErr(meta.(bitwarden.Client).DeleteAttachment(ctx, itemId, d.Id()))
return diag.FromErr(meta.(bitwarden.PasswordManager).DeleteAttachment(ctx, itemId, d.Id()))
}

func attachmentDataFromStruct(d *schema.ResourceData, attachment models.Attachment) error {
Expand Down Expand Up @@ -101,7 +101,7 @@ func readDataSourceAttachment() schema.ReadContextFunc {

attachmentId := d.Get(attributeID).(string)

content, err := meta.(bitwarden.Client).GetAttachment(ctx, itemId, attachmentId)
content, err := meta.(bitwarden.PasswordManager).GetAttachment(ctx, itemId, attachmentId)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -112,7 +112,7 @@ func readDataSourceAttachment() schema.ReadContextFunc {
}
}

func listExistingAttachments(ctx context.Context, client bitwarden.Client, itemId string) ([]models.Attachment, error) {
func listExistingAttachments(ctx context.Context, client bitwarden.PasswordManager, itemId string) ([]models.Attachment, error) {
obj, err := client.GetObject(ctx, models.Object{ID: itemId, Object: models.ObjectTypeItem})
if err != nil {
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions internal/provider/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func objectCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return diag.FromErr(objectOperation(ctx, d, meta.(bitwarden.Client).CreateObject))
return diag.FromErr(objectOperation(ctx, d, meta.(bitwarden.PasswordManager).CreateObject))
}

func objectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand All @@ -23,7 +23,7 @@ func objectRead(ctx context.Context, d *schema.ResourceData, meta interface{}) d
}

return diag.FromErr(objectOperation(ctx, d, func(ctx context.Context, secret models.Object) (*models.Object, error) {
obj, err := meta.(bitwarden.Client).GetObject(ctx, secret)
obj, err := meta.(bitwarden.PasswordManager).GetObject(ctx, secret)
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.
Expand All @@ -50,7 +50,7 @@ func objectSearch(ctx context.Context, d *schema.ResourceData, meta interface{})
return fmt.Errorf("BUG: object type not set in the resource data")
}

objs, err := meta.(bitwarden.Client).ListObjects(ctx, models.ObjectType(objType.(string)), listOptionsFromData(d)...)
objs, err := meta.(bitwarden.PasswordManager).ListObjects(ctx, models.ObjectType(objType.(string)), listOptionsFromData(d)...)
if err != nil {
return err
}
Expand Down Expand Up @@ -115,7 +115,7 @@ func listOptionsFromData(d *schema.ResourceData) []bitwarden.ListObjectsOption {

func objectReadIgnoreMissing(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
err := objectOperation(ctx, d, func(ctx context.Context, secret models.Object) (*models.Object, error) {
return meta.(bitwarden.Client).GetObject(ctx, secret)
return meta.(bitwarden.PasswordManager).GetObject(ctx, secret)
})

if errors.Is(err, models.ErrObjectNotFound) {
Expand All @@ -134,12 +134,12 @@ func objectReadIgnoreMissing(ctx context.Context, d *schema.ResourceData, meta i
}

func objectUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return diag.FromErr(objectOperation(ctx, d, meta.(bitwarden.Client).EditObject))
return diag.FromErr(objectOperation(ctx, d, meta.(bitwarden.PasswordManager).EditObject))
}

func objectDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return diag.FromErr(objectOperation(ctx, d, func(ctx context.Context, secret models.Object) (*models.Object, error) {
return nil, meta.(bitwarden.Client).DeleteObject(ctx, secret)
return nil, meta.(bitwarden.PasswordManager).DeleteObject(ctx, secret)
}))
}

Expand Down
4 changes: 2 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func newBitwardenClient(d *schema.ResourceData, version string) (bwcli.CLIClient
return bwcli.NewClient(bwExecutable, opts...), nil
}

func newBitwardenEmbeddedClient(ctx context.Context, d *schema.ResourceData, version string) (bitwarden.Client, error) {
func newBitwardenEmbeddedClient(ctx context.Context, d *schema.ResourceData, version string) (bitwarden.PasswordManager, error) {
deviceId, err := getOrGenerateDeviceIdentifier(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -351,7 +351,7 @@ func getOrGenerateDeviceIdentifier(ctx context.Context) (string, error) {
return deviceId, nil
}

func ensureLoggedInEmbedded(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.Client) error {
func ensureLoggedInEmbedded(ctx context.Context, d *schema.ResourceData, bwClient bitwarden.PasswordManager) error {
masterPassword, hasMasterPassword := d.GetOk(attributeMasterPassword)
if !hasMasterPassword {
return fmt.Errorf("master password is required")
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/provider_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func clearTestVault(t *testing.T) {
}
}

func bwTestClient(t *testing.T) bitwarden.Client {
func bwTestClient(t *testing.T) bitwarden.PasswordManager {
client := embedded.NewWebAPIVault(testServerURL)
err := client.LoginWithPassword(context.Background(), testEmail, testPassword)
if err != nil {
Expand Down

0 comments on commit 23ba13c

Please sign in to comment.