|
| 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 | +const ( |
| 9 | + obfuscatedValue = "mesh/hidden-secret" |
| 10 | +) |
| 11 | + |
| 12 | +// This function is necessary to handle obfuscated secrets for meshPlatforms. |
| 13 | +// The meshPlatform API won't return secrets in plain text, but obfuscated values. |
| 14 | +// As a result we keep those from the plan/state and re-apply them to the object read from the API. |
| 15 | +// |
| 16 | +// MUST NOT PASS ANY NIL VALUES |
| 17 | +// MUST PASS compatible types |
| 18 | +func handleObfuscatedSecrets(obfuscated *client.PlatformConfig, plain *client.PlatformConfig, d diag.Diagnostics) { |
| 19 | + if obfuscated == nil || plain == nil || obfuscated.Type != plain.Type { |
| 20 | + d.AddError( |
| 21 | + "Internal Error", |
| 22 | + "Could not handle obfuscated secrets due to invalid input parameters.", |
| 23 | + ) |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + switch obfuscated.Type { |
| 28 | + |
| 29 | + case "aks": |
| 30 | + if obfuscated.Aks != nil && obfuscated.Aks.Replication != nil && plain.Aks != nil && plain.Aks.Replication != nil { |
| 31 | + // access token |
| 32 | + if obfuscated.Aks.Replication.AccessToken != nil && |
| 33 | + plain.Aks.Replication.AccessToken != nil && |
| 34 | + *obfuscated.Aks.Replication.AccessToken == obfuscatedValue { |
| 35 | + obfuscated.Aks.Replication.AccessToken = plain.Aks.Replication.AccessToken |
| 36 | + } |
| 37 | + // SP client secret |
| 38 | + if obfuscated.Aks.Replication.ServicePrincipal != nil && |
| 39 | + plain.Aks.Replication.ServicePrincipal != nil && |
| 40 | + *obfuscated.Aks.Replication.ServicePrincipal.CredentialsAuthClientSecret == obfuscatedValue { |
| 41 | + obfuscated.Aks.Replication.ServicePrincipal.CredentialsAuthClientSecret = plain.Aks.Replication.ServicePrincipal.CredentialsAuthClientSecret |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + case "aws": |
| 46 | + if obfuscated.Aws != nil && obfuscated.Aws.Replication != nil && plain.Aws != nil && plain.Aws.Replication != nil { |
| 47 | + // replication access-config service-user secret key |
| 48 | + if obfuscated.Aws.Replication.AccessConfig != nil && |
| 49 | + plain.Aws.Replication.AccessConfig != nil && |
| 50 | + obfuscated.Aws.Replication.AccessConfig.ServiceUserConfig != nil && |
| 51 | + plain.Aws.Replication.AccessConfig.ServiceUserConfig != nil && |
| 52 | + *obfuscated.Aws.Replication.AccessConfig.ServiceUserConfig.SecretKey == obfuscatedValue { |
| 53 | + obfuscated.Aws.Replication.AccessConfig.ServiceUserConfig.SecretKey = plain.Aws.Replication.AccessConfig.ServiceUserConfig.SecretKey |
| 54 | + } |
| 55 | + // replication AWS SSO token |
| 56 | + if obfuscated.Aws.Replication.AwsSso != nil && |
| 57 | + plain.Aws.Replication.AwsSso != nil && |
| 58 | + *obfuscated.Aws.Replication.AwsSso.SsoAccessToken == obfuscatedValue { |
| 59 | + obfuscated.Aws.Replication.AwsSso.SsoAccessToken = plain.Aws.Replication.AwsSso.SsoAccessToken |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + case "azure": |
| 64 | + if obfuscated.Azure != nil && obfuscated.Azure.Replication != nil && plain.Azure != nil && plain.Azure.Replication != nil { |
| 65 | + // replication SP client secret |
| 66 | + if obfuscated.Azure.Replication.ServicePrincipal != nil && |
| 67 | + plain.Azure.Replication.ServicePrincipal != nil && |
| 68 | + *obfuscated.Azure.Replication.ServicePrincipal.CredentialsAuthClientSecret == obfuscatedValue { |
| 69 | + obfuscated.Azure.Replication.ServicePrincipal.CredentialsAuthClientSecret = plain.Azure.Replication.ServicePrincipal.CredentialsAuthClientSecret |
| 70 | + } |
| 71 | + // replication provisioning customer agreement SP client secret |
| 72 | + if obfuscated.Azure.Replication.Provisioning.CustomerAgreement != nil && |
| 73 | + plain.Azure.Replication.Provisioning.CustomerAgreement != nil { |
| 74 | + if obfuscated.Azure.Replication.Provisioning.CustomerAgreement.SourceServicePrincipal != nil && |
| 75 | + plain.Azure.Replication.Provisioning.CustomerAgreement.SourceServicePrincipal != nil && |
| 76 | + *obfuscated.Azure.Replication.Provisioning.CustomerAgreement.SourceServicePrincipal.CredentialsAuthClientSecret == obfuscatedValue { |
| 77 | + obfuscated.Azure.Replication.Provisioning.CustomerAgreement.SourceServicePrincipal.CredentialsAuthClientSecret = plain.Azure.Replication.Provisioning.CustomerAgreement.SourceServicePrincipal.CredentialsAuthClientSecret |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + case "azurerg": |
| 83 | + if obfuscated.AzureRg != nil && obfuscated.AzureRg.Replication != nil && plain.AzureRg != nil && plain.AzureRg.Replication != nil { |
| 84 | + // replication SP client secret |
| 85 | + if obfuscated.AzureRg.Replication.ServicePrincipal != nil && |
| 86 | + plain.AzureRg.Replication.ServicePrincipal != nil && |
| 87 | + *obfuscated.AzureRg.Replication.ServicePrincipal.CredentialsAuthClientSecret == obfuscatedValue { |
| 88 | + obfuscated.AzureRg.Replication.ServicePrincipal.CredentialsAuthClientSecret = plain.AzureRg.Replication.ServicePrincipal.CredentialsAuthClientSecret |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + case "kubernetes": |
| 93 | + if obfuscated.Kubernetes != nil && obfuscated.Kubernetes.Replication != nil && plain.Kubernetes != nil && plain.Kubernetes.Replication != nil { |
| 94 | + // access token |
| 95 | + if obfuscated.Kubernetes.Replication.ClientConfig != nil && |
| 96 | + plain.Kubernetes.Replication.ClientConfig != nil && |
| 97 | + *obfuscated.Kubernetes.Replication.ClientConfig.AccessToken == obfuscatedValue { |
| 98 | + obfuscated.Kubernetes.Replication.ClientConfig.AccessToken = plain.Kubernetes.Replication.ClientConfig.AccessToken |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + case "gcp": |
| 103 | + if obfuscated.Gcp != nil && obfuscated.Gcp.Replication != nil && plain.Gcp != nil && plain.Gcp.Replication != nil { |
| 104 | + // service account credentials |
| 105 | + if obfuscated.Gcp.Replication.ServiceAccountConfig != nil && |
| 106 | + plain.Gcp.Replication.ServiceAccountConfig != nil && |
| 107 | + obfuscated.Gcp.Replication.ServiceAccountConfig.ServiceAccountCredentialsConfig != nil && |
| 108 | + plain.Gcp.Replication.ServiceAccountConfig.ServiceAccountCredentialsConfig != nil && |
| 109 | + *obfuscated.Gcp.Replication.ServiceAccountConfig.ServiceAccountCredentialsConfig.ServiceAccountCredentialsB64 == obfuscatedValue { |
| 110 | + obfuscated.Gcp.Replication.ServiceAccountConfig.ServiceAccountCredentialsConfig = plain.Gcp.Replication.ServiceAccountConfig.ServiceAccountCredentialsConfig |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + case "openshift": |
| 115 | + if obfuscated.OpenShift != nil && obfuscated.OpenShift.Replication != nil && plain.OpenShift != nil && plain.OpenShift.Replication != nil { |
| 116 | + // access token |
| 117 | + if obfuscated.OpenShift.Replication.ClientConfig != nil && |
| 118 | + plain.OpenShift.Replication.ClientConfig != nil && |
| 119 | + *obfuscated.OpenShift.Replication.ClientConfig.AccessToken == obfuscatedValue { |
| 120 | + obfuscated.OpenShift.Replication.ClientConfig.AccessToken = plain.OpenShift.Replication.ClientConfig.AccessToken |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + } |
| 125 | +} |
0 commit comments