mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 13: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]>
152 lines
4.5 KiB
Go
152 lines
4.5 KiB
Go
package mirror
|
|
|
|
import (
|
|
"context"
|
|
_ "embed"
|
|
"errors"
|
|
"io"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/stdlib"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/instrumentation/logging"
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
)
|
|
|
|
func systemCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "system",
|
|
Short: "mirrors the system tables of ZITADEL from one database to another",
|
|
Long: `mirrors the system tables of ZITADEL from one database to another
|
|
ZITADEL needs to be initialized
|
|
Only keys and assets are mirrored`,
|
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
|
defer func() {
|
|
logging.OnError(cmd.Context(), err).Error("zitadel mirror system command failed")
|
|
}()
|
|
config, shutdown, err := newMigrationConfig(cmd, viper.GetViper())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
err = errors.Join(err, shutdown(cmd.Context()))
|
|
}()
|
|
copySystem(cmd.Context(), config)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().BoolVar(&shouldReplace, "replace", false, "allow delete ALL keys and assets of defined instances before copy")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func copySystem(ctx context.Context, config *Migration) {
|
|
sourceClient, err := database.Connect(config.Source, false)
|
|
logging.OnError(ctx, err).Fatal("unable to connect to source database")
|
|
defer sourceClient.Close()
|
|
|
|
destClient, err := database.Connect(config.Destination, false)
|
|
logging.OnError(ctx, err).Fatal("unable to connect to destination database")
|
|
defer destClient.Close()
|
|
|
|
copyAssets(ctx, sourceClient, destClient)
|
|
copyEncryptionKeys(ctx, sourceClient, destClient)
|
|
}
|
|
|
|
func copyAssets(ctx context.Context, source, dest *database.DB) {
|
|
logging.Info(ctx, "starting to copy assets")
|
|
start := time.Now()
|
|
|
|
sourceConn, err := source.Conn(ctx)
|
|
logging.OnError(ctx, err).Fatal("unable to acquire source connection")
|
|
defer sourceConn.Close()
|
|
|
|
r, w := io.Pipe()
|
|
errs := make(chan error, 1)
|
|
|
|
go func() {
|
|
err = sourceConn.Raw(func(driverConn interface{}) error {
|
|
conn := driverConn.(*stdlib.Conn).Conn()
|
|
// ignore hash column because it's computed
|
|
_, err := conn.PgConn().CopyTo(ctx, w, "COPY (SELECT instance_id, asset_type, resource_owner, name, content_type, data, updated_at FROM system.assets "+instanceClause()+") TO stdout")
|
|
w.Close()
|
|
return err
|
|
})
|
|
errs <- err
|
|
}()
|
|
|
|
destConn, err := dest.Conn(ctx)
|
|
logging.OnError(ctx, err).Fatal("unable to acquire dest connection")
|
|
defer destConn.Close()
|
|
|
|
var assetCount int64
|
|
err = destConn.Raw(func(driverConn interface{}) error {
|
|
conn := driverConn.(*stdlib.Conn).Conn()
|
|
|
|
if shouldReplace {
|
|
_, err := conn.Exec(ctx, "DELETE FROM system.assets "+instanceClause())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
tag, err := conn.PgConn().CopyFrom(ctx, r, "COPY system.assets (instance_id, asset_type, resource_owner, name, content_type, data, updated_at) FROM stdin")
|
|
assetCount = tag.RowsAffected()
|
|
|
|
return err
|
|
})
|
|
logging.OnError(ctx, err).Fatal("unable to copy assets to destination")
|
|
logging.OnError(ctx, <-errs).Fatal("unable to copy assets from source")
|
|
logging.Info(ctx, "assets migrated", "took", time.Since(start), "count", assetCount)
|
|
}
|
|
|
|
func copyEncryptionKeys(ctx context.Context, source, dest *database.DB) {
|
|
logging.Info(ctx, "starting to copy encryption keys")
|
|
start := time.Now()
|
|
|
|
sourceConn, err := source.Conn(ctx)
|
|
logging.OnError(ctx, err).Fatal("unable to acquire source connection")
|
|
defer sourceConn.Close()
|
|
|
|
r, w := io.Pipe()
|
|
errs := make(chan error, 1)
|
|
|
|
go func() {
|
|
err = sourceConn.Raw(func(driverConn interface{}) error {
|
|
conn := driverConn.(*stdlib.Conn).Conn()
|
|
// ignore hash column because it's computed
|
|
_, err := conn.PgConn().CopyTo(ctx, w, "COPY system.encryption_keys TO stdout")
|
|
w.Close()
|
|
return err
|
|
})
|
|
errs <- err
|
|
}()
|
|
|
|
destConn, err := dest.Conn(ctx)
|
|
logging.OnError(ctx, err).Fatal("unable to acquire dest connection")
|
|
defer destConn.Close()
|
|
|
|
var keyCount int64
|
|
err = destConn.Raw(func(driverConn interface{}) error {
|
|
conn := driverConn.(*stdlib.Conn).Conn()
|
|
|
|
if shouldReplace {
|
|
_, err := conn.Exec(ctx, "TRUNCATE system.encryption_keys")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
tag, err := conn.PgConn().CopyFrom(ctx, r, "COPY system.encryption_keys FROM stdin")
|
|
keyCount = tag.RowsAffected()
|
|
|
|
return err
|
|
})
|
|
logging.OnError(ctx, err).Fatal("unable to copy encryption keys to destination")
|
|
logging.OnError(ctx, <-errs).Fatal("unable to copy encryption keys from source")
|
|
logging.Info(ctx, "encryption keys migrated", "took", time.Since(start), "count", keyCount)
|
|
}
|