Skip to content

Commit

Permalink
fix: switch to port 25
Browse files Browse the repository at this point in the history
  • Loading branch information
akurilov committed Oct 10, 2024
1 parent 287ec8a commit ab0b957
Show file tree
Hide file tree
Showing 13 changed files with 348 additions and 106 deletions.
9 changes: 7 additions & 2 deletions api/smtp/backend.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package smtp

import (
"github.com/awakari/int-email/service/converter"
"github.com/awakari/int-email/service/writer"
"github.com/emersion/go-smtp"
)
Expand All @@ -9,17 +10,21 @@ type backend struct {
svcWriter writer.Service
rcpts map[string]bool
dataLimit int64
evtType string
conv converter.Service
}

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

func (b backend) NewSession(c *smtp.Conn) (s smtp.Session, err error) {
s = newSession(b.svcWriter, b.rcpts, b.dataLimit)
s = newSession(b.svcWriter, b.rcpts, b.dataLimit, b.evtType, b.conv)
return
}
5 changes: 2 additions & 3 deletions api/smtp/backend_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ func NewBackendLogging(b smtp.Backend, log *slog.Logger) smtp.Backend {
}

func (bl backendLogging) NewSession(c *smtp.Conn) (s smtp.Session, err error) {
tlsState, tlsOk := c.TLSConnectionState()
s, err = bl.b.NewSession(c)
switch err {
case nil:
bl.log.Debug(fmt.Sprintf("backend.NewSession(%s, %+v, %t)", c.Hostname(), tlsState, tlsOk))
bl.log.Debug(fmt.Sprintf("backend.NewSession(%s)", c.Hostname()))
s = NewSessionLogging(s, bl.log)
default:
bl.log.Error(fmt.Sprintf("backend.NewSession(%s, %+v, %t): err=%s", c.Hostname(), tlsState, tlsOk, err))
bl.log.Error(fmt.Sprintf("backend.NewSession(%s): err=%s", c.Hostname(), err))
}
return
}
118 changes: 68 additions & 50 deletions api/smtp/session.go
Original file line number Diff line number Diff line change
@@ -1,74 +1,92 @@
package smtp

import (
"github.com/awakari/int-email/service/writer"
"github.com/emersion/go-smtp"
"io"
"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/emersion/go-smtp"
"github.com/segmentio/ksuid"
"io"
)

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

func newSession(svcWriter writer.Service, rcptsAllowed map[string]bool, dataLimit int64) smtp.Session {
return &session{
svcWriter: svcWriter,
rcptsAllowed: rcptsAllowed,
dataLimit: dataLimit,
}
func newSession(svcWriter writer.Service, rcptsAllowed map[string]bool, dataLimit int64, evtType string, conv converter.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
s.allowed = false
s.from = ""
s.data = nil
return
}

func (s *session) Logout() (err error) {
return
return
}

func (s *session) Mail(from string, opts *smtp.MailOptions) (err error) {
s.from = from
return
s.from = from
return
}

func (s *session) Rcpt(to string, opts *smtp.RcptOptions) (err error) {
if s.rcptsAllowed[to] {
s.allowed = true
}
return
if s.rcptsAllowed[to] {
s.allowed = true
}
return
}

func (s *session) Data(r io.Reader) (err error) {
switch s.allowed {
case true:
r = io.LimitReader(r, s.dataLimit)
s.data, err = io.ReadAll(r)
if err != nil {
err = &smtp.SMTPError{
Code: 554,
EnhancedCode: smtp.EnhancedCode{
5, 3, 0,
},
Message: err.Error(),
}
}
default:
err = &smtp.SMTPError{
Code: 550,
EnhancedCode: smtp.EnhancedCode{
5, 1, 1,
},
Message: "recipient rejected",
}
}
return
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 = &smtp.SMTPError{
Code: 554,
EnhancedCode: smtp.EnhancedCode{
5, 3, 0,
},
Message: err.Error(),
}
}
default:
err = &smtp.SMTPError{
Code: 550,
EnhancedCode: smtp.EnhancedCode{
5, 1, 1,
},
Message: "recipient rejected",
}
}
return
}
9 changes: 8 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"crypto/tls"
"github.com/kelseyhightower/envconfig"
"time"
)
Expand All @@ -15,7 +16,7 @@ type Config struct {
type ApiConfig struct {
Smtp struct {
Host string `envconfig:"API_SMTP_HOST" required:"true"`
Port uint16 `envconfig:"API_SMTP_PORT" default:"587" required:"true"`
Port uint16 `envconfig:"API_SMTP_PORT" default:"465" required:"true"`
Data struct {
Limit uint32 `envconfig:"API_SMTP_DATA_LIMIT" default:"1048576" required:"true"`
}
Expand All @@ -27,6 +28,12 @@ type ApiConfig struct {
Read time.Duration `envconfig:"API_SMTP_TIMEOUT_READ" default:"1m" required:"true"`
Write time.Duration `envconfig:"API_SMTP_TIMEOUT_WRITE" default:"1m" required:"true"`
}
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"`
ClientAuthType tls.ClientAuthType `envconfig:"API_SMTP_TLS_CLIENT_AUTH_TYPE" default:"4" required:"true"`
}
}
EventType EventTypeConfig
Interests struct {
Expand Down
30 changes: 16 additions & 14 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package config

import (
"github.com/stretchr/testify/assert"
"log/slog"
"os"
"testing"
"time"
"crypto/tls"
"github.com/stretchr/testify/assert"
"log/slog"
"os"
"testing"
"time"
)

func TestConfig(t *testing.T) {
os.Setenv("API_SMTP_HOST", "email.awakari.com")
os.Setenv("API_WRITER_BACKOFF", "23h")
os.Setenv("API_WRITER_URI", "writer:56789")
os.Setenv("LOG_LEVEL", "4")
cfg, err := NewConfigFromEnv()
assert.Nil(t, err)
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))
os.Setenv("API_SMTP_HOST", "email.awakari.com")
os.Setenv("API_WRITER_BACKOFF", "23h")
os.Setenv("API_WRITER_URI", "writer:56789")
os.Setenv("LOG_LEVEL", "4")
cfg, err := NewConfigFromEnv()
assert.Nil(t, err)
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)
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,27 @@ require (
github.com/cloudevents/sdk-go/binding/format/protobuf/v2 v2.15.2
github.com/emersion/go-smtp v0.21.3
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/jhillyerd/enmime v1.3.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/segmentio/ksuid v1.0.4
github.com/stretchr/testify v1.9.0
google.golang.org/grpc v1.67.1
)

