Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
bugfix in policy
  • Loading branch information
Amit Mor committed Dec 8, 2021
commit f46840efff9875e0572f59b5995dd28fe72aff2e
44 changes: 44 additions & 0 deletions pubsub/aws/snssqs/policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package snssqs

type arnEquals struct {
AwsSourceArn string `json:"aws:SourceArn"`
}

type condition struct {
ArnEquals arnEquals
}

type principal struct {
Service string
}

type statement struct {
Effect string
Principal principal
Action string
Resource string
Condition condition
}

type policy struct {
Version string
Statement []statement
}

func (p *policy) statementExists(other *statement) bool {
for _, s := range p.Statement {
if s.Effect == other.Effect &&
s.Principal.Service == other.Principal.Service &&
s.Action == other.Action &&
s.Resource == other.Resource &&
s.Condition.ArnEquals.AwsSourceArn == other.Condition.ArnEquals.AwsSourceArn {
return true
}
}

return false
}

func (p *policy) addStatement(other *statement) {
p.Statement = append(p.Statement, *other)
}
42 changes: 2 additions & 40 deletions pubsub/aws/snssqs/snssqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,27 +65,6 @@ type snsSqsMetadata struct {
messageMaxNumber int64
}

type arnEquals struct {
AwsSourceArn string `json:"aws:SourceArn"`
}

type condition struct {
ArnEquals arnEquals
}

type statement struct {
Effect string
Principal string
Action string
Resource string
Condition condition
}

type policy struct {
Version string
Statement []statement
}

const (
awsSqsQueueNameKey = "dapr-queue-name"
awsSnsTopicNameKey = "dapr-topic-name"
Expand Down Expand Up @@ -143,23 +122,6 @@ func nameToAWSSanitizedName(name string) string {
return string(s[:j])
}

func (p *policy) statementExists(other *statement) bool {
for _, s := range p.Statement {
if s.Effect == other.Effect &&
s.Principal == other.Principal &&
s.Action == other.Action &&
s.Resource == other.Resource &&
s.Condition.ArnEquals.AwsSourceArn == other.Condition.ArnEquals.AwsSourceArn {
return true
}
}
return false
}

func (p *policy) addStatement(other *statement) {
p.Statement = append(p.Statement, *other)
}

func (s *snsSqs) getSnsSqsMetatdata(metadata pubsub.Metadata) (*snsSqsMetadata, error) {
md := snsSqsMetadata{}
props := metadata.Properties
Expand Down Expand Up @@ -558,7 +520,7 @@ func (s *snsSqs) restrictQueuePublishPolicyToOnlySNS(sqsQueueInfo *sqsQueueInfo,

newStatement := &statement{
Effect: "Allow",
Principal: `{"Service": "sns.amazonaws.com"}`,
Principal: principal{Service: "sns.amazonaws.com"},
Action: "sqs:SendMessage",
Resource: sqsQueueInfo.arn,
Condition: condition{
Expand All @@ -568,7 +530,7 @@ func (s *snsSqs) restrictQueuePublishPolicyToOnlySNS(sqsQueueInfo *sqsQueueInfo,
},
}

policy := &policy{Version: "2012-11-05"}
policy := &policy{Version: "2012-10-17"}
if policyStr, ok := getQueueAttributesOutput.Attributes[sqs.QueueAttributeNamePolicy]; ok {
// look for the current statement if exists, else add it and store.
if err = json.Unmarshal([]byte(*policyStr), policy); err != nil {
Expand Down