|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright Authors of Tetragon |
| 3 | + |
| 4 | +package tetragoninfo |
| 5 | + |
| 6 | +import ( |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/spf13/viper" |
| 11 | + "google.golang.org/protobuf/types/known/anypb" |
| 12 | + "google.golang.org/protobuf/types/known/structpb" |
| 13 | + "google.golang.org/protobuf/types/known/wrapperspb" |
| 14 | + |
| 15 | + "github.com/cilium/tetragon/api/v1/tetragon" |
| 16 | + "github.com/cilium/tetragon/pkg/version" |
| 17 | +) |
| 18 | + |
| 19 | +// Info is a simplified version of tetragon.GetInfoResponse. |
| 20 | +type Info struct { |
| 21 | + Name string |
| 22 | + Version string |
| 23 | + Probes map[string]bool |
| 24 | + Conf map[string]any |
| 25 | + Build version.BuildInfo |
| 26 | +} |
| 27 | + |
| 28 | +// Decode decodes a gRPC response into an Info structure |
| 29 | +func Decode(res *tetragon.GetInfoResponse) *Info { |
| 30 | + probes := make(map[string]bool, len(res.Probes)) |
| 31 | + for _, probe := range res.Probes { |
| 32 | + probes[probe.Name] = probe.Enabled.GetValue() |
| 33 | + } |
| 34 | + return &Info{ |
| 35 | + Name: res.Name, |
| 36 | + Version: res.Version, |
| 37 | + Probes: probes, |
| 38 | + Conf: decodeConf(res.Conf), |
| 39 | + Build: version.BuildInfo{ |
| 40 | + GoVersion: res.GetBuild().GoVersion, |
| 41 | + Commit: res.GetBuild().Commit, |
| 42 | + Time: res.GetBuild().Time, |
| 43 | + Modified: res.GetBuild().Modified, |
| 44 | + }, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// decodeConf decodes the confiugration part of info |
| 49 | +func decodeConf(conf []*tetragon.GetInfoResponse_ConfVal) map[string]any { |
| 50 | + ret := make(map[string]any, len(conf)) |
| 51 | + for _, cnf := range conf { |
| 52 | + var val any |
| 53 | + key := cnf.GetKey() |
| 54 | + value, err := cnf.GetValue().UnmarshalNew() |
| 55 | + if err != nil { |
| 56 | + ret[key] = fmt.Errorf("failed to unmarshall value: %w", err) |
| 57 | + continue |
| 58 | + } |
| 59 | + switch v := value.(type) { |
| 60 | + case *wrapperspb.StringValue: |
| 61 | + val = v.GetValue() |
| 62 | + case *wrapperspb.Int64Value: |
| 63 | + val = v.GetValue() |
| 64 | + case *wrapperspb.BoolValue: |
| 65 | + val = v.GetValue() |
| 66 | + case *structpb.ListValue: |
| 67 | + val = v.AsSlice() |
| 68 | + default: |
| 69 | + val = fmt.Errorf("unknown value type: %T", val) |
| 70 | + } |
| 71 | + ret[key] = val |
| 72 | + } |
| 73 | + return ret |
| 74 | +} |
| 75 | + |
| 76 | +// Gather gathers all the necessary inormation and encodes it in the appropriate gRPC message |
| 77 | +func Gather() *tetragon.GetInfoResponse { |
| 78 | + // NB: ignore errors, and let's provide users with partial information when this happens |
| 79 | + conf, _ := buildConf() |
| 80 | + build := version.ReadBuildInfo() |
| 81 | + res := &tetragon.GetInfoResponse{ |
| 82 | + Name: version.Name, |
| 83 | + Version: version.Version, |
| 84 | + Probes: bpfProbes(), |
| 85 | + Conf: conf, |
| 86 | + Build: &tetragon.GetInfoResponse_BuildInfo{ |
| 87 | + GoVersion: build.GoVersion, |
| 88 | + Commit: build.Commit, |
| 89 | + Time: build.Time, |
| 90 | + Modified: build.Modified, |
| 91 | + }, |
| 92 | + } |
| 93 | + return res |
| 94 | +} |
| 95 | + |
| 96 | +func buildConf() ([]*tetragon.GetInfoResponse_ConfVal, error) { |
| 97 | + return encodeConf(viper.AllSettings()) |
| 98 | +} |
| 99 | + |
| 100 | +func encodeConf(conf map[string]any) ([]*tetragon.GetInfoResponse_ConfVal, error) { |
| 101 | + var ret []*tetragon.GetInfoResponse_ConfVal |
| 102 | + var retErr error |
| 103 | + |
| 104 | + for key, val := range conf { |
| 105 | + var value *anypb.Any |
| 106 | + var err error |
| 107 | + switch x := val.(type) { |
| 108 | + case string: |
| 109 | + value, err = anypb.New(wrapperspb.String(x)) |
| 110 | + case bool: |
| 111 | + value, err = anypb.New(wrapperspb.Bool(x)) |
| 112 | + case int: |
| 113 | + value, err = anypb.New(wrapperspb.Int64(int64(x))) |
| 114 | + case []string: |
| 115 | + values := make([]*structpb.Value, 0, len(x)) |
| 116 | + for _, s := range x { |
| 117 | + values = append(values, structpb.NewStringValue(s)) |
| 118 | + } |
| 119 | + value, err = anypb.New(&structpb.ListValue{ |
| 120 | + Values: values, |
| 121 | + }) |
| 122 | + default: |
| 123 | + err = fmt.Errorf("unknown type: %T", val) |
| 124 | + } |
| 125 | + |
| 126 | + if err != nil { |
| 127 | + err := fmt.Errorf("failed to wrap type key %s (with value type %T): %w", key, val, err) |
| 128 | + errors.Join(retErr, err) |
| 129 | + } |
| 130 | + ret = append(ret, &tetragon.GetInfoResponse_ConfVal{ |
| 131 | + Key: key, |
| 132 | + Value: value, |
| 133 | + }) |
| 134 | + } |
| 135 | + return ret, retErr |
| 136 | +} |
0 commit comments