2022-04-06 06:13:40 +00:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-12 14:20:17 +00:00
|
|
|
"fmt"
|
2022-12-09 13:04:33 +00:00
|
|
|
"os"
|
2022-04-28 08:30:41 +00:00
|
|
|
"strings"
|
2022-04-06 06:13:40 +00:00
|
|
|
|
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"
|
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
|
|
|
"github.com/zitadel/zitadel/internal/config/systemdefaults"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
crypto_db "github.com/zitadel/zitadel/internal/crypto/database"
|
2023-08-22 10:49:22 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2022-09-23 12:08:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-04-06 06:13:40 +00:00
|
|
|
)
|
|
|
|
|
2022-07-27 08:22:20 +00:00
|
|
|
type FirstInstance struct {
|
2022-05-03 13:58:38 +00:00
|
|
|
InstanceName string
|
|
|
|
DefaultLanguage language.Tag
|
2023-08-11 14:19:14 +00:00
|
|
|
Org command.InstanceOrgSetup
|
2022-12-09 13:04:33 +00:00
|
|
|
MachineKeyPath string
|
2023-06-15 06:16:39 +00:00
|
|
|
PatPath string
|
2023-09-29 08:21:32 +00:00
|
|
|
Features map[domain.Feature]any
|
2022-04-12 14:20:17 +00:00
|
|
|
|
2022-04-28 08:30:41 +00:00
|
|
|
instanceSetup command.InstanceSetup
|
2022-04-12 14:20:17 +00:00
|
|
|
userEncryptionKey *crypto.KeyConfig
|
2022-05-13 12:13:07 +00:00
|
|
|
smtpEncryptionKey *crypto.KeyConfig
|
2023-06-15 06:16:39 +00:00
|
|
|
oidcEncryptionKey *crypto.KeyConfig
|
2022-04-12 14:20:17 +00:00
|
|
|
masterKey string
|
2023-08-22 10:49:22 +00:00
|
|
|
db *database.DB
|
2022-04-12 14:20:17 +00:00
|
|
|
es *eventstore.Eventstore
|
|
|
|
defaults systemdefaults.SystemDefaults
|
|
|
|
zitadelRoles []authz.RoleMapping
|
2022-04-28 08:30:41 +00:00
|
|
|
externalDomain string
|
2022-04-21 10:37:39 +00:00
|
|
|
externalSecure bool
|
2022-04-25 09:16:36 +00:00
|
|
|
externalPort uint16
|
2022-07-27 08:22:20 +00:00
|
|
|
domain string
|
2022-04-06 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-25 16:28:20 +00:00
|
|
|
func (mig *FirstInstance) Execute(ctx context.Context, _ eventstore.Event) error {
|
|
|
|
keyStorage, err := mig.verifyEncryptionKeys(ctx)
|
2022-04-12 14:20:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userAlg, err := crypto.NewAESCrypto(mig.userEncryptionKey, keyStorage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-13 12:13:07 +00:00
|
|
|
smtpEncryption, err := crypto.NewAESCrypto(mig.smtpEncryptionKey, keyStorage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-15 06:16:39 +00:00
|
|
|
oidcEncryption, err := crypto.NewAESCrypto(mig.oidcEncryptionKey, keyStorage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-20 14:59:37 +00:00
|
|
|
cmd, err := command.StartCommands(mig.es,
|
|
|
|
mig.defaults,
|
|
|
|
mig.zitadelRoles,
|
|
|
|
nil,
|
|
|
|
nil,
|
2022-04-28 08:30:41 +00:00
|
|
|
mig.externalDomain,
|
2022-04-25 09:16:36 +00:00
|
|
|
mig.externalSecure,
|
|
|
|
mig.externalPort,
|
2022-04-20 14:59:37 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
2022-05-13 12:13:07 +00:00
|
|
|
smtpEncryption,
|
2022-04-20 14:59:37 +00:00
|
|
|
nil,
|
|
|
|
userAlg,
|
|
|
|
nil,
|
2023-06-15 06:16:39 +00:00
|
|
|
oidcEncryption,
|
2022-09-12 16:18:08 +00:00
|
|
|
nil,
|
|
|
|
nil,
|
2023-04-25 07:02:29 +00:00
|
|
|
nil,
|
2023-05-05 15:34:53 +00:00
|
|
|
nil,
|
2023-07-10 13:27:00 +00:00
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
2023-08-23 08:04:29 +00:00
|
|
|
nil,
|
2022-09-12 16:18:08 +00:00
|
|
|
)
|
2022-04-21 10:37:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-28 08:30:41 +00:00
|
|
|
mig.instanceSetup.InstanceName = mig.InstanceName
|
2022-07-27 08:22:20 +00:00
|
|
|
mig.instanceSetup.CustomDomain = mig.externalDomain
|
2022-05-03 13:58:38 +00:00
|
|
|
mig.instanceSetup.DefaultLanguage = mig.DefaultLanguage
|
2022-04-28 08:30:41 +00:00
|
|
|
mig.instanceSetup.Org = mig.Org
|
2022-09-23 12:08:10 +00:00
|
|
|
// check if username is email style or else append @<orgname>.<custom-domain>
|
|
|
|
//this way we have the same value as before changing `UserLoginMustBeDomain` to false
|
|
|
|
if !mig.instanceSetup.DomainPolicy.UserLoginMustBeDomain && !strings.Contains(mig.instanceSetup.Org.Human.Username, "@") {
|
2023-10-11 07:55:01 +00:00
|
|
|
orgDomain, err := domain.NewIAMDomainName(mig.instanceSetup.Org.Name, mig.instanceSetup.CustomDomain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mig.instanceSetup.Org.Human.Username = mig.instanceSetup.Org.Human.Username + "@" + orgDomain
|
2022-09-23 12:08:10 +00:00
|
|
|
}
|
2023-03-14 19:20:38 +00:00
|
|
|
mig.instanceSetup.Org.Human.Email.Address = mig.instanceSetup.Org.Human.Email.Address.Normalize()
|
2022-04-28 08:30:41 +00:00
|
|
|
if mig.instanceSetup.Org.Human.Email.Address == "" {
|
2023-03-14 19:20:38 +00:00
|
|
|
mig.instanceSetup.Org.Human.Email.Address = domain.EmailAddress(mig.instanceSetup.Org.Human.Username)
|
|
|
|
if !strings.Contains(string(mig.instanceSetup.Org.Human.Email.Address), "@") {
|
2023-10-11 07:55:01 +00:00
|
|
|
orgDomain, err := domain.NewIAMDomainName(mig.instanceSetup.Org.Name, mig.instanceSetup.CustomDomain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
mig.instanceSetup.Org.Human.Email.Address = domain.EmailAddress(mig.instanceSetup.Org.Human.Username + "@" + orgDomain)
|
2022-09-23 12:08:10 +00:00
|
|
|
}
|
2022-04-28 08:30:41 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 06:16:39 +00:00
|
|
|
_, token, key, _, err := cmd.SetUpInstance(ctx, &mig.instanceSetup)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if mig.instanceSetup.Org.Machine != nil &&
|
|
|
|
((mig.instanceSetup.Org.Machine.Pat != nil && token == "") ||
|
|
|
|
(mig.instanceSetup.Org.Machine.MachineKey != nil && key == nil)) {
|
2022-12-09 13:04:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-01-25 16:28:20 +00:00
|
|
|
return mig.outputMachineAuthentication(key, token)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mig *FirstInstance) verifyEncryptionKeys(ctx context.Context) (*crypto_db.Database, error) {
|
|
|
|
keyStorage, err := crypto_db.NewKeyStorage(mig.db, mig.masterKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot start key storage: %w", err)
|
|
|
|
}
|
|
|
|
if err = verifyKey(ctx, mig.userEncryptionKey, keyStorage); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = verifyKey(ctx, mig.smtpEncryptionKey, keyStorage); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = verifyKey(ctx, mig.oidcEncryptionKey, keyStorage); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return keyStorage, nil
|
|
|
|
}
|
2022-12-09 13:04:33 +00:00
|
|
|
|
2024-01-25 16:28:20 +00:00
|
|
|
func (mig *FirstInstance) outputMachineAuthentication(key *command.MachineKey, token string) error {
|
2023-06-15 06:16:39 +00:00
|
|
|
if key != nil {
|
|
|
|
keyDetails, err := key.Detail()
|
2022-12-09 13:04:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-15 06:16:39 +00:00
|
|
|
if err := outputStdoutOrPath(mig.MachineKeyPath, string(keyDetails)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-06-15 06:16:39 +00:00
|
|
|
if token != "" {
|
|
|
|
if err := outputStdoutOrPath(mig.PatPath, token); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-09 13:04:33 +00:00
|
|
|
|
2023-06-15 06:16:39 +00:00
|
|
|
func outputStdoutOrPath(path string, content string) (err error) {
|
|
|
|
f := os.Stdout
|
|
|
|
if path != "" {
|
|
|
|
f, err = os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2022-12-09 13:04:33 +00:00
|
|
|
}
|
2023-06-15 06:16:39 +00:00
|
|
|
_, err = fmt.Fprintln(f, content)
|
2022-04-06 06:13:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-27 08:22:20 +00:00
|
|
|
func (mig *FirstInstance) String() string {
|
2022-04-06 06:13:40 +00:00
|
|
|
return "03_default_instance"
|
|
|
|
}
|
2022-04-12 14:20:17 +00:00
|
|
|
|
2024-01-04 16:12:20 +00:00
|
|
|
func verifyKey(ctx context.Context, key *crypto.KeyConfig, storage crypto.KeyStorage) (err error) {
|
2022-04-12 14:20:17 +00:00
|
|
|
_, err = crypto.LoadKey(key.EncryptionKeyID, storage)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
k, err := crypto.NewKey(key.EncryptionKeyID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-04 16:12:20 +00:00
|
|
|
return storage.CreateKeys(ctx, k)
|
2022-04-12 14:20:17 +00:00
|
|
|
}
|