mirror of
https://github.com/kubeshark/kubeshark.git
synced 2026-04-05 10:17:50 +00:00
* Fix the OOMKilled error by calling `debug.FreeOSMemory` periodically * Remove `MAX_NUMBER_OF_GOROUTINES` environment variable * Change the line * Increase the default value of `TCP_STREAM_CHANNEL_TIMEOUT_MS` to `10000` * Write the client and integrate to the new real-time database * Refactor the WebSocket implementaiton for `/ws` * Adapt the UI to the new filtering system * Fix the rest of the issues in the UI * Increase the buffer of the scanner * Implement accessing single records * Increase the buffer of another scanner * Populate `Request` and `Response` fields of `MizuEntry` * Add syntax highlighting for the query * Add database to `Dockerfile` * Fix some issues * Update the `realtime_dbms` Git module commit hash * Upgrade Gin version and print the query string * Revert "Upgrade Gin version and print the query string" This reverts commitaa09f904ee. * Use WebSocket's itself to query instead of the query string * Fix some errors related to conversion to HAR * Fix the issues caused by the latest merge * Fix the build error * Fix PR validation GitHub workflow * Replace the git submodule with latest Basenine version `0.1.0` Remove `realtime_client.go` and use the official client library `github.com/up9inc/basenine/client/go` instead. * Move Basenine host and port constants to `shared` module * Reliably execute and wait for Basenine to become available * Upgrade Basenine version * Properly close WebSocket and data channel * Fix the issues caused by the recent merge commit * Clean up the TypeScript code * Update `.gitignore` * Limit the database size * Add `Macros` method signature to `Dissector` interface and set the macros provided by the protocol extensions * Run `go mod tidy` on `agent` * Upgrade `github.com/up9inc/basenine/client/go` version * Implement a mechanism to update the query using click events in the UI and use it for protocol macros * Update the query on click to timestamps * Fix some issues in the WebSocket and channel handling * Update the query on clicks to status code * Update the query on clicks to method, path and service * Update the query on clicks to is outgoing, source and destination ports * Add an API endpoint to validate the query against syntax errors * Move the query background color state into `TrafficPage` * Fix the logic in `setQuery` * Display a toast message in case of a syntax error in the query * Remove a call to `fmt.Printf` * Upgrade Basenine version to `0.1.3` * Fix an issue related to getting `MAX_ENTRIES_DB_BYTES` environment variable * Have the `path` key in request details, in HTTP * Rearrange the HTTP headers for the querying * Do the same thing for `cookies` and `queryString` * Update the query on click to table elements Add the selectors for `TABLE` type representations in HTTP extension. * Update the query on click to `bodySize` and `elapsedTime` in `EntryTitle` * Add the selectors for `TABLE` type representations in AMQP extension * Add the selectors for `TABLE` type representations in Kafka extension * Add the selectors for `TABLE` type representations in Redis extension * Define a struct in `tap/api.go` for the section representation data * Add the selectors for `BODY` type representations * Add `request.path` to the HTTP request details * Change the summary string's field name from `path` to `summary` * Introduce `queryable` CSS class for queryable UI elements and underline them on hover * Instead of `N requests` at the bottom, make it `Displaying N results (queried X/Y)` and live update the values Upgrade Basenine version to `0.2.0`. * Verify the sha256sum of Basenine executable inside `Dockerfile` * Pass the start time to web UI through WebSocket and always show the `EntriesList` footer * Pipe the `stderr` of Basenine as well * Fix the layout issues related to `CodeEditor` in the UI * Use the correct `shasum` command in `Dockerfile` * Upgrade Basenine version to `0.2.1` * Limit the height of `CodeEditor` container * Remove `Paused` enum `ConnectionStatus` in UI * Fix the issue caused by the recent merge * Add the filtering guide (cheatsheet) * Update open cheatsheet button's title * Update cheatsheet content * Remove the old SQLite code, adapt the `--analyze` related code to Basenine * Change the method signature of `NewEntry` * Change the method signature of `Represent` * Introduce `HTTPPair` field in `MizuEntry` specific to HTTP * Remove `Entry`, `EntryId` and `EstimatedSizeBytes` fields from `MizuEntry` Also remove the `getEstimatedEntrySizeBytes` method. * Remove `gorm.io/gorm` dependency * Remove unused `sensitiveDataFiltering` folder * Increase the left margin of open cheatsheet button * Add `overflow: auto` to the cheatsheet `Modal` * Fix `GetEntry` method * Fix the macro for gRPC * Fix an interface conversion in case of AMQP * Fix two more interface conversion errors in AMQP * Make the `syncEntriesImpl` method blocking * Fix a grammar mistake in the cheatsheet * Adapt to the changes in the recent merge commit * Improve the cheatsheet text * Always display the timestamp in `en-US` * Upgrade Basenine version to `0.2.2` * Fix the order of closing Basenine connections and channels * Don't close the Basenine channels at all * Upgrade Basenine version to `0.2.3` * Set the initial filter to `rlimit(100)` * Make Basenine persistent * Upgrade Basenine version to `0.2.4` * Update `debug.Dockerfile` * Fix a failing test * Upgrade Basenine version to `0.2.5` * Revert "Do not show play icon when disconnected (#428)" This reverts commit8af2e562f8. * Upgrade Basenine version to `0.2.6` * Make all non-informative things informative * Make `100` a constant * Use `===` in JavaScript no matter what * Remove a forgotten `console.log` * Add a comment and update the `query` in `syncEntriesImpl` * Don't call `panic` in `GetEntry` * Replace `panic` calls in `startBasenineServer` with `logger.Log.Panicf` * Remove unnecessary `\n` characters in the logs
404 lines
12 KiB
Go
404 lines
12 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/up9inc/mizu/tap/api"
|
|
"k8s.io/apimachinery/pkg/util/json"
|
|
|
|
"github.com/up9inc/mizu/shared"
|
|
"github.com/up9inc/mizu/shared/logger"
|
|
|
|
"github.com/creasty/defaults"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
"github.com/up9inc/mizu/cli/uiUtils"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
const (
|
|
Separator = "="
|
|
SetCommandName = "set"
|
|
FieldNameTag = "yaml"
|
|
ReadonlyTag = "readonly"
|
|
)
|
|
|
|
var (
|
|
Config = ConfigStruct{}
|
|
cmdName string
|
|
)
|
|
|
|
func InitConfig(cmd *cobra.Command) error {
|
|
cmdName = cmd.Name()
|
|
|
|
if err := defaults.Set(&Config); err != nil {
|
|
return err
|
|
}
|
|
|
|
configFilePathFlag := cmd.Flags().Lookup(ConfigFilePathCommandName)
|
|
configFilePath := configFilePathFlag.Value.String()
|
|
if err := loadConfigFile(configFilePath, &Config); err != nil {
|
|
if configFilePathFlag.Changed || !os.IsNotExist(err) {
|
|
return fmt.Errorf("invalid config, %w\n"+
|
|
"you can regenerate the file by removing it (%v) and using `mizu config -r`", err, configFilePath)
|
|
}
|
|
}
|
|
|
|
cmd.Flags().Visit(initFlag)
|
|
|
|
finalConfigPrettified, _ := uiUtils.PrettyJson(Config)
|
|
logger.Log.Debugf("Init config finished\n Final config: %v", finalConfigPrettified)
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetConfigWithDefaults() (*ConfigStruct, error) {
|
|
defaultConf := ConfigStruct{}
|
|
if err := defaults.Set(&defaultConf); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
configElem := reflect.ValueOf(&defaultConf).Elem()
|
|
setZeroForReadonlyFields(configElem)
|
|
|
|
return &defaultConf, nil
|
|
}
|
|
|
|
func WriteConfig(config *ConfigStruct) error {
|
|
template, err := uiUtils.PrettyYaml(config)
|
|
if err != nil {
|
|
return fmt.Errorf("failed converting config to yaml, err: %v", err)
|
|
}
|
|
|
|
data := []byte(template)
|
|
if err := ioutil.WriteFile(Config.ConfigFilePath, data, 0644); err != nil {
|
|
return fmt.Errorf("failed writing config, err: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type updateConfigStruct func(*ConfigStruct)
|
|
|
|
func UpdateConfig(updateConfigStruct updateConfigStruct) error {
|
|
configFile, err := GetConfigWithDefaults()
|
|
if err != nil {
|
|
return fmt.Errorf("failed getting config with defaults, err: %v", err)
|
|
}
|
|
|
|
if err := loadConfigFile(Config.ConfigFilePath, configFile); err != nil && !os.IsNotExist(err) {
|
|
return fmt.Errorf("failed getting config file, err: %v", err)
|
|
}
|
|
|
|
updateConfigStruct(configFile)
|
|
|
|
if err := WriteConfig(configFile); err != nil {
|
|
return fmt.Errorf("failed writing config, err: %v", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func loadConfigFile(configFilePath string, config *ConfigStruct) error {
|
|
reader, openErr := os.Open(configFilePath)
|
|
if openErr != nil {
|
|
return openErr
|
|
}
|
|
|
|
buf, readErr := ioutil.ReadAll(reader)
|
|
if readErr != nil {
|
|
return readErr
|
|
}
|
|
|
|
if err := yaml.Unmarshal(buf, config); err != nil {
|
|
return err
|
|
}
|
|
|
|
logger.Log.Debugf("Found config file, config path: %s", configFilePath)
|
|
|
|
return nil
|
|
}
|
|
|
|
func initFlag(f *pflag.Flag) {
|
|
configElemValue := reflect.ValueOf(&Config).Elem()
|
|
|
|
var flagPath []string
|
|
if shared.Contains([]string{ConfigFilePathCommandName}, f.Name) {
|
|
flagPath = []string{f.Name}
|
|
} else {
|
|
flagPath = []string{cmdName, f.Name}
|
|
}
|
|
|
|
sliceValue, isSliceValue := f.Value.(pflag.SliceValue)
|
|
if !isSliceValue {
|
|
if err := mergeFlagValue(configElemValue, flagPath, strings.Join(flagPath, "."), f.Value.String()); err != nil {
|
|
logger.Log.Warningf(uiUtils.Warning, err)
|
|
}
|
|
return
|
|
}
|
|
|
|
if f.Name == SetCommandName {
|
|
if err := mergeSetFlag(configElemValue, sliceValue.GetSlice()); err != nil {
|
|
logger.Log.Warningf(uiUtils.Warning, err)
|
|
}
|
|
return
|
|
}
|
|
|
|
if err := mergeFlagValues(configElemValue, flagPath, strings.Join(flagPath, "."), sliceValue.GetSlice()); err != nil {
|
|
logger.Log.Warningf(uiUtils.Warning, err)
|
|
}
|
|
}
|
|
|
|
func mergeSetFlag(configElemValue reflect.Value, setValues []string) error {
|
|
var setErrors []string
|
|
setMap := map[string][]string{}
|
|
|
|
for _, setValue := range setValues {
|
|
if !strings.Contains(setValue, Separator) {
|
|
setErrors = append(setErrors, fmt.Sprintf("Ignoring set argument %s (set argument format: <flag name>=<flag value>)", setValue))
|
|
continue
|
|
}
|
|
|
|
split := strings.SplitN(setValue, Separator, 2)
|
|
argumentKey, argumentValue := split[0], split[1]
|
|
|
|
setMap[argumentKey] = append(setMap[argumentKey], argumentValue)
|
|
}
|
|
|
|
for argumentKey, argumentValues := range setMap {
|
|
flagPath := strings.Split(argumentKey, ".")
|
|
|
|
if len(argumentValues) > 1 {
|
|
if err := mergeFlagValues(configElemValue, flagPath, argumentKey, argumentValues); err != nil {
|
|
setErrors = append(setErrors, fmt.Sprintf("%v", err))
|
|
}
|
|
} else {
|
|
if err := mergeFlagValue(configElemValue, flagPath, argumentKey, argumentValues[0]); err != nil {
|
|
setErrors = append(setErrors, fmt.Sprintf("%v", err))
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(setErrors) > 0 {
|
|
return fmt.Errorf(strings.Join(setErrors, "\n"))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func mergeFlagValue(configElemValue reflect.Value, flagPath []string, fullFlagName string, flagValue string) error {
|
|
mergeFunction := func(flagName string, currentFieldStruct reflect.StructField, currentFieldElemValue reflect.Value, currentElemValue reflect.Value) error {
|
|
currentFieldKind := currentFieldStruct.Type.Kind()
|
|
|
|
if currentFieldKind == reflect.Slice {
|
|
return mergeFlagValues(currentElemValue, []string{flagName}, fullFlagName, []string{flagValue})
|
|
}
|
|
|
|
parsedValue, err := getParsedValue(currentFieldKind, flagValue)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid value %s for flag name %s, expected %s", flagValue, flagName, currentFieldKind)
|
|
}
|
|
|
|
currentFieldElemValue.Set(parsedValue)
|
|
return nil
|
|
}
|
|
|
|
return mergeFlag(configElemValue, flagPath, fullFlagName, mergeFunction)
|
|
}
|
|
|
|
func mergeFlagValues(configElemValue reflect.Value, flagPath []string, fullFlagName string, flagValues []string) error {
|
|
mergeFunction := func(flagName string, currentFieldStruct reflect.StructField, currentFieldElemValue reflect.Value, currentElemValue reflect.Value) error {
|
|
currentFieldKind := currentFieldStruct.Type.Kind()
|
|
|
|
if currentFieldKind != reflect.Slice {
|
|
return fmt.Errorf("invalid values %s for flag name %s, expected %s", strings.Join(flagValues, ","), flagName, currentFieldKind)
|
|
}
|
|
|
|
flagValueKind := currentFieldStruct.Type.Elem().Kind()
|
|
|
|
parsedValues := reflect.MakeSlice(reflect.SliceOf(currentFieldStruct.Type.Elem()), 0, 0)
|
|
for _, flagValue := range flagValues {
|
|
parsedValue, err := getParsedValue(flagValueKind, flagValue)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid value %s for flag name %s, expected %s", flagValue, flagName, flagValueKind)
|
|
}
|
|
|
|
parsedValues = reflect.Append(parsedValues, parsedValue)
|
|
}
|
|
|
|
currentFieldElemValue.Set(parsedValues)
|
|
return nil
|
|
}
|
|
|
|
return mergeFlag(configElemValue, flagPath, fullFlagName, mergeFunction)
|
|
}
|
|
|
|
func mergeFlag(currentElemValue reflect.Value, currentFlagPath []string, fullFlagName string, mergeFunction func(flagName string, currentFieldStruct reflect.StructField, currentFieldElemValue reflect.Value, currentElemValue reflect.Value) error) error {
|
|
if len(currentFlagPath) == 0 {
|
|
return fmt.Errorf("flag \"%s\" not found", fullFlagName)
|
|
}
|
|
|
|
for i := 0; i < currentElemValue.NumField(); i++ {
|
|
currentFieldStruct := currentElemValue.Type().Field(i)
|
|
currentFieldElemValue := currentElemValue.FieldByName(currentFieldStruct.Name)
|
|
|
|
if currentFieldStruct.Type.Kind() == reflect.Struct && getFieldNameByTag(currentFieldStruct) == currentFlagPath[0] {
|
|
return mergeFlag(currentFieldElemValue, currentFlagPath[1:], fullFlagName, mergeFunction)
|
|
}
|
|
|
|
if len(currentFlagPath) > 1 || getFieldNameByTag(currentFieldStruct) != currentFlagPath[0] {
|
|
continue
|
|
}
|
|
|
|
return mergeFunction(currentFlagPath[0], currentFieldStruct, currentFieldElemValue, currentElemValue)
|
|
}
|
|
|
|
return fmt.Errorf("flag \"%s\" not found", fullFlagName)
|
|
}
|
|
|
|
func getFieldNameByTag(field reflect.StructField) string {
|
|
return strings.Split(field.Tag.Get(FieldNameTag), ",")[0]
|
|
}
|
|
|
|
func getParsedValue(kind reflect.Kind, value string) (reflect.Value, error) {
|
|
switch kind {
|
|
case reflect.String:
|
|
return reflect.ValueOf(value), nil
|
|
case reflect.Bool:
|
|
boolArgumentValue, err := strconv.ParseBool(value)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(boolArgumentValue), nil
|
|
case reflect.Int:
|
|
intArgumentValue, err := strconv.ParseInt(value, 10, 64)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(int(intArgumentValue)), nil
|
|
case reflect.Int8:
|
|
intArgumentValue, err := strconv.ParseInt(value, 10, 8)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(int8(intArgumentValue)), nil
|
|
case reflect.Int16:
|
|
intArgumentValue, err := strconv.ParseInt(value, 10, 16)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(int16(intArgumentValue)), nil
|
|
case reflect.Int32:
|
|
intArgumentValue, err := strconv.ParseInt(value, 10, 32)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(int32(intArgumentValue)), nil
|
|
case reflect.Int64:
|
|
intArgumentValue, err := strconv.ParseInt(value, 10, 64)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(intArgumentValue), nil
|
|
case reflect.Uint:
|
|
uintArgumentValue, err := strconv.ParseUint(value, 10, 64)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(uint(uintArgumentValue)), nil
|
|
case reflect.Uint8:
|
|
uintArgumentValue, err := strconv.ParseUint(value, 10, 8)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(uint8(uintArgumentValue)), nil
|
|
case reflect.Uint16:
|
|
uintArgumentValue, err := strconv.ParseUint(value, 10, 16)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(uint16(uintArgumentValue)), nil
|
|
case reflect.Uint32:
|
|
uintArgumentValue, err := strconv.ParseUint(value, 10, 32)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(uint32(uintArgumentValue)), nil
|
|
case reflect.Uint64:
|
|
uintArgumentValue, err := strconv.ParseUint(value, 10, 64)
|
|
if err != nil {
|
|
break
|
|
}
|
|
|
|
return reflect.ValueOf(uintArgumentValue), nil
|
|
}
|
|
|
|
return reflect.ValueOf(nil), errors.New("value to parse does not match type")
|
|
}
|
|
|
|
func setZeroForReadonlyFields(currentElem reflect.Value) {
|
|
for i := 0; i < currentElem.NumField(); i++ {
|
|
currentField := currentElem.Type().Field(i)
|
|
currentFieldByName := currentElem.FieldByName(currentField.Name)
|
|
|
|
if currentField.Type.Kind() == reflect.Struct {
|
|
setZeroForReadonlyFields(currentFieldByName)
|
|
continue
|
|
}
|
|
|
|
if _, ok := currentField.Tag.Lookup(ReadonlyTag); ok {
|
|
currentFieldByName.Set(reflect.Zero(currentField.Type))
|
|
}
|
|
}
|
|
}
|
|
|
|
func GetSerializedMizuAgentConfig(targetNamespaces []string, mizuApiFilteringOptions *api.TrafficFilteringOptions) (string, error) {
|
|
mizuConfig, err := getMizuAgentConfig(targetNamespaces, mizuApiFilteringOptions)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
serializedConfig, err := json.Marshal(mizuConfig)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(serializedConfig), nil
|
|
}
|
|
|
|
func getMizuAgentConfig(targetNamespaces []string, mizuApiFilteringOptions *api.TrafficFilteringOptions) (*shared.MizuAgentConfig, error) {
|
|
serializableRegex, err := api.CompileRegexToSerializableRegexp(Config.Tap.PodRegexStr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
config := shared.MizuAgentConfig{
|
|
TapTargetRegex: *serializableRegex,
|
|
MaxDBSizeBytes: Config.Tap.MaxEntriesDBSizeBytes(),
|
|
DaemonMode: Config.Tap.DaemonMode,
|
|
TargetNamespaces: targetNamespaces,
|
|
AgentImage: Config.AgentImage,
|
|
PullPolicy: Config.ImagePullPolicyStr,
|
|
DumpLogs: Config.DumpLogs,
|
|
IgnoredUserAgents: Config.Tap.IgnoredUserAgents,
|
|
TapperResources: Config.Tap.TapperResources,
|
|
MizuResourcesNamespace: Config.MizuResourcesNamespace,
|
|
MizuApiFilteringOptions: *mizuApiFilteringOptions,
|
|
AgentDatabasePath: shared.DataDirPath,
|
|
}
|
|
return &config, nil
|
|
}
|