mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 05:52:51 +00:00
# Which Problems Are Solved Zitadel did not provide easy correlation between errors, logs, traces and metrics. The configuration for those instrumentations was also not consistent, with some supporting different exporters then others. Implementation and parsing of config was also spaghettified over multiple packages, with awkward parsing and inconsistent naming of options. # How the Problems Are Solved All telemetry is now merged under the name "instrumentation". Why? 1. We thought it was a good idea in the past to call the milestone exporter `Telemtry` in the runtime config. Calling this `TelemetryV2` looks weird. 2. Not everything is a meter and not everything is sent (tele...). 3. It's also [defined](https://opentelemetry.io/docs/concepts/instrumentation/) as such by the OTEL documentation. ## New features - Adds structured, context based logging with trace-ID awareness - Static log fields are added to the context, such as service and request path - Static log fields are injected in each logline emitted by the application - Structured logs can also be send to an otel exporter - Structured logs can be printed to StdErr in text and JSON format - Error sinks make sure every error is logged at the correct level: - Warnings for client side errors (HTTP 400 range, Invalid request etc) - Error for server side errors (Internal server errors) - Metrics can now also be send to a OTEL collector. (previously they could only be scraped from `/debug/metrics` with prometheus) ## Exporters This change adds all the exporters supported by OTEL upstream and some google specific exporters for our cloud deployment. - StdOut / StdErr: all instrumentations - OTEL gRPC / HTTP: all instrumentations - Google: all instrumentations except logging - Prometheus (pull-based): only metrics The exception is profiling, which only supports the google exporting due to lack of support by OTEL upstream. ## Configuration and structure - All instrumentation is moved into the new `backend/v3/instrumentation` package. It reuses configuration types, so both code and runtime configuration are easier to understand. - The `internal/telemetry` packages are removed. - Instrumentation is started with a single function and a proper shutdown function is now provided. - Legacy configuration is still parsed from the runtime config, as long as the new configuration is disabled. This allows backporting this feature to v4 without breaking existing configurations. # Additional Changes - Devcontainer: set `$PATH` variable so installed go binaries can be run individually, without NX. - NX: install GCI tool to fix imports # Additional Context - Closes https://github.com/zitadel/zitadel/issues/8408 - Closes https://github.com/zitadel/zitadel/issues/6664 - Backport to v4
139 lines
3.2 KiB
Go
139 lines
3.2 KiB
Go
package key
|
|
|
|
import (
|
|
"io"
|
|
"log/slog"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
"sigs.k8s.io/yaml"
|
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
cryptoDB "github.com/zitadel/zitadel/internal/crypto/database"
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
const (
|
|
flagKeyFile = "file"
|
|
)
|
|
|
|
type Config struct {
|
|
Database database.Config
|
|
}
|
|
|
|
func New() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "keys",
|
|
Short: "manage encryption keys",
|
|
}
|
|
AddMasterKeyFlag(cmd)
|
|
cmd.AddCommand(newKey())
|
|
return cmd
|
|
}
|
|
|
|
func newKey() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "new [keyID=key]... [-f file]",
|
|
Short: "create new encryption key(s)",
|
|
Long: `create new encryption key(s) (encrypted by the provided master key)
|
|
provide key(s) by YAML file and/or by argument
|
|
Requirements:
|
|
- postgreSQL`,
|
|
Example: `new -f keys.yaml
|
|
new key1=somekey key2=anotherkey
|
|
new -f keys.yaml key2=anotherkey`,
|
|
RunE: func(cmd *cobra.Command, args []string) (err error) {
|
|
defer func() {
|
|
if err != nil {
|
|
slog.Error("zitadel keys new command failed", "err", err)
|
|
}
|
|
}()
|
|
|
|
keys, err := keysFromArgs(args)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
filePath, _ := cmd.Flags().GetString(flagKeyFile)
|
|
if filePath != "" {
|
|
file, err := openFile(filePath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
yamlKeys, err := keysFromYAML(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
keys = append(keys, yamlKeys...)
|
|
}
|
|
config := new(Config)
|
|
if err := viper.Unmarshal(config); err != nil {
|
|
return err
|
|
}
|
|
masterKey, err := MasterKey(cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
storage, err := keyStorage(config.Database, masterKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return storage.CreateKeys(cmd.Context(), keys...)
|
|
},
|
|
}
|
|
cmd.PersistentFlags().StringP(flagKeyFile, "f", "", "path to keys file")
|
|
return cmd
|
|
}
|
|
|
|
func keysFromArgs(args []string) ([]*crypto.Key, error) {
|
|
keys := make([]*crypto.Key, len(args))
|
|
for i, arg := range args {
|
|
key := strings.Split(arg, "=")
|
|
if len(key) != 2 {
|
|
return nil, zerrors.ThrowInternal(nil, "KEY-JKd82", "argument is not in the valid format [keyID=key]")
|
|
}
|
|
keys[i] = &crypto.Key{
|
|
ID: key[0],
|
|
Value: key[1],
|
|
}
|
|
}
|
|
return keys, nil
|
|
}
|
|
|
|
func keysFromYAML(file io.Reader) ([]*crypto.Key, error) {
|
|
data, err := io.ReadAll(file)
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "KEY-ajGFr", "unable to extract keys from file")
|
|
}
|
|
keysYAML := make(map[string]string)
|
|
if err = yaml.Unmarshal(data, &keysYAML); err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "KEY-sd34K", "unable to extract keys from file")
|
|
}
|
|
keys := make([]*crypto.Key, 0, len(keysYAML))
|
|
for id, key := range keysYAML {
|
|
keys = append(keys, &crypto.Key{
|
|
ID: id,
|
|
Value: key,
|
|
})
|
|
}
|
|
return keys, nil
|
|
}
|
|
|
|
func openFile(fileName string) (io.Reader, error) {
|
|
file, err := os.Open(fileName)
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternalf(err, "KEY-asGr2", "failed to open file: %s", fileName)
|
|
}
|
|
return file, nil
|
|
}
|
|
|
|
func keyStorage(config database.Config, masterKey string) (crypto.KeyStorage, error) {
|
|
db, err := database.Connect(config, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return cryptoDB.NewKeyStorage(db, masterKey)
|
|
}
|