-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathqueue.go
More file actions
253 lines (225 loc) · 6.75 KB
/
Copy pathqueue.go
File metadata and controls
253 lines (225 loc) · 6.75 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package lifecycled
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/aws-sdk-go-v2/service/sqs"
sqstypes "github.com/aws/aws-sdk-go-v2/service/sqs/types"
)
const (
longPollingWaitTimeSeconds = 20
queuePolicy = `
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Principal":"*",
"Action":"sqs:SendMessage",
"Resource":"*",
"Condition":{
"ArnEquals":{
"aws:SourceArn":"%s"
}
}
}
]
}
`
)
// SQSClient is the subset of the SQS API used by the daemon.
//
//go:generate go tool mockgen -destination=mocks/mock_sqs_client.go -package=mocks github.com/buildkite/lifecycled SQSClient
type SQSClient interface {
CreateQueue(context.Context, *sqs.CreateQueueInput, ...func(*sqs.Options)) (*sqs.CreateQueueOutput, error)
GetQueueAttributes(context.Context, *sqs.GetQueueAttributesInput, ...func(*sqs.Options)) (*sqs.GetQueueAttributesOutput, error)
ReceiveMessage(context.Context, *sqs.ReceiveMessageInput, ...func(*sqs.Options)) (*sqs.ReceiveMessageOutput, error)
DeleteMessage(context.Context, *sqs.DeleteMessageInput, ...func(*sqs.Options)) (*sqs.DeleteMessageOutput, error)
DeleteQueue(context.Context, *sqs.DeleteQueueInput, ...func(*sqs.Options)) (*sqs.DeleteQueueOutput, error)
}
// SNSClient is the subset of the SNS API used by the daemon.
//
//go:generate go tool mockgen -destination=mocks/mock_sns_client.go -package=mocks github.com/buildkite/lifecycled SNSClient
type SNSClient interface {
Subscribe(context.Context, *sns.SubscribeInput, ...func(*sns.Options)) (*sns.SubscribeOutput, error)
Unsubscribe(context.Context, *sns.UnsubscribeInput, ...func(*sns.Options)) (*sns.UnsubscribeOutput, error)
}
// Queue manages the SQS queue and SNS subscription.
type Queue struct {
name string
url string
arn string
topicArn string
subscriptionArn string
tags string
sqsClient SQSClient
snsClient SNSClient
}
// NewQueue returns a new... Queue.
func NewQueue(queueName, topicArn string, sqsClient SQSClient, snsClient SNSClient, tags string) *Queue {
return &Queue{
name: queueName,
topicArn: topicArn,
sqsClient: sqsClient,
snsClient: snsClient,
tags: tags,
}
}
// Create the SQS queue.
func (q *Queue) Create(ctx context.Context) error {
tags, err := parseTags(q.tags)
if err != nil {
return err
}
out, err := q.sqsClient.CreateQueue(ctx, &sqs.CreateQueueInput{
QueueName: aws.String(q.name),
Attributes: map[string]string{
"Policy": fmt.Sprintf(queuePolicy, q.topicArn),
"ReceiveMessageWaitTimeSeconds": strconv.Itoa(longPollingWaitTimeSeconds),
},
Tags: tags,
})
if err != nil {
return err
}
q.url = aws.ToString(out.QueueUrl)
return nil
}
// GetArn for the SQS queue.
func (q *Queue) getArn(ctx context.Context) (string, error) {
if q.arn == "" {
out, err := q.sqsClient.GetQueueAttributes(ctx, &sqs.GetQueueAttributesInput{
AttributeNames: []sqstypes.QueueAttributeName{sqstypes.QueueAttributeNameQueueArn},
QueueUrl: aws.String(q.url),
})
if err != nil {
return "", err
}
arn, ok := out.Attributes[string(sqstypes.QueueAttributeNameQueueArn)]
if !ok {
return "", errors.New("no attribute QueueArn")
}
q.arn = arn
}
return q.arn, nil
}
// Subscribe the queue to an SNS topic
func (q *Queue) Subscribe(ctx context.Context) error {
arn, err := q.getArn(ctx)
if err != nil {
return err
}
out, err := q.snsClient.Subscribe(ctx, &sns.SubscribeInput{
TopicArn: aws.String(q.topicArn),
Protocol: aws.String("sqs"),
Endpoint: aws.String(arn),
})
if err != nil {
return err
}
q.subscriptionArn = aws.ToString(out.SubscriptionArn)
return nil
}
// GetMessages long polls for messages from the SQS queue.
func (q *Queue) GetMessages(ctx context.Context) ([]sqstypes.Message, error) {
out, err := q.sqsClient.ReceiveMessage(ctx, &sqs.ReceiveMessageInput{
QueueUrl: aws.String(q.url),
// SQS max per receive. Under SNS fan-out the queue also carries other
// instances' events, so draining a batch per poll beats one-at-a-time.
MaxNumberOfMessages: 10,
WaitTimeSeconds: longPollingWaitTimeSeconds,
VisibilityTimeout: 0,
})
if err != nil {
// Ignore error if the context was cancelled (i.e. we are shutting down)
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return nil, nil
}
return nil, err
}
return out.Messages, nil
}
// DeleteMessage from the queue.
func (q *Queue) DeleteMessage(ctx context.Context, receiptHandle string) error {
_, err := q.sqsClient.DeleteMessage(ctx, &sqs.DeleteMessageInput{
QueueUrl: aws.String(q.url),
ReceiptHandle: aws.String(receiptHandle),
})
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return nil
}
return err
}
return nil
}
// Unsubscribe the queue from the SNS topic.
func (q *Queue) Unsubscribe(ctx context.Context) error {
_, err := q.snsClient.Unsubscribe(ctx, &sns.UnsubscribeInput{
SubscriptionArn: aws.String(q.subscriptionArn),
})
return err
}
// Delete the SQS queue.
func (q *Queue) Delete(ctx context.Context) error {
_, err := q.sqsClient.DeleteQueue(ctx, &sqs.DeleteQueueInput{
QueueUrl: aws.String(q.url),
})
if err != nil {
// Ignore error if queue does not exist (which is what we want)
var notExist *sqstypes.QueueDoesNotExist
if !errors.As(err, ¬Exist) {
return err
}
}
return nil
}
// Expects format like "key1=alpha,key2=beta"
func parseTags(input string) (map[string]string, error) {
if input == "" {
return nil, nil
}
const (
maxTags = 50
maxKeyLength = 128
maxValueLength = 256
)
tags := make(map[string]string)
pairs := strings.Split(input, ",")
for _, pair := range pairs {
parts := strings.SplitN(pair, "=", 2)
if len(parts) != 2 {
continue
}
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
if key == "" {
continue
}
// Check key length
if len(key) > maxKeyLength {
return nil, fmt.Errorf("tag key exceeds maximum length of %d characters: %q", maxKeyLength, key)
}
// Check value length
if len(value) > maxValueLength {
return nil, fmt.Errorf("tag value exceeds maximum length of %d characters for key %q", maxValueLength, key)
}
// Check for aws: prefix (case-insensitive)
if len(key) >= 4 && strings.ToLower(key[:4]) == "aws:" {
return nil, fmt.Errorf("tag keys cannot start with 'aws:' prefix: %q", key)
}
tags[key] = value
}
// Check total number of tags
if len(tags) > maxTags {
return nil, fmt.Errorf("number of tags (%d) exceeds maximum allowed (%d)", len(tags), maxTags)
}
if len(tags) == 0 {
return map[string]string{}, nil
}
return tags, nil
}