mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 05:52:51 +00:00
# Which Problems Are Solved This PR implements the endpoints listed in https://github.com/zitadel/zitadel/issues/10444 . # How the Problems Are Solved The implementation follows the same pattern as the Instance one. The `Add` and `Remove` are implementing the `Commander` interface, while `List` Domains endpoints are implementing the `Querier` interace. The PR adds also some forgotten calls to the relational logic for the Instance API. The commits are identifiable by the label `instance:` prepended to them. This is the complete list: bafce01b0 , 6fe7257ab , e27bc9c34 , 8133ede73 , fde667953 , a3e1135ed , ee1a6f9c1 # Additional Context - Closes #10444 --------- Co-authored-by: Elio Bischof <[email protected]> Co-authored-by: Lebogang Phoshoko <[email protected]> Co-authored-by: Wim Van Laer <[email protected]> Co-authored-by: Max Peintner <[email protected]> Co-authored-by: Gayathri Vijayan <[email protected]>
124 lines
3.8 KiB
Go
124 lines
3.8 KiB
Go
package domain
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/muhlemmer/gu"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
var allowedDomainCharacters = regexp.MustCompile(`^[a-zA-Z0-9\\.\\-]+$`)
|
|
|
|
const maxDomainNameLength = 253
|
|
|
|
type AddInstanceDomainCommand struct {
|
|
InstanceID string `json:"instance_id"`
|
|
DomainName string `json:"domain_name"`
|
|
DType DomainType `json:"domain_type"`
|
|
}
|
|
|
|
// RequiresTransaction implements [Transactional].
|
|
func (a *AddInstanceDomainCommand) RequiresTransaction() {}
|
|
|
|
func NewAddInstanceDomainCommand(instanceID, domainName string, domainType DomainType) *AddInstanceDomainCommand {
|
|
return &AddInstanceDomainCommand{
|
|
InstanceID: instanceID,
|
|
DomainName: domainName,
|
|
DType: domainType,
|
|
}
|
|
}
|
|
|
|
// Events implements [Commander].
|
|
func (a *AddInstanceDomainCommand) Events(ctx context.Context, _ *InvokeOpts) ([]eventstore.Command, error) {
|
|
var event eventstore.Command
|
|
switch a.DType {
|
|
case DomainTypeCustom:
|
|
event = instance.NewDomainAddedEvent(ctx, &instance.NewAggregate(a.InstanceID).Aggregate, a.DomainName, false)
|
|
case DomainTypeTrusted:
|
|
event = instance.NewTrustedDomainAddedEvent(ctx, &instance.NewAggregate(a.InstanceID).Aggregate, a.DomainName)
|
|
default:
|
|
return nil, nil
|
|
}
|
|
|
|
return []eventstore.Command{
|
|
event,
|
|
}, nil
|
|
}
|
|
|
|
// Execute implements [Commander].
|
|
func (a *AddInstanceDomainCommand) Execute(ctx context.Context, opts *InvokeOpts) (err error) {
|
|
instanceDomainRepo := opts.instanceDomainRepo
|
|
|
|
var isPrimary, isGenerated *bool
|
|
if a.DType == DomainTypeCustom {
|
|
isPrimary, isGenerated = gu.Ptr(false), gu.Ptr(false)
|
|
}
|
|
|
|
err = instanceDomainRepo.Add(ctx, opts.DB(), &AddInstanceDomain{
|
|
InstanceID: a.InstanceID,
|
|
Domain: a.DomainName,
|
|
IsPrimary: isPrimary,
|
|
IsGenerated: isGenerated,
|
|
Type: a.DType,
|
|
})
|
|
if err != nil {
|
|
return zerrors.ThrowInternal(err, "DOM-uSCVn3", "Errors.Instance.Domain.Add")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// String implements [Commander].
|
|
func (a *AddInstanceDomainCommand) String() string {
|
|
return "AddInstanceDomainCommand"
|
|
}
|
|
|
|
// Validate implements [Commander].
|
|
func (a *AddInstanceDomainCommand) Validate(ctx context.Context, opts *InvokeOpts) (err error) {
|
|
if a.DomainName = strings.TrimSpace(a.DomainName); a.DomainName == "" || len(a.DomainName) > maxDomainNameLength {
|
|
return zerrors.ThrowInvalidArgument(nil, "DOM-jieuM8", "Errors.Invalid.Argument")
|
|
}
|
|
|
|
if a.InstanceID = strings.TrimSpace(a.InstanceID); a.InstanceID == "" {
|
|
return zerrors.ThrowInvalidArgument(nil, "DOM-YaUBp5", "Errors.Invalid.Argument")
|
|
}
|
|
|
|
if !allowedDomainCharacters.MatchString(a.DomainName) {
|
|
return zerrors.ThrowInvalidArgument(nil, "DOM-98VcSQ", "Errors.Instance.Domain.InvalidCharacter")
|
|
}
|
|
|
|
// TODO(IAM-Marco): Do we want to restrict to the instance in context?
|
|
if a.InstanceID != authz.GetInstance(ctx).InstanceID() {
|
|
return zerrors.ThrowInvalidArgument(nil, "DOM-x01cai", "Errors.Invalid.Argument")
|
|
}
|
|
|
|
if authZErr := opts.Permissions.CheckInstancePermission(ctx, DomainWritePermission); authZErr != nil {
|
|
return zerrors.ThrowPermissionDenied(authZErr, "DOM-c83vPX", "Errors.PermissionDenied")
|
|
}
|
|
|
|
domainRepo := opts.instanceDomainRepo
|
|
_, err = domainRepo.Get(ctx, opts.DB(), database.WithCondition(domainRepo.DomainCondition(database.TextOperationEqual, a.DomainName)))
|
|
if err == nil {
|
|
return zerrors.ThrowAlreadyExists(nil, "DOM-CvQ8tf", "Errors.Instance.Domain.AlreadyExists")
|
|
}
|
|
|
|
if !errors.Is(err, &database.NoRowFoundError{}) {
|
|
return zerrors.ThrowInternal(err, "DOM-LrTy2z", "Errors.Instance.Domain.Get")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
var (
|
|
_ Commander = (*AddInstanceDomainCommand)(nil)
|
|
_ Transactional = (*AddInstanceDomainCommand)(nil)
|
|
)
|