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

resource/kubernetes_secret: Prevent binary data corruption #103

Merged
merged 1 commit into from
Feb 22, 2018
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
2 changes: 1 addition & 1 deletion kubernetes/resource_kubernetes_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func resourceKubernetesSecretCreate(d *schema.ResourceData, meta interface{}) er
metadata := expandMetadata(d.Get("metadata").([]interface{}))
secret := api.Secret{
ObjectMeta: metadata,
StringData: expandStringMap(d.Get("data").(map[string]interface{})),
Data: expandStringMapToByteMap(d.Get("data").(map[string]interface{})),
}

if v, ok := d.GetOk("type"); ok {
Expand Down
53 changes: 53 additions & 0 deletions kubernetes/resource_kubernetes_secret_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,34 @@ func TestAccKubernetesSecret_importGeneratedName(t *testing.T) {
})
}

func TestAccKubernetesSecret_binaryData(t *testing.T) {
var conf api.Secret
prefix := "tf-acc-test-gen-"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
IDRefreshName: "kubernetes_secret.test",
Providers: testAccProviders,
CheckDestroy: testAccCheckKubernetesSecretDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesSecretConfig_binaryData(prefix),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesSecretExists("kubernetes_secret.test", &conf),
resource.TestCheckResourceAttr("kubernetes_secret.test", "data.%", "1"),
),
},
{
Config: testAccKubernetesSecretConfig_binaryData2(prefix),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesSecretExists("kubernetes_secret.test", &conf),
resource.TestCheckResourceAttr("kubernetes_secret.test", "data.%", "2"),
),
},
},
})
}

func testAccCheckSecretData(m *api.Secret, expected map[string]string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if len(expected) == 0 && len(m.Data) == 0 {
Expand Down Expand Up @@ -329,3 +357,28 @@ resource "kubernetes_secret" "test" {
}
}`, prefix)
}

func testAccKubernetesSecretConfig_binaryData(prefix string) string {
return fmt.Sprintf(`
resource "kubernetes_secret" "test" {
metadata {
generate_name = "%s"
}
data {
one = "${file("./test-fixtures/binary.data")}"
}
}`, prefix)
}

func testAccKubernetesSecretConfig_binaryData2(prefix string) string {
return fmt.Sprintf(`
resource "kubernetes_secret" "test" {
metadata {
generate_name = "%s"
}
data {
one = "${file("./test-fixtures/binary2.data")}"
two = "${file("./test-fixtures/binary.data")}"
}
}`, prefix)
}
8 changes: 8 additions & 0 deletions kubernetes/structures.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func expandStringMap(m map[string]interface{}) map[string]string {
return result
}

func expandStringMapToByteMap(m map[string]interface{}) map[string][]byte {
result := make(map[string][]byte)
for k, v := range m {
result[k] = []byte(v.(string))
}
return result
}

func expandStringSlice(s []interface{}) []string {
result := make([]string, len(s), len(s))
for k, v := range s {
Expand Down
Binary file added kubernetes/test-fixtures/binary.data
Binary file not shown.
Binary file added kubernetes/test-fixtures/binary2.data
Binary file not shown.