Skip to content

Commit 557923b

Browse files
committed
fix: POC for handling obfusctated secrets in meshPlatform Azure Type
CU-86c63jkw8 #64
1 parent d8759c9 commit 557923b

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed

docs/data-sources/platform.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: |-
1010

1111
Represents a meshStack platform.
1212

13+
Note that it is not possible to retrieve sensitive fields with this data source. They will be obfuscated by the backend API.
14+
1315
## Example Usage
1416

1517
```terraform

internal/provider/platform_resource.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,6 @@ func openShiftPlatformSchema() schema.Attribute {
350350
}
351351
}
352352

353-
// TODO review done until here
354-
355353
func aksReplicationConfigSchema() schema.Attribute {
356354
return schema.SingleNestedAttribute{
357355
MarkdownDescription: "Replication configuration for AKS (optional, but required for replication)",
@@ -812,8 +810,6 @@ func azureReplicationConfigSchema() schema.Attribute {
812810
}
813811
}
814812

815-
// TODO continue here.
816-
817813
func azureRgReplicationConfigSchema() schema.Attribute {
818814
return schema.SingleNestedAttribute{
819815
MarkdownDescription: "Azure Resource Group-specific replication configuration for the platform.",
@@ -1155,6 +1151,8 @@ func (r *platformResource) Create(ctx context.Context, req resource.CreateReques
11551151
return
11561152
}
11571153

1154+
handleObfuscatedSecrets(&createdPlatform.Spec.Config, &platform.Spec.Config, resp.Diagnostics)
1155+
11581156
resp.Diagnostics.Append(resp.State.Set(ctx, createdPlatform)...)
11591157
}
11601158

@@ -1163,7 +1161,7 @@ func (r *platformResource) Read(ctx context.Context, req resource.ReadRequest, r
11631161
var uuid string
11641162
resp.Diagnostics.Append(req.State.GetAttribute(ctx, path.Root("metadata").AtName("uuid"), &uuid)...)
11651163

1166-
platform, err := r.client.ReadPlatform(uuid)
1164+
readPlatform, err := r.client.ReadPlatform(uuid)
11671165
if err != nil {
11681166
resp.Diagnostics.AddError(
11691167
fmt.Sprintf("Could not read platform with UUID '%s'", uuid),
@@ -1172,14 +1170,17 @@ func (r *platformResource) Read(ctx context.Context, req resource.ReadRequest, r
11721170
return
11731171
}
11741172

1175-
if platform == nil {
1173+
if readPlatform == nil {
11761174
// The platform was deleted outside of Terraform, so we remove it from the state
11771175
resp.State.RemoveResource(ctx)
11781176
return
11791177
}
11801178

1181-
// client data maps directly to the schema so we just need to set the state
1182-
resp.Diagnostics.Append(resp.State.Set(ctx, platform)...)
1179+
statePlatformSpec := client.MeshPlatformSpec{}
1180+
req.State.GetAttribute(ctx, path.Root("spec"), &statePlatformSpec)
1181+
handleObfuscatedSecrets(&readPlatform.Spec.Config, &statePlatformSpec.Config, resp.Diagnostics)
1182+
1183+
resp.Diagnostics.Append(resp.State.Set(ctx, readPlatform)...)
11831184
}
11841185

11851186
func (r *platformResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
@@ -1220,6 +1221,8 @@ func (r *platformResource) Update(ctx context.Context, req resource.UpdateReques
12201221
return
12211222
}
12221223

1224+
handleObfuscatedSecrets(&updatedPlatform.Spec.Config, &platform.Spec.Config, resp.Diagnostics)
1225+
12231226
resp.Diagnostics.Append(resp.State.Set(ctx, updatedPlatform)...)
12241227
}
12251228

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package provider
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/diag"
5+
"github.com/meshcloud/terraform-provider-meshstack/client"
6+
)
7+
8+
// TODO This is a WIP POC implementation for one secret within Azure Type only.
9+
10+
// This function is necessary to handle obfuscated secrets for meshPlatforms.
11+
// The meshPlatform API won't return secrets in plain text, but obfuscated values.
12+
// As a result we keep those from the plan/state and re-apply them to the object read from the API.
13+
//
14+
// MUST NOT PASS ANY NIL VALUES
15+
// MUST PASS compatible types
16+
func handleObfuscatedSecrets(obfuscated *client.PlatformConfig, plain *client.PlatformConfig, d diag.Diagnostics) {
17+
if obfuscated == nil || plain == nil || obfuscated.Type != plain.Type {
18+
d.AddError(
19+
"Internal Error",
20+
"Could not handle obfuscated secrets due to invalid input parameters.",
21+
)
22+
return
23+
}
24+
25+
switch obfuscated.Type {
26+
27+
case "azure":
28+
if obfuscated.Azure != nil && obfuscated.Azure.Replication != nil && plain.Azure != nil && plain.Azure.Replication != nil {
29+
// replication SP
30+
if obfuscated.Azure.Replication.ServicePrincipal != nil && plain.Azure.Replication.ServicePrincipal != nil {
31+
obfuscated.Azure.Replication.ServicePrincipal.CredentialsAuthClientSecret = plain.Azure.Replication.ServicePrincipal.CredentialsAuthClientSecret
32+
}
33+
// replication provisioning customer agreement SP
34+
if obfuscated.Azure.Replication.Provisioning.CustomerAgreement != nil && plain.Azure.Replication.Provisioning.CustomerAgreement != nil {
35+
if obfuscated.Azure.Replication.Provisioning.CustomerAgreement.SourceServicePrincipal != nil && plain.Azure.Replication.Provisioning.CustomerAgreement.SourceServicePrincipal != nil {
36+
obfuscated.Azure.Replication.Provisioning.CustomerAgreement.SourceServicePrincipal.CredentialsAuthClientSecret = plain.Azure.Replication.Provisioning.CustomerAgreement.SourceServicePrincipal.CredentialsAuthClientSecret
37+
}
38+
}
39+
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)