zitadel/internal/query/smtp.go

153 lines
4.2 KiB
Go
Raw Normal View History

package query
import (
"context"
"database/sql"
errs "errors"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/api/call"
"github.com/zitadel/zitadel/internal/crypto"
"github.com/zitadel/zitadel/internal/errors"
"github.com/zitadel/zitadel/internal/query/projection"
"github.com/zitadel/zitadel/internal/telemetry/tracing"
)
var (
smtpConfigsTable = table{
name: projection.SMTPConfigProjectionTable,
instanceIDCol: projection.SMTPConfigColumnInstanceID,
}
SMTPConfigColumnAggregateID = Column{
name: projection.SMTPConfigColumnAggregateID,
table: smtpConfigsTable,
}
SMTPConfigColumnCreationDate = Column{
name: projection.SMTPConfigColumnCreationDate,
table: smtpConfigsTable,
}
SMTPConfigColumnChangeDate = Column{
name: projection.SMTPConfigColumnChangeDate,
table: smtpConfigsTable,
}
SMTPConfigColumnResourceOwner = Column{
name: projection.SMTPConfigColumnResourceOwner,
table: smtpConfigsTable,
}
SMTPConfigColumnInstanceID = Column{
name: projection.SMTPConfigColumnInstanceID,
table: smtpConfigsTable,
}
SMTPConfigColumnSequence = Column{
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,
}
SMTPConfigColumnSMTPHost = Column{
name: projection.SMTPConfigColumnSMTPHost,
table: smtpConfigsTable,
}
SMTPConfigColumnSMTPUser = Column{
name: projection.SMTPConfigColumnSMTPUser,
table: smtpConfigsTable,
}
SMTPConfigColumnSMTPPassword = Column{
name: projection.SMTPConfigColumnSMTPPassword,
table: smtpConfigsTable,
}
)
type SMTPConfigs struct {
SearchResponse
SMTPConfigs []*SMTPConfig
}
type SMTPConfig struct {
AggregateID string
CreationDate time.Time
ChangeDate time.Time
ResourceOwner string
Sequence uint64
TLS bool
SenderAddress string
SenderName string
Host string
User string
Password *crypto.CryptoValue
}
func (q *Queries) SMTPConfigByAggregateID(ctx context.Context, aggregateID string) (_ *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{
SMTPConfigColumnAggregateID.identifier(): aggregateID,
feat: handle instance from context (#3382) * commander * commander * selber! * move to packages * fix(errors): implement Is interface * test: command * test: commands * add init steps * setup tenant * add default step yaml * possibility to set password * merge v2 into v2-commander * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: rename iam command side to instance * fix: search query builder can filter events in memory * fix: filters for add member * fix(setup): add `ExternalSecure` to config * chore: name iam to instance * fix: matching * remove unsued func * base url * base url * test(command): filter funcs * test: commands * fix: rename orgiampolicy to domain policy * start from init * commands * config * fix indexes and add constraints * fixes * fix: merge conflicts * fix: protos * fix: md files * setup * add deprecated org iam policy again * typo * fix search query * fix filter * Apply suggestions from code review * remove custom org from org setup * add todos for verification * change apps creation * simplify package structure * fix error * move preparation helper for tests * fix unique constraints * fix config mapping in setup * fix error handling in encryption_keys.go * fix projection config * fix query from old views to projection * fix setup of mgmt api * set iam project and fix instance projection * fix tokens view * fix steps.yaml and defaults.yaml * fix projections * change instance context to interface * instance interceptors and additional events in setup * cleanup * tests for interceptors * fix label policy * add todo * single api endpoint in environment.json Co-authored-by: adlerhurst <silvan.reusser@gmail.com> Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2022-03-29 09:53:19 +00:00
SMTPConfigColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
}).ToSql()
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-3m9sl", "Errors.Query.SQLStatment")
}
row := q.client.QueryRowContext(ctx, query, args...)
return scan(row)
}
func prepareSMTPConfigQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Row) (*SMTPConfig, error)) {
password := new(crypto.CryptoValue)
return sq.Select(
SMTPConfigColumnAggregateID.identifier(),
SMTPConfigColumnCreationDate.identifier(),
SMTPConfigColumnChangeDate.identifier(),
SMTPConfigColumnResourceOwner.identifier(),
SMTPConfigColumnSequence.identifier(),
SMTPConfigColumnTLS.identifier(),
SMTPConfigColumnSenderAddress.identifier(),
SMTPConfigColumnSenderName.identifier(),
SMTPConfigColumnSMTPHost.identifier(),
SMTPConfigColumnSMTPUser.identifier(),
SMTPConfigColumnSMTPPassword.identifier()).
From(smtpConfigsTable.identifier() + db.Timetravel(call.Took(ctx))).
PlaceholderFormat(sq.Dollar),
func(row *sql.Row) (*SMTPConfig, error) {
config := new(SMTPConfig)
err := row.Scan(
&config.AggregateID,
&config.CreationDate,
&config.ChangeDate,
&config.ResourceOwner,
&config.Sequence,
&config.TLS,
&config.SenderAddress,
&config.SenderName,
&config.Host,
&config.User,
&password,
)
if err != nil {
if errs.Is(err, sql.ErrNoRows) {
return nil, errors.ThrowNotFound(err, "QUERY-fwofw", "Errors.SMTPConfig.NotFound")
}
return nil, errors.ThrowInternal(err, "QUERY-9k87F", "Errors.Internal")
}
config.Password = password
return config, nil
}
}