2022-04-06 08:13:40 +02:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-12 16:20:17 +02:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2022-04-28 10:30:41 +02:00
|
|
|
"strings"
|
2022-04-06 08:13:40 +02:00
|
|
|
|
2022-05-03 15:58:38 +02:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
2022-04-27 01:01:45 +02: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"
|
2022-09-23 14:08:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-04-06 08:13:40 +02:00
|
|
|
)
|
|
|
|
|
2022-07-27 10:22:20 +02:00
|
|
|
type FirstInstance struct {
|
2022-05-03 15:58:38 +02:00
|
|
|
InstanceName string
|
|
|
|
DefaultLanguage language.Tag
|
|
|
|
Org command.OrgSetup
|
2022-04-12 16:20:17 +02:00
|
|
|
|
2022-04-28 10:30:41 +02:00
|
|
|
instanceSetup command.InstanceSetup
|
2022-04-12 16:20:17 +02:00
|
|
|
userEncryptionKey *crypto.KeyConfig
|
2022-05-13 14:13:07 +02:00
|
|
|
smtpEncryptionKey *crypto.KeyConfig
|
2022-04-12 16:20:17 +02:00
|
|
|
masterKey string
|
|
|
|
db *sql.DB
|
|
|
|
es *eventstore.Eventstore
|
|
|
|
defaults systemdefaults.SystemDefaults
|
|
|
|
zitadelRoles []authz.RoleMapping
|
2022-04-28 10:30:41 +02:00
|
|
|
externalDomain string
|
2022-04-21 12:37:39 +02:00
|
|
|
externalSecure bool
|
2022-04-25 11:16:36 +02:00
|
|
|
externalPort uint16
|
2022-07-27 10:22:20 +02:00
|
|
|
domain string
|
2022-04-06 08:13:40 +02:00
|
|
|
}
|
|
|
|
|
2022-07-27 10:22:20 +02:00
|
|
|
func (mig *FirstInstance) Execute(ctx context.Context) error {
|
2022-04-12 16:20:17 +02:00
|
|
|
keyStorage, err := crypto_db.NewKeyStorage(mig.db, mig.masterKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot start key storage: %w", err)
|
|
|
|
}
|
|
|
|
if err = verifyKey(mig.userEncryptionKey, keyStorage); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
userAlg, err := crypto.NewAESCrypto(mig.userEncryptionKey, keyStorage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-13 14:13:07 +02:00
|
|
|
if err = verifyKey(mig.smtpEncryptionKey, keyStorage); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
smtpEncryption, err := crypto.NewAESCrypto(mig.smtpEncryptionKey, keyStorage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-20 16:59:37 +02:00
|
|
|
cmd, err := command.StartCommands(mig.es,
|
|
|
|
mig.defaults,
|
|
|
|
mig.zitadelRoles,
|
|
|
|
nil,
|
|
|
|
nil,
|
2022-04-28 10:30:41 +02:00
|
|
|
mig.externalDomain,
|
2022-04-25 11:16:36 +02:00
|
|
|
mig.externalSecure,
|
|
|
|
mig.externalPort,
|
2022-04-20 16:59:37 +02:00
|
|
|
nil,
|
|
|
|
nil,
|
2022-05-13 14:13:07 +02:00
|
|
|
smtpEncryption,
|
2022-04-20 16:59:37 +02:00
|
|
|
nil,
|
|
|
|
userAlg,
|
|
|
|
nil,
|
2022-09-12 17:18:08 +01:00
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
)
|
2022-04-06 08:13:40 +02:00
|
|
|
|
2022-04-21 12:37:39 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-28 10:30:41 +02:00
|
|
|
mig.instanceSetup.InstanceName = mig.InstanceName
|
2022-07-27 10:22:20 +02:00
|
|
|
mig.instanceSetup.CustomDomain = mig.externalDomain
|
2022-05-03 15:58:38 +02:00
|
|
|
mig.instanceSetup.DefaultLanguage = mig.DefaultLanguage
|
2022-04-28 10:30:41 +02:00
|
|
|
mig.instanceSetup.Org = mig.Org
|
2022-09-23 14:08:10 +02: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, "@") {
|
|
|
|
mig.instanceSetup.Org.Human.Username = mig.instanceSetup.Org.Human.Username + "@" + domain.NewIAMDomainName(mig.instanceSetup.Org.Name, mig.instanceSetup.CustomDomain)
|
|
|
|
}
|
2022-04-28 10:30:41 +02:00
|
|
|
mig.instanceSetup.Org.Human.Email.Address = strings.TrimSpace(mig.instanceSetup.Org.Human.Email.Address)
|
|
|
|
if mig.instanceSetup.Org.Human.Email.Address == "" {
|
2022-09-23 14:08:10 +02:00
|
|
|
mig.instanceSetup.Org.Human.Email.Address = mig.instanceSetup.Org.Human.Username
|
|
|
|
if !strings.Contains(mig.instanceSetup.Org.Human.Email.Address, "@") {
|
|
|
|
mig.instanceSetup.Org.Human.Email.Address = mig.instanceSetup.Org.Human.Username + "@" + domain.NewIAMDomainName(mig.instanceSetup.Org.Name, mig.instanceSetup.CustomDomain)
|
|
|
|
}
|
2022-04-28 10:30:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = cmd.SetUpInstance(ctx, &mig.instanceSetup)
|
2022-04-06 08:13:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-27 10:22:20 +02:00
|
|
|
func (mig *FirstInstance) String() string {
|
2022-04-06 08:13:40 +02:00
|
|
|
return "03_default_instance"
|
|
|
|
}
|
2022-04-12 16:20:17 +02:00
|
|
|
|
|
|
|
func verifyKey(key *crypto.KeyConfig, storage crypto.KeyStorage) (err error) {
|
|
|
|
_, err = crypto.LoadKey(key.EncryptionKeyID, storage)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
k, err := crypto.NewKey(key.EncryptionKeyID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return storage.CreateKeys(k)
|
|
|
|
}
|