require (
github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 // indirect
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f // indirect
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/processout/grpc-go-pool v1.2.1 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
Expand Down
35 changes: 24 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,66 @@ github.com/awakari/client-sdk-go v1.2.1 h1:Eo/X2a6i2/Ym7G3mWhG2tkLK4uuNRcNOpsPn9
github.com/awakari/client-sdk-go v1.2.1/go.mod h1:HJS2exDHFHg5QS3CvojF8B2dQiiAKULa49iIXUcUb3A=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a h1:MISbI8sU/PSK/ztvmWKFcI7UGb5/HQT7B+i3a2myKgI=
github.com/cention-sany/utf7 v0.0.0-20170124080048-26cad61bd60a/go.mod h1:2GxOXOlEPAMFPfp014mK1SWq8G8BN8o7/dfYqJrVGn8=
github.com/cloudevents/sdk-go/binding/format/protobuf/v2 v2.15.2 h1:FIvfKlS2mcuP0qYY6yzdIU9xdrRd/YMP0bNwFjXd0u8=
github.com/cloudevents/sdk-go/binding/format/protobuf/v2 v2.15.2/go.mod h1:POsdVp/08Mki0WD9QvvgRRpg9CQ6zhjfRrBoEY8JFS8=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21 h1:OJyUGMJTzHTd1XQp98QTaHernxMYzRaOasRir9hUlFQ=
github.com/emersion/go-sasl v0.0.0-20200509203442-7bfe0ed36a21/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43 h1:hH4PQfOndHDlpzYfLAAfl63E8Le6F2+EL/cdhlkyRJY=
github.com/emersion/go-sasl v0.0.0-20231106173351-e73c9f7bad43/go.mod h1:iL2twTeMvZnrg54ZoPDNfJaJaqy0xIQFuBdrLsmspwQ=
github.com/emersion/go-smtp v0.21.3 h1:7uVwagE8iPYE48WhNsng3RRpCUpFvNl39JGNSIyGVMY=
github.com/emersion/go-smtp v0.21.3/go.mod h1:qm27SGYgoIPRot6ubfQ/GpiPy/g3PaZAVRxiO/sDUgQ=
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f h1:3BSP1Tbs2djlpprl7wCLuiqMaUh5SJkkzI2gDs+FgLs=
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f/go.mod h1:Pcatq5tYkCW2Q6yrR2VRHlbHpZ/R4/7qyL1TCF7vl14=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056 h1:iCHtR9CQyktQ5+f3dMVZfwD2KWJUgm7M0gdL9NGr8KA=
github.com/jaytaylor/html2text v0.0.0-20230321000545-74c2419ad056/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
github.com/jhillyerd/enmime v1.3.0 h1:LV5kzfLidiOr8qRGIpYYmUZCnhrPbcFAnAFUnWn99rw=
github.com/jhillyerd/enmime v1.3.0/go.mod h1:6c6jg5HdRRV2FtvVL69LjiX1M8oE0xDX9VEhV3oy4gs=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/processout/grpc-go-pool v1.2.1 h1:hbp1BOA02CIxEAoRLHGpUhhPFv77nwfBLBeO3Ya9P7I=
github.com/processout/grpc-go-pool v1.2.1/go.mod h1:F4hiNj96O6VQ87jv4rdz8R9tkHdelQQJ/J2B1a5VSt4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c=
github.com/segmentio/ksuid v1.0.4/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf h1:pvbZ0lM0XWPBqUKqFU8cmavspvIl9nulOYwdy6IFRRo=
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf/go.mod h1:RJID2RhlZKId02nZ62WenDCkgHFerpIOmW0iT7GKmXM=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240924160255-9d4c2d233b61 h1:N9BgCIAUvn/M+p4NJccWPWb3BWh88+zyL0ll9HgbEeM=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240924160255-9d4c2d233b61/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240930140551-af27646dc61f h1:cUMEy+8oS78BWIH9OWazBkzbr090Od9tWBNtZHkOhf0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240930140551-af27646dc61f/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
google.golang.org/grpc v1.67.0 h1:IdH9y6PF5MPSdAntIcpjQ+tXO41pcQsfZV2RxtQgVcw=
google.golang.org/grpc v1.67.0/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
google.golang.org/grpc v1.67.1/go.mod h1:1gLDyUQU7CTLJI90u3nXZ9ekeghjeM7pTDZlqFNg2AA=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
Expand Down
2 changes: 1 addition & 1 deletion helm/int-email/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ securityContext: {}

service:
type: LoadBalancer
port: 465
port: 25

ingress:
enabled: false
Expand Down
Loading

0 comments on commit ab0b957

Please sign in to comment.