mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 20:57:31 +00:00
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:
@@ -52,34 +52,6 @@ var (
|
||||
name: projection.SMTPConfigColumnSequence,
|
||||
table: smtpConfigsTable,
|
||||
}
|
||||
SMTPConfigColumnTLS = Column{
|
||||
name: projection.SMTPConfigColumnTLS,
|
||||
table: smtpConfigsTable,
|
||||
}
|
||||
SMTPConfigColumnSenderAddress = Column{
|
||||
name: projection.SMTPConfigColumnSenderAddress,
|
||||
table: smtpConfigsTable,
|
||||
}
|
||||
SMTPConfigColumnSenderName = Column{
|
||||
name: projection.SMTPConfigColumnSenderName,
|
||||
table: smtpConfigsTable,
|
||||
}
|
||||
SMTPConfigColumnReplyToAddress = Column{
|
||||
name: projection.SMTPConfigColumnReplyToAddress,
|
||||
table: smtpConfigsTable,
|
||||
}
|
||||
SMTPConfigColumnSMTPHost = Column{
|
||||
name: projection.SMTPConfigColumnSMTPHost,
|
||||
table: smtpConfigsTable,
|
||||
}
|
||||
SMTPConfigColumnSMTPUser = Column{
|
||||
name: projection.SMTPConfigColumnSMTPUser,
|
||||
table: smtpConfigsTable,
|
||||
}
|
||||
SMTPConfigColumnSMTPPassword = Column{
|
||||
name: projection.SMTPConfigColumnSMTPPassword,
|
||||
table: smtpConfigsTable,
|
||||
}
|
||||
SMTPConfigColumnID = Column{
|
||||
name: projection.SMTPConfigColumnID,
|
||||
table: smtpConfigsTable,
|
||||
@@ -92,13 +64,82 @@ var (
|
||||
name: projection.SMTPConfigColumnDescription,
|
||||
table: smtpConfigsTable,
|
||||
}
|
||||
|
||||
smtpConfigsSMTPTable = table{
|
||||
name: projection.SMTPConfigTable,
|
||||
instanceIDCol: projection.SMTPConfigColumnInstanceID,
|
||||
}
|
||||
SMTPConfigSMTPColumnInstanceID = Column{
|
||||
name: projection.SMTPConfigColumnInstanceID,
|
||||
table: smtpConfigsSMTPTable,
|
||||
}
|
||||
SMTPConfigSMTPColumnID = Column{
|
||||
name: projection.SMTPConfigColumnID,
|
||||
table: smtpConfigsSMTPTable,
|
||||
}
|
||||
SMTPConfigSMTPColumnTLS = Column{
|
||||
name: projection.SMTPConfigSMTPColumnTLS,
|
||||
table: smtpConfigsSMTPTable,
|
||||
}
|
||||
SMTPConfigSMTPColumnSenderAddress = Column{
|
||||
name: projection.SMTPConfigSMTPColumnSenderAddress,
|
||||
table: smtpConfigsSMTPTable,
|
||||
}
|
||||
SMTPConfigSMTPColumnSenderName = Column{
|
||||
name: projection.SMTPConfigSMTPColumnSenderName,
|
||||
table: smtpConfigsSMTPTable,
|
||||
}
|
||||
SMTPConfigSMTPColumnReplyToAddress = Column{
|
||||
name: projection.SMTPConfigSMTPColumnReplyToAddress,
|
||||
table: smtpConfigsSMTPTable,
|
||||
}
|
||||
SMTPConfigSMTPColumnHost = Column{
|
||||
name: projection.SMTPConfigSMTPColumnHost,
|
||||
table: smtpConfigsSMTPTable,
|
||||
}
|
||||
SMTPConfigSMTPColumnUser = Column{
|
||||
name: projection.SMTPConfigSMTPColumnUser,
|
||||
table: smtpConfigsSMTPTable,
|
||||
}
|
||||
SMTPConfigSMTPColumnPassword = Column{
|
||||
name: projection.SMTPConfigSMTPColumnPassword,
|
||||
table: smtpConfigsSMTPTable,
|
||||
}
|
||||
|
||||
smtpConfigsHTTPTable = table{
|
||||
name: projection.SMTPConfigHTTPTable,
|
||||
instanceIDCol: projection.SMTPConfigHTTPColumnInstanceID,
|
||||
}
|
||||
SMTPConfigHTTPColumnInstanceID = Column{
|
||||
name: projection.SMTPConfigHTTPColumnInstanceID,
|
||||
table: smtpConfigsHTTPTable,
|
||||
}
|
||||
SMTPConfigHTTPColumnID = Column{
|
||||
name: projection.SMTPConfigHTTPColumnID,
|
||||
table: smtpConfigsHTTPTable,
|
||||
}
|
||||
SMTPConfigHTTPColumnEndpoint = Column{
|
||||
name: projection.SMTPConfigHTTPColumnEndpoint,
|
||||
table: smtpConfigsHTTPTable,
|
||||
}
|
||||
)
|
||||
|
||||
type SMTPConfig struct {
|
||||
CreationDate time.Time
|
||||
ChangeDate time.Time
|
||||
ResourceOwner string
|
||||
Sequence uint64
|
||||
CreationDate time.Time
|
||||
ChangeDate time.Time
|
||||
ResourceOwner string
|
||||
AggregateID string
|
||||
ID string
|
||||
Sequence uint64
|
||||
Description string
|
||||
|
||||
SMTPConfig *SMTP
|
||||
HTTPConfig *HTTP
|
||||
|
||||
State domain.SMTPConfigState
|
||||
}
|
||||
|
||||
type SMTP struct {
|
||||
TLS bool
|
||||
SenderAddress string
|
||||
SenderName string
|
||||
@@ -106,9 +147,6 @@ type SMTPConfig struct {
|
||||
Host string
|
||||
User string
|
||||
Password *crypto.CryptoValue
|
||||
ID string
|
||||
State domain.SMTPConfigState
|
||||
Description string
|
||||
}
|
||||
|
||||
func (q *Queries) SMTPConfigActive(ctx context.Context, resourceOwner string) (config *SMTPConfig, err error) {
|
||||
@@ -132,15 +170,14 @@ func (q *Queries) SMTPConfigActive(ctx context.Context, resourceOwner string) (c
|
||||
return config, err
|
||||
}
|
||||
|
||||
func (q *Queries) SMTPConfigByID(ctx context.Context, instanceID, resourceOwner, id string) (config *SMTPConfig, err error) {
|
||||
func (q *Queries) SMTPConfigByID(ctx context.Context, instanceID, id string) (config *SMTPConfig, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
stmt, scan := prepareSMTPConfigQuery(ctx, q.client)
|
||||
query, args, err := stmt.Where(sq.Eq{
|
||||
SMTPConfigColumnResourceOwner.identifier(): resourceOwner,
|
||||
SMTPConfigColumnInstanceID.identifier(): instanceID,
|
||||
SMTPConfigColumnID.identifier(): id,
|
||||
SMTPConfigColumnInstanceID.identifier(): instanceID,
|
||||
SMTPConfigColumnID.identifier(): id,
|
||||
}).ToSql()
|
||||
if err != nil {
|
||||
return nil, zerrors.ThrowInternal(err, "QUERY-8f8gw", "Errors.Query.SQLStatement")
|
||||
@@ -161,35 +198,49 @@ func prepareSMTPConfigQuery(ctx context.Context, db prepareDatabase) (sq.SelectB
|
||||
SMTPConfigColumnChangeDate.identifier(),
|
||||
SMTPConfigColumnResourceOwner.identifier(),
|
||||
SMTPConfigColumnSequence.identifier(),
|
||||
SMTPConfigColumnTLS.identifier(),
|
||||
SMTPConfigColumnSenderAddress.identifier(),
|
||||
SMTPConfigColumnSenderName.identifier(),
|
||||
SMTPConfigColumnReplyToAddress.identifier(),
|
||||
SMTPConfigColumnSMTPHost.identifier(),
|
||||
SMTPConfigColumnSMTPUser.identifier(),
|
||||
SMTPConfigColumnSMTPPassword.identifier(),
|
||||
SMTPConfigColumnID.identifier(),
|
||||
SMTPConfigColumnState.identifier(),
|
||||
SMTPConfigColumnDescription.identifier()).
|
||||
From(smtpConfigsTable.identifier() + db.Timetravel(call.Took(ctx))).
|
||||
SMTPConfigColumnDescription.identifier(),
|
||||
|
||||
SMTPConfigSMTPColumnID.identifier(),
|
||||
SMTPConfigSMTPColumnTLS.identifier(),
|
||||
SMTPConfigSMTPColumnSenderAddress.identifier(),
|
||||
SMTPConfigSMTPColumnSenderName.identifier(),
|
||||
SMTPConfigSMTPColumnReplyToAddress.identifier(),
|
||||
SMTPConfigSMTPColumnHost.identifier(),
|
||||
SMTPConfigSMTPColumnUser.identifier(),
|
||||
SMTPConfigSMTPColumnPassword.identifier(),
|
||||
|
||||
SMTPConfigHTTPColumnID.identifier(),
|
||||
SMTPConfigHTTPColumnEndpoint.identifier()).
|
||||
From(smtpConfigsTable.identifier()).
|
||||
LeftJoin(join(SMTPConfigSMTPColumnID, SMTPConfigColumnID)).
|
||||
LeftJoin(join(SMTPConfigHTTPColumnID, SMTPConfigColumnID) + db.Timetravel(call.Took(ctx))).
|
||||
PlaceholderFormat(sq.Dollar),
|
||||
func(row *sql.Row) (*SMTPConfig, error) {
|
||||
config := new(SMTPConfig)
|
||||
var (
|
||||
smtpConfig = sqlSmtpConfig{}
|
||||
httpConfig = sqlHTTPConfig{}
|
||||
)
|
||||
err := row.Scan(
|
||||
&config.CreationDate,
|
||||
&config.ChangeDate,
|
||||
&config.ResourceOwner,
|
||||
&config.Sequence,
|
||||
&config.TLS,
|
||||
&config.SenderAddress,
|
||||
&config.SenderName,
|
||||
&config.ReplyToAddress,
|
||||
&config.Host,
|
||||
&config.User,
|
||||
&password,
|
||||
&config.ID,
|
||||
&config.State,
|
||||
&config.Description,
|
||||
&smtpConfig.id,
|
||||
&smtpConfig.tls,
|
||||
&smtpConfig.senderAddress,
|
||||
&smtpConfig.senderName,
|
||||
&smtpConfig.replyToAddress,
|
||||
&smtpConfig.host,
|
||||
&smtpConfig.user,
|
||||
&password,
|
||||
&httpConfig.id,
|
||||
&httpConfig.endpoint,
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
@@ -197,7 +248,9 @@ func prepareSMTPConfigQuery(ctx context.Context, db prepareDatabase) (sq.SelectB
|
||||
}
|
||||
return nil, zerrors.ThrowInternal(err, "QUERY-9k87F", "Errors.Internal")
|
||||
}
|
||||
config.Password = password
|
||||
smtpConfig.password = password
|
||||
smtpConfig.set(config)
|
||||
httpConfig.setSMTP(config)
|
||||
return config, nil
|
||||
}
|
||||
}
|
||||
@@ -208,38 +261,53 @@ func prepareSMTPConfigsQuery(ctx context.Context, db prepareDatabase) (sq.Select
|
||||
SMTPConfigColumnChangeDate.identifier(),
|
||||
SMTPConfigColumnResourceOwner.identifier(),
|
||||
SMTPConfigColumnSequence.identifier(),
|
||||
SMTPConfigColumnTLS.identifier(),
|
||||
SMTPConfigColumnSenderAddress.identifier(),
|
||||
SMTPConfigColumnSenderName.identifier(),
|
||||
SMTPConfigColumnReplyToAddress.identifier(),
|
||||
SMTPConfigColumnSMTPHost.identifier(),
|
||||
SMTPConfigColumnSMTPUser.identifier(),
|
||||
SMTPConfigColumnSMTPPassword.identifier(),
|
||||
SMTPConfigColumnID.identifier(),
|
||||
SMTPConfigColumnState.identifier(),
|
||||
SMTPConfigColumnDescription.identifier(),
|
||||
countColumn.identifier()).
|
||||
From(smtpConfigsTable.identifier() + db.Timetravel(call.Took(ctx))).
|
||||
|
||||
SMTPConfigSMTPColumnID.identifier(),
|
||||
SMTPConfigSMTPColumnTLS.identifier(),
|
||||
SMTPConfigSMTPColumnSenderAddress.identifier(),
|
||||
SMTPConfigSMTPColumnSenderName.identifier(),
|
||||
SMTPConfigSMTPColumnReplyToAddress.identifier(),
|
||||
SMTPConfigSMTPColumnHost.identifier(),
|
||||
SMTPConfigSMTPColumnUser.identifier(),
|
||||
SMTPConfigSMTPColumnPassword.identifier(),
|
||||
|
||||
SMTPConfigHTTPColumnID.identifier(),
|
||||
SMTPConfigHTTPColumnEndpoint.identifier(),
|
||||
countColumn.identifier(),
|
||||
).From(smtpConfigsTable.identifier()).
|
||||
LeftJoin(join(SMTPConfigSMTPColumnID, SMTPConfigColumnID)).
|
||||
LeftJoin(join(SMTPConfigHTTPColumnID, SMTPConfigColumnID) + db.Timetravel(call.Took(ctx))).
|
||||
PlaceholderFormat(sq.Dollar),
|
||||
func(rows *sql.Rows) (*SMTPConfigs, error) {
|
||||
configs := &SMTPConfigs{Configs: []*SMTPConfig{}}
|
||||
for rows.Next() {
|
||||
config := new(SMTPConfig)
|
||||
password := new(crypto.CryptoValue)
|
||||
var (
|
||||
smtpConfig = sqlSmtpConfig{}
|
||||
httpConfig = sqlHTTPConfig{}
|
||||
)
|
||||
err := rows.Scan(
|
||||
&config.CreationDate,
|
||||
&config.ChangeDate,
|
||||
&config.ResourceOwner,
|
||||
&config.Sequence,
|
||||
&config.TLS,
|
||||
&config.SenderAddress,
|
||||
&config.SenderName,
|
||||
&config.ReplyToAddress,
|
||||
&config.Host,
|
||||
&config.User,
|
||||
&config.Password,
|
||||
&config.ID,
|
||||
&config.State,
|
||||
&config.Description,
|
||||
&smtpConfig.id,
|
||||
&smtpConfig.tls,
|
||||
&smtpConfig.senderAddress,
|
||||
&smtpConfig.senderName,
|
||||
&smtpConfig.replyToAddress,
|
||||
&smtpConfig.host,
|
||||
&smtpConfig.user,
|
||||
&password,
|
||||
&httpConfig.id,
|
||||
&httpConfig.endpoint,
|
||||
&configs.Count,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -248,6 +316,9 @@ func prepareSMTPConfigsQuery(ctx context.Context, db prepareDatabase) (sq.Select
|
||||
}
|
||||
return nil, zerrors.ThrowInternal(err, "QUERY-9k87F", "Errors.Internal")
|
||||
}
|
||||
smtpConfig.password = password
|
||||
smtpConfig.set(config)
|
||||
httpConfig.setSMTP(config)
|
||||
configs.Configs = append(configs.Configs, config)
|
||||
}
|
||||
return configs, nil
|
||||
@@ -277,3 +348,38 @@ func (q *Queries) SearchSMTPConfigs(ctx context.Context, queries *SMTPConfigsSea
|
||||
configs.State, err = q.latestState(ctx, smsConfigsTable)
|
||||
return configs, err
|
||||
}
|
||||
|
||||
type sqlSmtpConfig struct {
|
||||
id sql.NullString
|
||||
tls sql.NullBool
|
||||
senderAddress sql.NullString
|
||||
senderName sql.NullString
|
||||
replyToAddress sql.NullString
|
||||
host sql.NullString
|
||||
user sql.NullString
|
||||
password *crypto.CryptoValue
|
||||
}
|
||||
|
||||
func (c sqlSmtpConfig) set(smtpConfig *SMTPConfig) {
|
||||
if !c.id.Valid {
|
||||
return
|
||||
}
|
||||
smtpConfig.SMTPConfig = &SMTP{
|
||||
TLS: c.tls.Bool,
|
||||
SenderAddress: c.senderAddress.String,
|
||||
SenderName: c.senderName.String,
|
||||
ReplyToAddress: c.replyToAddress.String,
|
||||
Host: c.host.String,
|
||||
User: c.user.String,
|
||||
Password: c.password,
|
||||
}
|
||||
}
|
||||
|
||||
func (c sqlHTTPConfig) setSMTP(smtpConfig *SMTPConfig) {
|
||||
if !c.id.Valid {
|
||||
return
|
||||
}
|
||||
smtpConfig.HTTPConfig = &HTTP{
|
||||
Endpoint: c.endpoint.String,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user