|
| 1 | + |
| 2 | +--- |
| 3 | +subcategory : "Key Management Service (KMS)" |
| 4 | +--- |
| 5 | + |
| 6 | +# ovh_okms_secret (Resource) |
| 7 | + |
| 8 | +Manages a secret stored in OVHcloud KMS. |
| 9 | + |
| 10 | +> WARNING: `version.data` is marked **Sensitive** but still ends up in the state file. To mitigate that, it is recommended to protect your state with encryption and access controls. Avoid committing it to source control. |
| 11 | +
|
| 12 | +## Example Usage |
| 13 | + |
| 14 | +Create a secret whose value is a JSON object. Use `jsonencode()` to produce a deterministic JSON string (ordering/whitespace) to minimize diffs. |
| 15 | + |
| 16 | +```terraform |
| 17 | +resource "ovh_okms_secret" "example" { |
| 18 | + okms_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 19 | + path = "app/api_credentials" |
| 20 | +
|
| 21 | + metadata = { |
| 22 | + max_versions = 10 # keep last 10 versions |
| 23 | + cas_required = true # enforce optimistic concurrency control (server will require current secret version on the cas attribute to allow update) |
| 24 | + deactivate_version_after = "0s" # keep versions active indefinitely (example) |
| 25 | + custom_metadata = { |
| 26 | + environment = "prod" |
| 27 | + owner = "payments-team" |
| 28 | + } |
| 29 | + } |
| 30 | +
|
| 31 | + # Initial version (will create version 1) |
| 32 | + version = { |
| 33 | + data = jsonencode({ |
| 34 | + api_key = var.api_key |
| 35 | + api_secret = var.api_secret |
| 36 | + }) |
| 37 | + } |
| 38 | +} |
| 39 | +
|
| 40 | +# Reading a field from the secret version data |
| 41 | +locals { |
| 42 | + secret_json = jsondecode(ovh_okms_secret.example.version.data) |
| 43 | +} |
| 44 | +
|
| 45 | +output "api_key" { |
| 46 | + value = local.secret_json.api_key |
| 47 | + sensitive = true |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +Updating the secret (new version) while enforcing optimistic concurrency control using CAS: |
| 52 | + |
| 53 | +```terraform |
| 54 | +resource "ovh_okms_secret" "example" { |
| 55 | + okms_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 56 | + path = "app/api_credentials" |
| 57 | +
|
| 58 | + # Ensure no concurrent update happened: set cas to the current version |
| 59 | + # (metadata.current_version is populated after first apply) |
| 60 | + cas = ovh_okms_secret.example.metadata.current_version |
| 61 | +
|
| 62 | + metadata = { |
| 63 | + cas_required = true |
| 64 | + } |
| 65 | +
|
| 66 | + version = { |
| 67 | + data = jsonencode({ |
| 68 | + api_key = var.api_key |
| 69 | + api_secret = var.new_api_secret # changed value -> creates new version |
| 70 | + }) |
| 71 | + } |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +## Argument Reference |
| 76 | + |
| 77 | +The following arguments are supported: |
| 78 | + |
| 79 | +### Required |
| 80 | + |
| 81 | +- `okms_id` (String) ID of the OKMS service to create the secret in. |
| 82 | +- `path` (String) Secret path (acts as the secret identifier within the OKMS instance). Immutable after creation. |
| 83 | +- `version` (Block) Definition of the version to create/update. See Version Block below. (On updates providing a new `version.data` creates a new version.) |
| 84 | + |
| 85 | +### Optional |
| 86 | + |
| 87 | +- `cas` (Number) Check‑and‑set parameter used only on update. Must equal the current secret version (`metadata.current_version`) for the update to succeed. Ignored on create. |
| 88 | +- `metadata` (Block) Secret metadata configuration (subset of fields are user-settable). See Metadata Block below. |
| 89 | + |
| 90 | +### Metadata Block |
| 91 | + |
| 92 | +User configurable attributes inside `metadata`: |
| 93 | + |
| 94 | +- `cas_required` (Boolean) If true, the server will enforce optimistic concurrency control by requiring the `cas` parameter to match the current version number on every write (update) request. |
| 95 | +- `custom_metadata` (Map of String) Arbitrary key/value metadata. |
| 96 | +- `deactivate_version_after` (String) Duration (e.g. `"24h"`) after which a version is deactivated. `"0s"` (default) means never automatically deactivate. |
| 97 | +- `max_versions` (Number) Number of versions to retain (default 10). Older versions beyond this limit are pruned. |
| 98 | + |
| 99 | +Computed (read‑only) metadata attributes: |
| 100 | + |
| 101 | +- `created_at` (String) Creation timestamp of the secret. |
| 102 | +- `updated_at` (String) Last update timestamp. |
| 103 | +- `current_version` (Number) Current (latest) version number. |
| 104 | +- `oldest_version` (Number) Oldest retained version number. |
| 105 | + |
| 106 | +### Version Block |
| 107 | + |
| 108 | +Required attribute: |
| 109 | + |
| 110 | +- `data` (String, Sensitive) Secret payload. Commonly set with `jsonencode(...)` so that Terraform comparisons are stable. Any valid JSON (object, array, string, number, bool) is accepted. Changing `data` creates a new secret version. |
| 111 | + |
| 112 | +Computed (read‑only) attributes: |
| 113 | + |
| 114 | +- `id` (Number) Version number. |
| 115 | +- `created_at` (String) Version creation timestamp. |
| 116 | +- `deactivated_at` (String) Deactivation timestamp if the version was deactivated. |
| 117 | +- `state` (String) Version state (e.g. `ACTIVE`). |
| 118 | + |
| 119 | +## Attributes Reference (Read-Only) |
| 120 | + |
| 121 | +In addition to arguments above, the following attributes are exported: |
| 122 | + |
| 123 | +- `iam` (Block) IAM metadata: `display_name`, `id`, `tags`, `urn`. |
| 124 | +- `metadata.*` computed fields as listed above. |
| 125 | +- `version.*` computed fields as listed above. |
| 126 | + |
| 127 | +## Behavior & Notes |
| 128 | + |
| 129 | +- Updating with a new `version.data` performs an API PUT that creates a new version; the previous version remains (subject to `max_versions`). |
| 130 | +- If `cas_required` is true, all write operations must include a correct `cas` query parameter (the expected current version number). Set `cas = ovh_okms_secret.example.metadata.current_version` to enforce optimistic concurrency. A mismatch causes the API to reject the update (preventing overwriting unseen changes). |
| 131 | +- `cas` is ignored on create (no existing version). |
0 commit comments