Skip to content

Commit 4f132f0

Browse files
committed
docs: terraform secret crud - wip
1 parent b507afd commit 4f132f0

File tree

1 file changed

+206
-9
lines changed

1 file changed

+206
-9
lines changed

src/pages/integrations/platforms/hashicorp-terraform.mdx

Lines changed: 206 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const description = 'Use Phase with Terraform to manage your secrets'
77

88
# Terraform Provider
99

10-
The Phase Terraform Provider allows you to securely retrieve secrets directly from your Terraform configurations. This integration enables you to incorporate secret management into your infrastructure-as-code workflows.
10+
The Phase Terraform Provider allows you to securely manage and retrieve secrets directly from your Terraform configurations. This integration enables you to incorporate secure secret management into your infrastructure-as-code workflows.
1111

1212
## Prerequisites
1313

@@ -21,7 +21,7 @@ The Phase Terraform Provider allows you to securely retrieve secrets directly fr
2121

2222
2. Fetch your Phase **Application ID** (AppID) by going to your application settings in the Phase Console, hovering over UUID under the App section and clicking the `Copy` button:
2323

24-
![hello world](/assets/images/console/settings/application-id.png)
24+
![Application ID](/assets/images/console/settings/application-id.png)
2525

2626
## Step 1: Install the Provider
2727

@@ -32,7 +32,7 @@ terraform {
3232
required_providers {
3333
phase = {
3434
source = "phasehq/phase"
35-
version = "0.1.1" // replace with latest version
35+
version = "0.2.0" // replace with latest version
3636
}
3737
}
3838
}
@@ -47,15 +47,17 @@ To configure the provider, you need to provide your Phase API credentials. We re
4747
```hcl
4848
provider "phase" {
4949
phase_token = "pss_service:v1:..." # or "pss_user:v1:..." // A Phase Service Token or a Phase User Token (PAT)
50+
// Alternatively supply a PHASE_TOKEN environment variable
5051
}
5152
```
5253

5354
If you are using a self-hosted instance of Phase, you can specify the API host using the `host` argument in the provider configuration:
5455

5556
```hcl
5657
provider "phase" {
57-
host = "https://phase.example.io"
58-
phase_token = "pss_service:v1:..." # or "pss_user:v1:..." // A Phase Service Token or a Phase User Token (PAT)
58+
host = "https://phase.example.io"
59+
skip_tls_verification = true # Optional, if your Phase instance is using a self-signed certificate, you can set this to true to skip TLS verification.
60+
phase_token = "pss_service:v1:..." # or "pss_user:v1:..." // A Phase Service Token or a Phase User Token (PAT)
5961
}
6062
```
6163

@@ -71,9 +73,9 @@ To fetch secrets from Phase, use the `phase_secrets` data source:
7173

7274
```hcl
7375
data "phase_secrets" "all" {
74-
env = "development"
75-
app_id = "your-app-id"
76-
path = ""
76+
env = "development" // The environment to fetch secrets from.
77+
app_id = "your-app-id" // The ID of the Phase application to fetch secrets from.
78+
path = "" // Use an empty string to fetch all secrets in the application.
7779
}
7880
7981
output "all_secret_keys" {
@@ -84,6 +86,35 @@ output "all_secret_keys" {
8486

8587
☝️ This will fetch all secrets stored inside your Phase application in the development environment.
8688

89+
Example:
90+
91+
```hcl
92+
terraform {
93+
required_providers {
94+
phase = {
95+
source = "phasehq/phase"
96+
version = "0.2.0"
97+
}
98+
}
99+
}
100+
101+
provider "phase" {
102+
skip_tls_verification = true
103+
host = "https://phase.internal.acme.com"
104+
}
105+
106+
data "phase_secrets" "all" {
107+
env = "production"
108+
app_id = "907549ca-1430-4aa0-9998-290525741005"
109+
path = ""
110+
}
111+
112+
output "all_secret_keys" {
113+
value = data.phase_secrets.all.secrets
114+
sensitive = true
115+
}
116+
```
117+
87118
### Fetching Secrets from a Specific Path
88119

89120
To fetch all secrets under a specific path:
@@ -101,6 +132,34 @@ output "backend_secret_keys" {
101132
}
102133
```
103134

135+
Example:
136+
137+
```hcl
138+
terraform {
139+
required_providers {
140+
phase = {
141+
source = "phasehq/phase"
142+
version = "0.2.0"
143+
}
144+
}
145+
}
146+
147+
provider "phase" {
148+
skip_tls_verification = true
149+
host = "https://phase.internal.acme.com"
150+
}
151+
152+
data "phase_secrets" "all" {
153+
env = "production"
154+
app_id = "907549ca-1430-4aa0-9998-290525741005"
155+
path = "/folder/path"
156+
}
157+
158+
output "all_secret_keys" {
159+
value = data.phase_secrets.all.secrets
160+
sensitive = true
161+
}
162+
```
104163
☝️ This will fetch the `JWT_SECRET` secret from the `/backend` folder inside your Phase application in the production environment.
105164

