zitadel/cmd/e2e-setup/execute.go

139 lines
3.5 KiB
Go
Raw Normal View History

2022-07-08 10:07:17 +02:00
package main
import (
"context"
"io/ioutil"
"os"
"path/filepath"
2022-07-25 16:33:53 +02:00
"strings"
2022-07-08 10:07:17 +02:00
"time"
2022-07-25 16:33:53 +02:00
"github.com/zitadel/logging"
2022-07-08 15:20:35 +02:00
"github.com/zitadel/zitadel/internal/api/authz"
2022-07-08 10:07:17 +02:00
"github.com/zitadel/zitadel/internal/command"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
)
2022-07-20 11:50:49 +02:00
func execute(ctx context.Context, cmd *command.Commands, cfg E2EConfig, users []userData, instanceID string) error {
2022-07-08 10:07:17 +02:00
2022-07-20 11:50:49 +02:00
ctx = authz.WithInstanceID(ctx, instanceID)
2022-07-18 10:58:43 +02:00
ctx = authz.WithRequestedDomain(ctx, "localhost")
2022-07-08 15:20:35 +02:00
2022-07-08 10:07:17 +02:00
orgOwner := newHuman(users[0])
orgOwnerID, org, err := cmd.SetUpOrg(ctx, &command.OrgSetup{
Name: cfg.Org,
CustomDomain: "localhost",
Human: *orgOwner,
})
if err != nil {
2022-07-25 16:33:53 +02:00
// TODO: Why is this error not typed?
if strings.Contains(err.Error(), "Errors.Org.AlreadyExists") {
logging.New().Info("Looks like setup is already done")
err = nil
}
2022-07-08 10:07:17 +02:00
return err
}
// Avoids the MFA nudge
if _, err = cmd.AddLoginPolicy(ctx, org.ResourceOwner, &domain.LoginPolicy{
2022-07-20 16:55:11 +02:00
AllowUsernamePassword: true,
ExternalLoginCheckLifetime: 24 * 365 * time.Hour, // 1 year
MFAInitSkipLifetime: 24 * 365 * time.Hour, // 1 year
MultiFactorCheckLifetime: 24 * 365 * time.Hour, // 1 year
PasswordCheckLifetime: 24 * 365 * time.Hour, // 1 year
SecondFactorCheckLifetime: 24 * 365 * time.Hour, // 1 year
2022-07-08 10:07:17 +02:00
}); err != nil {
return err
}
2022-07-18 13:22:06 +02:00
if err = initHuman(ctx, cmd, orgOwnerID, users[0], org.ResourceOwner); err != nil {
2022-07-18 10:58:43 +02:00
return err
}
2022-07-08 10:07:17 +02:00
sa, err := cmd.AddMachine(ctx, org.ResourceOwner, &domain.Machine{
Username: "e2e",
Name: "e2e",
Description: "User who calls the ZITADEL API for preparing end-to-end tests",
})
if err != nil {
return err
}
if _, err = cmd.AddOrgMember(ctx, org.ResourceOwner, sa.AggregateID, domain.RoleOrgOwner); err != nil {
return err
}
key, err := cmd.AddUserMachineKey(ctx, &domain.MachineKey{
ObjectRoot: models.ObjectRoot{
AggregateID: sa.AggregateID,
},
ExpirationDate: time.Now().Add(30 * 24 * time.Hour),
Type: domain.AuthNKeyTypeJSON,
}, org.ResourceOwner)
if err != nil {
return err
}
json, err := key.MarshalJSON()
if err != nil {
return err
}
if err = os.MkdirAll(filepath.Dir(cfg.MachineKeyPath), 0700); err != nil {
return err
}
if err = ioutil.WriteFile(cfg.MachineKeyPath, json, 0600); err != nil {
return err
}
for idx := range users[1:] {
2022-07-08 15:20:35 +02:00
user := users[idx+1]
2022-07-08 10:07:17 +02:00
createdHuman, err := cmd.AddHuman(ctx, org.ResourceOwner, newHuman(user))
if err != nil {
return err
}
2022-07-18 13:22:06 +02:00
if err = initHuman(ctx, cmd, createdHuman.ID, user, org.ResourceOwner); err != nil {
2022-07-18 10:58:43 +02:00
return err
}
2022-07-08 10:07:17 +02:00
if user.role != "" {
2022-07-18 13:22:06 +02:00
if _, err := cmd.AddOrgMember(ctx, org.ResourceOwner, createdHuman.ID, user.role); err != nil {
2022-07-08 10:07:17 +02:00
return err
}
}
}
return nil
}
func newHuman(u userData) *command.AddHuman {
return &command.AddHuman{
Username: u.desc + "_user_name",
FirstName: u.desc + "_first_name",
LastName: u.desc + "_last_name",
Password: u.pw,
Email: command.Email{
Address: u.desc + ".e2e@zitadel.com",
Verified: true,
},
2022-07-18 13:22:06 +02:00
PasswordChangeRequired: false,
Register: false,
}
}
// initHuman skips the MFA and change password screens
func initHuman(ctx context.Context, cmd *command.Commands, userID string, user userData, orgID string) error {
// skip mfa
if err := cmd.HumanSkipMFAInit(ctx, userID, orgID); err != nil {
return err
2022-07-08 10:07:17 +02:00
}
2022-07-18 13:22:06 +02:00
// Avoids the change password screen
_, err := cmd.ChangePassword(ctx, orgID, userID, user.pw, user.pw, "")
return err
2022-07-08 10:07:17 +02:00
}