mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 04:17:32 +00:00
feat: trusted (instance) domains (#8369)
# Which Problems Are Solved ZITADEL currently selects the instance context based on a HTTP header (see https://github.com/zitadel/zitadel/issues/8279#issue-2399959845 and checks it against the list of instance domains. Let's call it instance or API domain. For any context based URL (e.g. OAuth, OIDC, SAML endpoints, links in emails, ...) the requested domain (instance domain) will be used. Let's call it the public domain. In cases of proxied setups, all exposed domains (public domains) require the domain to be managed as instance domain. This can either be done using the "ExternalDomain" in the runtime config or via system API, which requires a validation through CustomerPortal on zitadel.cloud. # How the Problems Are Solved - Two new headers / header list are added: - `InstanceHostHeaders`: an ordered list (first sent wins), which will be used to match the instance. (For backward compatibility: the `HTTP1HostHeader`, `HTTP2HostHeader` and `forwarded`, `x-forwarded-for`, `x-forwarded-host` are checked afterwards as well) - `PublicHostHeaders`: an ordered list (first sent wins), which will be used as public host / domain. This will be checked against a list of trusted domains on the instance. - The middleware intercepts all requests to the API and passes a `DomainCtx` object with the hosts and protocol into the context (previously only a computed `origin` was passed) - HTTP / GRPC server do not longer try to match the headers to instances themself, but use the passed `http.DomainContext` in their interceptors. - The `RequestedHost` and `RequestedDomain` from authz.Instance are removed in favor of the `http.DomainContext` - When authenticating to or signing out from Console UI, the current `http.DomainContext(ctx).Origin` (already checked by instance interceptor for validity) is used to compute and dynamically add a `redirect_uri` and `post_logout_redirect_uri`. - Gateway passes all configured host headers (previously only did `x-zitadel-*`) - Admin API allows to manage trusted domain # Additional Changes None # Additional Context - part of #8279 - open topics: - "single-instance" mode - Console UI
This commit is contained in:
@@ -195,19 +195,24 @@ var (
|
||||
instanceByIDQuery string
|
||||
)
|
||||
|
||||
func (q *Queries) InstanceByHost(ctx context.Context, host string) (_ authz.Instance, err error) {
|
||||
func (q *Queries) InstanceByHost(ctx context.Context, instanceHost, publicHost string) (_ authz.Instance, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("unable to get instance by host %s: %w", host, err)
|
||||
err = fmt.Errorf("unable to get instance by host: instanceHost %s, publicHost %s: %w", instanceHost, publicHost, err)
|
||||
}
|
||||
span.EndWithError(err)
|
||||
}()
|
||||
|
||||
domain := strings.Split(host, ":")[0] // remove possible port
|
||||
instance, scan := scanAuthzInstance(host, domain)
|
||||
err = q.client.QueryRowContext(ctx, scan, instanceByDomainQuery, domain)
|
||||
logging.OnError(err).WithField("host", host).WithField("domain", domain).Warn("instance by host")
|
||||
instanceDomain := strings.Split(instanceHost, ":")[0] // remove possible port
|
||||
publicDomain := strings.Split(publicHost, ":")[0] // remove possible port
|
||||
instance, scan := scanAuthzInstance()
|
||||
// in case public domain is the same as the instance domain, we do not need to check it
|
||||
// and can empty it for the check
|
||||
if instanceDomain == publicDomain {
|
||||
publicDomain = ""
|
||||
}
|
||||
err = q.client.QueryRowContext(ctx, scan, instanceByDomainQuery, instanceDomain, publicDomain)
|
||||
return instance, err
|
||||
}
|
||||
|
||||
@@ -216,7 +221,7 @@ func (q *Queries) InstanceByID(ctx context.Context) (_ authz.Instance, err error
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
instanceID := authz.GetInstance(ctx).InstanceID()
|
||||
instance, scan := scanAuthzInstance("", "")
|
||||
instance, scan := scanAuthzInstance()
|
||||
err = q.client.QueryRowContext(ctx, scan, instanceByIDQuery, instanceID)
|
||||
logging.OnError(err).WithField("instance_id", instanceID).Warn("instance by ID")
|
||||
return instance, err
|
||||
@@ -421,8 +426,6 @@ type authzInstance struct {
|
||||
iamProjectID string
|
||||
consoleID string
|
||||
consoleAppID string
|
||||
host string
|
||||
domain string
|
||||
defaultLang language.Tag
|
||||
defaultOrgID string
|
||||
csp csp
|
||||
@@ -453,14 +456,6 @@ func (i *authzInstance) ConsoleApplicationID() string {
|
||||
return i.consoleAppID
|
||||
}
|
||||
|
||||
func (i *authzInstance) RequestedDomain() string {
|
||||
return strings.Split(i.host, ":")[0]
|
||||
}
|
||||
|
||||
func (i *authzInstance) RequestedHost() string {
|
||||
return i.host
|
||||
}
|
||||
|
||||
func (i *authzInstance) DefaultLanguage() language.Tag {
|
||||
return i.defaultLang
|
||||
}
|
||||
@@ -492,11 +487,8 @@ func (i *authzInstance) Features() feature.Features {
|
||||
return i.features
|
||||
}
|
||||
|
||||
func scanAuthzInstance(host, domain string) (*authzInstance, func(row *sql.Row) error) {
|
||||
instance := &authzInstance{
|
||||
host: host,
|
||||
domain: domain,
|
||||
}
|
||||
func scanAuthzInstance() (*authzInstance, func(row *sql.Row) error) {
|
||||
instance := &authzInstance{}
|
||||
return instance, func(row *sql.Row) error {
|
||||
var (
|
||||
lang string
|
||||
|
@@ -30,6 +30,8 @@ select
|
||||
f.features
|
||||
from domain d
|
||||
join projections.instances i on i.id = d.instance_id
|
||||
left join projections.instance_trusted_domains td on i.id = td.instance_id
|
||||
left join projections.security_policies2 s on i.id = s.instance_id
|
||||
left join projections.limits l on i.id = l.instance_id
|
||||
left join features f on i.id = f.instance_id;
|
||||
left join features f on i.id = f.instance_id
|
||||
where case when $2 = '' then true else td.domain = $2 end;
|
||||
|
142
internal/query/instance_trusted_domain.go
Normal file
142
internal/query/instance_trusted_domain.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"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/query/projection"
|
||||
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
type InstanceTrustedDomain struct {
|
||||
CreationDate time.Time
|
||||
ChangeDate time.Time
|
||||
Sequence uint64
|
||||
Domain string
|
||||
InstanceID string
|
||||
}
|
||||
|
||||
type InstanceTrustedDomains struct {
|
||||
SearchResponse
|
||||
Domains []*InstanceTrustedDomain
|
||||
}
|
||||
|
||||
type InstanceTrustedDomainSearchQueries struct {
|
||||
SearchRequest
|
||||
Queries []SearchQuery
|
||||
}
|
||||
|
||||
func (q *InstanceTrustedDomainSearchQueries) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
|
||||
query = q.SearchRequest.toQuery(query)
|
||||
for _, q := range q.Queries {
|
||||
query = q.toQuery(query)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func NewInstanceTrustedDomainDomainSearchQuery(method TextComparison, value string) (SearchQuery, error) {
|
||||
return NewTextQuery(InstanceTrustedDomainDomainCol, value, method)
|
||||
}
|
||||
|
||||
func (q *Queries) SearchInstanceTrustedDomains(ctx context.Context, queries *InstanceTrustedDomainSearchQueries) (domains *InstanceTrustedDomains, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
query, scan := prepareInstanceTrustedDomainsQuery(ctx, q.client)
|
||||
stmt, args, err := queries.toQuery(query).
|
||||
Where(sq.Eq{
|
||||
InstanceTrustedDomainInstanceIDCol.identifier(): authz.GetInstance(ctx).InstanceID(),
|
||||
}).ToSql()
|
||||
if err != nil {
|
||||
return nil, zerrors.ThrowInvalidArgument(err, "QUERY-SGrt4", "Errors.Query.SQLStatement")
|
||||
}
|
||||
|
||||
return q.queryInstanceTrustedDomains(ctx, stmt, scan, args...)
|
||||
}
|
||||
|
||||
func (q *Queries) queryInstanceTrustedDomains(ctx context.Context, stmt string, scan func(*sql.Rows) (*InstanceTrustedDomains, error), args ...interface{}) (domains *InstanceTrustedDomains, err error) {
|
||||
err = q.client.QueryContext(ctx, func(rows *sql.Rows) error {
|
||||
domains, err = scan(rows)
|
||||
return err
|
||||
}, stmt, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
domains.State, err = q.latestState(ctx, instanceDomainsTable)
|
||||
return domains, err
|
||||
}
|
||||
|
||||
func prepareInstanceTrustedDomainsQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Rows) (*InstanceTrustedDomains, error)) {
|
||||
return sq.Select(
|
||||
InstanceTrustedDomainCreationDateCol.identifier(),
|
||||
InstanceTrustedDomainChangeDateCol.identifier(),
|
||||
InstanceTrustedDomainSequenceCol.identifier(),
|
||||
InstanceTrustedDomainDomainCol.identifier(),
|
||||
InstanceTrustedDomainInstanceIDCol.identifier(),
|
||||
countColumn.identifier(),
|
||||
).From(instanceTrustedDomainsTable.identifier() + db.Timetravel(call.Took(ctx))).
|
||||
PlaceholderFormat(sq.Dollar),
|
||||
func(rows *sql.Rows) (*InstanceTrustedDomains, error) {
|
||||
domains := make([]*InstanceTrustedDomain, 0)
|
||||
var count uint64
|
||||
for rows.Next() {
|
||||
domain := new(InstanceTrustedDomain)
|
||||
err := rows.Scan(
|
||||
&domain.CreationDate,
|
||||
&domain.ChangeDate,
|
||||
&domain.Sequence,
|
||||
&domain.Domain,
|
||||
&domain.InstanceID,
|
||||
&count,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
domains = append(domains, domain)
|
||||
}
|
||||
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, zerrors.ThrowInternal(err, "QUERY-SDg4h", "Errors.Query.CloseRows")
|
||||
}
|
||||
|
||||
return &InstanceTrustedDomains{
|
||||
Domains: domains,
|
||||
SearchResponse: SearchResponse{
|
||||
Count: count,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
instanceTrustedDomainsTable = table{
|
||||
name: projection.InstanceTrustedDomainTable,
|
||||
instanceIDCol: projection.InstanceTrustedDomainInstanceIDCol,
|
||||
}
|
||||
InstanceTrustedDomainCreationDateCol = Column{
|
||||
name: projection.InstanceTrustedDomainCreationDateCol,
|
||||
table: instanceTrustedDomainsTable,
|
||||
}
|
||||
InstanceTrustedDomainChangeDateCol = Column{
|
||||
name: projection.InstanceTrustedDomainChangeDateCol,
|
||||
table: instanceTrustedDomainsTable,
|
||||
}
|
||||
InstanceTrustedDomainSequenceCol = Column{
|
||||
name: projection.InstanceTrustedDomainSequenceCol,
|
||||
table: instanceTrustedDomainsTable,
|
||||
}
|
||||
InstanceTrustedDomainDomainCol = Column{
|
||||
name: projection.InstanceTrustedDomainDomainCol,
|
||||
table: instanceTrustedDomainsTable,
|
||||
}
|
||||
InstanceTrustedDomainInstanceIDCol = Column{
|
||||
name: projection.InstanceTrustedDomainInstanceIDCol,
|
||||
table: instanceTrustedDomainsTable,
|
||||
}
|
||||
)
|
157
internal/query/instance_trusted_domain_test.go
Normal file
157
internal/query/instance_trusted_domain_test.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
prepareInstanceTrustedDomainsStmt = `SELECT projections.instance_trusted_domains.creation_date,` +
|
||||
` projections.instance_trusted_domains.change_date,` +
|
||||
` projections.instance_trusted_domains.sequence,` +
|
||||
` projections.instance_trusted_domains.domain,` +
|
||||
` projections.instance_trusted_domains.instance_id,` +
|
||||
` COUNT(*) OVER ()` +
|
||||
` FROM projections.instance_trusted_domains` +
|
||||
` AS OF SYSTEM TIME '-1 ms'`
|
||||
prepareInstanceTrustedDomainsCols = []string{
|
||||
"creation_date",
|
||||
"change_date",
|
||||
"sequence",
|
||||
"domain",
|
||||
"instance_id",
|
||||
"count",
|
||||
}
|
||||
)
|
||||
|
||||
func Test_InstanceTrustedDomainPrepares(t *testing.T) {
|
||||
type want struct {
|
||||
sqlExpectations sqlExpectation
|
||||
err checkErr
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
prepare interface{}
|
||||
want want
|
||||
object interface{}
|
||||
}{
|
||||
{
|
||||
name: "prepareInstanceTrustedDomainsQuery no result",
|
||||
prepare: prepareInstanceTrustedDomainsQuery,
|
||||
want: want{
|
||||
sqlExpectations: mockQueries(
|
||||
regexp.QuoteMeta(prepareInstanceTrustedDomainsStmt),
|
||||
nil,
|
||||
nil,
|
||||
),
|
||||
},
|
||||
object: &InstanceTrustedDomains{Domains: []*InstanceTrustedDomain{}},
|
||||
},
|
||||
{
|
||||
name: "prepareInstanceTrustedDomainsQuery one result",
|
||||
prepare: prepareInstanceTrustedDomainsQuery,
|
||||
want: want{
|
||||
sqlExpectations: mockQueries(
|
||||
regexp.QuoteMeta(prepareInstanceTrustedDomainsStmt),
|
||||
prepareInstanceTrustedDomainsCols,
|
||||
[][]driver.Value{
|
||||
{
|
||||
testNow,
|
||||
testNow,
|
||||
uint64(20211109),
|
||||
"zitadel.ch",
|
||||
"inst-id",
|
||||
},
|
||||
},
|
||||
),
|
||||
},
|
||||
object: &InstanceTrustedDomains{
|
||||
SearchResponse: SearchResponse{
|
||||
Count: 1,
|
||||
},
|
||||
Domains: []*InstanceTrustedDomain{
|
||||
{
|
||||
CreationDate: testNow,
|
||||
ChangeDate: testNow,
|
||||
Sequence: 20211109,
|
||||
Domain: "zitadel.ch",
|
||||
InstanceID: "inst-id",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "prepareInstanceTrustedDomainsQuery multiple result",
|
||||
prepare: prepareInstanceTrustedDomainsQuery,
|
||||
want: want{
|
||||
sqlExpectations: mockQueries(
|
||||
regexp.QuoteMeta(prepareInstanceTrustedDomainsStmt),
|
||||
prepareInstanceTrustedDomainsCols,
|
||||
[][]driver.Value{
|
||||
{
|
||||
testNow,
|
||||
testNow,
|
||||
uint64(20211109),
|
||||
"zitadel.ch",
|
||||
"inst-id",
|
||||
},
|
||||
{
|
||||
testNow,
|
||||
testNow,
|
||||
uint64(20211109),
|
||||
"zitadel.com",
|
||||
"inst-id",
|
||||
},
|
||||
},
|
||||
),
|
||||
},
|
||||
object: &InstanceTrustedDomains{
|
||||
SearchResponse: SearchResponse{
|
||||
Count: 2,
|
||||
},
|
||||
Domains: []*InstanceTrustedDomain{
|
||||
{
|
||||
CreationDate: testNow,
|
||||
ChangeDate: testNow,
|
||||
Sequence: 20211109,
|
||||
Domain: "zitadel.ch",
|
||||
InstanceID: "inst-id",
|
||||
},
|
||||
{
|
||||
CreationDate: testNow,
|
||||
ChangeDate: testNow,
|
||||
Sequence: 20211109,
|
||||
Domain: "zitadel.com",
|
||||
InstanceID: "inst-id",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "prepareInstanceTrustedDomainsQuery sql err",
|
||||
prepare: prepareInstanceTrustedDomainsQuery,
|
||||
want: want{
|
||||
sqlExpectations: mockQueryErr(
|
||||
regexp.QuoteMeta(prepareInstanceTrustedDomainsStmt),
|
||||
sql.ErrConnDone,
|
||||
),
|
||||
err: func(err error) (error, bool) {
|
||||
if !errors.Is(err, sql.ErrConnDone) {
|
||||
return fmt.Errorf("err should be sql.ErrConnDone got: %w", err), false
|
||||
}
|
||||
return nil, true
|
||||
},
|
||||
},
|
||||
object: (*Domains)(nil),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err, defaultPrepareArgs...)
|
||||
})
|
||||
}
|
||||
}
|
@@ -8,6 +8,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/api/authz"
|
||||
http_util "github.com/zitadel/zitadel/internal/api/http"
|
||||
"github.com/zitadel/zitadel/internal/api/ui/console/path"
|
||||
"github.com/zitadel/zitadel/internal/database"
|
||||
"github.com/zitadel/zitadel/internal/domain"
|
||||
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
||||
@@ -56,5 +58,9 @@ func (q *Queries) GetOIDCClientByID(ctx context.Context, clientID string, getKey
|
||||
if err != nil {
|
||||
return nil, zerrors.ThrowInternal(err, "QUERY-ieR7R", "Errors.Internal")
|
||||
}
|
||||
if authz.GetInstance(ctx).ConsoleClientID() == clientID {
|
||||
client.RedirectURIs = append(client.RedirectURIs, http_util.DomainContext(ctx).Origin()+path.RedirectPath)
|
||||
client.PostLogoutRedirectURIs = append(client.PostLogoutRedirectURIs, http_util.DomainContext(ctx).Origin()+path.PostLogoutPath)
|
||||
}
|
||||
return client, err
|
||||
}
|
||||
|
102
internal/query/projection/instance_trusted_domain.go
Normal file
102
internal/query/projection/instance_trusted_domain.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package projection
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
||||
"github.com/zitadel/zitadel/internal/repository/instance"
|
||||
)
|
||||
|
||||
const (
|
||||
InstanceTrustedDomainTable = "projections.instance_trusted_domains"
|
||||
|
||||
InstanceTrustedDomainInstanceIDCol = "instance_id"
|
||||
InstanceTrustedDomainCreationDateCol = "creation_date"
|
||||
InstanceTrustedDomainChangeDateCol = "change_date"
|
||||
InstanceTrustedDomainSequenceCol = "sequence"
|
||||
InstanceTrustedDomainDomainCol = "domain"
|
||||
)
|
||||
|
||||
type instanceTrustedDomainProjection struct{}
|
||||
|
||||
func newInstanceTrustedDomainProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
||||
return handler.NewHandler(ctx, &config, new(instanceTrustedDomainProjection))
|
||||
}
|
||||
|
||||
func (*instanceTrustedDomainProjection) Name() string {
|
||||
return InstanceTrustedDomainTable
|
||||
}
|
||||
|
||||
func (*instanceTrustedDomainProjection) Init() *old_handler.Check {
|
||||
return handler.NewTableCheck(
|
||||
handler.NewTable([]*handler.InitColumn{
|
||||
handler.NewColumn(InstanceTrustedDomainInstanceIDCol, handler.ColumnTypeText),
|
||||
handler.NewColumn(InstanceTrustedDomainCreationDateCol, handler.ColumnTypeTimestamp),
|
||||
handler.NewColumn(InstanceTrustedDomainChangeDateCol, handler.ColumnTypeTimestamp),
|
||||
handler.NewColumn(InstanceTrustedDomainSequenceCol, handler.ColumnTypeInt64),
|
||||
handler.NewColumn(InstanceTrustedDomainDomainCol, handler.ColumnTypeText),
|
||||
},
|
||||
handler.NewPrimaryKey(InstanceTrustedDomainInstanceIDCol, InstanceTrustedDomainDomainCol),
|
||||
handler.WithIndex(
|
||||
handler.NewIndex("instance_trusted_domain", []string{InstanceTrustedDomainDomainCol},
|
||||
handler.WithInclude(InstanceTrustedDomainCreationDateCol, InstanceTrustedDomainChangeDateCol, InstanceTrustedDomainSequenceCol),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func (p *instanceTrustedDomainProjection) Reducers() []handler.AggregateReducer {
|
||||
return []handler.AggregateReducer{
|
||||
{
|
||||
Aggregate: instance.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: instance.TrustedDomainAddedEventType,
|
||||
Reduce: p.reduceDomainAdded,
|
||||
},
|
||||
{
|
||||
Event: instance.TrustedDomainRemovedEventType,
|
||||
Reduce: p.reduceDomainRemoved,
|
||||
},
|
||||
{
|
||||
Event: instance.InstanceRemovedEventType,
|
||||
Reduce: reduceInstanceRemovedHelper(InstanceTrustedDomainInstanceIDCol),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *instanceTrustedDomainProjection) reduceDomainAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*instance.TrustedDomainAddedEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return handler.NewCreateStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(InstanceTrustedDomainCreationDateCol, e.CreatedAt()),
|
||||
handler.NewCol(InstanceTrustedDomainChangeDateCol, e.CreatedAt()),
|
||||
handler.NewCol(InstanceTrustedDomainSequenceCol, e.Sequence()),
|
||||
handler.NewCol(InstanceTrustedDomainDomainCol, e.Domain),
|
||||
handler.NewCol(InstanceTrustedDomainInstanceIDCol, e.Aggregate().ID),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *instanceTrustedDomainProjection) reduceDomainRemoved(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*instance.TrustedDomainRemovedEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return handler.NewDeleteStatement(
|
||||
e,
|
||||
[]handler.Condition{
|
||||
handler.NewCond(InstanceTrustedDomainDomainCol, e.Domain),
|
||||
handler.NewCond(InstanceTrustedDomainInstanceIDCol, e.Aggregate().ID),
|
||||
},
|
||||
), nil
|
||||
}
|
119
internal/query/projection/instance_trusted_domain_test.go
Normal file
119
internal/query/projection/instance_trusted_domain_test.go
Normal file
@@ -0,0 +1,119 @@
|
||||
package projection
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
||||
"github.com/zitadel/zitadel/internal/repository/instance"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
func TestInstanceTrustedDomainProjection_reduces(t *testing.T) {
|
||||
type args struct {
|
||||
event func(t *testing.T) eventstore.Event
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
reduce func(event eventstore.Event) (*handler.Statement, error)
|
||||
want wantReduce
|
||||
}{
|
||||
{
|
||||
name: "reduceDomainAdded",
|
||||
args: args{
|
||||
event: getEvent(
|
||||
testEvent(
|
||||
instance.TrustedDomainAddedEventType,
|
||||
instance.AggregateType,
|
||||
[]byte(`{"domain": "domain.new"}`),
|
||||
), eventstore.GenericEventMapper[instance.TrustedDomainAddedEvent]),
|
||||
},
|
||||
reduce: (&instanceTrustedDomainProjection{}).reduceDomainAdded,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("instance"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "INSERT INTO projections.instance_trusted_domains (creation_date, change_date, sequence, domain, instance_id) VALUES ($1, $2, $3, $4, $5)",
|
||||
expectedArgs: []interface{}{
|
||||
anyArg{},
|
||||
anyArg{},
|
||||
uint64(15),
|
||||
"domain.new",
|
||||
"agg-id",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceDomainRemoved",
|
||||
args: args{
|
||||
event: getEvent(
|
||||
testEvent(
|
||||
instance.TrustedDomainRemovedEventType,
|
||||
instance.AggregateType,
|
||||
[]byte(`{"domain": "domain.new"}`),
|
||||
), eventstore.GenericEventMapper[instance.TrustedDomainRemovedEvent]),
|
||||
},
|
||||
reduce: (&instanceTrustedDomainProjection{}).reduceDomainRemoved,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("instance"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "DELETE FROM projections.instance_trusted_domains WHERE (domain = $1) AND (instance_id = $2)",
|
||||
expectedArgs: []interface{}{
|
||||
"domain.new",
|
||||
"agg-id",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "instance reduceInstanceRemoved",
|
||||
args: args{
|
||||
event: getEvent(
|
||||
testEvent(
|
||||
instance.InstanceRemovedEventType,
|
||||
instance.AggregateType,
|
||||
nil,
|
||||
), instance.InstanceRemovedEventMapper),
|
||||
},
|
||||
reduce: reduceInstanceRemovedHelper(InstanceTrustedDomainInstanceIDCol),
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("instance"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "DELETE FROM projections.instance_trusted_domains WHERE (instance_id = $1)",
|
||||
expectedArgs: []interface{}{
|
||||
"agg-id",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
event := baseEvent(t)
|
||||
got, err := tt.reduce(event)
|
||||
if ok := zerrors.IsErrorInvalidArgument(err); !ok {
|
||||
t.Errorf("no wrong event mapping: %v, got: %v", err, got)
|
||||
}
|
||||
|
||||
event = tt.args.event(t)
|
||||
got, err = tt.reduce(event)
|
||||
assertReduce(t, got, err, InstanceTrustedDomainTable, tt.want)
|
||||
})
|
||||
}
|
||||
}
|
@@ -45,6 +45,7 @@ var (
|
||||
LoginNameProjection *handler.Handler
|
||||
OrgMemberProjection *handler.Handler
|
||||
InstanceDomainProjection *handler.Handler
|
||||
InstanceTrustedDomainProjection *handler.Handler
|
||||
InstanceMemberProjection *handler.Handler
|
||||
ProjectMemberProjection *handler.Handler
|
||||
ProjectGrantMemberProjection *handler.Handler
|
||||
@@ -132,6 +133,7 @@ func Create(ctx context.Context, sqlClient *database.DB, es handler.EventStore,
|
||||
LoginNameProjection = newLoginNameProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["login_names"]))
|
||||
OrgMemberProjection = newOrgMemberProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["org_members"]))
|
||||
InstanceDomainProjection = newInstanceDomainProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["instance_domains"]))
|
||||
InstanceTrustedDomainProjection = newInstanceTrustedDomainProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["instance_trusted_domains"]))
|
||||
InstanceMemberProjection = newInstanceMemberProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["iam_members"]))
|
||||
ProjectMemberProjection = newProjectMemberProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["project_members"]))
|
||||
ProjectGrantMemberProjection = newProjectGrantMemberProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["project_grant_members"]))
|
||||
@@ -260,6 +262,7 @@ func newProjectionsList() {
|
||||
LoginNameProjection,
|
||||
OrgMemberProjection,
|
||||
InstanceDomainProjection,
|
||||
InstanceTrustedDomainProjection,
|
||||
InstanceMemberProjection,
|
||||
ProjectMemberProjection,
|
||||
ProjectGrantMemberProjection,
|
||||
|
Reference in New Issue
Block a user