Skip to content

Commit

Permalink
restructure project for multiple binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoney committed Aug 5, 2018
1 parent b0c912a commit c0e8867
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 65 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.zip
lambda
./cmd/dmarc/dmarc
./cmd/block/block
./cmd/spam/spam
dist/
vendor/
31 changes: 22 additions & 9 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
project_name: ses-inbound-mail-filtering-lambda
build:
main: main.go
binary: ses-inbound-mail-filtering-lambda
goos:
- linux
goarch:
- amd64
checksum:
name_template: "{{ .ProjectName }}-v{{ .Version }}_checksums.txt"
builds:
-
main: ./cmd/dmarc/main.go
binary: ses-inbound-dmarc-filtering-lambda
goos:
- linux
goarch:
- amd64
-
main: ./cmd/block/main.go
binary: ses-inbound-block-filtering-lambda
goos:
- linux
goarch:
- amd64
-
main: ./cmd/spam/main.go
binary: ses-inbound-spam-filtering-lambda
goos:
- linux
goarch:
- amd64

changelog:
sort: asc
Expand Down
11 changes: 0 additions & 11 deletions Makefile

This file was deleted.

8 changes: 8 additions & 0 deletions cmd/block/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
clean:
rm -rvf lambda

dependency:
dep ensure

build: dependency
GOOS=linux GOARCH=amd64 go build -o block main.go
43 changes: 0 additions & 43 deletions main.go → cmd/block/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import (
)

