|
| 1 | +package v1 |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "compress/flate" |
| 6 | + "compress/gzip" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + "net/url" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/go-kit/log" |
| 14 | + "github.com/go-kit/log/level" |
| 15 | + |
| 16 | + // nolint:staticcheck |
| 17 | + "github.com/golang/protobuf/jsonpb" |
| 18 | + "github.com/grafana/tempo/pkg/tempopb" |
| 19 | + commonv1 "github.com/grafana/tempo/pkg/tempopb/common/v1" |
| 20 | + tracev1 "github.com/grafana/tempo/pkg/tempopb/trace/v1" |
| 21 | + |
| 22 | + apilogsv1 "github.com/observatorium/api/api/logs/v1" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + namespaceAttributeKey = "k8s.namespace.name" |
| 27 | + serviceAttributeKey = "service.name" |
| 28 | +) |
| 29 | + |
| 30 | +func WithTraceQLNamespaceSelect() func(http.Handler) http.Handler { |
| 31 | + return func(next http.Handler) http.Handler { |
| 32 | + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 33 | + // do not run if request is not for Tempo |
| 34 | + if !strings.Contains(r.URL.Path, "/tempo") { |
| 35 | + next.ServeHTTP(w, r) |
| 36 | + return |
| 37 | + } |
| 38 | + |
| 39 | + // block other APIs than api/search and api/traces |
| 40 | + if !strings.Contains(r.URL.Path, "/api/traces") && |
| 41 | + !strings.Contains(r.URL.Path, "/api/search") { |
| 42 | + w.WriteHeader(http.StatusForbidden) |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + if strings.Contains(r.URL.Path, "/api/search") { |
| 47 | + query := r.URL.Query() |
| 48 | + q := query.Get("q") |
| 49 | + traceQL, err := url.QueryUnescape(q) |
| 50 | + if err != nil { |
| 51 | + next.ServeHTTP(w, r) |
| 52 | + return |
| 53 | + } |
| 54 | + if traceQL == "" { |
| 55 | + traceQL = "{ }" |
| 56 | + } |
| 57 | + traceQL = traceQL + " | select(resource.k8s.namespace.name)" |
| 58 | + query.Set("q", traceQL) |
| 59 | + r.URL.RawQuery = query.Encode() |
| 60 | + } |
| 61 | + |
| 62 | + next.ServeHTTP(w, r) |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func responseRBACModifier(log log.Logger) func(response *http.Response) error { |
| 68 | + return func(response *http.Response) error { |
| 69 | + if strings.Contains(response.Request.URL.Path, "/api/traces/") || strings.Contains(response.Request.URL.Path, "/api/search") { |
| 70 | + allowedNamespaces := map[string]bool{} |
| 71 | + namespaces := apilogsv1.AllowedNamespaces(response.Request.Context()) |
| 72 | + for _, ns := range namespaces { |
| 73 | + allowedNamespaces[ns] = true |
| 74 | + } |
| 75 | + level.Debug(log).Log("AllowedNamespaces", allowedNamespaces) |
| 76 | + |
| 77 | + if response.StatusCode == http.StatusOK { |
| 78 | + // Uncompressed reader |
| 79 | + var reader io.ReadCloser |
| 80 | + var err error |
| 81 | + |
| 82 | + // Read what Jaeger UI sent back (which might be compressed) |
| 83 | + switch response.Header.Get("Content-Encoding") { |
| 84 | + case "gzip": |
| 85 | + reader, err = gzip.NewReader(response.Body) |
| 86 | + if err != nil { |
| 87 | + return err |
| 88 | + } |
| 89 | + defer reader.Close() |
| 90 | + case "deflate": |
| 91 | + reader = flate.NewReader(response.Body) |
| 92 | + defer reader.Close() |
| 93 | + default: |
| 94 | + reader = response.Body |
| 95 | + } |
| 96 | + |
| 97 | + b, err := io.ReadAll(reader) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + responseBuffer := &bytes.Buffer{} |
| 103 | + if strings.Contains(response.Request.URL.Path, "/api/traces/") { |
| 104 | + trace := &tempopb.Trace{} |
| 105 | + err = tempopb.UnmarshalFromJSONV1(b, trace) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + trace = traceRBAC(allowedNamespaces, trace) |
| 110 | + |
| 111 | + traceResponseBody, err := tempopb.MarshalToJSONV1(trace) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + responseBuffer = bytes.NewBuffer(traceResponseBody) |
| 116 | + } else { |
| 117 | + searchResponse := &tempopb.SearchResponse{} |
| 118 | + err = jsonpb.UnmarshalString(string(b), searchResponse) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + searchResponse = searchResponseRBAC(allowedNamespaces, searchResponse) |
| 123 | + |
| 124 | + marshaller := jsonpb.Marshaler{} |
| 125 | + err = marshaller.Marshal(responseBuffer, searchResponse) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + } |
| 130 | + response.Body = io.NopCloser(responseBuffer) |
| 131 | + response.Header["Content-Length"] = []string{fmt.Sprint(responseBuffer.Len())} |
| 132 | + // We could re-encode in gzip/deflate, but there is no need, so send it raw |
| 133 | + response.Header["Content-Encoding"] = []string{} |
| 134 | + } |
| 135 | + |
| 136 | + return nil |
| 137 | + } |
| 138 | + |
| 139 | + return nil |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func traceRBAC(allowedNamespaces map[string]bool, trace *tempopb.Trace) *tempopb.Trace { |
| 144 | + for _, rs := range trace.ResourceSpans { |
| 145 | + notAllowedNamespace := "" |
| 146 | + missingNamespaceAttribute := true |
| 147 | + if rs.Resource != nil && rs.Resource.Attributes != nil { |
| 148 | + for _, resAttr := range rs.Resource.Attributes { |
| 149 | + if resAttr.Key == namespaceAttributeKey { |
| 150 | + missingNamespaceAttribute = false |
| 151 | + if !allowedNamespaces[resAttr.Value.GetStringValue()] { |
| 152 | + notAllowedNamespace = resAttr.Value.GetStringValue() |
| 153 | + for _, scopeSpan := range rs.ScopeSpans { |
| 154 | + scopeSpan.Scope.Attributes = []*commonv1.KeyValue{} |
| 155 | + for _, span := range scopeSpan.Spans { |
| 156 | + span.Attributes = []*commonv1.KeyValue{} |
| 157 | + span.Events = []*tracev1.Span_Event{} |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + // when resource attribute is missing, all attributes are removed |
| 164 | + if missingNamespaceAttribute { |
| 165 | + serviceAttribute := getAttribute(rs.Resource.Attributes, serviceAttributeKey) |
| 166 | + rs.Resource.Attributes = []*commonv1.KeyValue{} |
| 167 | + if serviceAttribute != nil { |
| 168 | + rs.Resource.Attributes = append(rs.Resource.Attributes, serviceAttribute) |
| 169 | + } |
| 170 | + for _, scopeSpan := range rs.ScopeSpans { |
| 171 | + scopeSpan.Scope.Attributes = []*commonv1.KeyValue{} |
| 172 | + for _, span := range scopeSpan.Spans { |
| 173 | + span.Attributes = []*commonv1.KeyValue{} |
| 174 | + span.Events = []*tracev1.Span_Event{} |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + // add namespace back if it was not allowed |
| 179 | + if notAllowedNamespace != "" { |
| 180 | + serviceAttribute := getAttribute(rs.Resource.Attributes, serviceAttributeKey) |
| 181 | + rs.Resource.Attributes = []*commonv1.KeyValue{} |
| 182 | + rs.Resource.Attributes = append(rs.Resource.Attributes, &commonv1.KeyValue{ |
| 183 | + Key: namespaceAttributeKey, |
| 184 | + Value: &commonv1.AnyValue{Value: &commonv1.AnyValue_StringValue{StringValue: notAllowedNamespace}}, |
| 185 | + }) |
| 186 | + rs.Resource.Attributes = append(rs.Resource.Attributes, serviceAttribute) |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + return trace |
| 191 | +} |
| 192 | + |
| 193 | +func getAttribute(attributes []*commonv1.KeyValue, key string) *commonv1.KeyValue { |
| 194 | + for _, attr := range attributes { |
| 195 | + if attr.GetKey() == key { |
| 196 | + return attr |
| 197 | + } |
| 198 | + } |
| 199 | + return nil |
| 200 | +} |
| 201 | + |
| 202 | +func searchResponseRBAC(allowedNamespaces map[string]bool, searchResponse *tempopb.SearchResponse) *tempopb.SearchResponse { |
| 203 | + for _, traceSearchMetadata := range searchResponse.GetTraces() { |
| 204 | + for i := range traceSearchMetadata.GetSpanSets() { |
| 205 | + traceSearchMetadata.SpanSets[i] = spanSetRBAC(allowedNamespaces, traceSearchMetadata.SpanSets[i]) |
| 206 | + } |
| 207 | + traceSearchMetadata.SpanSet = spanSetRBAC(allowedNamespaces, traceSearchMetadata.GetSpanSet()) |
| 208 | + } |
| 209 | + return searchResponse |
| 210 | +} |
| 211 | + |
| 212 | +func spanSetRBAC(allowedNamespaces map[string]bool, spanSet *tempopb.SpanSet) *tempopb.SpanSet { |
| 213 | + for _, span := range spanSet.GetSpans() { |
| 214 | + notAllowedNamespace := "" |
| 215 | + missingNamespaceAttribute := true |
| 216 | + for _, attribute := range span.GetAttributes() { |
| 217 | + if attribute.GetKey() == namespaceAttributeKey { |
| 218 | + missingNamespaceAttribute = false |
| 219 | + if !allowedNamespaces[attribute.GetValue().GetStringValue()] { |
| 220 | + notAllowedNamespace = attribute.GetValue().GetStringValue() |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + if missingNamespaceAttribute { |
| 225 | + span.Attributes = []*commonv1.KeyValue{} |
| 226 | + } |
| 227 | + // remove attributes because span is from not allowed namespace |
| 228 | + if notAllowedNamespace != "" { |
| 229 | + span.Attributes = []*commonv1.KeyValue{} |
| 230 | + span.Attributes = append(span.Attributes, &commonv1.KeyValue{ |
| 231 | + Key: namespaceAttributeKey, |
| 232 | + Value: &commonv1.AnyValue{Value: &commonv1.AnyValue_StringValue{StringValue: notAllowedNamespace}}, |
| 233 | + }) |
| 234 | + } |
| 235 | + } |
| 236 | + return spanSet |
| 237 | +} |
0 commit comments