From 23ba13ca07502cbeab01d4ea1b9acbaadbc6330a Mon Sep 17 00:00:00 2001 From: Maxime Lagresle Date: Sun, 6 Oct 2024 13:33:31 +0200 Subject: [PATCH] rename client in password manager --- internal/bitwarden/bwcli/client.go | 10 +++++----- internal/bitwarden/client.go | 2 +- internal/bitwarden/embedded/vault_webapi.go | 12 ++++++------ internal/provider/attachment.go | 12 ++++++------ internal/provider/object.go | 12 ++++++------ internal/provider/provider.go | 4 ++-- internal/provider/provider_utils_test.go | 2 +- 7 files changed, 27 insertions(+), 27 deletions(-) diff --git a/internal/bitwarden/bwcli/client.go b/internal/bitwarden/bwcli/client.go index cb0b63c..664ddf7 100644 --- a/internal/bitwarden/bwcli/client.go +++ b/internal/bitwarden/bwcli/client.go @@ -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 } } diff --git a/internal/bitwarden/client.go b/internal/bitwarden/client.go index 4899d10..656e38d 100644 --- a/internal/bitwarden/client.go +++ b/internal/bitwarden/client.go @@ -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 diff --git a/internal/bitwarden/embedded/vault_webapi.go b/internal/bitwarden/embedded/vault_webapi.go index 7ea4ef6..8197671 100644 --- a/internal/bitwarden/embedded/vault_webapi.go +++ b/internal/bitwarden/embedded/vault_webapi.go @@ -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 } } @@ -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 } } @@ -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 } } diff --git a/internal/provider/attachment.go b/internal/provider/attachment.go index 9a6895c..d602fea 100644 --- a/internal/provider/attachment.go +++ b/internal/provider/attachment.go @@ -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) } @@ -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. @@ -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 { @@ -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) } @@ -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 diff --git a/internal/provider/object.go b/internal/provider/object.go index dde40e7..4ed9282 100644 --- a/internal/provider/object.go +++ b/internal/provider/object.go @@ -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 { @@ -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. @@ -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 } @@ -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) { @@ -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) })) } diff --git a/internal/provider/provider.go b/internal/provider/provider.go index c6166b6..5225520 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -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 @@ -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") diff --git a/internal/provider/provider_utils_test.go b/internal/provider/provider_utils_test.go index 1591e94..83f2725 100644 --- a/internal/provider/provider_utils_test.go +++ b/internal/provider/provider_utils_test.go @@ -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 {