Skip to content

Commit

Permalink
Merge pull request #13517 from hashicorp/b/iothub-connection-string-bug
Browse files Browse the repository at this point in the history
'azurerm_iothub' fix DiffSuppress for 'connection_string'
  • Loading branch information
stephybun authored Sep 28, 2021
2 parents 0edd625 + 429b767 commit 4983f3b
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions internal/services/iothub/iothub_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"regexp"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -158,19 +159,10 @@ func resourceIotHub() *pluginsdk.Resource {
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"connection_string": {
Type: pluginsdk.TypeString,
Required: true,
DiffSuppressFunc: func(k, old, new string, d *pluginsdk.ResourceData) bool {
secretKeyRegex := regexp.MustCompile("(SharedAccessKey|AccountKey)=[^;]+")
sbProtocolRegex := regexp.MustCompile("sb://([^:]+)(:5671)?/;")

// Azure will always mask the Access Keys and will include the port number in the GET response
// 5671 is the default port for Azure Service Bus connections
maskedNew := sbProtocolRegex.ReplaceAllString(new, "sb://$1:5671/;")
maskedNew = secretKeyRegex.ReplaceAllString(maskedNew, "$1=****")
return (new == d.Get(k).(string)) && (maskedNew == old)
},
Sensitive: true,
Type: pluginsdk.TypeString,
Required: true,
DiffSuppressFunc: fileUploadConnectionStringDiffSuppress,
Sensitive: true,
},
"container_name": {
Type: pluginsdk.TypeString,
Expand Down Expand Up @@ -1296,3 +1288,31 @@ func flattenIPFilterRules(in *[]devices.IPFilterRule) []interface{} {
}
return rules
}

func fileUploadConnectionStringDiffSuppress(k, old, new string, d *pluginsdk.ResourceData) bool {
// The access keys are always masked by Azure and the ordering of the parameters in the connection string
// differs across services, so we will compare the fields individually instead.
secretKeyRegex := regexp.MustCompile("(SharedAccessKey|AccountKey)=[^;]+")

if secretKeyRegex.MatchString(new) {
maskedNew := secretKeyRegex.ReplaceAllString(new, "$1=****")

oldSplit := strings.Split(old, ";")
newSplit := strings.Split(maskedNew, ";")

sort.Strings(oldSplit)
sort.Strings(newSplit)

if len(oldSplit) != len(newSplit) {
return false
}

for i := range oldSplit {
if !strings.EqualFold(oldSplit[i], newSplit[i]) {
return false
}
}
return true
}
return false
}

0 comments on commit 4983f3b

Please sign in to comment.