Sms audit (#186)

This commit is contained in:
Dominic Wong
2021-08-11 17:04:02 +01:00
committed by GitHub
parent 0f367123ca
commit b254b65549
5 changed files with 52 additions and 16 deletions

View File

@@ -2,18 +2,33 @@ package handler
import (
"context"
"encoding/json"
"fmt"
"net/url"
"regexp"
"strings"
"time"
"github.com/kevinburke/twilio-go"
"github.com/micro/micro/v3/service/auth"
"github.com/micro/micro/v3/service/config"
"github.com/micro/micro/v3/service/errors"
"github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/store"
"github.com/micro/services/pkg/tenant"
pb "github.com/micro/services/sms/proto"
)
const (
prefixUserID = "byUserID"
prefixTwilioID = "byTwilioID"
)
type Sent struct {
UserID string
TwilioMsgID string
}
type Sms struct{}
func (e *Sms) Send(ctx context.Context, req *pb.SendRequest, rsp *pb.SendResponse) error {
@@ -23,15 +38,19 @@ func (e *Sms) Send(ctx context.Context, req *pb.SendRequest, rsp *pb.SendRespons
if len(req.To) == 0 {
return errors.BadRequest("sms.send", "require to field")
}
reg, _ := regexp.Compile("^\\+[0-9]+$")
if !reg.MatchString(req.To) {
return errors.BadRequest("sms.send", "invalid to field format")
}
if len(req.Message) == 0 {
return errors.BadRequest("sms.send", "message is blank")
}
tnt, _ := tenant.FromContext(ctx)
// crudely ban any sender in the banned list aka no impersonating
frm := strings.ToLower(req.From)
for _, sender := range BanFrom {
if strings.Contains(frm, strings.ToLower(sender)) {
tnt, _ := tenant.FromContext(ctx)
acc, _ := auth.AccountFromContext(ctx)
logger.Error("Request to send from %v blocked by account: %v tenant: %v", req.From, acc, tnt)
@@ -74,12 +93,32 @@ func (e *Sms) Send(ctx context.Context, req *pb.SendRequest, rsp *pb.SendRespons
vals.Set("MaxPrice", "0.01")
client := twilio.NewClient(sid, token, nil)
_, err = client.Messages.Create(ctx, vals)
twMsg, err := client.Messages.Create(ctx, vals)
if err != nil {
logger.Errorf("Failed to send message: %v", err)
return errors.InternalServerError("sms.send", "failed to send message: %v", err.Error())
}
sent := Sent{
UserID: tnt,
TwilioMsgID: twMsg.Sid,
}
b, _ := json.Marshal(&sent)
// log the association so we can correlate who sent what
if err := store.Write(&store.Record{
Key: fmt.Sprintf("%s/%s/%s/%s", prefixUserID, sent.UserID, time.Now().Format("20060102"), sent.TwilioMsgID),
Value: b,
}); err != nil {
logger.Errorf("Failed to store association %+v %s", sent, err)
}
if err := store.Write(&store.Record{
Key: fmt.Sprintf("%s/%s", prefixTwilioID, sent.TwilioMsgID),
Value: b,
}); err != nil {
logger.Errorf("Failed to store association %+v %s", sent, err)
}
rsp.Status = "ok"
return nil

View File

@@ -1,7 +1,7 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.15.6
// protoc v3.15.5
// source: proto/sms.proto
package sms
@@ -20,17 +20,17 @@ const (
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// Send an SMS. Include international dialing code in the number
// Send an SMS.
type SendRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// who is the message from?
// who is the message from? The message will be suffixed with "Sent from <from>"
From string `protobuf:"bytes,1,opt,name=from,proto3" json:"from,omitempty"`
// number of the person it's to
// the destination phone number including the international dialling code (e.g. +44)
To string `protobuf:"bytes,2,opt,name=to,proto3" json:"to,omitempty"`
// the message to send
// the main body of the message to send
Message string `protobuf:"bytes,3,opt,name=message,proto3" json:"message,omitempty"`
}
@@ -92,7 +92,7 @@ type SendResponse struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// will return "ok" if sent and "failed" if there was a problem
// will return "ok" if successful
Status string `protobuf:"bytes,1,opt,name=status,proto3" json:"status,omitempty"`
// any additional info
Info string `protobuf:"bytes,2,opt,name=info,proto3" json:"info,omitempty"`

View File

@@ -8,18 +8,18 @@ service Sms {
rpc Send(SendRequest) returns (SendResponse) {}
}
// Send an SMS. Include international dialing code in the number
// Send an SMS.
message SendRequest {
// who is the message from?
// who is the message from? The message will be suffixed with "Sent from <from>"
string from = 1;
// number of the person it's to
// the destination phone number including the international dialling code (e.g. +44)
string to = 2;
// the message to send
// the main body of the message to send
string message = 3;
}
message SendResponse {
// will return "ok" if sent
// will return "ok" if successful
string status = 1;
// any additional info
string info = 2;