feat: add http as smtp provider (#8545)

# Which Problems Are Solved

Send Email messages as a HTTP call to a relay, for own logic on handling
different Email providers

# How the Problems Are Solved

Create endpoints under Email provider to manage SMTP and HTTP in the
notification handlers.

# Additional Changes

Clean up old logic in command and query side to handle the general Email
providers with deactivate, activate and remove.

# Additional Context

Partially closes #8270

---------

Co-authored-by: Livio Spring <livio.a@gmail.com>
This commit is contained in:
Stefan Benz
2024-09-12 06:27:29 +02:00
committed by GitHub
parent d8a71d217c
commit 21c38b061d
28 changed files with 3575 additions and 1152 deletions

View File

@@ -116,7 +116,7 @@ type InstanceSetup struct {
}
EmailTemplate []byte
MessageTexts []*domain.CustomMessageText
SMTPConfiguration *smtp.Config
SMTPConfiguration *SMTPConfiguration
OIDCSettings *OIDCSettings
Quotas *SetQuotas
Features *InstanceFeatures
@@ -124,6 +124,15 @@ type InstanceSetup struct {
Restrictions *SetRestrictions
}
type SMTPConfiguration struct {
Description string
SMTP smtp.SMTP
Tls bool
From string
FromName string
ReplyToAddress string
}
type OIDCSettings struct {
AccessTokenLifetime time.Duration
IdTokenLifetime time.Duration
@@ -440,7 +449,7 @@ func setupOIDCSettings(commands *Commands, validations *[]preparation.Validation
)
}
func setupSMTPSettings(commands *Commands, validations *[]preparation.Validation, smtpConfig *smtp.Config, instanceAgg *instance.Aggregate) {
func setupSMTPSettings(commands *Commands, validations *[]preparation.Validation, smtpConfig *SMTPConfiguration, instanceAgg *instance.Aggregate) {
if smtpConfig == nil {
return
}

View File

@@ -12,8 +12,20 @@ import (
type IAMSMTPConfigWriteModel struct {
eventstore.WriteModel
ID string
Description string
ID string
Description string
SMTPConfig *SMTPConfig
HTTPConfig *HTTPConfig
State domain.SMTPConfigState
domain string
domainState domain.InstanceDomainState
smtpSenderAddressMatchesInstanceDomain bool
}
type SMTPConfig struct {
TLS bool
Host string
User string
@@ -21,11 +33,6 @@ type IAMSMTPConfigWriteModel struct {
SenderAddress string
SenderName string
ReplyToAddress string
State domain.SMTPConfigState
domain string
domainState domain.InstanceDomainState
smtpSenderAddressMatchesInstanceDomain bool
}
func NewIAMSMTPConfigWriteModel(instanceID, id, domain string) *IAMSMTPConfigWriteModel {
@@ -73,6 +80,23 @@ func (wm *IAMSMTPConfigWriteModel) Reduce() error {
continue
}
wm.reduceSMTPConfigChangedEvent(e)
case *instance.SMTPConfigPasswordChangedEvent:
if wm.ID != e.ID {
continue
}
if e.Password != nil {
wm.SMTPConfig.Password = e.Password
}
case *instance.SMTPConfigHTTPAddedEvent:
if wm.ID != e.ID {
continue
}
wm.reduceSMTPConfigHTTPAddedEvent(e)
case *instance.SMTPConfigHTTPChangedEvent:
if wm.ID != e.ID {
continue
}
wm.reduceSMTPConfigHTTPChangedEvent(e)
case *instance.SMTPConfigRemovedEvent:
if wm.ID != e.ID {
continue
@@ -120,6 +144,8 @@ func (wm *IAMSMTPConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
instance.SMTPConfigRemovedEventType,
instance.SMTPConfigChangedEventType,
instance.SMTPConfigPasswordChangedEventType,
instance.SMTPConfigHTTPAddedEventType,
instance.SMTPConfigHTTPChangedEventType,
instance.SMTPConfigActivatedEventType,
instance.SMTPConfigDeactivatedEventType,
instance.SMTPConfigRemovedEventType,
@@ -133,6 +159,9 @@ func (wm *IAMSMTPConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
func (wm *IAMSMTPConfigWriteModel) NewChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, id, description string, tls bool, fromAddress, fromName, replyToAddress, smtpHost, smtpUser string, smtpPassword *crypto.CryptoValue) (*instance.SMTPConfigChangedEvent, bool, error) {
changes := make([]instance.SMTPConfigChanges, 0)
var err error
if wm.SMTPConfig == nil {
return nil, false, nil
}
if wm.ID != id {
changes = append(changes, instance.ChangeSMTPConfigID(id))
@@ -140,22 +169,22 @@ func (wm *IAMSMTPConfigWriteModel) NewChangedEvent(ctx context.Context, aggregat
if wm.Description != description {
changes = append(changes, instance.ChangeSMTPConfigDescription(description))
}
if wm.TLS != tls {
if wm.SMTPConfig.TLS != tls {
changes = append(changes, instance.ChangeSMTPConfigTLS(tls))
}
if wm.SenderAddress != fromAddress {
if wm.SMTPConfig.SenderAddress != fromAddress {
changes = append(changes, instance.ChangeSMTPConfigFromAddress(fromAddress))
}
if wm.SenderName != fromName {
if wm.SMTPConfig.SenderName != fromName {
changes = append(changes, instance.ChangeSMTPConfigFromName(fromName))
}
if wm.ReplyToAddress != replyToAddress {
if wm.SMTPConfig.ReplyToAddress != replyToAddress {
changes = append(changes, instance.ChangeSMTPConfigReplyToAddress(replyToAddress))
}
if wm.Host != smtpHost {
if wm.SMTPConfig.Host != smtpHost {
changes = append(changes, instance.ChangeSMTPConfigSMTPHost(smtpHost))
}
if wm.User != smtpUser {
if wm.SMTPConfig.User != smtpUser {
changes = append(changes, instance.ChangeSMTPConfigSMTPUser(smtpUser))
}
if smtpPassword != nil {
@@ -171,15 +200,58 @@ func (wm *IAMSMTPConfigWriteModel) NewChangedEvent(ctx context.Context, aggregat
return changeEvent, true, nil
}
func (wm *IAMSMTPConfigWriteModel) NewHTTPChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, id, description, endpoint string) (*instance.SMTPConfigHTTPChangedEvent, bool, error) {
changes := make([]instance.SMTPConfigHTTPChanges, 0)
var err error
if wm.HTTPConfig == nil {
return nil, false, nil
}
if wm.ID != id {
changes = append(changes, instance.ChangeSMTPConfigHTTPID(id))
}
if wm.Description != description {
changes = append(changes, instance.ChangeSMTPConfigHTTPDescription(description))
}
if wm.HTTPConfig.Endpoint != endpoint {
changes = append(changes, instance.ChangeSMTPConfigHTTPEndpoint(endpoint))
}
if len(changes) == 0 {
return nil, false, nil
}
changeEvent, err := instance.NewSMTPConfigHTTPChangeEvent(ctx, aggregate, id, changes)
if err != nil {
return nil, false, err
}
return changeEvent, true, nil
}
func (wm *IAMSMTPConfigWriteModel) reduceSMTPConfigAddedEvent(e *instance.SMTPConfigAddedEvent) {
wm.Description = e.Description
wm.TLS = e.TLS
wm.Host = e.Host
wm.User = e.User
wm.Password = e.Password
wm.SenderAddress = e.SenderAddress
wm.SenderName = e.SenderName
wm.ReplyToAddress = e.ReplyToAddress
wm.SMTPConfig = &SMTPConfig{
TLS: e.TLS,
Host: e.Host,
User: e.User,
Password: e.Password,
SenderName: e.SenderName,
SenderAddress: e.SenderAddress,
ReplyToAddress: e.ReplyToAddress,
}
wm.State = domain.SMTPConfigStateInactive
// If ID has empty value we're dealing with the old and unique smtp settings
// These would be the default values for ID and State
if e.ID == "" {
wm.Description = "generic"
wm.ID = e.Aggregate().ResourceOwner
wm.State = domain.SMTPConfigStateActive
}
}
func (wm *IAMSMTPConfigWriteModel) reduceSMTPConfigHTTPAddedEvent(e *instance.SMTPConfigHTTPAddedEvent) {
wm.Description = e.Description
wm.HTTPConfig = &HTTPConfig{
Endpoint: e.Endpoint,
}
wm.State = domain.SMTPConfigStateInactive
// If ID has empty value we're dealing with the old and unique smtp settings
// These would be the default values for ID and State
@@ -191,29 +263,54 @@ func (wm *IAMSMTPConfigWriteModel) reduceSMTPConfigAddedEvent(e *instance.SMTPCo
}
func (wm *IAMSMTPConfigWriteModel) reduceSMTPConfigChangedEvent(e *instance.SMTPConfigChangedEvent) {
if wm.SMTPConfig == nil {
return
}
if e.Description != nil {
wm.Description = *e.Description
}
if e.TLS != nil {
wm.TLS = *e.TLS
wm.SMTPConfig.TLS = *e.TLS
}
if e.Host != nil {
wm.Host = *e.Host
wm.SMTPConfig.Host = *e.Host
}
if e.User != nil {
wm.User = *e.User
wm.SMTPConfig.User = *e.User
}
if e.Password != nil {
wm.Password = e.Password
wm.SMTPConfig.Password = e.Password
}
if e.FromAddress != nil {
wm.SenderAddress = *e.FromAddress
wm.SMTPConfig.SenderAddress = *e.FromAddress
}
if e.FromName != nil {
wm.SenderName = *e.FromName
wm.SMTPConfig.SenderName = *e.FromName
}
if e.ReplyToAddress != nil {
wm.ReplyToAddress = *e.ReplyToAddress
wm.SMTPConfig.ReplyToAddress = *e.ReplyToAddress
}
// If ID has empty value we're dealing with the old and unique smtp settings
// These would be the default values for ID and State
if e.ID == "" {
wm.Description = "generic"
wm.ID = e.Aggregate().ResourceOwner
wm.State = domain.SMTPConfigStateActive
}
}
func (wm *IAMSMTPConfigWriteModel) reduceSMTPConfigHTTPChangedEvent(e *instance.SMTPConfigHTTPChangedEvent) {
if wm.HTTPConfig == nil {
return
}
if e.Description != nil {
wm.Description = *e.Description
}
if e.Endpoint != nil {
wm.HTTPConfig.Endpoint = *e.Endpoint
}
// If ID has empty value we're dealing with the old and unique smtp settings
@@ -227,13 +324,8 @@ func (wm *IAMSMTPConfigWriteModel) reduceSMTPConfigChangedEvent(e *instance.SMTP
func (wm *IAMSMTPConfigWriteModel) reduceSMTPConfigRemovedEvent(e *instance.SMTPConfigRemovedEvent) {
wm.Description = ""
wm.TLS = false
wm.SenderName = ""
wm.SenderAddress = ""
wm.ReplyToAddress = ""
wm.Host = ""
wm.User = ""
wm.Password = nil
wm.HTTPConfig = nil
wm.SMTPConfig = nil
wm.State = domain.SMTPConfigStateRemoved
// If ID has empty value we're dealing with the old and unique smtp settings

View File

@@ -15,151 +15,189 @@ import (
"github.com/zitadel/zitadel/internal/zerrors"
)
func (c *Commands) AddSMTPConfig(ctx context.Context, instanceID string, config *smtp.Config) (string, *domain.ObjectDetails, error) {
id, err := c.idGenerator.Next()
if err != nil {
return "", nil, err
type AddSMTPConfig struct {
Details *domain.ObjectDetails
ResourceOwner string
ID string
Description string
Host string
User string
Password string
Tls bool
From string
FromName string
ReplyToAddress string
}
func (c *Commands) AddSMTPConfig(ctx context.Context, config *AddSMTPConfig) (err error) {
if config.ResourceOwner == "" {
return zerrors.ThrowInvalidArgument(nil, "COMMAND-PQN0wsqSyi", "Errors.ResourceOwnerMissing")
}
if config.ID == "" {
config.ID, err = c.idGenerator.Next()
if err != nil {
return err
}
}
from := strings.TrimSpace(config.From)
if from == "" {
return "", nil, zerrors.ThrowInvalidArgument(nil, "INST-ASv2d", "Errors.Invalid.Argument")
return zerrors.ThrowInvalidArgument(nil, "COMMAND-SAAFpV8VKV", "Errors.Invalid.Argument")
}
fromSplitted := strings.Split(from, "@")
senderDomain := fromSplitted[len(fromSplitted)-1]
description := strings.TrimSpace(config.Description)
replyTo := strings.TrimSpace(config.ReplyToAddress)
hostAndPort := strings.TrimSpace(config.SMTP.Host)
hostAndPort := strings.TrimSpace(config.Host)
if _, _, err := net.SplitHostPort(hostAndPort); err != nil {
return "", nil, zerrors.ThrowInvalidArgument(nil, "INST-9JdRe", "Errors.Invalid.Argument")
return zerrors.ThrowInvalidArgument(nil, "COMMAND-EvAtufIinh", "Errors.Invalid.Argument")
}
var smtpPassword *crypto.CryptoValue
if config.SMTP.Password != "" {
smtpPassword, err = crypto.Encrypt([]byte(config.SMTP.Password), c.smtpEncryption)
if config.Password != "" {
smtpPassword, err = crypto.Encrypt([]byte(config.Password), c.smtpEncryption)
if err != nil {
return "", nil, err
return err
}
}
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, instanceID, id, senderDomain)
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, config.ResourceOwner, config.ID, senderDomain)
if err != nil {
return "", nil, err
return err
}
err = checkSenderAddress(smtpConfigWriteModel)
if err != nil {
return "", nil, err
return err
}
iamAgg := InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel)
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSMTPConfigAddedEvent(
ctx,
iamAgg,
id,
description,
config.Tls,
config.From,
config.FromName,
replyTo,
hostAndPort,
config.SMTP.User,
smtpPassword,
))
err = c.pushAppendAndReduce(ctx,
smtpConfigWriteModel,
instance.NewSMTPConfigAddedEvent(
ctx,
InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel),
config.ID,
description,
config.Tls,
config.From,
config.FromName,
replyTo,
hostAndPort,
config.User,
smtpPassword,
),
)
if err != nil {
return "", nil, err
return err
}
err = AppendAndReduce(smtpConfigWriteModel, pushedEvents...)
if err != nil {
return "", nil, err
}
return id, writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel), nil
config.Details = writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel)
return nil
}
func (c *Commands) ChangeSMTPConfig(ctx context.Context, instanceID string, id string, config *smtp.Config) (*domain.ObjectDetails, error) {
if id == "" {
return nil, zerrors.ThrowInvalidArgument(nil, "SMTP-x8vo9", "Errors.IDMissing")
type ChangeSMTPConfig struct {
Details *domain.ObjectDetails
ResourceOwner string
ID string
Description string
Host string
User string
Password string
Tls bool
From string
FromName string
ReplyToAddress string
}
func (c *Commands) ChangeSMTPConfig(ctx context.Context, config *ChangeSMTPConfig) error {
if config.ResourceOwner == "" {
return zerrors.ThrowInvalidArgument(nil, "COMMAND-jwA8gxldy3", "Errors.ResourceOwnerMissing")
}
if config.ID == "" {
return zerrors.ThrowInvalidArgument(nil, "COMMAND-2JPlSRzuHy", "Errors.IDMissing")
}
from := strings.TrimSpace(config.From)
if from == "" {
return nil, zerrors.ThrowInvalidArgument(nil, "INST-HSv2d", "Errors.Invalid.Argument")
return zerrors.ThrowInvalidArgument(nil, "COMMAND-gyPUXOTA4N", "Errors.Invalid.Argument")
}
fromSplitted := strings.Split(from, "@")
senderDomain := fromSplitted[len(fromSplitted)-1]
description := strings.TrimSpace(config.Description)
replyTo := strings.TrimSpace(config.ReplyToAddress)
hostAndPort := strings.TrimSpace(config.SMTP.Host)
hostAndPort := strings.TrimSpace(config.Host)
if _, _, err := net.SplitHostPort(hostAndPort); err != nil {
return nil, zerrors.ThrowInvalidArgument(nil, "INST-Kv875", "Errors.Invalid.Argument")
return zerrors.ThrowInvalidArgument(nil, "COMMAND-kZNVkuL32L", "Errors.Invalid.Argument")
}
var smtpPassword *crypto.CryptoValue
var err error
if config.SMTP.Password != "" {
smtpPassword, err = crypto.Encrypt([]byte(config.SMTP.Password), c.smtpEncryption)
if config.Password != "" {
smtpPassword, err = crypto.Encrypt([]byte(config.Password), c.smtpEncryption)
if err != nil {
return nil, err
return err
}
}
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, instanceID, id, senderDomain)
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, config.ResourceOwner, config.ID, senderDomain)
if err != nil {
return nil, err
return err
}
if !smtpConfigWriteModel.State.Exists() {
return nil, zerrors.ThrowNotFound(nil, "COMMAND-7j8gv", "Errors.SMTPConfig.NotFound")
return zerrors.ThrowNotFound(nil, "COMMAND-j5IDFtt3T1", "Errors.SMTPConfig.NotFound")
}
err = checkSenderAddress(smtpConfigWriteModel)
if err != nil {
return nil, err
return err
}
iamAgg := InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel)
changedEvent, hasChanged, err := smtpConfigWriteModel.NewChangedEvent(
ctx,
iamAgg,
id,
InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel),
config.ID,
description,
config.Tls,
from,
config.FromName,
replyTo,
hostAndPort,
config.SMTP.User,
config.User,
smtpPassword,
)
if err != nil {
return nil, err
return err
}
if !hasChanged {
return nil, zerrors.ThrowPreconditionFailed(nil, "COMMAND-lh3op", "Errors.NoChangesFound")
config.Details = writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel)
return nil
}
pushedEvents, err := c.eventstore.Push(ctx, changedEvent)
err = c.pushAppendAndReduce(ctx, smtpConfigWriteModel, changedEvent)
if err != nil {
return nil, err
return err
}
err = AppendAndReduce(smtpConfigWriteModel, pushedEvents...)
if err != nil {
return nil, err
}
return writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel), nil
config.Details = writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel)
return nil
}
func (c *Commands) ChangeSMTPConfigPassword(ctx context.Context, instanceID, id string, password string) (*domain.ObjectDetails, error) {
instanceAgg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, instanceID, id, "")
func (c *Commands) ChangeSMTPConfigPassword(ctx context.Context, resourceOwner, id string, password string) (*domain.ObjectDetails, error) {
if resourceOwner == "" {
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-gHAyvUXCAF", "Errors.ResourceOwnerMissing")
}
if id == "" {
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-BCkAf7LcJA", "Errors.IDMissing")
}
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, resourceOwner, id, "")
if err != nil {
return nil, err
}
if smtpConfigWriteModel.State != domain.SMTPConfigStateActive {
return nil, zerrors.ThrowNotFound(nil, "COMMAND-3n9ls", "Errors.SMTPConfig.NotFound")
return nil, zerrors.ThrowNotFound(nil, "COMMAND-rDHzqjGuKQ", "Errors.SMTPConfig.NotFound")
}
var smtpPassword *crypto.CryptoValue
@@ -170,68 +208,152 @@ func (c *Commands) ChangeSMTPConfigPassword(ctx context.Context, instanceID, id
}
}
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSMTPConfigPasswordChangedEvent(
ctx,
&instanceAgg.Aggregate,
id,
smtpPassword))
err = c.pushAppendAndReduce(ctx,
smtpConfigWriteModel,
instance.NewSMTPConfigPasswordChangedEvent(
ctx,
InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel),
id,
smtpPassword,
),
)
if err != nil {
return nil, err
}
err = AppendAndReduce(smtpConfigWriteModel, pushedEvents...)
if err != nil {
return nil, err
}
return writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel), nil
}
func (c *Commands) ActivateSMTPConfig(ctx context.Context, instanceID, id, activatedId string) (*domain.ObjectDetails, error) {
if id == "" {
return nil, zerrors.ThrowInvalidArgument(nil, "SMTP-nm56k", "Errors.IDMissing")
}
type AddSMTPConfigHTTP struct {
Details *domain.ObjectDetails
ResourceOwner string
ID string
if len(activatedId) > 0 {
_, err := c.DeactivateSMTPConfig(ctx, instanceID, activatedId)
Description string
Endpoint string
}
func (c *Commands) AddSMTPConfigHTTP(ctx context.Context, config *AddSMTPConfigHTTP) (err error) {
if config.ResourceOwner == "" {
return zerrors.ThrowInvalidArgument(nil, "COMMAND-FTNDXc8ACS", "Errors.ResourceOwnerMissing")
}
if config.ID == "" {
config.ID, err = c.idGenerator.Next()
if err != nil {
return nil, err
return err
}
}
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, instanceID, id, "")
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, config.ResourceOwner, config.ID, "")
if err != nil {
return err
}
err = c.pushAppendAndReduce(ctx, smtpConfigWriteModel, instance.NewSMTPConfigHTTPAddedEvent(
ctx,
InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel),
config.ID,
config.Description,
config.Endpoint,
))
if err != nil {
return err
}
config.Details = writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel)
return nil
}
type ChangeSMTPConfigHTTP struct {
Details *domain.ObjectDetails
ResourceOwner string
ID string
Description string
Endpoint string
}
func (c *Commands) ChangeSMTPConfigHTTP(ctx context.Context, config *ChangeSMTPConfigHTTP) (err error) {
if config.ResourceOwner == "" {
return zerrors.ThrowInvalidArgument(nil, "COMMAND-k7QCGOWyJA", "Errors.ResourceOwnerMissing")
}
if config.ID == "" {
return zerrors.ThrowInvalidArgument(nil, "COMMAND-2MHkV8ObWo", "Errors.IDMissing")
}
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, config.ResourceOwner, config.ID, "")
if err != nil {
return err
}
if !smtpConfigWriteModel.State.Exists() || smtpConfigWriteModel.HTTPConfig == nil {
return zerrors.ThrowNotFound(nil, "COMMAND-xIrdledqv4", "Errors.SMTPConfig.NotFound")
}
changedEvent, hasChanged, err := smtpConfigWriteModel.NewHTTPChangedEvent(
ctx,
InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel),
config.ID,
config.Description,
config.Endpoint,
)
if err != nil {
return err
}
if !hasChanged {
config.Details = writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel)
return nil
}
err = c.pushAppendAndReduce(ctx, smtpConfigWriteModel, changedEvent)
if err != nil {
return err
}
config.Details = writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel)
return nil
}
func (c *Commands) ActivateSMTPConfig(ctx context.Context, resourceOwner, id string) (*domain.ObjectDetails, error) {
if resourceOwner == "" {
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-h5htMCebv3", "Errors.ResourceOwnerMissing")
}
if id == "" {
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-1hPl6oVMJa", "Errors.IDMissing")
}
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, resourceOwner, id, "")
if err != nil {
return nil, err
}
if !smtpConfigWriteModel.State.Exists() {
return nil, zerrors.ThrowNotFound(nil, "COMMAND-kg8yr", "Errors.SMTPConfig.NotFound")
return nil, zerrors.ThrowNotFound(nil, "COMMAND-E9K20hxOS9", "Errors.SMTPConfig.NotFound")
}
if smtpConfigWriteModel.State == domain.SMTPConfigStateActive {
return nil, zerrors.ThrowNotFound(nil, "COMMAND-ed3lr", "Errors.SMTPConfig.AlreadyActive")
return nil, zerrors.ThrowPreconditionFailed(nil, "COMMAND-vUHBSmBzaw", "Errors.SMTPConfig.AlreadyActive")
}
iamAgg := InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel)
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSMTPConfigActivatedEvent(
ctx,
iamAgg,
id))
if err != nil {
return nil, err
}
err = AppendAndReduce(smtpConfigWriteModel, pushedEvents...)
err = c.pushAppendAndReduce(ctx,
smtpConfigWriteModel,
instance.NewSMTPConfigActivatedEvent(
ctx,
InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel),
id,
),
)
if err != nil {
return nil, err
}
return writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel), nil
}
func (c *Commands) DeactivateSMTPConfig(ctx context.Context, instanceID, id string) (*domain.ObjectDetails, error) {
func (c *Commands) DeactivateSMTPConfig(ctx context.Context, resourceOwner, id string) (*domain.ObjectDetails, error) {
if resourceOwner == "" {
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-pvNHou89Tw", "Errors.ResourceOwnerMissing")
}
if id == "" {
return nil, zerrors.ThrowInvalidArgument(nil, "SMTP-98ikl", "Errors.IDMissing")
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-jLTIMrtApO", "Errors.IDMissing")
}
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, instanceID, id, "")
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, resourceOwner, id, "")
if err != nil {
return nil, err
}
@@ -239,46 +361,47 @@ func (c *Commands) DeactivateSMTPConfig(ctx context.Context, instanceID, id stri
return nil, zerrors.ThrowNotFound(nil, "COMMAND-k39PJ", "Errors.SMTPConfig.NotFound")
}
if smtpConfigWriteModel.State == domain.SMTPConfigStateInactive {
return nil, zerrors.ThrowNotFound(nil, "COMMAND-km8g3", "Errors.SMTPConfig.AlreadyDeactivated")
return nil, zerrors.ThrowPreconditionFailed(nil, "COMMAND-km8g3", "Errors.SMTPConfig.AlreadyDeactivated")
}
iamAgg := InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel)
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSMTPConfigDeactivatedEvent(
ctx,
iamAgg,
id))
if err != nil {
return nil, err
}
err = AppendAndReduce(smtpConfigWriteModel, pushedEvents...)
err = c.pushAppendAndReduce(ctx,
smtpConfigWriteModel,
instance.NewSMTPConfigDeactivatedEvent(
ctx,
InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel),
id,
),
)
if err != nil {
return nil, err
}
return writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel), nil
}
func (c *Commands) RemoveSMTPConfig(ctx context.Context, instanceID, id string) (*domain.ObjectDetails, error) {
func (c *Commands) RemoveSMTPConfig(ctx context.Context, resourceOwner, id string) (*domain.ObjectDetails, error) {
if resourceOwner == "" {
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-t2WsPRgGaK", "Errors.ResourceOwnerMissing")
}
if id == "" {
return nil, zerrors.ThrowInvalidArgument(nil, "SMTP-7f5cv", "Errors.IDMissing")
return nil, zerrors.ThrowInvalidArgument(nil, "COMMAND-0ZV5whuUfu", "Errors.IDMissing")
}
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, instanceID, id, "")
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, resourceOwner, id, "")
if err != nil {
return nil, err
}
if !smtpConfigWriteModel.State.Exists() {
return nil, zerrors.ThrowNotFound(nil, "COMMAND-kg8rt", "Errors.SMTPConfig.NotFound")
return nil, zerrors.ThrowNotFound(nil, "COMMAND-09CXlTDL6w", "Errors.SMTPConfig.NotFound")
}
iamAgg := InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel)
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSMTPConfigRemovedEvent(
ctx,
iamAgg,
id))
if err != nil {
return nil, err
}
err = AppendAndReduce(smtpConfigWriteModel, pushedEvents...)
err = c.pushAppendAndReduce(ctx,
smtpConfigWriteModel,
instance.NewSMTPConfigRemovedEvent(
ctx,
InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel),
id,
),
)
if err != nil {
return nil, err
}
@@ -303,11 +426,11 @@ func (c *Commands) TestSMTPConfig(ctx context.Context, instanceID, id, email str
if err != nil {
return err
}
if !smtpConfigWriteModel.State.Exists() {
if !smtpConfigWriteModel.State.Exists() || smtpConfigWriteModel.SMTPConfig == nil {
return zerrors.ThrowNotFound(nil, "SMTP-p9cc", "Errors.SMTPConfig.NotFound")
}
password, err = crypto.DecryptString(smtpConfigWriteModel.Password, c.smtpEncryption)
password, err = crypto.DecryptString(smtpConfigWriteModel.SMTPConfig.Password, c.smtpEncryption)
if err != nil {
return err
}
@@ -338,23 +461,22 @@ func (c *Commands) TestSMTPConfigById(ctx context.Context, instanceID, id, email
return err
}
if !smtpConfigWriteModel.State.Exists() {
if !smtpConfigWriteModel.State.Exists() || smtpConfigWriteModel.SMTPConfig == nil {
return zerrors.ThrowNotFound(nil, "SMTP-99klw", "Errors.SMTPConfig.NotFound")
}
password, err := crypto.DecryptString(smtpConfigWriteModel.Password, c.smtpEncryption)
password, err := crypto.DecryptString(smtpConfigWriteModel.SMTPConfig.Password, c.smtpEncryption)
if err != nil {
return err
}
smtpConfig := &smtp.Config{
Description: smtpConfigWriteModel.Description,
Tls: smtpConfigWriteModel.TLS,
From: smtpConfigWriteModel.SenderAddress,
FromName: smtpConfigWriteModel.SenderName,
Tls: smtpConfigWriteModel.SMTPConfig.TLS,
From: smtpConfigWriteModel.SMTPConfig.SenderAddress,
FromName: smtpConfigWriteModel.SMTPConfig.SenderName,
SMTP: smtp.SMTP{
Host: smtpConfigWriteModel.Host,
User: smtpConfigWriteModel.User,
Host: smtpConfigWriteModel.SMTPConfig.Host,
User: smtpConfigWriteModel.SMTPConfig.User,
Password: password,
},
}
@@ -373,7 +495,7 @@ func checkSenderAddress(writeModel *IAMSMTPConfigWriteModel) error {
return nil
}
if !writeModel.domainState.Exists() {
return zerrors.ThrowInvalidArgument(nil, "INST-83nl8", "Errors.SMTPConfig.SenderAdressNotCustomDomain")
return zerrors.ThrowInvalidArgument(nil, "INST-xtWIiR2ZbR", "Errors.SMTPConfig.SenderAdressNotCustomDomain")
}
return nil
}

File diff suppressed because it is too large Load Diff