-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamodb_matcher.go
140 lines (115 loc) · 3.25 KB
/
dynamodb_matcher.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
package dynamodb_matcher
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"regexp"
"strings"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
)
// DynamoDBMatcher is a Caddy matcher that checks for the existence of a key-value pair in a DynamoDB table.
type DynamoDBMatcher struct {
TableName string `json:"table_name"`
KeyName string `json:"key_name"`
Regex string `json:"regex"`
AccessKey string `json:"access_key"`
SecretKey string `json:"secret_key"`
Region string `json:"region"`
dynamoDBSvc *dynamodb.Client
}
// Provision sets up the module's configuration.
func (m *DynamoDBMatcher) Provision(ctx caddy.Context) error {
creds := credentials.NewStaticCredentialsProvider(m.AccessKey, m.SecretKey, "")
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithCredentialsProvider(creds), config.WithRegion(m.Region))
if err != nil {
return err
}
m.dynamoDBSvc = dynamodb.NewFromConfig(cfg)
return nil
}
func (m DynamoDBMatcher) Match(r *http.Request) bool {
log.Println("DynamoDB Matcher init: ", r.Host)
var valueCheck string
rgx, err := regexp.Compile(m.Regex)
patternWww := regexp.MustCompile(`(?:www)`)
if err != nil {
fmt.Println("Error compiling regex: ", err.Error())
return false
}
if patternWww.MatchString(r.Host) {
// get rid off www
valueCheck = strings.Split(rgx.FindString(r.Host), ".")[1]
} else {
valueCheck = rgx.FindString(r.Host)
}
log.Println("Value to check - DynamoDB Matcher: ", valueCheck)
input := &dynamodb.GetItemInput{
TableName: &m.TableName,
Key: map[string]types.AttributeValue{
m.KeyName: &types.AttributeValueMemberS{Value: valueCheck},
},
}
result, err := m.dynamoDBSvc.GetItem(context.TODO(), input)
if err != nil {
// Log error or handle accordingly
fmt.Println("Got error calling GetItem: ", err.Error())
return false
}
if result.Item != nil {
return true
}
return false
}
func (DynamoDBMatcher) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.matchers.ddb_matcher",
New: func() caddy.Module { return new(DynamoDBMatcher) },
}
}
func (dm *DynamoDBMatcher) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
d.Next()
for d.Next() {
switch d.Val() {
case "table_name":
d.NextArg()
dm.TableName = d.Val()
case "key_name":
d.NextArg()
dm.KeyName = d.Val()
case "access_key":
d.NextArg()
dm.AccessKey = d.Val()
case "secret_key":
d.NextArg()
dm.SecretKey = d.Val()
case "regex":
d.NextArg()
dm.Regex = d.Val()
case "region":
d.NextArg()
dm.Region = d.Val()
}
}
return nil
}
var (
_ caddy.Validator = (*DynamoDBMatcher)(nil)
_ caddyhttp.RequestMatcher = (*DynamoDBMatcher)(nil)
)
func (dm *DynamoDBMatcher) Validate() error {
if dm.TableName == "" || dm.KeyName == "" || dm.AccessKey == "" || dm.SecretKey == "" || dm.Region == "" {
return errors.New("DynamoDB parameters missed. Please check the configuration file")
}
return nil
}
func init() {
caddy.RegisterModule(DynamoDBMatcher{})
}