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]>
124 lines
3.8 KiB
Go
124 lines
3.8 KiB
Go
package mirror
|
|
|
|
import (
|
|
"bytes"
|
|
_ "embed"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/instrumentation/logging"
|
|
"github.com/zitadel/zitadel/cmd/key"
|
|
)
|
|
|
|
var (
|
|
instanceIDs []string
|
|
isSystem bool
|
|
shouldReplace bool
|
|
)
|
|
|
|
func New(configFiles *[]string) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "mirror",
|
|
Short: "mirrors all data of ZITADEL from one database to another",
|
|
Long: `mirrors all data of ZITADEL from one database to another
|
|
ZITADEL needs to be initialized and set up with --for-mirror
|
|
|
|
The command does mirror all data needed and recomputes the projections.
|
|
For more details call the help functions of the sub commands.
|
|
|
|
Order of execution:
|
|
1. mirror system tables
|
|
2. mirror auth tables
|
|
3. mirror event store tables
|
|
4. recompute projections
|
|
5. verify`,
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) (err error) {
|
|
defer func() {
|
|
logging.OnError(cmd.Context(), err).Error("zitadel mirror (sub)command failed")
|
|
}()
|
|
|
|
err = viper.MergeConfig(bytes.NewBuffer(defaultConfig))
|
|
if err != nil {
|
|
return fmt.Errorf("unable to read default config: %w", err)
|
|
}
|
|
for _, file := range *configFiles {
|
|
viper.SetConfigFile(file)
|
|
err := viper.MergeInConfig()
|
|
logging.OnError(cmd.Context(), err).Error("unable to read config file")
|
|
}
|
|
return nil
|
|
},
|
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
|
defer func() {
|
|
logging.OnError(cmd.Context(), err).Error("zitadel mirror command failed")
|
|
}()
|
|
|
|
config, shutdown, err := newMigrationConfig(cmd, viper.GetViper())
|
|
if err != nil {
|
|
return fmt.Errorf("unable to create migration config: %w", err)
|
|
}
|
|
defer func() {
|
|
err = errors.Join(err, shutdown(cmd.Context()))
|
|
}()
|
|
|
|
projectionConfig, _, err := newProjectionsConfig(cmd, viper.GetViper())
|
|
if err != nil {
|
|
return fmt.Errorf("unable to create projections config: %w", err)
|
|
}
|
|
|
|
masterKey, err := key.MasterKey(cmd)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to read master key: %w", err)
|
|
}
|
|
|
|
copySystem(cmd.Context(), config)
|
|
copyAuth(cmd.Context(), config)
|
|
copyEventstore(cmd.Context(), config)
|
|
projections(cmd.Context(), projectionConfig, masterKey)
|
|
return nil
|
|
},
|
|
}
|
|
|
|
mirrorFlags(cmd)
|
|
cmd.Flags().BoolVar(&shouldIgnorePrevious, "ignore-previous", false, "ignores previous migrations of the events table")
|
|
cmd.Flags().BoolVar(&shouldReplace, "replace", false, `replaces all data of the following tables for the provided instances or all if the "--system"-flag is set:
|
|
* system.assets
|
|
* auth.auth_requests
|
|
* eventstore.unique_constraints
|
|
The flag should be provided if you want to execute the mirror command multiple times so that the static data are also mirrored to prevent inconsistent states.`)
|
|
migrateProjectionsFlags(cmd)
|
|
|
|
cmd.AddCommand(
|
|
eventstoreCmd(),
|
|
systemCmd(),
|
|
projectionsCmd(),
|
|
authCmd(),
|
|
verifyCmd(),
|
|
)
|
|
|
|
return cmd
|
|
}
|
|
|
|
func mirrorFlags(cmd *cobra.Command) {
|
|
cmd.PersistentFlags().StringSliceVar(&instanceIDs, "instance", nil, "id or comma separated ids of the instance(s) to migrate. Either this or the `--system`-flag must be set. Make sure to always use the same flag if you execute the command multiple times.")
|
|
cmd.PersistentFlags().BoolVar(&isSystem, "system", false, "migrates the whole system. Either this or the `--instance`-flag must be set. Make sure to always use the same flag if you execute the command multiple times.")
|
|
cmd.MarkFlagsOneRequired("system", "instance")
|
|
cmd.MarkFlagsMutuallyExclusive("system", "instance")
|
|
}
|
|
|
|
func instanceClause() string {
|
|
if isSystem {
|
|
return "WHERE instance_id <> ''"
|
|
}
|
|
for i := range instanceIDs {
|
|
instanceIDs[i] = "'" + instanceIDs[i] + "'"
|
|
}
|
|
|
|
// COPY does not allow parameters so we need to set them directly
|
|
return "WHERE instance_id IN (" + strings.Join(instanceIDs, ", ") + ")"
|
|
}
|