Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datasource: kubernetes_secret: add binary_data attribute #1285

Merged
merged 4 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions kubernetes/data_source_kubernetes_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubernetes

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -19,6 +20,12 @@ func dataSourceKubernetesSecret() *schema.Resource {
Computed: true,
Sensitive: true,
},
"binary_data": {
Type: schema.TypeMap,
Description: "A map of the secret data with values encoded in base64 format",
Optional: true,
Sensitive: true,
},
"type": {
Type: schema.TypeString,
Description: "Type of secret",
Expand Down
10 changes: 10 additions & 0 deletions kubernetes/data_source_kubernetes_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestAccKubernetesDataSourceSecret_basic(t *testing.T) {
resource.TestCheckResourceAttr("kubernetes_secret.test", "data.one", "first"),
resource.TestCheckResourceAttr("kubernetes_secret.test", "data.two", "second"),
resource.TestCheckResourceAttr("kubernetes_secret.test", "type", "Opaque"),
resource.TestCheckResourceAttr("kubernetes_secret.test", "binary_data.raw", "UmF3IGRhdGEgc2hvdWxkIGNvbWUgYmFjayBhcyBpcyBpbiB0aGUgcG9k"),
),
},
{
Expand All @@ -52,6 +53,7 @@ func TestAccKubernetesDataSourceSecret_basic(t *testing.T) {
resource.TestCheckResourceAttr("data.kubernetes_secret.test", "data.one", "first"),
resource.TestCheckResourceAttr("data.kubernetes_secret.test", "data.two", "second"),
resource.TestCheckResourceAttr("data.kubernetes_secret.test", "type", "Opaque"),
resource.TestCheckResourceAttr("data.kubernetes_secret.test", "binary_data.raw", "UmF3IGRhdGEgc2hvdWxkIGNvbWUgYmFjayBhcyBpcyBpbiB0aGUgcG9k"),
),
},
},
Expand Down Expand Up @@ -79,6 +81,10 @@ func testAccKubernetesDataSourceSecretConfig_basic(name string) string {
one = "first"
two = "second"
}

binary_data = {
raw = "${base64encode("Raw data should come back as is in the pod")}"
}
}
`, name)
}
Expand All @@ -88,6 +94,10 @@ func testAccKubernetesDataSourceSecretConfig_read() string {
metadata {
name = "${kubernetes_secret.test.metadata.0.name}"
}

binary_data = {
raw = ""
}
}
`)
}
17 changes: 17 additions & 0 deletions website/docs/d/secret.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,21 @@ The following arguments are supported:
## Attribute Reference

* `data` - A map of the secret data.
* `binary_data` - A map of the secret data with values encoded in base64 format.

~> In case the secret has been created outside terraform in order to retrieve binary data from the secret in base64 format you need to define a `binary_data` map with data to retrieve as key and an empty string as a value

```hcl
data "kubernetes_secret" "example" {
metadata {
name = "example-secret"
namespace = "kube-system"
}
binary_data = {
"keystore.p12" = ""
another_field = ""
}
}
```

* `type` - The secret type. Defaults to `Opaque`. For more info see [Kubernetes reference](https://github.com/kubernetes/community/blob/c7151dd8dd7e487e96e5ce34c6a416bb3b037609/contributors/design-proposals/auth/secrets.md#proposed-design)