2022-03-28 08:05:09 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-05-03 13:58:38 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
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
2024-07-31 15:00:38 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/http"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-05 11:12:01 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/i18n"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/id"
|
2022-05-13 12:13:07 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/notification/channels/smtp"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2023-10-25 11:42:00 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/limits"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/project"
|
2023-02-15 01:52:11 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/quota"
|
2023-11-22 09:29:38 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/restrictions"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-03-28 08:05:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
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
2024-07-31 15:00:38 +00:00
|
|
|
zitadelProjectName = "ZITADEL"
|
|
|
|
mgmtAppName = "Management-API"
|
|
|
|
adminAppName = "Admin-API"
|
|
|
|
authAppName = "Auth-API"
|
|
|
|
consoleAppName = "Console"
|
2022-03-28 08:05:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InstanceSetup struct {
|
2024-08-14 14:18:14 +00:00
|
|
|
zitadel ZitadelConfig
|
|
|
|
InstanceName string
|
|
|
|
CustomDomain string
|
|
|
|
DefaultLanguage language.Tag
|
|
|
|
Org InstanceOrgSetup
|
|
|
|
SecretGenerators *SecretGenerators
|
|
|
|
WebKeys struct {
|
|
|
|
Type crypto.WebKeyConfigType
|
|
|
|
Config struct {
|
|
|
|
RSABits crypto.RSABits
|
|
|
|
RSAHasher crypto.RSAHasher
|
|
|
|
EllipticCurve crypto.EllipticCurve
|
|
|
|
}
|
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
PasswordComplexityPolicy struct {
|
|
|
|
MinLength uint64
|
|
|
|
HasLowercase bool
|
|
|
|
HasUppercase bool
|
|
|
|
HasNumber bool
|
|
|
|
HasSymbol bool
|
|
|
|
}
|
|
|
|
PasswordAgePolicy struct {
|
|
|
|
ExpireWarnDays uint64
|
|
|
|
MaxAgeDays uint64
|
|
|
|
}
|
|
|
|
DomainPolicy struct {
|
2022-05-16 14:08:47 +00:00
|
|
|
UserLoginMustBeDomain bool
|
|
|
|
ValidateOrgDomains bool
|
|
|
|
SMTPSenderAddressMatchesInstanceDomain bool
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
LoginPolicy struct {
|
|
|
|
AllowUsernamePassword bool
|
|
|
|
AllowRegister bool
|
|
|
|
AllowExternalIDP bool
|
|
|
|
ForceMFA bool
|
2023-07-20 04:06:16 +00:00
|
|
|
ForceMFALocalOnly bool
|
2022-03-28 08:05:09 +00:00
|
|
|
HidePasswordReset bool
|
2022-05-16 13:39:09 +00:00
|
|
|
IgnoreUnknownUsername bool
|
2022-10-06 11:30:14 +00:00
|
|
|
AllowDomainDiscovery bool
|
2022-10-17 19:19:15 +00:00
|
|
|
DisableLoginWithEmail bool
|
|
|
|
DisableLoginWithPhone bool
|
2022-03-28 08:05:09 +00:00
|
|
|
PasswordlessType domain.PasswordlessType
|
2022-05-16 13:39:09 +00:00
|
|
|
DefaultRedirectURI string
|
2022-03-28 08:05:09 +00:00
|
|
|
PasswordCheckLifetime time.Duration
|
|
|
|
ExternalLoginCheckLifetime time.Duration
|
|
|
|
MfaInitSkipLifetime time.Duration
|
|
|
|
SecondFactorCheckLifetime time.Duration
|
|
|
|
MultiFactorCheckLifetime time.Duration
|
|
|
|
}
|
2023-01-25 08:49:41 +00:00
|
|
|
NotificationPolicy struct {
|
|
|
|
PasswordChange bool
|
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
PrivacyPolicy struct {
|
2024-05-13 14:01:50 +00:00
|
|
|
TOSLink string
|
|
|
|
PrivacyLink string
|
|
|
|
HelpLink string
|
|
|
|
SupportEmail domain.EmailAddress
|
|
|
|
DocsLink string
|
|
|
|
CustomLink string
|
|
|
|
CustomLinkText string
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
2022-03-29 09:53:19 +00:00
|
|
|
LabelPolicy struct {
|
|
|
|
PrimaryColor string
|
|
|
|
BackgroundColor string
|
|
|
|
WarnColor string
|
|
|
|
FontColor string
|
|
|
|
PrimaryColorDark string
|
|
|
|
BackgroundColorDark string
|
|
|
|
WarnColorDark string
|
|
|
|
FontColorDark string
|
|
|
|
HideLoginNameSuffix bool
|
|
|
|
ErrorMsgPopup bool
|
|
|
|
DisableWatermark bool
|
2023-10-26 05:54:09 +00:00
|
|
|
ThemeMode domain.LabelPolicyThemeMode
|
2022-03-29 09:53:19 +00:00
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
LockoutPolicy struct {
|
2024-04-10 09:14:55 +00:00
|
|
|
MaxPasswordAttempts uint64
|
|
|
|
MaxOTPAttempts uint64
|
2022-03-28 08:05:09 +00:00
|
|
|
ShouldShowLockoutFailure bool
|
|
|
|
}
|
2022-05-13 12:13:07 +00:00
|
|
|
EmailTemplate []byte
|
|
|
|
MessageTexts []*domain.CustomMessageText
|
2024-09-12 04:27:29 +00:00
|
|
|
SMTPConfiguration *SMTPConfiguration
|
2023-12-05 11:12:01 +00:00
|
|
|
OIDCSettings *OIDCSettings
|
|
|
|
Quotas *SetQuotas
|
2024-02-28 08:55:54 +00:00
|
|
|
Features *InstanceFeatures
|
2023-12-05 11:12:01 +00:00
|
|
|
Limits *SetLimits
|
|
|
|
Restrictions *SetRestrictions
|
|
|
|
}
|
|
|
|
|
2024-09-12 04:27:29 +00:00
|
|
|
type SMTPConfiguration struct {
|
|
|
|
Description string
|
|
|
|
SMTP smtp.SMTP
|
|
|
|
Tls bool
|
|
|
|
From string
|
|
|
|
FromName string
|
|
|
|
ReplyToAddress string
|
|
|
|
}
|
|
|
|
|
2023-12-05 11:12:01 +00:00
|
|
|
type OIDCSettings struct {
|
|
|
|
AccessTokenLifetime time.Duration
|
|
|
|
IdTokenLifetime time.Duration
|
|
|
|
RefreshTokenIdleExpiration time.Duration
|
|
|
|
RefreshTokenExpiration time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type SetQuotas struct {
|
|
|
|
Items []*SetQuota
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
2023-08-23 08:04:29 +00:00
|
|
|
type SecretGenerators struct {
|
|
|
|
ClientSecret *crypto.GeneratorConfig
|
|
|
|
InitializeUserCode *crypto.GeneratorConfig
|
|
|
|
EmailVerificationCode *crypto.GeneratorConfig
|
|
|
|
PhoneVerificationCode *crypto.GeneratorConfig
|
|
|
|
PasswordVerificationCode *crypto.GeneratorConfig
|
|
|
|
PasswordlessInitCode *crypto.GeneratorConfig
|
|
|
|
DomainVerification *crypto.GeneratorConfig
|
|
|
|
OTPSMS *crypto.GeneratorConfig
|
|
|
|
OTPEmail *crypto.GeneratorConfig
|
2024-09-11 10:53:55 +00:00
|
|
|
InviteCode *crypto.GeneratorConfig
|
2023-08-23 08:04:29 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
type ZitadelConfig struct {
|
2024-05-23 10:28:46 +00:00
|
|
|
instanceID string
|
|
|
|
orgID string
|
2023-11-22 09:29:38 +00:00
|
|
|
projectID string
|
|
|
|
mgmtAppID string
|
|
|
|
adminAppID string
|
|
|
|
authAppID string
|
|
|
|
consoleAppID string
|
|
|
|
limitsID string
|
|
|
|
restrictionsID string
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
func (s *InstanceSetup) generateIDs(idGenerator id.Generator) (err error) {
|
2024-05-23 10:28:46 +00:00
|
|
|
s.zitadel.instanceID, err = idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.zitadel.orgID, err = idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
s.zitadel.projectID, err = idGenerator.Next()
|
2022-03-28 08:05:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
s.zitadel.mgmtAppID, err = idGenerator.Next()
|
2022-03-28 08:05:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
s.zitadel.adminAppID, err = idGenerator.Next()
|
2022-03-28 08:05:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
s.zitadel.authAppID, err = idGenerator.Next()
|
2022-03-28 08:05:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
s.zitadel.consoleAppID, err = idGenerator.Next()
|
2023-10-25 11:42:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.zitadel.limitsID, err = idGenerator.Next()
|
2023-11-22 09:29:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.zitadel.restrictionsID, err = idGenerator.Next()
|
2023-10-19 10:19:10 +00:00
|
|
|
return err
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 13:04:33 +00:00
|
|
|
func (c *Commands) SetUpInstance(ctx context.Context, setup *InstanceSetup) (string, string, *MachineKey, *domain.ObjectDetails, error) {
|
2024-05-23 10:28:46 +00:00
|
|
|
if err := setup.generateIDs(c.idGenerator); err != nil {
|
2022-12-09 13:04:33 +00:00
|
|
|
return "", "", nil, nil, err
|
2022-04-05 05:58:09 +00:00
|
|
|
}
|
2024-05-23 10:28:46 +00:00
|
|
|
ctx = contextWithInstanceSetupInfo(ctx, setup.zitadel.instanceID, setup.zitadel.projectID, setup.zitadel.consoleAppID, c.externalDomain)
|
2022-04-13 05:42:48 +00:00
|
|
|
|
2024-05-23 10:28:46 +00:00
|
|
|
validations, pat, machineKey, err := setUpInstance(ctx, c, setup)
|
2022-03-28 08:05:09 +00:00
|
|
|
if err != nil {
|
2022-12-09 13:04:33 +00:00
|
|
|
return "", "", nil, nil, err
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 10:28:46 +00:00
|
|
|
//nolint:staticcheck
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, validations...)
|
2022-03-28 08:05:09 +00:00
|
|
|
if err != nil {
|
2022-12-09 13:04:33 +00:00
|
|
|
return "", "", nil, nil, err
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 10:28:46 +00:00
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
2022-12-09 13:04:33 +00:00
|
|
|
return "", "", nil, nil, err
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 10:28:46 +00:00
|
|
|
var token string
|
|
|
|
if pat != nil {
|
|
|
|
token = pat.Token
|
|
|
|
}
|
|
|
|
|
|
|
|
return setup.zitadel.instanceID, token, machineKey, &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
|
|
|
EventDate: events[len(events)-1].CreatedAt(),
|
|
|
|
ResourceOwner: setup.zitadel.orgID,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
|
2024-05-23 10:28:46 +00:00
|
|
|
func contextWithInstanceSetupInfo(ctx context.Context, instanceID, projectID, consoleAppID, externalDomain string) context.Context {
|
|
|
|
return authz.WithConsole(
|
|
|
|
authz.SetCtxData(
|
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
2024-07-31 15:00:38 +00:00
|
|
|
http.WithRequestedHost(
|
2024-05-23 10:28:46 +00:00
|
|
|
authz.WithInstanceID(
|
|
|
|
ctx,
|
|
|
|
instanceID),
|
|
|
|
externalDomain,
|
|
|
|
),
|
|
|
|
authz.CtxData{ResourceOwner: instanceID},
|
|
|
|
),
|
|
|
|
projectID,
|
|
|
|
consoleAppID,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setUpInstance(ctx context.Context, c *Commands, setup *InstanceSetup) (validations []preparation.Validation, pat *PersonalAccessToken, machineKey *MachineKey, err error) {
|
|
|
|
instanceAgg := instance.NewAggregate(setup.zitadel.instanceID)
|
|
|
|
|
|
|
|
validations = setupInstanceElements(instanceAgg, setup)
|
|
|
|
|
|
|
|
// default organization on setup'd instance
|
|
|
|
pat, machineKey, err = setupDefaultOrg(ctx, c, &validations, instanceAgg, setup.Org.Name, setup.Org.Machine, setup.Org.Human, setup.zitadel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// domains
|
|
|
|
if err := setupGeneratedDomain(ctx, c, &validations, instanceAgg, setup.InstanceName); err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
setupCustomDomain(c, &validations, instanceAgg, setup.CustomDomain)
|
|
|
|
|
|
|
|
// optional setting if set
|
|
|
|
setupMessageTexts(&validations, setup.MessageTexts, instanceAgg)
|
|
|
|
if err := setupQuotas(c, &validations, setup.Quotas, setup.zitadel.instanceID); err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
setupSMTPSettings(c, &validations, setup.SMTPConfiguration, instanceAgg)
|
2024-08-14 14:18:14 +00:00
|
|
|
if err := setupWebKeys(c, &validations, setup.zitadel.instanceID, setup); err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
2024-05-23 10:28:46 +00:00
|
|
|
setupOIDCSettings(c, &validations, setup.OIDCSettings, instanceAgg)
|
|
|
|
setupFeatures(&validations, setup.Features, setup.zitadel.instanceID)
|
|
|
|
setupLimits(c, &validations, limits.NewAggregate(setup.zitadel.limitsID, setup.zitadel.instanceID), setup.Limits)
|
|
|
|
setupRestrictions(c, &validations, restrictions.NewAggregate(setup.zitadel.restrictionsID, setup.zitadel.instanceID, setup.zitadel.instanceID), setup.Restrictions)
|
|
|
|
|
|
|
|
return validations, pat, machineKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupInstanceElements(instanceAgg *instance.Aggregate, setup *InstanceSetup) []preparation.Validation {
|
|
|
|
return []preparation.Validation{
|
2022-05-24 09:28:17 +00:00
|
|
|
prepareAddInstance(instanceAgg, setup.InstanceName, setup.DefaultLanguage),
|
|
|
|
prepareAddSecretGeneratorConfig(instanceAgg, domain.SecretGeneratorTypeAppSecret, setup.SecretGenerators.ClientSecret),
|
|
|
|
prepareAddSecretGeneratorConfig(instanceAgg, domain.SecretGeneratorTypeInitCode, setup.SecretGenerators.InitializeUserCode),
|
|
|
|
prepareAddSecretGeneratorConfig(instanceAgg, domain.SecretGeneratorTypeVerifyEmailCode, setup.SecretGenerators.EmailVerificationCode),
|
|
|
|
prepareAddSecretGeneratorConfig(instanceAgg, domain.SecretGeneratorTypeVerifyPhoneCode, setup.SecretGenerators.PhoneVerificationCode),
|
|
|
|
prepareAddSecretGeneratorConfig(instanceAgg, domain.SecretGeneratorTypePasswordResetCode, setup.SecretGenerators.PasswordVerificationCode),
|
|
|
|
prepareAddSecretGeneratorConfig(instanceAgg, domain.SecretGeneratorTypePasswordlessInitCode, setup.SecretGenerators.PasswordlessInitCode),
|
|
|
|
prepareAddSecretGeneratorConfig(instanceAgg, domain.SecretGeneratorTypeVerifyDomain, setup.SecretGenerators.DomainVerification),
|
2023-07-26 11:00:41 +00:00
|
|
|
prepareAddSecretGeneratorConfig(instanceAgg, domain.SecretGeneratorTypeOTPSMS, setup.SecretGenerators.OTPSMS),
|
|
|
|
prepareAddSecretGeneratorConfig(instanceAgg, domain.SecretGeneratorTypeOTPEmail, setup.SecretGenerators.OTPEmail),
|
2022-05-24 09:28:17 +00:00
|
|
|
|
|
|
|
prepareAddDefaultPasswordComplexityPolicy(
|
2022-03-28 08:05:09 +00:00
|
|
|
instanceAgg,
|
|
|
|
setup.PasswordComplexityPolicy.MinLength,
|
|
|
|
setup.PasswordComplexityPolicy.HasLowercase,
|
|
|
|
setup.PasswordComplexityPolicy.HasUppercase,
|
|
|
|
setup.PasswordComplexityPolicy.HasNumber,
|
|
|
|
setup.PasswordComplexityPolicy.HasSymbol,
|
|
|
|
),
|
2022-05-24 09:28:17 +00:00
|
|
|
prepareAddDefaultPasswordAgePolicy(
|
2022-03-28 08:05:09 +00:00
|
|
|
instanceAgg,
|
|
|
|
setup.PasswordAgePolicy.ExpireWarnDays,
|
|
|
|
setup.PasswordAgePolicy.MaxAgeDays,
|
|
|
|
),
|
2022-05-24 09:28:17 +00:00
|
|
|
prepareAddDefaultDomainPolicy(
|
2022-03-28 08:05:09 +00:00
|
|
|
instanceAgg,
|
|
|
|
setup.DomainPolicy.UserLoginMustBeDomain,
|
2022-04-13 09:24:03 +00:00
|
|
|
setup.DomainPolicy.ValidateOrgDomains,
|
2022-05-16 14:08:47 +00:00
|
|
|
setup.DomainPolicy.SMTPSenderAddressMatchesInstanceDomain,
|
2022-03-28 08:05:09 +00:00
|
|
|
),
|
2022-05-24 09:28:17 +00:00
|
|
|
prepareAddDefaultLoginPolicy(
|
2022-03-28 08:05:09 +00:00
|
|
|
instanceAgg,
|
|
|
|
setup.LoginPolicy.AllowUsernamePassword,
|
|
|
|
setup.LoginPolicy.AllowRegister,
|
|
|
|
setup.LoginPolicy.AllowExternalIDP,
|
|
|
|
setup.LoginPolicy.ForceMFA,
|
2023-07-20 04:06:16 +00:00
|
|
|
setup.LoginPolicy.ForceMFALocalOnly,
|
2022-03-28 08:05:09 +00:00
|
|
|
setup.LoginPolicy.HidePasswordReset,
|
2022-05-16 13:39:09 +00:00
|
|
|
setup.LoginPolicy.IgnoreUnknownUsername,
|
2022-10-06 11:30:14 +00:00
|
|
|
setup.LoginPolicy.AllowDomainDiscovery,
|
2022-10-17 19:19:15 +00:00
|
|
|
setup.LoginPolicy.DisableLoginWithEmail,
|
|
|
|
setup.LoginPolicy.DisableLoginWithPhone,
|
2022-03-28 08:05:09 +00:00
|
|
|
setup.LoginPolicy.PasswordlessType,
|
2022-05-16 13:39:09 +00:00
|
|
|
setup.LoginPolicy.DefaultRedirectURI,
|
2022-03-28 08:05:09 +00:00
|
|
|
setup.LoginPolicy.PasswordCheckLifetime,
|
|
|
|
setup.LoginPolicy.ExternalLoginCheckLifetime,
|
|
|
|
setup.LoginPolicy.MfaInitSkipLifetime,
|
|
|
|
setup.LoginPolicy.SecondFactorCheckLifetime,
|
|
|
|
setup.LoginPolicy.MultiFactorCheckLifetime,
|
|
|
|
),
|
2023-07-28 05:39:30 +00:00
|
|
|
prepareAddSecondFactorToDefaultLoginPolicy(instanceAgg, domain.SecondFactorTypeTOTP),
|
2022-05-24 09:28:17 +00:00
|
|
|
prepareAddSecondFactorToDefaultLoginPolicy(instanceAgg, domain.SecondFactorTypeU2F),
|
2023-07-28 05:39:30 +00:00
|
|
|
/* TODO: incomment when usable
|
|
|
|
prepareAddSecondFactorToDefaultLoginPolicy(instanceAgg, domain.SecondFactorTypeOTPEmail),
|
|
|
|
prepareAddSecondFactorToDefaultLoginPolicy(instanceAgg, domain.SecondFactorTypeOTPSMS),
|
|
|
|
*/
|
2022-05-24 09:28:17 +00:00
|
|
|
prepareAddMultiFactorToDefaultLoginPolicy(instanceAgg, domain.MultiFactorTypeU2FWithPIN),
|
2022-03-28 08:05:09 +00:00
|
|
|
|
2024-05-13 14:01:50 +00:00
|
|
|
prepareAddDefaultPrivacyPolicy(instanceAgg, setup.PrivacyPolicy.TOSLink, setup.PrivacyPolicy.PrivacyLink, setup.PrivacyPolicy.HelpLink, setup.PrivacyPolicy.SupportEmail, setup.PrivacyPolicy.DocsLink, setup.PrivacyPolicy.CustomLink, setup.PrivacyPolicy.CustomLinkText),
|
2023-01-25 08:49:41 +00:00
|
|
|
prepareAddDefaultNotificationPolicy(instanceAgg, setup.NotificationPolicy.PasswordChange),
|
2024-04-10 09:14:55 +00:00
|
|
|
prepareAddDefaultLockoutPolicy(instanceAgg, setup.LockoutPolicy.MaxPasswordAttempts, setup.LockoutPolicy.MaxOTPAttempts, setup.LockoutPolicy.ShouldShowLockoutFailure),
|
2022-03-28 08:05:09 +00:00
|
|
|
|
2022-05-24 09:28:17 +00:00
|
|
|
prepareAddDefaultLabelPolicy(
|
2022-03-29 09:53:19 +00:00
|
|
|
instanceAgg,
|
|
|
|
setup.LabelPolicy.PrimaryColor,
|
|
|
|
setup.LabelPolicy.BackgroundColor,
|
|
|
|
setup.LabelPolicy.WarnColor,
|
|
|
|
setup.LabelPolicy.FontColor,
|
|
|
|
setup.LabelPolicy.PrimaryColorDark,
|
|
|
|
setup.LabelPolicy.BackgroundColorDark,
|
|
|
|
setup.LabelPolicy.WarnColorDark,
|
|
|
|
setup.LabelPolicy.FontColorDark,
|
|
|
|
setup.LabelPolicy.HideLoginNameSuffix,
|
|
|
|
setup.LabelPolicy.ErrorMsgPopup,
|
|
|
|
setup.LabelPolicy.DisableWatermark,
|
2023-10-26 05:54:09 +00:00
|
|
|
setup.LabelPolicy.ThemeMode,
|
2022-03-29 09:53:19 +00:00
|
|
|
),
|
2022-05-24 09:28:17 +00:00
|
|
|
prepareAddDefaultEmailTemplate(instanceAgg, setup.EmailTemplate),
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
2023-12-05 11:12:01 +00:00
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
|
2023-12-05 11:12:01 +00:00
|
|
|
func setupLimits(commands *Commands, validations *[]preparation.Validation, limitsAgg *limits.Aggregate, setLimits *SetLimits) {
|
|
|
|
if setLimits != nil {
|
|
|
|
*validations = append(*validations, commands.SetLimitsCommand(limitsAgg, &limitsWriteModel{}, setLimits))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupRestrictions(commands *Commands, validations *[]preparation.Validation, restrictionsAgg *restrictions.Aggregate, setRestrictions *SetRestrictions) {
|
|
|
|
if setRestrictions != nil {
|
|
|
|
*validations = append(*validations, commands.SetRestrictionsCommand(restrictionsAgg, &restrictionsWriteModel{}, setRestrictions))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupQuotas(commands *Commands, validations *[]preparation.Validation, setQuotas *SetQuotas, instanceID string) error {
|
|
|
|
if setQuotas == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
for _, q := range setQuotas.Items {
|
|
|
|
quotaId, err := commands.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*validations = append(*validations, commands.SetQuotaCommand(quota.NewAggregate(quotaId, instanceID), nil, true, q))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-28 08:55:54 +00:00
|
|
|
func setupFeatures(validations *[]preparation.Validation, features *InstanceFeatures, instanceID string) {
|
2024-05-23 10:28:46 +00:00
|
|
|
if features != nil {
|
|
|
|
*validations = append(*validations, prepareSetFeatures(instanceID, features))
|
|
|
|
}
|
2023-12-05 11:12:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 14:18:14 +00:00
|
|
|
func setupWebKeys(c *Commands, validations *[]preparation.Validation, instanceID string, setup *InstanceSetup) error {
|
|
|
|
var conf crypto.WebKeyConfig
|
|
|
|
switch setup.WebKeys.Type {
|
|
|
|
case crypto.WebKeyConfigTypeUnspecified:
|
|
|
|
return nil // config disabled, skip
|
|
|
|
case crypto.WebKeyConfigTypeRSA:
|
|
|
|
conf = &crypto.WebKeyRSAConfig{
|
|
|
|
Bits: setup.WebKeys.Config.RSABits,
|
|
|
|
Hasher: setup.WebKeys.Config.RSAHasher,
|
|
|
|
}
|
|
|
|
case crypto.WebKeyConfigTypeECDSA:
|
|
|
|
conf = &crypto.WebKeyECDSAConfig{
|
|
|
|
Curve: setup.WebKeys.Config.EllipticCurve,
|
|
|
|
}
|
|
|
|
case crypto.WebKeyConfigTypeED25519:
|
|
|
|
conf = &crypto.WebKeyED25519Config{}
|
|
|
|
default:
|
|
|
|
return zerrors.ThrowInternalf(nil, "COMMAND-sieX0", "Errors.Internal unknown web key type %q", setup.WebKeys.Type)
|
|
|
|
}
|
|
|
|
*validations = append(*validations, c.prepareGenerateInitialWebKeys(instanceID, conf))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-05 11:12:01 +00:00
|
|
|
func setupOIDCSettings(commands *Commands, validations *[]preparation.Validation, oidcSettings *OIDCSettings, instanceAgg *instance.Aggregate) {
|
|
|
|
if oidcSettings == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*validations = append(*validations,
|
|
|
|
commands.prepareAddOIDCSettings(
|
|
|
|
instanceAgg,
|
|
|
|
oidcSettings.AccessTokenLifetime,
|
|
|
|
oidcSettings.IdTokenLifetime,
|
|
|
|
oidcSettings.RefreshTokenIdleExpiration,
|
|
|
|
oidcSettings.RefreshTokenExpiration,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
2023-02-15 01:52:11 +00:00
|
|
|
|
2024-09-12 04:27:29 +00:00
|
|
|
func setupSMTPSettings(commands *Commands, validations *[]preparation.Validation, smtpConfig *SMTPConfiguration, instanceAgg *instance.Aggregate) {
|
2023-12-05 11:12:01 +00:00
|
|
|
if smtpConfig == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*validations = append(*validations,
|
2024-05-07 16:24:53 +00:00
|
|
|
commands.prepareAddAndActivateSMTPConfig(
|
2023-12-05 11:12:01 +00:00
|
|
|
instanceAgg,
|
2024-04-11 07:16:10 +00:00
|
|
|
smtpConfig.Description,
|
2023-12-05 11:12:01 +00:00
|
|
|
smtpConfig.From,
|
|
|
|
smtpConfig.FromName,
|
|
|
|
smtpConfig.ReplyToAddress,
|
|
|
|
smtpConfig.SMTP.Host,
|
|
|
|
smtpConfig.SMTP.User,
|
|
|
|
[]byte(smtpConfig.SMTP.Password),
|
|
|
|
smtpConfig.Tls,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupCustomDomain(commands *Commands, validations *[]preparation.Validation, instanceAgg *instance.Aggregate, customDomain string) {
|
|
|
|
if customDomain == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
*validations = append(*validations,
|
|
|
|
commands.addInstanceDomain(instanceAgg, customDomain, false),
|
|
|
|
setPrimaryInstanceDomain(instanceAgg, customDomain),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupGeneratedDomain(ctx context.Context, commands *Commands, validations *[]preparation.Validation, instanceAgg *instance.Aggregate, instanceName string) error {
|
|
|
|
addGeneratedDomain, err := commands.addGeneratedInstanceDomain(ctx, instanceAgg, instanceName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
2023-12-05 11:12:01 +00:00
|
|
|
*validations = append(*validations, addGeneratedDomain...)
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
|
2024-05-23 10:28:46 +00:00
|
|
|
func setupMinimalInterfaces(commands *Commands, validations *[]preparation.Validation, instanceAgg *instance.Aggregate, orgAgg *org.Aggregate, projectOwner string, ids ZitadelConfig) {
|
|
|
|
projectAgg := project.NewAggregate(ids.projectID, orgAgg.ID)
|
|
|
|
|
2023-12-05 11:12:01 +00:00
|
|
|
cnsl := &addOIDCApp{
|
2022-04-12 14:20:17 +00:00
|
|
|
AddApp: AddApp{
|
|
|
|
Aggregate: *projectAgg,
|
2023-12-05 11:12:01 +00:00
|
|
|
ID: ids.consoleAppID,
|
2022-04-12 14:20:17 +00:00
|
|
|
Name: consoleAppName,
|
|
|
|
},
|
|
|
|
Version: domain.OIDCVersionV1,
|
2022-04-25 09:16:36 +00:00
|
|
|
RedirectUris: []string{},
|
2022-04-12 14:20:17 +00:00
|
|
|
ResponseTypes: []domain.OIDCResponseType{domain.OIDCResponseTypeCode},
|
|
|
|
GrantTypes: []domain.OIDCGrantType{domain.OIDCGrantTypeAuthorizationCode},
|
|
|
|
ApplicationType: domain.OIDCApplicationTypeUserAgent,
|
|
|
|
AuthMethodType: domain.OIDCAuthMethodTypeNone,
|
2022-04-25 09:16:36 +00:00
|
|
|
PostLogoutRedirectUris: []string{},
|
2023-12-05 11:12:01 +00:00
|
|
|
DevMode: !commands.externalSecure,
|
2022-04-12 14:20:17 +00:00
|
|
|
AccessTokenType: domain.OIDCTokenTypeBearer,
|
|
|
|
AccessTokenRoleAssertion: false,
|
|
|
|
IDTokenRoleAssertion: false,
|
|
|
|
IDTokenUserinfoAssertion: false,
|
|
|
|
ClockSkew: 0,
|
|
|
|
}
|
2024-05-23 10:28:46 +00:00
|
|
|
|
2023-12-05 11:12:01 +00:00
|
|
|
*validations = append(*validations,
|
2024-05-23 10:28:46 +00:00
|
|
|
AddProjectCommand(projectAgg, zitadelProjectName, projectOwner, false, false, false, domain.PrivateLabelingSettingUnspecified),
|
2022-03-28 08:05:09 +00:00
|
|
|
SetIAMProject(instanceAgg, projectAgg.ID),
|
|
|
|
|
2023-12-05 11:12:01 +00:00
|
|
|
commands.AddAPIAppCommand(
|
2022-04-12 14:20:17 +00:00
|
|
|
&addAPIApp{
|
|
|
|
AddApp: AddApp{
|
|
|
|
Aggregate: *projectAgg,
|
2023-12-05 11:12:01 +00:00
|
|
|
ID: ids.mgmtAppID,
|
2022-04-12 14:20:17 +00:00
|
|
|
Name: mgmtAppName,
|
|
|
|
},
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypePrivateKeyJWT,
|
|
|
|
},
|
2022-03-28 08:05:09 +00:00
|
|
|
),
|
|
|
|
|
2023-12-05 11:12:01 +00:00
|
|
|
commands.AddAPIAppCommand(
|
2022-04-12 14:20:17 +00:00
|
|
|
&addAPIApp{
|
|
|
|
AddApp: AddApp{
|
|
|
|
Aggregate: *projectAgg,
|
2023-12-05 11:12:01 +00:00
|
|
|
ID: ids.adminAppID,
|
2022-04-12 14:20:17 +00:00
|
|
|
Name: adminAppName,
|
|
|
|
},
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypePrivateKeyJWT,
|
|
|
|
},
|
2022-03-28 08:05:09 +00:00
|
|
|
),
|
|
|
|
|
2023-12-05 11:12:01 +00:00
|
|
|
commands.AddAPIAppCommand(
|
2022-04-12 14:20:17 +00:00
|
|
|
&addAPIApp{
|
|
|
|
AddApp: AddApp{
|
|
|
|
Aggregate: *projectAgg,
|
2023-12-05 11:12:01 +00:00
|
|
|
ID: ids.authAppID,
|
2022-04-12 14:20:17 +00:00
|
|
|
Name: authAppName,
|
|
|
|
},
|
|
|
|
AuthMethodType: domain.APIAuthMethodTypePrivateKeyJWT,
|
|
|
|
},
|
2022-03-28 08:05:09 +00:00
|
|
|
),
|
|
|
|
|
2024-04-05 09:35:49 +00:00
|
|
|
commands.AddOIDCAppCommand(cnsl),
|
2023-12-05 11:12:01 +00:00
|
|
|
SetIAMConsoleID(instanceAgg, &cnsl.ClientID, &ids.consoleAppID),
|
2022-04-25 14:36:10 +00:00
|
|
|
)
|
2023-12-05 11:12:01 +00:00
|
|
|
}
|
2022-05-30 15:39:18 +00:00
|
|
|
|
2024-05-23 10:28:46 +00:00
|
|
|
func setupDefaultOrg(ctx context.Context,
|
|
|
|
commands *Commands,
|
|
|
|
validations *[]preparation.Validation,
|
|
|
|
instanceAgg *instance.Aggregate,
|
|
|
|
name string,
|
|
|
|
machine *AddMachine,
|
|
|
|
human *AddHuman,
|
|
|
|
ids ZitadelConfig,
|
|
|
|
) (pat *PersonalAccessToken, machineKey *MachineKey, err error) {
|
|
|
|
orgAgg := org.NewAggregate(ids.orgID)
|
|
|
|
|
|
|
|
*validations = append(
|
|
|
|
*validations,
|
|
|
|
AddOrgCommand(ctx, orgAgg, name),
|
|
|
|
commands.prepareSetDefaultOrg(instanceAgg, ids.orgID),
|
|
|
|
)
|
|
|
|
|
|
|
|
projectOwner, pat, machineKey, err := setupAdmins(commands, validations, instanceAgg, orgAgg, machine, human)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
setupMinimalInterfaces(commands, validations, instanceAgg, orgAgg, projectOwner, ids)
|
|
|
|
return pat, machineKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupAdmins(commands *Commands,
|
|
|
|
validations *[]preparation.Validation,
|
|
|
|
instanceAgg *instance.Aggregate,
|
|
|
|
orgAgg *org.Aggregate,
|
|
|
|
machine *AddMachine,
|
|
|
|
human *AddHuman,
|
|
|
|
) (owner string, pat *PersonalAccessToken, machineKey *MachineKey, err error) {
|
|
|
|
if human == nil && machine == nil {
|
|
|
|
return "", nil, nil, zerrors.ThrowInvalidArgument(nil, "INSTANCE-z1yi2q2ot7", "Error.Instance.NoAdmin")
|
|
|
|
}
|
|
|
|
|
2023-12-05 11:12:01 +00:00
|
|
|
if machine != nil && machine.Machine != nil && !machine.Machine.IsZero() {
|
2024-05-23 10:28:46 +00:00
|
|
|
machineUserID, err := commands.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, nil, err
|
2023-09-29 08:21:32 +00:00
|
|
|
}
|
2024-05-23 10:28:46 +00:00
|
|
|
owner = machineUserID
|
|
|
|
|
|
|
|
pat, machineKey, err = setupMachineAdmin(commands, validations, machine, orgAgg.ID, machineUserID)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, nil, err
|
2023-12-05 11:12:01 +00:00
|
|
|
}
|
2024-05-23 10:28:46 +00:00
|
|
|
|
|
|
|
setupAdminMembers(commands, validations, instanceAgg, orgAgg, machineUserID)
|
|
|
|
}
|
|
|
|
if human != nil {
|
|
|
|
humanUserID, err := commands.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, nil, err
|
|
|
|
}
|
|
|
|
owner = humanUserID
|
|
|
|
human.ID = humanUserID
|
|
|
|
|
2023-12-05 11:12:01 +00:00
|
|
|
*validations = append(*validations,
|
2024-05-23 10:28:46 +00:00
|
|
|
commands.AddHumanCommand(human, orgAgg.ID, commands.userPasswordHasher, commands.userEncryption, true),
|
2023-12-05 11:12:01 +00:00
|
|
|
)
|
2024-05-23 10:28:46 +00:00
|
|
|
|
|
|
|
setupAdminMembers(commands, validations, instanceAgg, orgAgg, humanUserID)
|
|
|
|
}
|
|
|
|
return owner, pat, machineKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupMachineAdmin(commands *Commands, validations *[]preparation.Validation, machine *AddMachine, orgID, userID string) (pat *PersonalAccessToken, machineKey *MachineKey, err error) {
|
|
|
|
*validations = append(*validations,
|
|
|
|
AddMachineCommand(user.NewAggregate(userID, orgID), machine.Machine),
|
|
|
|
)
|
|
|
|
if machine.Pat != nil {
|
|
|
|
pat = NewPersonalAccessToken(orgID, userID, machine.Pat.ExpirationDate, machine.Pat.Scopes, domain.UserTypeMachine)
|
|
|
|
pat.TokenID, err = commands.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
*validations = append(*validations, prepareAddPersonalAccessToken(pat, commands.keyAlgorithm))
|
|
|
|
}
|
|
|
|
if machine.MachineKey != nil {
|
|
|
|
machineKey = NewMachineKey(orgID, userID, machine.MachineKey.ExpirationDate, machine.MachineKey.Type)
|
|
|
|
machineKey.KeyID, err = commands.idGenerator.Next()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
*validations = append(*validations, prepareAddUserMachineKey(machineKey, commands.machineKeySize))
|
2023-09-29 08:21:32 +00:00
|
|
|
}
|
2023-12-05 11:12:01 +00:00
|
|
|
return pat, machineKey, nil
|
|
|
|
}
|
2023-09-29 08:21:32 +00:00
|
|
|
|
2024-05-23 10:28:46 +00:00
|
|
|
func setupAdminMembers(commands *Commands, validations *[]preparation.Validation, instanceAgg *instance.Aggregate, orgAgg *org.Aggregate, userID string) {
|
|
|
|
*validations = append(*validations,
|
|
|
|
commands.AddOrgMemberCommand(orgAgg, userID, domain.RoleOrgOwner),
|
|
|
|
commands.AddInstanceMemberCommand(instanceAgg, userID, domain.RoleIAMOwner),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-12-05 11:12:01 +00:00
|
|
|
func setupMessageTexts(validations *[]preparation.Validation, setupMessageTexts []*domain.CustomMessageText, instanceAgg *instance.Aggregate) {
|
|
|
|
for _, msg := range setupMessageTexts {
|
|
|
|
*validations = append(*validations, prepareSetInstanceCustomMessageTexts(instanceAgg, msg))
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 06:58:50 +00:00
|
|
|
func (c *Commands) UpdateInstance(ctx context.Context, name string) (*domain.ObjectDetails, error) {
|
|
|
|
instanceAgg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
|
|
|
|
validation := c.prepareUpdateInstance(instanceAgg, name)
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, validation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
return pushedEventsToObjectDetails(events), nil
|
2022-09-27 06:58:50 +00:00
|
|
|
}
|
|
|
|
|
2022-05-03 13:58:38 +00:00
|
|
|
func (c *Commands) SetDefaultLanguage(ctx context.Context, defaultLanguage language.Tag) (*domain.ObjectDetails, error) {
|
|
|
|
instanceAgg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
|
|
|
|
validation := c.prepareSetDefaultLanguage(instanceAgg, defaultLanguage)
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, validation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
return pushedEventsToObjectDetails(events), nil
|
2022-05-03 13:58:38 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 12:30:39 +00:00
|
|
|
func (c *Commands) SetDefaultOrg(ctx context.Context, orgID string) (*domain.ObjectDetails, error) {
|
|
|
|
instanceAgg := instance.NewAggregate(authz.GetInstance(ctx).InstanceID())
|
|
|
|
validation := c.prepareSetDefaultOrg(instanceAgg, orgID)
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, validation)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-11-30 16:01:17 +00:00
|
|
|
return pushedEventsToObjectDetails(events), nil
|
2022-06-03 12:30:39 +00:00
|
|
|
}
|
|
|
|
|
2022-07-20 09:20:49 +00:00
|
|
|
func (c *Commands) ChangeSystemConfig(ctx context.Context, externalDomain string, externalPort uint16, externalSecure bool) error {
|
|
|
|
validations, err := c.prepareChangeSystemConfig(externalDomain, externalPort, externalSecure)(ctx, c.eventstore.Filter)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for instanceID, instanceValidations := range validations {
|
|
|
|
if len(instanceValidations.Validations) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ctx := authz.WithConsole(authz.WithInstanceID(ctx, instanceID), instanceValidations.ProjectID, instanceValidations.ConsoleAppID)
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, instanceValidations.Validations...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-07 14:15:19 +00:00
|
|
|
if len(cmds) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-07-20 09:20:49 +00:00
|
|
|
_, err = c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) prepareChangeSystemConfig(externalDomain string, externalPort uint16, externalSecure bool) func(ctx context.Context, filter preparation.FilterToQueryReducer) (map[string]*SystemConfigChangesValidation, error) {
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) (map[string]*SystemConfigChangesValidation, error) {
|
|
|
|
if externalDomain == "" || externalPort == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
writeModel, err := getSystemConfigWriteModel(ctx, filter, externalDomain, c.externalDomain, externalPort, c.externalPort, externalSecure, c.externalSecure)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel.NewChangedEvents(c), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 09:28:17 +00:00
|
|
|
func prepareAddInstance(a *instance.Aggregate, instanceName string, defaultLanguage language.Tag) preparation.Validation {
|
2022-04-21 10:37:39 +00:00
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
return []eventstore.Command{
|
|
|
|
instance.NewInstanceAddedEvent(ctx, &a.Aggregate, instanceName),
|
2022-05-03 13:58:38 +00:00
|
|
|
instance.NewDefaultLanguageSetEvent(ctx, &a.Aggregate, defaultLanguage),
|
2022-04-21 10:37:39 +00:00
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 06:58:50 +00:00
|
|
|
// SetIAMProject defines the command to set the id of the IAM project onto the instance
|
2022-03-28 08:05:09 +00:00
|
|
|
func SetIAMProject(a *instance.Aggregate, projectID string) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
return []eventstore.Command{
|
|
|
|
instance.NewIAMProjectSetEvent(ctx, &a.Aggregate, projectID),
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
2022-03-29 09:53:19 +00:00
|
|
|
|
2022-09-27 06:58:50 +00:00
|
|
|
// SetIAMConsoleID defines the command to set the clientID of the Console App onto the instance
|
2022-04-14 12:19:18 +00:00
|
|
|
func SetIAMConsoleID(a *instance.Aggregate, clientID, appID *string) preparation.Validation {
|
2022-03-29 09:53:19 +00:00
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
return []eventstore.Command{
|
2022-04-14 12:19:18 +00:00
|
|
|
instance.NewIAMConsoleSetEvent(ctx, &a.Aggregate, clientID, appID),
|
2022-03-29 09:53:19 +00:00
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
2022-04-12 14:20:17 +00:00
|
|
|
|
2022-06-03 12:30:39 +00:00
|
|
|
func (c *Commands) prepareSetDefaultOrg(a *instance.Aggregate, orgID string) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
if orgID == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "INST-SWffe", "Errors.Invalid.Argument")
|
2022-06-03 12:30:39 +00:00
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
writeModel, err := getInstanceWriteModel(ctx, filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if writeModel.DefaultOrgID == orgID {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "INST-SDfw2", "Errors.Instance.NotChanged")
|
2022-06-03 12:30:39 +00:00
|
|
|
}
|
|
|
|
if exists, err := ExistsOrg(ctx, filter, orgID); err != nil || !exists {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(err, "INSTA-Wfe21", "Errors.Org.NotFound")
|
2022-06-03 12:30:39 +00:00
|
|
|
}
|
|
|
|
return []eventstore.Command{instance.NewDefaultOrgSetEventEvent(ctx, &a.Aggregate, orgID)}, nil
|
|
|
|
}, nil
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) setIAMProject(ctx context.Context, iamAgg *eventstore.Aggregate, iamWriteModel *InstanceWriteModel, projectID string) (eventstore.Command, error) {
|
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, iamWriteModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if iamWriteModel.ProjectID != "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "IAM-EGbw2", "Errors.IAM.IAMProjectAlreadySet")
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|
|
|
|
return instance.NewIAMProjectSetEvent(ctx, iamAgg, projectID), nil
|
|
|
|
}
|
2022-05-03 13:58:38 +00:00
|
|
|
|
2022-09-27 06:58:50 +00:00
|
|
|
func (c *Commands) prepareUpdateInstance(a *instance.Aggregate, name string) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
if name == "" {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgument(nil, "INST-092mid", "Errors.Invalid.Argument")
|
2022-09-27 06:58:50 +00:00
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
writeModel, err := getInstanceWriteModel(ctx, filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-10-26 13:06:48 +00:00
|
|
|
if !writeModel.State.Exists() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(nil, "INST-nuso2m", "Errors.Instance.NotFound")
|
2022-09-27 06:58:50 +00:00
|
|
|
}
|
|
|
|
if writeModel.Name == name {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "INST-alpxism", "Errors.Instance.NotChanged")
|
2022-09-27 06:58:50 +00:00
|
|
|
}
|
|
|
|
return []eventstore.Command{instance.NewInstanceChangedEvent(ctx, &a.Aggregate, name)}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-03 13:58:38 +00:00
|
|
|
func (c *Commands) prepareSetDefaultLanguage(a *instance.Aggregate, defaultLanguage language.Tag) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
2023-12-05 11:12:01 +00:00
|
|
|
if err := domain.LanguageIsDefined(defaultLanguage); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := domain.LanguagesAreSupported(i18n.SupportedLanguages(), defaultLanguage); err != nil {
|
|
|
|
return nil, err
|
2022-05-03 13:58:38 +00:00
|
|
|
}
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
writeModel, err := getInstanceWriteModel(ctx, filter)
|
2023-12-05 11:12:01 +00:00
|
|
|
if writeModel.DefaultLanguage == defaultLanguage {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "INST-DS3rq", "Errors.Instance.NotChanged")
|
2023-12-05 11:12:01 +00:00
|
|
|
}
|
|
|
|
instanceID := authz.GetInstance(ctx).InstanceID()
|
|
|
|
restrictionsWM, err := c.getRestrictionsWriteModel(ctx, instanceID, instanceID)
|
2022-05-03 13:58:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-05 11:12:01 +00:00
|
|
|
if err := domain.LanguageIsAllowed(false, restrictionsWM.allowedLanguages, defaultLanguage); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-05-03 13:58:38 +00:00
|
|
|
}
|
|
|
|
return []eventstore.Command{instance.NewDefaultLanguageSetEvent(ctx, &a.Aggregate, defaultLanguage)}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInstanceWriteModel(ctx context.Context, filter preparation.FilterToQueryReducer) (*InstanceWriteModel, error) {
|
|
|
|
writeModel := NewInstanceWriteModel(authz.GetInstance(ctx).InstanceID())
|
|
|
|
events, err := filter(ctx, writeModel.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
|
|
|
return writeModel, nil
|
|
|
|
}
|
|
|
|
writeModel.AppendEvents(events...)
|
|
|
|
err = writeModel.Reduce()
|
|
|
|
return writeModel, err
|
|
|
|
}
|
2022-07-20 09:20:49 +00:00
|
|
|
|
|
|
|
func getSystemConfigWriteModel(ctx context.Context, filter preparation.FilterToQueryReducer, externalDomain, newExternalDomain string, externalPort, newExternalPort uint16, externalSecure, newExternalSecure bool) (*SystemConfigWriteModel, error) {
|
|
|
|
writeModel := NewSystemConfigWriteModel(externalDomain, newExternalDomain, externalPort, newExternalPort, externalSecure, newExternalSecure)
|
|
|
|
events, err := filter(ctx, writeModel.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
|
|
|
return writeModel, nil
|
|
|
|
}
|
|
|
|
writeModel.AppendEvents(events...)
|
|
|
|
err = writeModel.Reduce()
|
|
|
|
return writeModel, err
|
|
|
|
}
|
2022-10-20 12:36:52 +00:00
|
|
|
|
|
|
|
func (c *Commands) RemoveInstance(ctx context.Context, id string) (*domain.ObjectDetails, error) {
|
|
|
|
instanceAgg := instance.NewAggregate(id)
|
|
|
|
cmds, err := preparation.PrepareCommands(ctx, c.eventstore.Filter, c.prepareRemoveInstance(instanceAgg))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
events, err := c.eventstore.Push(ctx, cmds...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &domain.ObjectDetails{
|
|
|
|
Sequence: events[len(events)-1].Sequence(),
|
2023-10-19 10:19:10 +00:00
|
|
|
EventDate: events[len(events)-1].CreatedAt(),
|
2022-10-20 12:36:52 +00:00
|
|
|
ResourceOwner: events[len(events)-1].Aggregate().InstanceID,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) prepareRemoveInstance(a *instance.Aggregate) preparation.Validation {
|
|
|
|
return func() (preparation.CreateCommands, error) {
|
|
|
|
return func(ctx context.Context, filter preparation.FilterToQueryReducer) ([]eventstore.Command, error) {
|
|
|
|
writeModel, err := c.getInstanceWriteModelByID(ctx, a.ID)
|
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(err, "COMMA-pax9m3", "Errors.Instance.NotFound")
|
2022-10-20 12:36:52 +00:00
|
|
|
}
|
2022-10-26 13:06:48 +00:00
|
|
|
if !writeModel.State.Exists() {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowNotFound(err, "COMMA-AE3GS", "Errors.Instance.NotFound")
|
2022-10-20 12:36:52 +00:00
|
|
|
}
|
2022-10-26 13:06:48 +00:00
|
|
|
return []eventstore.Command{instance.NewInstanceRemovedEvent(ctx,
|
|
|
|
&a.Aggregate,
|
|
|
|
writeModel.Name,
|
|
|
|
writeModel.Domains)},
|
|
|
|
nil
|
2022-10-20 12:36:52 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) getInstanceWriteModelByID(ctx context.Context, orgID string) (*InstanceWriteModel, error) {
|
|
|
|
instanceWriteModel := NewInstanceWriteModel(orgID)
|
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, instanceWriteModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return instanceWriteModel, nil
|
|
|
|
}
|