1Password / terraform-provider-onepassword

Use the 1Password Terraform Provider to reference, create, or update items in your 1Password Vaults.
https://1password.com/secrets
MIT License
314 stars 41 forks source link

Fix/137/handle date field on item read #150

Closed volodymyrZotov closed 4 months ago

volodymyrZotov commented 4 months ago

Resolves: #137

This PR prevents the item from being updated after running terraform apply when it has DATE field which was not changed.

To achieve this the itemToData function was changed and now it checks if the field is of DATE type and converts seconds to YYYY-MM-DD string format. The date value in this format will be saved in the tfstate, so then on the next run when it will compare the date value in tfstate and tf files they will be equal, so no changes will be applied.

In addition, the dataToItem function was changed to validate the format of DATE fields to make sure they are in YYYY-MM-DD format. That will prevent users from setting the dates in other formats and thus potential issues.

How to test

  1. Checkout to fix/137/handle-date-field-on-item-read branch
  2. make build
  3. Update/create ~/.terraformrc file with the following content to tell Terraform to use your local provider version. Change PATH_TO_THE_PROJECT with the real path.

    provider_installation {
    
    dev_overrides {
      "1Password/onepassword" = "PATH_TO_THE_PROJECT/dist"
    }
    
    # For all other providers, install them directly from their origin provider
    # registries as normal. If you omit this, Terraform will _only_ use
    # the dev_overrides block, and so no other providers will be available.
    direct {}
    }
  4. In different folder create main.tf file with the following content
    
    terraform {
    required_providers {
    onepassword = {
      source  = "1Password/onepassword"
      version = "~> 1.4"
    }
    }
    }

provider "onepassword" {

provide Connect credentials, service account or account

}

data "onepassword_vault" "vault" { name = "Example" # there should be the name of you existing vault in 1Password }

resource "onepassword_item" "item" { vault = data.onepassword_vault.vault.uuid title = "Item 2" section { label = "Section 1" field { label = "text" type = "DATE" value = "2023-10-10" } } }


5. `terraform init` 
6. `terraform apply` and see that item was created
7. `terraform apply` again and see that it prints `No changes. Your infrastructure matches the configuration.`