**Related issue:** Resolves #42288 # Summary This PR adds support for configuring an optional SES sender domain. When the SES email backend is enabled, Fleet can now use a configured sender domain for the `From` address instead of always deriving the domain from `server.server_url`. If the setting is not provided, Fleet keeps the existing behavior. # Impact This gives self-hosted operators a server-side SES configuration option for email sending without changing UI-managed SMTP settings. # Root cause The SES sender path only generated `do-not-reply@<server host>` from the Fleet server URL, so there was no way to override the sender domain through server configuration. # Checklist for submitter - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. - [x] Added/updated automated tests - [x] Setting(s) is/are explicitly excluded from GitOps ## Testing - [x] `go test -tags full,fts5,netgo ./server/mail -run 'Test_(getFromSES|sesSender_SendEmail)$'` - [x] `go test -tags full,fts5,netgo ./server/config -run 'TestConfig(SESSenderDomain|Roundtrip)$'` - [x] `go test -tags full,fts5,netgo ./server/service -run 'TestService_EmailConfig$'` - [ ] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added optional SES sender domain configuration. Users can specify a custom domain for the email "From" address via config or environment variable; when unset it falls back to the server hostname. * **Tests** * Added and expanded tests to verify sender-domain precedence, From-header generation, and related error cases. <!-- review_stack_entry_start --> [](https://app.coderabbit.ai/change-stack/fleetdm/fleet/pull/43811?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack) <!-- review_stack_entry_end --> <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Lucas Manuel Rodriguez <lucas@fleetdm.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
141 lines
3.6 KiB
Go
141 lines
3.6 KiB
Go
package mail
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/ses"
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func Test_getFromSES(t *testing.T) {
|
|
type args struct {
|
|
e fleet.Email
|
|
senderDomain string
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
want string
|
|
wantErr assert.ErrorAssertionFunc
|
|
}{
|
|
{
|
|
name: "should return properly formatted SMTP from for use in SES",
|
|
args: args{e: fleet.Email{
|
|
ServerURL: "https://foobar.fleetdm.com",
|
|
}},
|
|
want: "From: do-not-reply@foobar.fleetdm.com\r\n",
|
|
wantErr: assert.NoError,
|
|
},
|
|
{
|
|
name: "should use configured sender domain when provided",
|
|
args: args{
|
|
e: fleet.Email{
|
|
ServerURL: "not-a-url",
|
|
},
|
|
senderDomain: "notifications.example.com",
|
|
},
|
|
want: "From: do-not-reply@notifications.example.com\r\n",
|
|
wantErr: assert.NoError,
|
|
},
|
|
{
|
|
name: "should error when we fail to parse fleet server url",
|
|
args: args{e: fleet.Email{
|
|
ServerURL: "not-a-url",
|
|
}},
|
|
want: "",
|
|
wantErr: assert.Error,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got, err := getFromSES(tt.args.e, tt.args.senderDomain)
|
|
if !tt.wantErr(t, err, fmt.Sprintf("getFromSES(%v)", tt.args.e)) {
|
|
return
|
|
}
|
|
assert.Equalf(t, tt.want, got, "getFromSES(%v)", tt.args.e)
|
|
})
|
|
}
|
|
}
|
|
|
|
type mockSESSender struct {
|
|
shouldErr bool
|
|
input *ses.SendRawEmailInput
|
|
}
|
|
|
|
func (m *mockSESSender) SendRawEmail(ctx context.Context, input *ses.SendRawEmailInput, optFns ...func(*ses.Options)) (*ses.SendRawEmailOutput, error) {
|
|
if m.shouldErr {
|
|
return nil, errors.New("some error")
|
|
}
|
|
m.input = input
|
|
return nil, nil
|
|
}
|
|
|
|
func Test_sesSender_SendEmail(t *testing.T) {
|
|
baseEmail := fleet.Email{
|
|
Subject: "Hello from Fleet!",
|
|
To: []string{"foouser@fleetdm.com"},
|
|
ServerURL: "https://foobar.fleetdm.com",
|
|
Mailer: &SMTPTestMailer{
|
|
BaseURL: "https://localhost:8080",
|
|
},
|
|
}
|
|
|
|
t.Run("should send email with configured sender domain", func(t *testing.T) {
|
|
client := &mockSESSender{}
|
|
s := &sesSender{
|
|
client: client,
|
|
sourceArn: "foo",
|
|
senderDomain: "notifications.example.com",
|
|
}
|
|
|
|
email := baseEmail
|
|
email.ServerURL = "not-a-url"
|
|
err := s.SendEmail(context.Background(), email)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, client.input)
|
|
assert.Contains(t, string(client.input.RawMessage.Data), "From: do-not-reply@notifications.example.com\r\n")
|
|
})
|
|
|
|
t.Run("should send email with server url host when sender domain is not configured", func(t *testing.T) {
|
|
client := &mockSESSender{}
|
|
s := &sesSender{
|
|
client: client,
|
|
sourceArn: "foo",
|
|
}
|
|
|
|
err := s.SendEmail(context.Background(), baseEmail)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, client.input)
|
|
assert.Contains(t, string(client.input.RawMessage.Data), "From: do-not-reply@foobar.fleetdm.com\r\n")
|
|
})
|
|
|
|
t.Run("should error when server url is invalid and sender domain is not configured", func(t *testing.T) {
|
|
s := &sesSender{
|
|
client: &mockSESSender{},
|
|
sourceArn: "foo",
|
|
}
|
|
|
|
email := baseEmail
|
|
email.ServerURL = "not-a-url"
|
|
assert.Error(t, s.SendEmail(context.Background(), email))
|
|
})
|
|
|
|
t.Run("should error when ses client is nil", func(t *testing.T) {
|
|
s := &sesSender{sourceArn: "foo"}
|
|
assert.Error(t, s.SendEmail(context.Background(), baseEmail))
|
|
})
|
|
|
|
t.Run("should error when ses client returns an error", func(t *testing.T) {
|
|
s := &sesSender{
|
|
client: &mockSESSender{shouldErr: true},
|
|
sourceArn: "foo",
|
|
}
|
|
assert.Error(t, s.SendEmail(context.Background(), baseEmail))
|
|
})
|
|
}
|