Skip to content

Commit

Permalink
Merge pull request #118 from Comcast/fix-shallow-copy-bug
Browse files Browse the repository at this point in the history
Fix a shallow copy bug by not using that pattern before updating the …
  • Loading branch information
kristinapathak authored Feb 14, 2019
2 parents f10f3c1 + 800e305 commit 98a97d0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 39 deletions.
71 changes: 33 additions & 38 deletions src/caduceus/outboundSender.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,50 +239,41 @@ func (osf OutboundSenderFactory) New() (obs OutboundSender, err error) {
// Update applies user configurable values for the outbound sender when a
// webhook is registered
func (obs *CaduceusOutboundSender) Update(wh webhook.W) (err error) {
// make a copy
obsCopy := *obs

// set events & matchers to empty
obsCopy.events = []*regexp.Regexp{}
obsCopy.matcher = []*regexp.Regexp{}

obsCopy.listener = wh
obsCopy.failureMsg.Original = wh

// Don't share the secret with others when there is an error.
obsCopy.failureMsg.Original.Config.Secret = "XxxxxX"

if "" != obsCopy.listener.Config.Secret {
obsCopy.secret = []byte(obsCopy.listener.Config.Secret)
}

if "" != obsCopy.listener.FailureURL {
if _, err = url.ParseRequestURI(obsCopy.listener.FailureURL); nil != err {
// Validate the failure URL, if present
if "" != wh.FailureURL {
if _, err = url.ParseRequestURI(wh.FailureURL); nil != err {
return
}
}

obsCopy.deliverUntil = obsCopy.listener.Until

// Create the event regex objects
// Create and validate the event regex objects
var events []*regexp.Regexp
for _, event := range wh.Events {
var re *regexp.Regexp
if re, err = regexp.Compile(event); nil != err {
return
}

obsCopy.events = append(obsCopy.events, re)
events = append(events, re)
}
if nil == obsCopy.events || len(obsCopy.events) == 0 {
if len(events) < 1 {
err = errors.New("Events must not be empty.")
return
}

// Make the secret bytes if available
var secret []byte
if "" != wh.Config.Secret {
secret = []byte(wh.Config.Secret)
}

// Create the matcher regex objects
matcher := []*regexp.Regexp{}
for _, item := range wh.Matcher.DeviceId {
if ".*" == item {
// Match everything - skip the filtering
obsCopy.matcher = nil
matcher = []*regexp.Regexp{}
break
}

Expand All @@ -291,28 +282,32 @@ func (obs *CaduceusOutboundSender) Update(wh webhook.W) (err error) {
err = fmt.Errorf("Invalid matcher item: '%s'", item)
return
}
obsCopy.matcher = append(obsCopy.matcher, re)
}
// if matcher list is empty set it nil for Queue() logic
if len(obsCopy.matcher) == 0 {
obsCopy.matcher = nil
matcher = append(matcher, re)
}

// write/update obs
obs.mutex.Lock()

obs.listener = obsCopy.listener
obs.failureMsg.Original = obsCopy.failureMsg.Original
obs.failureMsg.Original.Config.Secret = obsCopy.failureMsg.Original.Config.Secret
obs.secret = obsCopy.secret
obs.listener.FailureURL = obsCopy.listener.FailureURL
obs.deliverUntil = obsCopy.deliverUntil
obs.events = obsCopy.events
obs.matcher = obsCopy.matcher
obs.listener = wh

obs.failureMsg.Original = wh
// Don't share the secret with others when there is an error.
obs.failureMsg.Original.Config.Secret = "XxxxxX"

obs.secret = secret
obs.listener.FailureURL = wh.FailureURL
obs.deliverUntil = wh.Until
obs.events = events

// if matcher list is empty set it nil for Queue() logic
obs.matcher = nil
if 0 < len(matcher) {
obs.matcher = matcher
}

obs.mutex.Unlock()

obs.secretChan <- obsCopy.secret
obs.secretChan <- secret

return
}
Expand Down
1 change: 0 additions & 1 deletion src/glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ import:
version: 6ca7d6c5e78ebba17483f5d0e61c7c119b43619a
- package: github.com/satori/go.uuid
version: f58768cc1a7a7e77a3bd49e98cdd21419399b6a3
- package: github.com/gorilla/websocket

0 comments on commit 98a97d0

Please sign in to comment.