mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 05:52:51 +00:00
# Which Problems Are Solved Streams allow differentiating logs produced by different components of Zitadel. # How the Problems Are Solved The `backend/v3/instrumentation/logging` package now exposes convenience function for setting and getting a logger from the context. As well as high-level functions to emit log records at various levels. When constructing a new logger a "stream" needs to be specified: - **runtime**: General runtime logs, such as startup and shutdown messages. Default for logs that do not belong to the other categories. - **request**: Logs for incoming API and HTTP requests. - **event_handler**: Logs for event handling in projections. - **queue**: Logs for the job queue processing. - **event_pusher**: Logs for event pushing to the database. Disabled by default, contains sensitive information. Each line from the returned logger contains a `stream` field as well as a `version` field with the current Zitadel version. ## Runtime config Streams can be enabled by passing an array of stream names in the runtime config. Because some log streams may contain sensitive data (especially events), it is now also possible to mask values by their key. # Additional Changes - Wrap `slogctx` in the `logging` package. (Except API error converter packages, because of import cycle) - Add some docs to `logging` package so other devs understand how to add logging to Zitadel - Add `logging.OnError` and `logging.WithError` helper functions with `Panic()` and `Fatal()` methods, to preserve current calls in the `cmd` packages. - Add instance context extractor. - Only output request details in the request info log. Request ID remains propagated through context. - Moved middleware functionality into protocol specific packages. - Removed setting of URI to context in metric middleware. There were ony setters and no getters. (Unused value) - Reuse a single statusWriter in the middleware package for middlewares that need to know the response status. # Additional Context - Closes #11333 - Closes #11331 - Partly #11330 --------- Co-authored-by: Silvan <[email protected]>
146 lines
3.3 KiB
Go
146 lines
3.3 KiB
Go
package initialise
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
_ "embed"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/instrumentation/logging"
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
es_v3 "github.com/zitadel/zitadel/internal/eventstore/v3"
|
|
)
|
|
|
|
func newZitadel() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "zitadel",
|
|
Short: "initialize ZITADEL internals",
|
|
Long: `initialize ZITADEL internals.
|
|
|
|
Prerequisites:
|
|
- postgreSQL with user and database
|
|
`,
|
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
|
defer func() {
|
|
logging.OnError(cmd.Context(), err).Error("zitadel verify zitadel command failed")
|
|
}()
|
|
config, shutdown, err := NewConfig(cmd, viper.GetViper())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
err = errors.Join(err, shutdown(cmd.Context()))
|
|
}()
|
|
return verifyZitadel(cmd.Context(), config.Database)
|
|
},
|
|
}
|
|
}
|
|
|
|
func VerifyZitadel(ctx context.Context, db *database.DB, config database.Config) error {
|
|
err := ReadStmts()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
conn, err := db.Conn(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer conn.Close()
|
|
|
|
logging.Info(ctx, "verify system")
|
|
if err := exec(ctx, conn, fmt.Sprintf(createSystemStmt, config.Username()), nil); err != nil {
|
|
return err
|
|
}
|
|
|
|
logging.Info(ctx, "verify encryption keys")
|
|
if err := createEncryptionKeys(ctx, conn); err != nil {
|
|
return err
|
|
}
|
|
|
|
logging.Info(ctx, "verify projections")
|
|
if err := exec(ctx, conn, fmt.Sprintf(createProjectionsStmt, config.Username()), nil); err != nil {
|
|
return err
|
|
}
|
|
|
|
logging.Info(ctx, "verify eventstore")
|
|
if err := exec(ctx, conn, fmt.Sprintf(createEventstoreStmt, config.Username()), nil); err != nil {
|
|
return err
|
|
}
|
|
|
|
logging.Info(ctx, "verify events tables")
|
|
if err := createEvents(ctx, conn); err != nil {
|
|
return err
|
|
}
|
|
|
|
logging.Info(ctx, "verify unique constraints")
|
|
if err := exec(ctx, conn, createUniqueConstraints, nil); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func verifyZitadel(ctx context.Context, config database.Config) error {
|
|
logging.Info(ctx, "verify zitadel", "database", config.DatabaseName())
|
|
|
|
db, err := database.Connect(config, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := VerifyZitadel(ctx, db, config); err != nil {
|
|
return err
|
|
}
|
|
|
|
return db.Close()
|
|
}
|
|
|
|
func createEncryptionKeys(ctx context.Context, db database.Beginner) error {
|
|
tx, err := db.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err = tx.Exec(createEncryptionKeysStmt); err != nil {
|
|
rollbackErr := tx.Rollback()
|
|
logging.WithError(ctx, rollbackErr).Error("rollback failed")
|
|
return err
|
|
}
|
|
|
|
return tx.Commit()
|
|
}
|
|
|
|
func createEvents(ctx context.Context, conn *sql.Conn) (err error) {
|
|
tx, err := conn.BeginTx(ctx, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
if err != nil {
|
|
rollbackErr := tx.Rollback()
|
|
logging.WithError(ctx, rollbackErr).Error("rollback failed")
|
|
return
|
|
}
|
|
err = tx.Commit()
|
|
}()
|
|
|
|
// if events already exists events2 is created during a setup job
|
|
var count int
|
|
row := tx.QueryRow("SELECT count(*) FROM information_schema.tables WHERE table_schema = 'eventstore' AND table_name like 'events%'")
|
|
if err = row.Scan(&count); err != nil {
|
|
return err
|
|
}
|
|
if row.Err() != nil || count >= 1 {
|
|
return row.Err()
|
|
}
|
|
_, err = tx.Exec(createEventsStmt)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return es_v3.CheckExecutionPlan(ctx, conn)
|
|
}
|