/
table_aws_sqs_queue.go
323 lines (288 loc) · 10.1 KB
/
table_aws_sqs_queue.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package aws
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/sqs"
sqsTypes "github.com/aws/aws-sdk-go-v2/service/sqs/types"
sqsv1 "github.com/aws/aws-sdk-go/service/sqs"
"github.com/turbot/go-kit/types"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)
//// TABLE DEFINITION
func tableAwsSqsQueue(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_sqs_queue",
Description: "AWS SQS Queue",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("queue_url"),
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"AWS.SimpleQueueService.NonExistentQueue"}),
},
Hydrate: getQueueAttributes,
Tags: map[string]string{"service": "sqs", "action": "GetQueueAttributes"},
},
List: &plugin.ListConfig{
Hydrate: listAwsSqsQueues,
Tags: map[string]string{"service": "sqs", "action": "ListQueues"},
},
HydrateConfig: []plugin.HydrateConfig{
{
Func: getQueueAttributes,
Tags: map[string]string{"service": "sqs", "action": "GetQueueAttributes"},
},
{
Func: listQueueTags,
Tags: map[string]string{"service": "sqs", "action": "ListQueueTags"},
},
},
DefaultIgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"AWS.SimpleQueueService.NonExistentQueue"}),
},
GetMatrixItemFunc: SupportedRegionMatrix(sqsv1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "queue_url",
Description: "The URL of the Amazon SQS queue.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Attributes.QueueUrl"),
},
{
Name: "queue_arn",
Description: "The Amazon resource name (ARN) of the queue.",
Type: proto.ColumnType_STRING,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.QueueArn"),
},
{
Name: "fifo_queue",
Description: "Returns true if the queue is FIFO.",
Type: proto.ColumnType_BOOL,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.FifoQueue"),
Default: false,
},
{
Name: "fifo_throughput_limit",
Description: "Specifies whether the FIFO queue throughput quota applies to the entire queue or per message group.",
Type: proto.ColumnType_STRING,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.FifoThroughputLimit"),
Default: false,
},
{
Name: "delay_seconds",
Description: "The default delay on the queue in seconds.",
Type: proto.ColumnType_STRING,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.DelaySeconds"),
},
{
Name: "max_message_size",
Description: "The limit of how many bytes a message can contain before Amazon SQS rejects it.",
Type: proto.ColumnType_STRING,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.MaximumMessageSize"),
},
{
Name: "message_retention_seconds",
Description: "The length of time, in seconds, for which Amazon SQS retains a message.",
Type: proto.ColumnType_STRING,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.MessageRetentionPeriod"),
},
{
Name: "receive_wait_time_seconds",
Description: "The length of time, in seconds, for which the ReceiveMessage action waits for a message to arrive.",
Type: proto.ColumnType_STRING,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.ReceiveMessageWaitTimeSeconds"),
},
{
Name: "sqs_managed_sse_enabled",
Description: "Returns true if the queue is using SSE-SQS encryption with SQS-owned encryption keys.",
Type: proto.ColumnType_BOOL,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.SqsManagedSseEnabled"),
},
{
Name: "visibility_timeout_seconds",
Description: "The visibility timeout for the queue in seconds.",
Type: proto.ColumnType_STRING,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.VisibilityTimeout"),
},
{
Name: "policy",
Description: "The resource IAM policy of the queue.",
Type: proto.ColumnType_JSON,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.Policy").Transform(transform.UnmarshalYAML),
},
{
Name: "policy_std",
Description: "Contains the policy in a canonical form for easier searching.",
Type: proto.ColumnType_JSON,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.Policy").Transform(unescape).Transform(policyToCanonical),
},
{
Name: "redrive_policy",
Description: "The string that includes the parameters for the dead-letter queue functionality of the source queue as a JSON object.",
Type: proto.ColumnType_JSON,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.RedrivePolicy").Transform(transform.UnmarshalYAML),
},
{
Name: "content_based_deduplication",
Description: "Mentions whether content-based deduplication is enabled for the queue.",
Type: proto.ColumnType_STRING,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.ContentBasedDeduplication"),
},
{
Name: "deduplication_scope",
Description: "Specifies whether message deduplication occurs at the message group or queue level.",
Type: proto.ColumnType_STRING,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.DeduplicationScope"),
},
{
Name: "kms_master_key_id",
Description: "The ID of an AWS-managed customer master key (CMK) for Amazon SQS or a custom CMK.",
Type: proto.ColumnType_STRING,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.KmsMasterKeyId"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: listQueueTags,
},
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.FromField("Attributes.QueueUrl").Transform(getAwsSqsQueueTitle),
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Hydrate: getQueueAttributes,
Transform: transform.FromField("Attributes.QueueArn").Transform(arnToAkas),
},
}),
}
}
//// LIST FUNCTION
func listAwsSqsQueues(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Get client
svc, err := SQSClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_sqs_queue.listAwsSqsQueues", "get_client_error", err)
return nil, err
}
input := &sqs.ListQueuesInput{
MaxResults: aws.Int32(1000),
}
// Limiting the results
maxLimit := int32(1000)
if d.QueryContext.Limit != nil {
limit := int32(*d.QueryContext.Limit)
if limit < maxLimit {
if limit < 1 {
input.MaxResults = aws.Int32(1)
} else {
input.MaxResults = aws.Int32(limit)
}
}
}
paginator := sqs.NewListQueuesPaginator(svc, input, func(o *sqs.ListQueuesPaginatorOptions) {
o.Limit = maxLimit
o.StopOnDuplicateToken = true
})
for paginator.HasMorePages() {
// apply rate limiting
d.WaitForListRateLimit(ctx)
output, err := paginator.NextPage(ctx)
if err != nil {
plugin.Logger(ctx).Error("aws_sqs_queue.listAwsSqsQueues", "api_error", err)
return nil, err
}
for _, queueURL := range output.QueueUrls {
d.StreamListItem(ctx, &sqs.GetQueueAttributesOutput{
Attributes: map[string]string{
"QueueUrl": queueURL,
},
})
// Context may get cancelled due to manual cancellation or if the limit has been reached
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}
return nil, err
}
//// HYDRATE FUNCTIONS
func getQueueAttributes(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
var queueURL string
if h.Item != nil {
data := h.Item.(*sqs.GetQueueAttributesOutput)
queueURL = types.SafeString(data.Attributes["QueueUrl"])
} else {
queueURL = d.EqualsQuals["queue_url"].GetStringValue()
}
if queueURL == "" {
return nil, nil
}
// Get client
svc, err := SQSClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_sqs_queue.getQueueAttributes", "get_client_error", err)
return nil, err
}
// Build params
params := &sqs.GetQueueAttributesInput{
QueueUrl: aws.String(queueURL),
AttributeNames: []sqsTypes.QueueAttributeName{sqsTypes.QueueAttributeName("All")},
}
op, err := svc.GetQueueAttributes(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_sqs_queue.getQueueAttributes", "api_error", err)
return nil, err
}
// Add QueueUrl info to the output as it is missing from GetQueueAttributesOutput
op.Attributes["QueueUrl"] = queueURL
return op, nil
}
func listQueueTags(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
queueAttributesOutput := h.Item.(*sqs.GetQueueAttributesOutput)
// Get client
svc, err := SQSClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_sqs_queue.listQueueTags", "get_client_error", err)
return nil, err
}
// Build the params
param := &sqs.ListQueueTagsInput{
QueueUrl: aws.String(queueAttributesOutput.Attributes["QueueUrl"]),
}
queueTags, err := svc.ListQueueTags(ctx, param)
if err != nil {
plugin.Logger(ctx).Error("aws_sqs_queue.listQueueTags", "api_error", err)
return nil, err
}
return queueTags, nil
}
//// TRANSFORM FUNCTION
func getAwsSqsQueueTitle(_ context.Context, d *transform.TransformData) (interface{}, error) {
queueURL := types.SafeString(d.Value)
queueName, err := extractNameFromSqsQueueURL(queueURL)
if err != nil {
return nil, err
}
return queueName, nil
}