Skip to content

Commit

Permalink
Hash custom_data in state storage (#12214)
Browse files Browse the repository at this point in the history
This also switches to helpers for b64.
  • Loading branch information
sethvargo authored and stack72 committed Mar 8, 2017
1 parent 3022eb6 commit d387860
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 43 deletions.
31 changes: 31 additions & 0 deletions builtin/providers/azurerm/provider.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package azurerm

import (
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"fmt"
"log"
"reflect"
Expand Down Expand Up @@ -323,3 +326,31 @@ func ignoreCaseDiffSuppressFunc(k, old, new string, d *schema.ResourceData) bool
func ignoreCaseStateFunc(val interface{}) string {
return strings.ToLower(val.(string))
}

func userDataStateFunc(v interface{}) string {
switch s := v.(type) {
case string:
s = base64Encode(s)
hash := sha1.Sum([]byte(s))
return hex.EncodeToString(hash[:])
default:
return ""
}
}

// Base64Encode encodes data if the input isn't already encoded using
// base64.StdEncoding.EncodeToString. If the input is already base64 encoded,
// return the original input unchanged.
func base64Encode(data string) string {
// Check whether the data is already Base64 encoded; don't double-encode
if isBase64Encoded(data) {
return data
}
// data has not been encoded encode and return
return base64.StdEncoding.EncodeToString([]byte(data))
}

func isBase64Encoded(data string) bool {
_, err := base64.StdEncoding.DecodeString(data)
return err == nil
}
24 changes: 6 additions & 18 deletions builtin/providers/azurerm/resource_arm_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package azurerm

import (
"bytes"
"encoding/base64"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -296,9 +295,10 @@ func resourceArmVirtualMachine() *schema.Resource {
},

"custom_data": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
StateFunc: userDataStateFunc,
},
},
},
Expand Down Expand Up @@ -887,14 +887,7 @@ func flattenAzureRmVirtualMachineOsProfile(osProfile *compute.OSProfile) []inter
result["computer_name"] = *osProfile.ComputerName
result["admin_username"] = *osProfile.AdminUsername
if osProfile.CustomData != nil {
var data string
if isBase64Encoded(*osProfile.CustomData) {
decodedData, _ := base64.StdEncoding.DecodeString(*osProfile.CustomData)
data = string(decodedData)
} else {
data = *osProfile.CustomData
}
result["custom_data"] = data
result["custom_data"] = *osProfile.CustomData
}

return []interface{}{result}
Expand Down Expand Up @@ -1047,12 +1040,7 @@ func expandAzureRmVirtualMachineOsProfile(d *schema.ResourceData) (*compute.OSPr
}

if v := osProfile["custom_data"].(string); v != "" {
if isBase64Encoded(v) {
log.Printf("[WARN] Future Versions of Terraform will automatically base64encode custom_data")
} else {
v = base64Encode(v)
}

v = base64Encode(v)
profile.CustomData = &v
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package azurerm

import (
"bytes"
"encoding/base64"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -93,8 +92,9 @@ func resourceArmVirtualMachineScaleSet() *schema.Resource {
},

"custom_data": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
StateFunc: userDataStateFunc,
},
},
},
Expand Down Expand Up @@ -652,14 +652,7 @@ func flattenAzureRMVirtualMachineScaleSetOsProfile(profile *compute.VirtualMachi
result["admin_username"] = *profile.AdminUsername

if profile.CustomData != nil {
var data string
if isBase64Encoded(*profile.CustomData) {
decodedData, _ := base64.StdEncoding.DecodeString(*profile.CustomData)
data = string(decodedData)
} else {
data = *profile.CustomData
}
result["custom_data"] = data
result["custom_data"] = *profile.CustomData
}

return []interface{}{result}
Expand Down Expand Up @@ -861,15 +854,6 @@ func expandAzureRmVirtualMachineScaleSetNetworkProfile(d *schema.ResourceData) *
}
}

func base64Encode(data string) string {
return base64.StdEncoding.EncodeToString([]byte(data))
}

func isBase64Encoded(data string) bool {
_, err := base64.StdEncoding.DecodeString(data)
return err == nil
}

func expandAzureRMVirtualMachineScaleSetsOsProfile(d *schema.ResourceData) (*compute.VirtualMachineScaleSetOSProfile, error) {
osProfileConfigs := d.Get("os_profile").(*schema.Set).List()

Expand All @@ -889,11 +873,7 @@ func expandAzureRMVirtualMachineScaleSetsOsProfile(d *schema.ResourceData) (*com
}

if customData != "" {
if isBase64Encoded(customData) {
log.Printf("[WARN] Future Versions of Terraform will automatically base64encode custom_data")
} else {
customData = base64Encode(customData)
}
customData = base64Encode(customData)
osProfile.CustomData = &customData
}

Expand Down

0 comments on commit d387860

Please sign in to comment.