/
sns.go
198 lines (163 loc) · 5.86 KB
/
sns.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
// Copyright 2021 Nitric Pty Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package topic
import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sfn"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/aws-sdk-go-v2/service/sns/types"
"github.com/aws/smithy-go"
"go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws"
"go.opentelemetry.io/contrib/propagators/aws/xray"
"go.opentelemetry.io/otel/propagation"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/proto"
"github.com/nitrictech/nitric/cloud/aws/ifaces/sfniface"
"github.com/nitrictech/nitric/cloud/aws/ifaces/snsiface"
"github.com/nitrictech/nitric/cloud/aws/runtime/env"
"github.com/nitrictech/nitric/cloud/aws/runtime/resource"
grpc_errors "github.com/nitrictech/nitric/core/pkg/grpc/errors"
"github.com/nitrictech/nitric/core/pkg/help"
topicpb "github.com/nitrictech/nitric/core/pkg/proto/topics/v1"
)
type SnsEventService struct {
client snsiface.SNSAPI
sfnClient sfniface.SFNAPI
provider resource.AwsResourceProvider
}
var _ topicpb.TopicsServer = &SnsEventService{}
func isSNSAccessDeniedErr(err error) bool {
var opErr *smithy.OperationError
if errors.As(err, &opErr) {
return opErr.Service() == "SNS" && strings.Contains(opErr.Unwrap().Error(), "AuthorizationError")
}
return false
}
func (s *SnsEventService) getTopics(ctx context.Context) (map[string]resource.ResolvedResource, error) {
return s.provider.GetResources(ctx, resource.AwsResource_Topic)
}
func (s *SnsEventService) getStateMachines(ctx context.Context) (map[string]resource.ResolvedResource, error) {
return s.provider.GetResources(ctx, resource.AwsResource_StateMachine)
}
func (s *SnsEventService) publish(ctx context.Context, topic string, message string) error {
topics, err := s.getTopics(ctx)
if err != nil {
return fmt.Errorf("error finding topics: %w", err)
}
snsTopic, ok := topics[topic]
if !ok {
return fmt.Errorf("could not resolve topic ARN from topic name")
}
mc := propagation.MapCarrier{}
xray.Propagator{}.Inject(ctx, mc)
attrs := map[string]types.MessageAttributeValue{}
for k, v := range mc {
attrs[k] = types.MessageAttributeValue{DataType: aws.String("String"), StringValue: aws.String(v)}
}
publishInput := &sns.PublishInput{
TopicArn: aws.String(snsTopic.ARN),
Message: &message,
// MessageStructure: json is for an AWS specific JSON format,
// which sends different messages to different subscription types. Don't use it.
// MessageStructure: aws.String("json"),
MessageAttributes: attrs,
}
_, err = s.client.Publish(ctx, publishInput)
return err
}
func (s *SnsEventService) publishDelayed(ctx context.Context, topic string, delay time.Duration, message string) error {
stepFunctions, err := s.getStateMachines(ctx)
if err != nil {
return fmt.Errorf("error getting state machines: %w", err)
}
stepFunction, ok := stepFunctions[topic]
if !ok {
return fmt.Errorf("error resolving state machine ARN for topic %s: %w", topic, err)
}
mc := propagation.MapCarrier{}
xray.Propagator{}.Inject(ctx, mc)
input, err := json.Marshal(map[string]interface{}{
"seconds": int(delay / time.Second),
"message": message,
})
if err != nil {
return err
}
_, err = s.sfnClient.StartExecution(ctx, &sfn.StartExecutionInput{
StateMachineArn: aws.String(stepFunction.ARN),
TraceHeader: aws.String(mc[xray.Propagator{}.Fields()[0]]),
Input: aws.String(string(input)),
})
return err
}
// Publish to a given topic
func (s *SnsEventService) Publish(ctx context.Context, req *topicpb.TopicPublishRequest) (*topicpb.TopicPublishResponse, error) {
newErr := grpc_errors.ErrorsWithScope("SnsEventService.Publish")
messageBytes, err := proto.Marshal(req.Message)
if err != nil {
return nil, newErr(
codes.Unknown,
fmt.Sprintf("unable to serialize message. %s", help.BugInNitricHelpText()),
err,
)
}
message := base64.StdEncoding.EncodeToString(messageBytes)
if req.Delay != nil && req.Delay.AsDuration() > 0 {
err = s.publishDelayed(ctx, req.TopicName, req.Delay.AsDuration(), message)
} else {
err = s.publish(ctx, req.TopicName, message)
}
if err != nil {
if isSNSAccessDeniedErr(err) {
return nil, newErr(
codes.PermissionDenied,
"unable to publish to topic, this may be due to a missing permissions request in your code.",
err,
)
}
return nil, newErr(codes.Internal, "error publishing message", err)
}
return &topicpb.TopicPublishResponse{}, nil
}
// Create new SNS event service plugin
func New(provider resource.AwsResourceProvider) (*SnsEventService, error) {
awsRegion := env.AWS_REGION.String()
cfg, sessionError := config.LoadDefaultConfig(context.TODO(), config.WithRegion(awsRegion))
if sessionError != nil {
return nil, fmt.Errorf("error creating new AWS session %w", sessionError)
}
otelaws.AppendMiddlewares(&cfg.APIOptions)
snsClient := sns.NewFromConfig(cfg)
sfnClient := sfn.NewFromConfig(cfg)
return &SnsEventService{
client: snsClient,
sfnClient: sfnClient,
provider: provider,
}, nil
}
func NewWithClient(provider resource.AwsResourceProvider, client snsiface.SNSAPI, sfnClient sfniface.SFNAPI) (*SnsEventService, error) {
return &SnsEventService{
provider: provider,
client: client,
sfnClient: sfnClient,
}, nil
}