106165
### Fetching a Single Secret
@@ -118,8 +177,103 @@ output "database_url" {
118177
sensitive = true
119178
}
120179
```
121-
☝️ This will fetch the `DATABASE_URL` secret from your Phase application in the development environment.
180+
☝️ This will fetch the value of the `DATABASE_URL` secret from your Phase application in the development environment.
181+
182+
Example:
183+
184+
```hcl
185+
terraform {
186+
required_providers {
187+
phase = {
188+
source = "phasehq/phase"
189+
version = "0.2.0"
190+
}
191+
}
192+
}
193+
194+
provider "phase" {
195+
host = "https://phase.internal.acme.com"
196+
}
197+
198+
data "phase_secrets" "single" {
199+
env = "production"
200+
app_id = "907549ca-1430-4aa0-9998-290525741005"
201+
202+
}
203+
204+
output "database_url" {
205+
value = data.phase_secrets.single.secrets["DATABASE_URL"]
206+
sensitive = true
207+
}
208+
209+
```
210+
211+
### Creating Secrets
212+
213+
```hcl
214+
resource "phase_secret" "example" {
215+
app_id = "8b94fe5c-ea7d-4091-9087-e0e03089bd47"
216+
env = "production"
217+
key = "DATABASE_URL"
218+
path = "/database/pgsql"
219+
comment = "AWS RDS PostgreSQL database creds"
220+
tags = ["database", "RDS"] // Tags must be pre-created in the Phase Console
221+
value = "postgres://${USER}:${PASSWORD}@${HOST}:{PORT}/${DATABASE}"
222+
}
223+
```
224+
225+
<Note>
226+
To be able to assign tags to secrets, the tags must already be created in the Phase Console beforehand.
227+
</Note>
228+
229+
Example:
230+
231+
```hcl
232+
terraform {
233+
required_providers {
234+
phase = {
235+
source = "phasehq/phase"
236+
version = "0.2.0"
237+
}
238+
random = {
239+
source = "hashicorp/random"
240+
version = "3.6.0"
241+
}
242+
}
243+
}
122244
245+
provider "phase" {
246+
host = "https://internal.phase.acme.com"
247+
}
248+
249+
# Generate random values for secrets
250+
resource "random_bytes" "secret_1" {
251+
length = 32
252+
}
253+
254+
resource "random_bytes" "secret_2" {
255+
length = 64
256+
}
257+
258+
resource "phase_secret" "terraform_secret_1" {
259+
app_id = "8b94fe5c-ea7d-4091-9087-e0e03089bd47"
260+
env = "development"
261+
key = "TF_SECRET_1"
262+
value = random_bytes.secret_1.hex
263+
path = "/"
264+
comment = "Created by Terraform"
265+
tags = ["database"] # Tag must already exist in Phase Console
266+
}
267+
268+
resource "phase_secret" "terraform_secret_2" {
269+
app_id = "8b94fe5c-ea7d-4091-9087-e0e03089bd47"
270+
env = "production"
271+
key = "TF_SECRET_2"
272+
value = random_bytes.secret_2.hex
273+
path = "/foo-bar"
274+
comment = "Created by Terraform"
275+
}
276+
```
123277

124278
### Using Secrets in Resources
125279

@@ -150,6 +304,48 @@ Execute your Terraform workflow:
150304
terraform apply
151305
```
152306

307+
## Step 5: Destroy the resources
308+
309+
To destroy the resources created by the Terraform configuration, run the following command:
310+
```fish
311+
terraform destroy
312+
```
313+
314+
### Importing Existing Secrets
315+
316+
You can import existing secrets from Phase into your Terraform state using:
317+
318+
```fish
319+
terraform import phase_secret.<resource_name> "<app_id>:<env>:<path>:<key>"
320+
```
321+
322+
For example:
323+
```fish
324+
terraform import phase_secret.imported_secret "907549ca-1430-4aa0-9998-290525741005:production:/database/:DB_HOST"
325+
```
326+
327+
### Secret Versions and Metadata
328+
329+
The provider automatically tracks secret versions and metadata:
330+
331+
```hcl
332+
resource "phase_secret" "database_url" {
333+
env = "production"
334+
app_id = "your-app-id"
335+
key = "DATABASE_URL"
336+
value = "postgres://user:password@localhost:5432/db"
337+
tags = ["database", "credentials"] # Tags must already exist in Phase Console
338+
}
339+
340+
output "secret_version" {
341+
value = phase_secret.database_url.version
342+
}
343+
344+
output "secret_created_at" {
345+
value = phase_secret.database_url.created_at
346+
}
347+
```
348+
153349
## Personal Secret Overrides
154350

155351
Personal Secret Overrides allow individual users to temporarily override a secret's value for their own use, without affecting the value for other users or systems. Important points to note:
@@ -167,3 +363,4 @@ Personal Secret Overrides allow individual users to temporarily override a secre
167363
1. Use variables or environment variables for the Phase token to keep it out of your Terraform configurations.
168364
2. Utilize Terraform's `sensitive` argument when outputting or using secret values to prevent accidental exposure.
169365
3. Be cautious when using `terraform output` commands, as these may display sensitive information.
366+
4. Create all necessary tags in the Phase Console before referencing them in Terraform configurations. In the near future, we will add an API to automatically create tags in Terraform.

0 commit comments

Comments
 (0)