var (
spam *log.Logger
virus *log.Logger
dmarc *log.Logger
blocklist *log.Logger

block map[string]string
Expand All @@ -39,22 +36,6 @@ type SimpleEmailDisposition struct {
}

func init() {

spam = log.New(os.Stdout,
"[SPAM]: ",
log.Ldate|log.Ltime,
)

virus = log.New(os.Stdout,
"[VIRUS]: ",
log.Ldate|log.Ltime,
)

dmarc = log.New(os.Stdout,
"[DMARC]: ",
log.Ldate|log.Ltime,
)

blocklist = log.New(os.Stdout,
"[BLOCKLIST]: ",
log.Ldate|log.Ltime,
Expand Down Expand Up @@ -82,30 +63,6 @@ func HandleRequest(ctx context.Context, event events.SimpleEmailEvent) (SimpleEm
// Default is assume this mail is compliant
disposition := "STOP_RULE"
for _, eventRecord := range event.Records {
// If DMARC checks failed, log the DKIM and SPF status. If block mode is on, stop the rule set
if eventRecord.SES.Receipt.DMARCVerdict.Status == "FAIL" {
dmarc.Printf("MessageID=%s failed DMARC: SPF=%v DKIM=%v", eventRecord.SES.Mail.MessageID, eventRecord.SES.Receipt.SPFVerdict, eventRecord.SES.Receipt.DKIMVerdict)
if os.Getenv("DMARC_BLOCK_MODE") == "BLOCK" {
disposition = "CONTINUE"
}
}

// If AWS has classified this mail as spam, log that this happened. If block mode is on, stop the rule set
if eventRecord.SES.Receipt.SpamVerdict.Status == "FAIL" {
spam.Printf("MessageID=%s was considered SPAM", eventRecord.SES.Mail.MessageID)
if os.Getenv("SPAM_BLOCK_MODE") == "BLOCK" {
disposition = "CONTINUE"
}
}

// If AWS has classified any attachments as containing viruses, log that this happened. If block mode is on, stop the rule set
if eventRecord.SES.Receipt.VirusVerdict.Status == "FAIL" {
virus.Printf("MessageID=%s possibly contained a VIRUS", eventRecord.SES.Mail.MessageID)
if os.Getenv("VIRUS_BLOCK_MODE") == "BLOCK" {
disposition = "CONTINUE"
}
}

// Here's the fun. Do not know why From is a slice but whatevs.
// Compare the from domain to the blocklist and log if there was a match. If the blocklist contained the domain of the from address, stop the rule set
for _, from := range eventRecord.SES.Mail.CommonHeaders.From {
Expand Down
8 changes: 8 additions & 0 deletions cmd/dmarc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
clean:
rm -rvf lambda

dependency:
dep ensure

build: dependency
GOOS=linux GOARCH=amd64 go build -o dmarc main.go
64 changes: 64 additions & 0 deletions cmd/dmarc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2018 Jonathan Monette
//
// 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 main

import (
"context"
"log"
"os"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

var (
dmarc *log.Logger
)

// SimpleEmailDisposition disposition return for SES
type SimpleEmailDisposition struct {
Disposition string `json:"disposition"`
}

func init() {
dmarc = log.New(os.Stdout,
"[DMARC]: ",
log.Ldate|log.Ltime,
)
}

func main() {
lambda.Start(HandleRequest)
}

// HandleRequest function that the lambda runtime service calls
func HandleRequest(ctx context.Context, event events.SimpleEmailEvent) (SimpleEmailDisposition, error) {

// Default is assume this mail is compliant
disposition := "STOP_RULE"
for _, eventRecord := range event.Records {
// If DMARC checks failed, log the DKIM and SPF status. If block mode is on, stop the rule set
if eventRecord.SES.Receipt.DMARCVerdict.Status == "FAIL" {
dmarc.Printf("MessageID=%s failed DMARC: SPF=%v DKIM=%v", eventRecord.SES.Mail.MessageID, eventRecord.SES.Receipt.SPFVerdict, eventRecord.SES.Receipt.DKIMVerdict)
if os.Getenv("DMARC_BLOCK_MODE") == "BLOCK" {
disposition = "CONTINUE"
}
}
}

return SimpleEmailDisposition{
Disposition: disposition,
}, nil
}
8 changes: 8 additions & 0 deletions cmd/spam/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
clean:
rm -rvf lambda

dependency:
dep ensure

build: dependency
GOOS=linux GOARCH=amd64 go build -o spam main.go
79 changes: 79 additions & 0 deletions cmd/spam/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2018 Jonathan Monette
//
// 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 main

import (
"context"
"log"
"os"

"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)

var (
spam *log.Logger
virus *log.Logger
)

// SimpleEmailDisposition disposition return for SES
type SimpleEmailDisposition struct {
Disposition string `json:"disposition"`
}

func init() {

spam = log.New(os.Stdout,
"[SPAM]: ",
log.Ldate|log.Ltime,
)

virus = log.New(os.Stdout,
"[VIRUS]: ",
log.Ldate|log.Ltime,
)
}

func main() {
lambda.Start(HandleRequest)
}

// HandleRequest function that the lambda runtime service calls
func HandleRequest(ctx context.Context, event events.SimpleEmailEvent) (SimpleEmailDisposition, error) {

// Default is assume this mail is compliant
disposition := "STOP_RULE"
for _, eventRecord := range event.Records {
// If AWS has classified this mail as spam, log that this happened. If block mode is on, stop the rule set
if eventRecord.SES.Receipt.SpamVerdict.Status == "FAIL" {
spam.Printf("MessageID=%s was considered SPAM", eventRecord.SES.Mail.MessageID)
if os.Getenv("SPAM_BLOCK_MODE") == "BLOCK" {
disposition = "CONTINUE"
}
}

// If AWS has classified any attachments as containing viruses, log that this happened. If block mode is on, stop the rule set
if eventRecord.SES.Receipt.VirusVerdict.Status == "FAIL" {
virus.Printf("MessageID=%s possibly contained a VIRUS", eventRecord.SES.Mail.MessageID)
if os.Getenv("VIRUS_BLOCK_MODE") == "BLOCK" {
disposition = "CONTINUE"
}
}
}

return SimpleEmailDisposition{
Disposition: disposition,
}, nil
}

0 comments on commit c0e8867

Please sign in to comment.