Skip to content

Commit

Permalink
feat: 1st release
Browse files Browse the repository at this point in the history
  • Loading branch information
akurilov committed Oct 11, 2024
1 parent ab0b957 commit 0d764d9
Show file tree
Hide file tree
Showing 17 changed files with 457 additions and 121 deletions.
15 changes: 5 additions & 10 deletions api/smtp/backend.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
package smtp

import (
"github.com/awakari/int-email/service/converter"
"github.com/awakari/int-email/service/writer"
"github.com/awakari/int-email/service"
"github.com/emersion/go-smtp"
)

type backend struct {
svcWriter writer.Service
rcpts map[string]bool
dataLimit int64
evtType string
conv converter.Service
svc service.Service
}

func NewBackend(svcWriter writer.Service, rcpts map[string]bool, dataLimit int64, evtType string, conv converter.Service) smtp.Backend {
func NewBackend(rcpts map[string]bool, dataLimit int64, svc service.Service) smtp.Backend {
return backend{
svcWriter: svcWriter,
rcpts: rcpts,
dataLimit: dataLimit,
evtType: evtType,
conv: conv,
svc: svc,
}
}

func (b backend) NewSession(c *smtp.Conn) (s smtp.Session, err error) {
s = newSession(b.svcWriter, b.rcpts, b.dataLimit, b.evtType, b.conv)
s = newSession(b.rcpts, b.dataLimit, b.svc)
return
}
30 changes: 5 additions & 25 deletions api/smtp/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,30 @@ package smtp

import (
"context"
"github.com/awakari/int-email/service/converter"
"github.com/awakari/int-email/service/writer"
"github.com/cloudevents/sdk-go/binding/format/protobuf/v2/pb"
"github.com/awakari/int-email/service"
"github.com/emersion/go-smtp"
"github.com/segmentio/ksuid"
"io"
)

type session struct {
svcWriter writer.Service
rcptsAllowed map[string]bool
dataLimit int64
evtType string
conv converter.Service
svc service.Service
//
allowed bool
from string
data []byte
}

func newSession(svcWriter writer.Service, rcptsAllowed map[string]bool, dataLimit int64, evtType string, conv converter.Service) smtp.Session {
func newSession(rcptsAllowed map[string]bool, dataLimit int64, svc service.Service) smtp.Session {
return &session{
svcWriter: svcWriter,
rcptsAllowed: rcptsAllowed,
dataLimit: dataLimit,
evtType: evtType,
conv: conv,
}
}

func (s *session) Reset() {
s.allowed = false
s.from = ""
s.data = nil
return
}

Expand All @@ -59,18 +49,8 @@ func (s *session) Data(r io.Reader) (err error) {
switch s.allowed {
case true:
r = io.LimitReader(r, s.dataLimit)
evt := &pb.CloudEvent{
Id: ksuid.New().String(),
Source: s.from,
SpecVersion: "1.0",
Type: s.evtType,
Attributes: make(map[string]*pb.CloudEventAttributeValue),
}
err = s.conv.Convert(r, evt)
switch err {
case nil:
err = s.svcWriter.Write(context.TODO(), evt, "default", s.from)
default:
err = s.svc.Submit(context.TODO(), s.from, r)
if err != nil {
err = &smtp.SMTPError{
Code: 554,
EnhancedCode: smtp.EnhancedCode{
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ type ApiConfig struct {
Tls struct {
CertPath string `envconfig:"API_SMTP_TLS_CERT_PATH" default:"/etc/smtp/tls/tls.crt" required:"true"`
KeyPath string `envconfig:"API_SMTP_TLS_KEY_PATH" default:"/etc/smtp/tls/tls.key" required:"true"`
MinVersion uint16 `envconfig:"API_SMTP_TLS_MIN_VERSION" default:"769" required:"true"`
VersionMin uint16 `envconfig:"API_SMTP_TLS_VERSION_MIN" default:"769" required:"true"`
ClientAuthType tls.ClientAuthType `envconfig:"API_SMTP_TLS_CLIENT_AUTH_TYPE" default:"4" required:"true"`
}
}
Group string `envconfig:"API_GROUP" default:"default" required:"true"`
EventType EventTypeConfig
Interests struct {
Uri string `envconfig:"API_INTERESTS_URI" required:"true" default:"subscriptions-proxy:50051"`
Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ func TestConfig(t *testing.T) {
assert.Equal(t, 23*time.Hour, cfg.Api.Writer.Backoff)
assert.Equal(t, "writer:56789", cfg.Api.Writer.Uri)
assert.Equal(t, slog.LevelWarn, slog.Level(cfg.Log.Level))
assert.Equal(t, tls.VerifyClientCertIfGiven, cfg.Api.Smtp.Tls.ClientAuthType)
assert.Equal(t, tls.RequireAndVerifyClientCert, cfg.Api.Smtp.Tls.ClientAuthType)
}
6 changes: 4 additions & 2 deletions helm/int-email/templates/certificate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ metadata:
name: "{{ include "int-email.fullname" . }}-tls-cert"
namespace: "{{ .Release.Namespace }}"
spec:
secretName: "{{ include "int-email.fullname" . }}-tls-secret"
{{- range .Values.ingress.tls }}
secretName: "{{ .secretName }}"
{{- end }}
issuerRef:
name: "{{ .Values.cert.issuer.name }}-int-email"
name: "{{ .Values.tls.cert.issuer.name }}-int-email"
kind: Issuer
dnsNames:
{{- range .Values.ingress.hosts }}
Expand Down
22 changes: 20 additions & 2 deletions helm/int-email/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,26 @@ spec:
- name: API_SMTP_HOST
value: "{{ .host }}"
{{- end }}
- name: API_EVENT_TYPE
value: "{{ .Values.api.event.type }}"
- name: API_SMTP_DATA_LIMIT
value: "{{ .Values.api.smtp.data.limit }}"
- name: API_SMTP_RECIPIENTS_NAMES
value: "{{ .Values.api.smtp.rcpt.names }}"
- name: API_SMTP_RECIPIENTS_LIMIT
value: "{{ .Values.api.smtp.rcpt.limit }}"
- name: API_SMTP_TIMEOUT_READ
value: "{{ .Values.api.smtp.timeout.read }}"
- name: API_SMTP_TIMEOUT_WRITE
value: "{{ .Values.api.smtp.timeout.write }}"
- name: API_SMTP_TLS_CERT_PATH
value: "{{ .Values.tls.cert.path }}"
- name: API_SMTP_TLS_KEY_PATH
value: "{{ .Values.tls.key.path }}"
- name: API_SMTP_TLS_VERSION_MIN
value: "{{ .Values.tls.version.min }}"
- name: API_SMTP_TLS_CLIENT_AUTH_TYPE
value: "{{ .Values.tls.client.auth.type }}"
- name: API_GROUP
value: "{{ .Values.api.group }}"
- name: API_INTERESTS_URI
value: "{{ .Values.api.interests.uri }}"
- name: API_INTERESTS_DETAILS_URI_PREFIX
Expand Down
8 changes: 4 additions & 4 deletions helm/int-email/templates/issuer-letsencrypt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: "{{ .Values.cert.issuer.name }}-int-email"
name: "{{ .Values.tls.cert.issuer.name }}-int-email"
namespace: "{{ .Release.Namespace }}"
spec:
acme:
server: {{ .Values.cert.acme.server }}
email: {{ .Values.cert.acme.email }}
server: {{ .Values.tls.cert.acme.server }}
email: {{ .Values.tls.cert.acme.email }}
privateKeySecretRef:
name: {{ .Values.cert.issuer.name }}
name: {{ .Values.tls.cert.issuer.name }}
solvers:
- dns01:
cloudDNS: # Example for Google Cloud DNS, use your appropriate provider
Expand Down
2 changes: 1 addition & 1 deletion helm/int-email/values-awakari-com.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ api:
uri: "api:50051"

# prod
cert:
tls:
acme:
server: "https://acme-v02.api.letsencrypt.org/directory"
issuer:
Expand Down
31 changes: 25 additions & 6 deletions helm/int-email/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ affinity:
- "true"

api:
group: "default"
smtp:
data:
limit: 1048576
rcpt:
names: "publish"
limit: 100
timeout:
read: "1m"
write: "1m"
event:
typ:
self: "com_awakari_email_v1"
Expand All @@ -109,12 +119,21 @@ backup:
schedule: "0 0 31 2 *" # never, manually only
volume:
name: "backup-secrets"
cert:
acme:
email: "[email protected]"
server: "https://acme-staging-v02.api.letsencrypt.org/directory"
issuer:
name: letsencrypt-staging
tls:
version:
min: 769
client:
auth:
type: 4
key:
path: "/etc/smtp/tls/tls.key"
cert:
path: "/etc/smtp/tls/tls.crt"
acme:
email: "[email protected]"
server: "https://acme-staging-v02.api.letsencrypt.org/directory"
issuer:
name: letsencrypt-staging
log:
# https://pkg.go.dev/golang.org/x/exp/slog#Level
level: -4
Expand Down
13 changes: 7 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/awakari/client-sdk-go/api"
apiSmtp "github.com/awakari/int-email/api/smtp"
"github.com/awakari/int-email/config"
"github.com/awakari/int-email/service"
"github.com/awakari/int-email/service/converter"
"github.com/awakari/int-email/service/writer"
"github.com/emersion/go-smtp"
Expand Down Expand Up @@ -43,17 +44,17 @@ func main() {

svcWriter := writer.NewService(clientAwk, cfg.Api.Writer.Backoff, cfg.Api.Writer.Cache, log)
svcWriter = writer.NewLogging(svcWriter, log)
svcConv := converter.NewConverter(cfg.Api.EventType.Self)
svcConv = converter.NewLogging(svcConv, log)
svc := service.NewService(svcConv, svcWriter, cfg.Api.Group)
svc = service.NewLogging(svc, log)

rcpts := map[string]bool{}
for _, name := range cfg.Api.Smtp.Recipients.Names {
rcpt := fmt.Sprintf("%s@%s", name, cfg.Api.Smtp.Host)
rcpts[rcpt] = true
}

svcConv := converter.NewConverter()
svcConv = converter.NewLogging(svcConv, log)

b := apiSmtp.NewBackend(svcWriter, rcpts, int64(cfg.Api.Smtp.Data.Limit), cfg.Api.EventType.Self, svcConv)
b := apiSmtp.NewBackend(rcpts, int64(cfg.Api.Smtp.Data.Limit), svc)
b = apiSmtp.NewBackendLogging(b, log)

srv := smtp.NewServer(b)
Expand All @@ -76,7 +77,7 @@ func main() {
cert,
},
ClientAuth: cfg.Api.Smtp.Tls.ClientAuthType,
MinVersion: cfg.Api.Smtp.Tls.MinVersion,
MinVersion: cfg.Api.Smtp.Tls.VersionMin,
}

log.Info("starting to listen for emails...")
Expand Down
2 changes: 1 addition & 1 deletion scripts/cover.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

COVERAGE=$(cat cover.tmp)
THRESHOLD=1
THRESHOLD=35
if [[ ${COVERAGE} -lt ${THRESHOLD} ]]; \
then \
echo "FAILED: test coverage ${COVERAGE}% < ${THRESHOLD}%"; \
Expand Down
2 changes: 1 addition & 1 deletion service/converter/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ func NewLogging(svc Service, log *slog.Logger) Service {

func (l logging) Convert(src io.Reader, dst *pb.CloudEvent) (err error) {
err = l.svc.Convert(src, dst)
l.log.Log(context.TODO(), util.LogLevel(err), fmt.Sprintf("converter.Convert(objectUrl=%s, evtId=%s): %s", dst.Attributes[ceKeyObjectUrl], dst.Id, err))
l.log.Log(context.TODO(), util.LogLevel(err), fmt.Sprintf("converter.Convert(source=%s, objectUrl=%s, evtId=%s): %s", dst.Source, dst.Attributes[ceKeyObjectUrl], dst.Id, err))
return
}
Loading

0 comments on commit 0d764d9

Please sign in to comment.