2022-02-16 15:49:17 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2023-12-08 14:30:55 +00:00
|
|
|
"errors"
|
2022-02-16 15:49:17 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-02-27 21:36:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/call"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2024-04-11 07:16:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-12-01 08:18:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
2024-04-11 07:16:10 +00:00
|
|
|
type SMTPConfigsSearchQueries struct {
|
|
|
|
SearchRequest
|
|
|
|
Queries []SearchQuery
|
|
|
|
}
|
|
|
|
|
|
|
|
type SMTPConfigs struct {
|
|
|
|
SearchResponse
|
|
|
|
Configs []*SMTPConfig
|
|
|
|
}
|
|
|
|
|
2022-02-16 15:49:17 +00:00
|
|
|
var (
|
|
|
|
smtpConfigsTable = table{
|
2022-10-27 06:08:36 +00:00
|
|
|
name: projection.SMTPConfigProjectionTable,
|
|
|
|
instanceIDCol: projection.SMTPConfigColumnInstanceID,
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
SMTPConfigColumnCreationDate = Column{
|
|
|
|
name: projection.SMTPConfigColumnCreationDate,
|
|
|
|
table: smtpConfigsTable,
|
|
|
|
}
|
|
|
|
SMTPConfigColumnChangeDate = Column{
|
|
|
|
name: projection.SMTPConfigColumnChangeDate,
|
|
|
|
table: smtpConfigsTable,
|
|
|
|
}
|
|
|
|
SMTPConfigColumnResourceOwner = Column{
|
|
|
|
name: projection.SMTPConfigColumnResourceOwner,
|
|
|
|
table: smtpConfigsTable,
|
|
|
|
}
|
2022-03-23 08:02:39 +00:00
|
|
|
SMTPConfigColumnInstanceID = Column{
|
|
|
|
name: projection.SMTPConfigColumnInstanceID,
|
|
|
|
table: smtpConfigsTable,
|
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
SMTPConfigColumnSequence = Column{
|
|
|
|
name: projection.SMTPConfigColumnSequence,
|
|
|
|
table: smtpConfigsTable,
|
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
SMTPConfigColumnID = Column{
|
|
|
|
name: projection.SMTPConfigColumnID,
|
2022-02-16 15:49:17 +00:00
|
|
|
table: smtpConfigsTable,
|
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
SMTPConfigColumnState = Column{
|
|
|
|
name: projection.SMTPConfigColumnState,
|
2022-02-16 15:49:17 +00:00
|
|
|
table: smtpConfigsTable,
|
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
SMTPConfigColumnDescription = Column{
|
|
|
|
name: projection.SMTPConfigColumnDescription,
|
2022-02-16 15:49:17 +00:00
|
|
|
table: smtpConfigsTable,
|
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
|
|
|
|
smtpConfigsSMTPTable = table{
|
|
|
|
name: projection.SMTPConfigTable,
|
|
|
|
instanceIDCol: projection.SMTPConfigColumnInstanceID,
|
2023-08-29 07:08:24 +00:00
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
SMTPConfigSMTPColumnInstanceID = Column{
|
|
|
|
name: projection.SMTPConfigColumnInstanceID,
|
|
|
|
table: smtpConfigsSMTPTable,
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
SMTPConfigSMTPColumnID = Column{
|
|
|
|
name: projection.SMTPConfigColumnID,
|
|
|
|
table: smtpConfigsSMTPTable,
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
SMTPConfigSMTPColumnTLS = Column{
|
|
|
|
name: projection.SMTPConfigSMTPColumnTLS,
|
|
|
|
table: smtpConfigsSMTPTable,
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
SMTPConfigSMTPColumnSenderAddress = Column{
|
|
|
|
name: projection.SMTPConfigSMTPColumnSenderAddress,
|
|
|
|
table: smtpConfigsSMTPTable,
|
2024-04-11 07:16:10 +00:00
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
SMTPConfigSMTPColumnSenderName = Column{
|
|
|
|
name: projection.SMTPConfigSMTPColumnSenderName,
|
|
|
|
table: smtpConfigsSMTPTable,
|
2024-04-11 07:16:10 +00:00
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
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,
|
2024-04-11 07:16:10 +00:00
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SMTPConfig struct {
|
2024-09-12 04:27:29 +00:00
|
|
|
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 {
|
2023-08-29 07:08:24 +00:00
|
|
|
TLS bool
|
|
|
|
SenderAddress string
|
|
|
|
SenderName string
|
|
|
|
ReplyToAddress string
|
|
|
|
Host string
|
|
|
|
User string
|
|
|
|
Password *crypto.CryptoValue
|
2024-04-11 07:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) SMTPConfigActive(ctx context.Context, resourceOwner 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(): resourceOwner,
|
|
|
|
SMTPConfigColumnState.identifier(): domain.SMTPConfigStateActive,
|
|
|
|
}).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-3m9sl", "Errors.Query.SQLStatement")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = q.client.QueryRowContext(ctx, func(row *sql.Row) error {
|
|
|
|
config, err = scan(row)
|
|
|
|
return err
|
|
|
|
}, query, args...)
|
|
|
|
return config, err
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2024-09-12 04:27:29 +00:00
|
|
|
func (q *Queries) SMTPConfigByID(ctx context.Context, instanceID, id string) (config *SMTPConfig, err error) {
|
2022-12-01 08:18:53 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
stmt, scan := prepareSMTPConfigQuery(ctx, q.client)
|
2022-02-16 15:49:17 +00:00
|
|
|
query, args, err := stmt.Where(sq.Eq{
|
2024-09-12 04:27:29 +00:00
|
|
|
SMTPConfigColumnInstanceID.identifier(): instanceID,
|
|
|
|
SMTPConfigColumnID.identifier(): id,
|
2022-02-16 15:49:17 +00:00
|
|
|
}).ToSql()
|
|
|
|
if err != nil {
|
2024-04-11 07:16:10 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-8f8gw", "Errors.Query.SQLStatement")
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 10:49:22 +00:00
|
|
|
err = q.client.QueryRowContext(ctx, func(row *sql.Row) error {
|
|
|
|
config, err = scan(row)
|
|
|
|
return err
|
|
|
|
}, query, args...)
|
|
|
|
return config, err
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
func prepareSMTPConfigQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Row) (*SMTPConfig, error)) {
|
2022-02-16 15:49:17 +00:00
|
|
|
password := new(crypto.CryptoValue)
|
|
|
|
|
|
|
|
return sq.Select(
|
|
|
|
SMTPConfigColumnCreationDate.identifier(),
|
|
|
|
SMTPConfigColumnChangeDate.identifier(),
|
|
|
|
SMTPConfigColumnResourceOwner.identifier(),
|
|
|
|
SMTPConfigColumnSequence.identifier(),
|
2024-04-11 07:16:10 +00:00
|
|
|
SMTPConfigColumnID.identifier(),
|
|
|
|
SMTPConfigColumnState.identifier(),
|
2024-09-12 04:27:29 +00:00
|
|
|
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))).
|
2023-02-27 21:36:43 +00:00
|
|
|
PlaceholderFormat(sq.Dollar),
|
2022-02-16 15:49:17 +00:00
|
|
|
func(row *sql.Row) (*SMTPConfig, error) {
|
|
|
|
config := new(SMTPConfig)
|
2024-09-12 04:27:29 +00:00
|
|
|
var (
|
|
|
|
smtpConfig = sqlSmtpConfig{}
|
|
|
|
httpConfig = sqlHTTPConfig{}
|
|
|
|
)
|
2022-02-16 15:49:17 +00:00
|
|
|
err := row.Scan(
|
|
|
|
&config.CreationDate,
|
|
|
|
&config.ChangeDate,
|
|
|
|
&config.ResourceOwner,
|
|
|
|
&config.Sequence,
|
2024-04-11 07:16:10 +00:00
|
|
|
&config.ID,
|
|
|
|
&config.State,
|
|
|
|
&config.Description,
|
2024-09-12 04:27:29 +00:00
|
|
|
&smtpConfig.id,
|
|
|
|
&smtpConfig.tls,
|
|
|
|
&smtpConfig.senderAddress,
|
|
|
|
&smtpConfig.senderName,
|
|
|
|
&smtpConfig.replyToAddress,
|
|
|
|
&smtpConfig.host,
|
|
|
|
&smtpConfig.user,
|
|
|
|
&password,
|
|
|
|
&httpConfig.id,
|
|
|
|
&httpConfig.endpoint,
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, zerrors.ThrowNotFound(err, "QUERY-fwofw", "Errors.SMTPConfig.NotFound")
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-9k87F", "Errors.Internal")
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
smtpConfig.password = password
|
|
|
|
smtpConfig.set(config)
|
|
|
|
httpConfig.setSMTP(config)
|
2022-02-16 15:49:17 +00:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
}
|
2024-04-11 07:16:10 +00:00
|
|
|
|
|
|
|
func prepareSMTPConfigsQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*SMTPConfigs, error)) {
|
|
|
|
return sq.Select(
|
|
|
|
SMTPConfigColumnCreationDate.identifier(),
|
|
|
|
SMTPConfigColumnChangeDate.identifier(),
|
|
|
|
SMTPConfigColumnResourceOwner.identifier(),
|
|
|
|
SMTPConfigColumnSequence.identifier(),
|
|
|
|
SMTPConfigColumnID.identifier(),
|
|
|
|
SMTPConfigColumnState.identifier(),
|
|
|
|
SMTPConfigColumnDescription.identifier(),
|
2024-09-12 04:27:29 +00:00
|
|
|
|
|
|
|
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))).
|
2024-04-11 07:16:10 +00:00
|
|
|
PlaceholderFormat(sq.Dollar),
|
|
|
|
func(rows *sql.Rows) (*SMTPConfigs, error) {
|
|
|
|
configs := &SMTPConfigs{Configs: []*SMTPConfig{}}
|
|
|
|
for rows.Next() {
|
|
|
|
config := new(SMTPConfig)
|
2024-09-12 04:27:29 +00:00
|
|
|
password := new(crypto.CryptoValue)
|
|
|
|
var (
|
|
|
|
smtpConfig = sqlSmtpConfig{}
|
|
|
|
httpConfig = sqlHTTPConfig{}
|
|
|
|
)
|
2024-04-11 07:16:10 +00:00
|
|
|
err := rows.Scan(
|
|
|
|
&config.CreationDate,
|
|
|
|
&config.ChangeDate,
|
|
|
|
&config.ResourceOwner,
|
|
|
|
&config.Sequence,
|
|
|
|
&config.ID,
|
|
|
|
&config.State,
|
|
|
|
&config.Description,
|
2024-09-12 04:27:29 +00:00
|
|
|
&smtpConfig.id,
|
|
|
|
&smtpConfig.tls,
|
|
|
|
&smtpConfig.senderAddress,
|
|
|
|
&smtpConfig.senderName,
|
|
|
|
&smtpConfig.replyToAddress,
|
|
|
|
&smtpConfig.host,
|
|
|
|
&smtpConfig.user,
|
|
|
|
&password,
|
|
|
|
&httpConfig.id,
|
|
|
|
&httpConfig.endpoint,
|
2024-04-11 07:16:10 +00:00
|
|
|
&configs.Count,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return nil, zerrors.ThrowNotFound(err, "QUERY-fwofw", "Errors.SMTPConfig.NotFound")
|
|
|
|
}
|
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-9k87F", "Errors.Internal")
|
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
smtpConfig.password = password
|
|
|
|
smtpConfig.set(config)
|
|
|
|
httpConfig.setSMTP(config)
|
2024-04-11 07:16:10 +00:00
|
|
|
configs.Configs = append(configs.Configs, config)
|
|
|
|
}
|
|
|
|
return configs, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) SearchSMTPConfigs(ctx context.Context, queries *SMTPConfigsSearchQueries) (configs *SMTPConfigs, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
query, scan := prepareSMTPConfigsQuery(ctx, q.client)
|
|
|
|
stmt, args, err := queries.toQuery(query).
|
|
|
|
Where(sq.Eq{
|
|
|
|
SMTPConfigColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
|
|
|
}).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, zerrors.ThrowInvalidArgument(err, "QUERY-sZ7Cx", "Errors.Query.InvalidRequest")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = q.client.QueryContext(ctx, func(rows *sql.Rows) error {
|
|
|
|
configs, err = scan(rows)
|
|
|
|
return err
|
|
|
|
}, stmt, args...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, zerrors.ThrowInternal(err, "QUERY-tOpKN", "Errors.Internal")
|
|
|
|
}
|
|
|
|
configs.State, err = q.latestState(ctx, smsConfigsTable)
|
|
|
|
return configs, err
|
|
|
|
}
|
2024-09-12 04:27:29 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|