-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqs_policy.go
84 lines (71 loc) · 1.71 KB
/
sqs_policy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package mq
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
type sqsPolicy struct {
Version string
ID string `json:"Id"`
Statement []json.RawMessage
}
type sqsPolicyStatement struct {
Sid string
Effect string
Principal map[string]string
Action string
Resource string
Condition map[string]map[string]string
}
func (policy *sqsPolicy) AddPermission(queueARN, topicARN string) error {
exists := false
for _, statementBytes := range policy.Statement {
statement := new(sqsPolicyStatement)
if err := json.Unmarshal(statementBytes, statement); err != nil {
return err
}
if statement.Effect != "Allow" ||
statement.Principal["AWS"] != "*" ||
statement.Action != "SQS:SendMessage" ||
statement.Resource != queueARN {
continue
}
if statement.Condition["ArnEquals"] == nil {
continue
}
if statement.Condition["ArnEquals"]["aws:SourceArn"] != topicARN {
continue
}
exists = true
break
}
if !exists {
statement := newSqsPolicyStatement(queueARN, topicARN)
statementBytes, _ := json.Marshal(statement)
policy.Statement = append(policy.Statement, json.RawMessage(statementBytes))
}
return nil
}
func newSqsPolicyStatement(queueARN, topicARN string) *sqsPolicyStatement {
return &sqsPolicyStatement{
Sid: fmt.Sprintf("Sid%s", strconv.FormatInt(time.Now().Unix(), 10)),
Effect: "Allow",
Principal: map[string]string{
"AWS": "*",
},
Action: "SQS:SendMessage",
Resource: queueARN,
Condition: map[string]map[string]string{
"ArnEquals": {
"aws:SourceArn": topicARN,
},
},
}
}
func newSqsPolicy(queueARN string) *sqsPolicy {
return &sqsPolicy{
Version: "2012-10-17",
ID: fmt.Sprintf("%s/SQSDefaultPolicy", queueARN),
}
}