mirror of
https://github.com/zitadel/zitadel.git
synced 2025-10-19 11:03:39 +00:00
read ids from database if not provided
This commit is contained in:
@@ -2,8 +2,6 @@ package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/crypto"
|
||||
|
||||
@@ -67,18 +65,6 @@ func (e E2EConfig) Validate() (err error) {
|
||||
if e.MachineKeyPath == "" {
|
||||
return errors.New("field MachineKeyPath is empty")
|
||||
}
|
||||
if e.ZitadelProjectResourceID == "" {
|
||||
return errors.New("field ZitadelProjectResourceID is empty")
|
||||
}
|
||||
|
||||
audPattern := "number-[0-9]{17}"
|
||||
matched, err := regexp.MatchString("bignumber-[0-9]{17}", e.ZitadelProjectResourceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("validating ZitadelProjectResourceID failed: %w", err)
|
||||
}
|
||||
if !matched {
|
||||
return fmt.Errorf("ZitadelProjectResourceID doesn't match regular expression %s", audPattern)
|
||||
}
|
||||
|
||||
if e.APIURL == "" {
|
||||
return errors.New("field APIURL is empty")
|
||||
|
@@ -8,7 +8,7 @@ import (
|
||||
"github.com/zitadel/logging"
|
||||
)
|
||||
|
||||
func awaitConsistency(ctx context.Context, cfg E2EConfig, expectUsers []userData) (err error) {
|
||||
func awaitConsistency(ctx context.Context, cfg E2EConfig, expectUsers []userData, zitadelProjectResourceID string) (err error) {
|
||||
|
||||
retry := make(chan struct{})
|
||||
go func() {
|
||||
@@ -18,7 +18,7 @@ func awaitConsistency(ctx context.Context, cfg E2EConfig, expectUsers []userData
|
||||
for {
|
||||
select {
|
||||
case <-retry:
|
||||
err = checkCondition(ctx, cfg, expectUsers)
|
||||
err = checkCondition(ctx, cfg, expectUsers, zitadelProjectResourceID)
|
||||
if err == nil {
|
||||
logging.Log("AWAIT-QIOOJ").Info("setup is consistent")
|
||||
return nil
|
||||
@@ -34,8 +34,8 @@ func awaitConsistency(ctx context.Context, cfg E2EConfig, expectUsers []userData
|
||||
}
|
||||
}
|
||||
|
||||
func checkCondition(ctx context.Context, cfg E2EConfig, expectUsers []userData) error {
|
||||
token, err := newToken(cfg)
|
||||
func checkCondition(ctx context.Context, cfg E2EConfig, expectUsers []userData, zitadelProjectResourceID string) error {
|
||||
token, err := newToken(cfg, zitadelProjectResourceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@@ -13,9 +13,9 @@ import (
|
||||
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
||||
)
|
||||
|
||||
func execute(ctx context.Context, cmd *command.Commands, cfg E2EConfig, users []userData) error {
|
||||
func execute(ctx context.Context, cmd *command.Commands, cfg E2EConfig, users []userData, instanceID string) error {
|
||||
|
||||
ctx = authz.WithInstanceID(ctx, cfg.InstanceID)
|
||||
ctx = authz.WithInstanceID(ctx, instanceID)
|
||||
ctx = authz.WithRequestedDomain(ctx, "localhost")
|
||||
|
||||
orgOwner := newHuman(users[0])
|
||||
|
58
cmd/e2e-setup/ids.go
Normal file
58
cmd/e2e-setup/ids.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ids(cfg *E2EConfig, dbClient *sql.DB) (string, string, error) {
|
||||
zitadelProjectResourceID := strings.TrimPrefix(cfg.ZitadelProjectResourceID, "bignumber-")
|
||||
instanceID := strings.TrimPrefix(cfg.InstanceID, "bignumber-")
|
||||
|
||||
if zitadelProjectResourceID != "" && instanceID != "" {
|
||||
return zitadelProjectResourceID, instanceID, nil
|
||||
}
|
||||
|
||||
zitadelProjectResourceID, err := querySingleString(dbClient, `select aggregate_id from eventstore.events where event_type = 'project.added' and event_data = '{\"name\": \"ZITADEL\"}'`)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
instanceID, err = querySingleString(dbClient, `select aggregate_id from eventstore.events where event_type = 'instance.added' and event_data = '{\"name\": \"Localhost\"}'`)
|
||||
return instanceID, zitadelProjectResourceID, err
|
||||
}
|
||||
|
||||
func querySingleString(dbClient *sql.DB, query string) (_ string, err error) {
|
||||
defer func() {
|
||||
if err != nil {
|
||||
err = fmt.Errorf("getting single string failed for query %s: %w", query, err)
|
||||
}
|
||||
}()
|
||||
|
||||
rows, err := dbClient.Query(query)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var read bool
|
||||
id := new(string)
|
||||
for rows.Next() {
|
||||
if read {
|
||||
return "", errors.New("read more than one row")
|
||||
}
|
||||
read = true
|
||||
if err := rows.Scan(id); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
if !read {
|
||||
return "", errors.New("no result")
|
||||
}
|
||||
|
||||
if *id == "" {
|
||||
return "", errors.New("could not parse result")
|
||||
}
|
||||
return *id, nil
|
||||
}
|
@@ -69,6 +69,9 @@ func startE2ESetup(conf *Config, masterkey string) {
|
||||
dbClient, err := database.Connect(conf.Database)
|
||||
logging.New().OnError(err).Fatalf("cannot start client for projection: %s", err)
|
||||
|
||||
zitadelProjectResourceID, instanceID, err := ids(conf.E2E, dbClient)
|
||||
logging.New().OnError(err).Fatalf("cannot get instance and project IDs: %s", err)
|
||||
|
||||
keyStorage, err := cryptoDB.NewKeyStorage(dbClient, masterkey)
|
||||
logging.New().OnError(err).Fatalf("cannot start key storage: %s", err)
|
||||
|
||||
@@ -124,7 +127,7 @@ func startE2ESetup(conf *Config, masterkey string) {
|
||||
pw: conf.E2E.PasswordComplexityUserPassword,
|
||||
}}
|
||||
|
||||
err = execute(ctx, commands, *conf.E2E, users)
|
||||
err = execute(ctx, commands, *conf.E2E, users, instanceID)
|
||||
logging.New().OnError(err).Fatalf("failed to execute commands steps")
|
||||
|
||||
eventualConsistencyCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
|
||||
@@ -133,6 +136,7 @@ func startE2ESetup(conf *Config, masterkey string) {
|
||||
eventualConsistencyCtx,
|
||||
*conf.E2E,
|
||||
users,
|
||||
zitadelProjectResourceID,
|
||||
)
|
||||
logging.New().OnError(err).Fatal("failed to await consistency")
|
||||
}
|
||||
|
@@ -3,15 +3,15 @@ package main
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
)
|
||||
|
||||
func newToken(cfg E2EConfig) (string, error) {
|
||||
func newToken(cfg E2EConfig, zitadelProjectResourceID string) (string, error) {
|
||||
|
||||
keyBytes, err := os.ReadFile(cfg.MachineKeyPath)
|
||||
if err != nil {
|
||||
@@ -61,7 +61,7 @@ func newToken(cfg E2EConfig) (string, error) {
|
||||
|
||||
resp, err := http.PostForm(fmt.Sprintf("%s/oauth/v2/token", cfg.APIURL), map[string][]string{
|
||||
"grant_type": {"urn:ietf:params:oauth:grant-type:jwt-bearer"},
|
||||
"scope": {fmt.Sprintf("openid urn:zitadel:iam:org:project:id:%s:aud", strings.TrimPrefix(cfg.ZitadelProjectResourceID, "bignumber-"))},
|
||||
"scope": {fmt.Sprintf("openid urn:zitadel:iam:org:project:id:%s:aud", zitadelProjectResourceID)},
|
||||
"assertion": {tokenString},
|
||||
})
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user