2022-04-06 06:13:40 +00:00
|
|
|
package setup
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-04-12 14:20:17 +00:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2022-04-06 06:13:40 +00:00
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
|
|
|
"github.com/caos/zitadel/internal/command"
|
|
|
|
"github.com/caos/zitadel/internal/config/systemdefaults"
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
crypto_db "github.com/caos/zitadel/internal/crypto/database"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2022-04-06 06:13:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DefaultInstance struct {
|
|
|
|
InstanceSetup command.InstanceSetup
|
2022-04-12 14:20:17 +00:00
|
|
|
|
|
|
|
userEncryptionKey *crypto.KeyConfig
|
|
|
|
masterKey string
|
|
|
|
db *sql.DB
|
|
|
|
es *eventstore.Eventstore
|
|
|
|
domain string
|
|
|
|
defaults systemdefaults.SystemDefaults
|
|
|
|
zitadelRoles []authz.RoleMapping
|
2022-04-06 06:13:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mig *DefaultInstance) Execute(ctx context.Context) error {
|
2022-04-12 14:20:17 +00: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
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := command.NewCommandV2(mig.es, mig.defaults, userAlg, mig.zitadelRoles)
|
2022-04-06 06:13:40 +00:00
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
ctx = authz.WithRequestedDomain(ctx, mig.domain)
|
|
|
|
_, err = cmd.SetUpInstance(ctx, &mig.InstanceSetup)
|
2022-04-06 06:13:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mig *DefaultInstance) String() string {
|
|
|
|
return "03_default_instance"
|
|
|
|
}
|
2022-04-12 14:20:17 +00: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)
|
|
|
|
}
|