Skip to content

Commit fb310b7

Browse files
authored
Make unmarshaling json config strict (#279)
* Replace yaml.Unmarshal() with strict version * Replace json.Unmarshal() with a strict version in config * Mark DeserializeJSONToMap() as a helper func * Update generated files * Fix e2e config
1 parent d7656a2 commit fb310b7

File tree

12 files changed

+107
-61
lines changed

12 files changed

+107
-61
lines changed

contrib/kubernetes/flowlogs-pipeline.conf.yaml

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
11
# This file was generated automatically by flowlogs-pipeline confgenerator
22
log-level: error
3+
pipeline:
4+
- name: ingest_collector
5+
- name: transform_generic
6+
follows: ingest_collector
7+
- name: transform_network
8+
follows: transform_generic
9+
- name: extract_aggregate
10+
follows: transform_network
11+
- name: encode_prom
12+
follows: extract_aggregate
13+
- name: write_loki
14+
follows: transform_network
315
parameters:
4-
- ingest:
16+
- name: ingest_collector
17+
ingest:
18+
type: collector
519
collector:
6-
hostname: 0.0.0.0
20+
hostName: 0.0.0.0
721
port: 2055
822
portLegacy: 2056
9-
type: collector
10-
name: ingest_collector
1123
- name: transform_generic
1224
transform:
25+
type: generic
1326
generic:
1427
policy: replace_keys
1528
rules:
@@ -31,9 +44,9 @@ parameters:
3144
output: srcAS
3245
- input: DstAS
3346
output: dstAS
34-
type: generic
3547
- name: transform_network
3648
transform:
49+
type: network
3750
network:
3851
rules:
3952
- input: dstPort
@@ -71,8 +84,9 @@ parameters:
7184
- input: dstIP
7285
output: dstLocation
7386
type: add_location
74-
type: network
75-
- extract:
87+
- name: extract_aggregate
88+
extract:
89+
type: aggregates
7690
aggregates:
7791
- name: bandwidth_network_service
7892
by:
@@ -140,9 +154,9 @@ parameters:
140154
by:
141155
- service
142156
operation: count
143-
type: aggregates
144-
name: extract_aggregate
145-
- encode:
157+
- name: encode_prom
158+
encode:
159+
type: prom
146160
prom:
147161
metrics:
148162
- name: bandwidth_per_network_service
@@ -292,25 +306,11 @@ parameters:
292306
buckets: []
293307
port: 9102
294308
prefix: flp_
295-
type: prom
296-
name: encode_prom
297309
- name: write_loki
298310
write:
311+
type: loki
299312
loki:
300313
url: http://loki.default.svc.cluster.local:3100
301314
staticLabels:
302315
job: flowlogs-pipeline
303-
type: loki
304-
pipeline:
305-
- name: ingest_collector
306-
- follows: ingest_collector
307-
name: transform_generic
308-
- follows: transform_generic
309-
name: transform_network
310-
- follows: transform_network
311-
name: extract_aggregate
312-
- follows: extract_aggregate
313-
name: encode_prom
314-
- follows: transform_network
315-
name: write_loki
316316

docs/metrics.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ and the transformation to generate the exported metric.
1717
|:---|:---|
1818
| **Details** | Sum bytes for all traffic per network service |
1919
| **Usage** | Evaluate network usage breakdown per network service |
20-
| **Labels** | bandwidth, graph, rate, network-service |
20+
| **Tags** | bandwidth, graph, rate, network-service |
2121
| **Operation** | aggregate by `service` and `sum` field `bytes` |
2222
| **Exposed as** | `flp_bandwidth_per_network_service` of type `counter` |
2323
| **Visualized as** | "Bandwidth per network service" on dashboard `details` |
@@ -29,7 +29,7 @@ and the transformation to generate the exported metric.
2929
|:---|:---|
3030
| **Details** | Sum bandwidth bytes for all traffic per source / destination subnet pair |
3131
| **Usage** | Evaluate network usage breakdown per source / destination subnet pair |
32-
| **Labels** | bandwidth, graph, rate, subnet |
32+
| **Tags** | bandwidth, graph, rate, subnet |
3333
| **Operation** | aggregate by `dstSubnet24, srcSubnet24` and `sum` field `bytes` |
3434
| **Exposed as** | `flp_bandwidth_per_source_destination_subnet` of type `counter` |
3535
| **Visualized as** | "Bandwidth per src and destination subnet" on dashboard `details` |
@@ -41,7 +41,7 @@ and the transformation to generate the exported metric.
4141
|:---|:---|
4242
| **Details** | Sum bytes for all traffic per source subnet |
4343
| **Usage** | Evaluate network usage breakdown per source subnet |
44-
| **Labels** | bandwidth, graph, rate, subnet |
44+
| **Tags** | bandwidth, graph, rate, subnet |
4545
| **Operation** | aggregate by `srcSubnet` and `sum` field `bytes` |
4646
| **Exposed as** | `flp_bandwidth_per_source_subnet` of type `counter` |
4747
| **Visualized as** | "Bandwidth per source subnet" on dashboard `details` |
@@ -53,7 +53,7 @@ and the transformation to generate the exported metric.
5353
|:---|:---|
5454
| **Details** | Counts the number of connections per subnet with network prefix length /16 (using conn_tracking sum isNewFlow field) |
5555
| **Usage** | Evaluate network connections per subnet |
56-
| **Labels** | rate, subnet |
56+
| **Tags** | rate, subnet |
5757
| **Operation** | aggregate by `dstSubnet` and `count` field `isNewFlow` |
5858
| **Exposed as** | `flp_connections_per_destination_subnet` of type `counter` |
5959
| **Visualized as** | "Connections rate per destinationIP /16 subnets" on dashboard `details` |
@@ -65,7 +65,7 @@ and the transformation to generate the exported metric.
6565
|:---|:---|
6666
| **Details** | Counts the number of connections per subnet with network prefix length /16 |
6767
| **Usage** | Evaluate network connections per subnet |
68-
| **Labels** | rate, subnet |
68+
| **Tags** | rate, subnet |
6969
| **Operation** | aggregate by `srcSubnet` and `count` |
7070
| **Exposed as** | `flp_connections_per_source_subnet` of type `counter` |
7171
| **Visualized as** | "Connections rate per sourceIP /16 subnets" on dashboard `details` |
@@ -77,7 +77,7 @@ and the transformation to generate the exported metric.
7777
|:---|:---|
7878
| **Details** | Counts the number of connections per tcp flags |
7979
| **Usage** | Evaluate difference in connections rate of different TCP Flags. Can be used, for example, to identify syn-attacks. |
80-
| **Labels** | rate, TCPFlags |
80+
| **Tags** | rate, TCPFlags |
8181
| **Operation** | aggregate by `TCPFlags` and `count` |
8282
| **Exposed as** | `flp_connections_per_tcp_flags` of type `counter` |
8383
| **Visualized as** | "Connections rate per TCPFlags" on dashboard `details` |
@@ -89,7 +89,7 @@ and the transformation to generate the exported metric.
8989
|:---|:---|
9090
| **Details** | Aggregates flow records by values of "DstAS" field and counts the number of entries in each aggregate with non zero value |
9191
| **Usage** | Evaluate amount of connections targeted at different Autonomous Systems |
92-
| **Labels** | rate, count, AS |
92+
| **Tags** | rate, count, AS |
9393
| **Operation** | aggregate by `dstAS` and `count` |
9494
| **Exposed as** | `flp_connections_per_destination_as` of type `counter` |
9595
| **Visualized as** | "Connections rate per destination AS" on dashboard `details` |
@@ -101,7 +101,7 @@ and the transformation to generate the exported metric.
101101
|:---|:---|
102102
| **Details** | Aggregates flow records by values of "SrcAS" field and counts the number of entries in each aggregate with non zero value |
103103
| **Usage** | Evaluate amount of connections initiated by different Autonomous Systems |
104-
| **Labels** | rate, count, AS |
104+
| **Tags** | rate, count, AS |
105105
| **Operation** | aggregate by `srcAS` and `count` |
106106
| **Exposed as** | `flp_connections_per_source_as` of type `counter` |
107107
| **Visualized as** | "Connections rate per source AS" on dashboard `details` |
@@ -113,7 +113,7 @@ and the transformation to generate the exported metric.
113113
|:---|:---|
114114
| **Details** | Count the number of distinct source / destination subnet pairs |
115115
| **Usage** | Evaluate network usage breakdown per source / destination subnet pair |
116-
| **Labels** | count, graph, rate, subnet |
116+
| **Tags** | count, graph, rate, subnet |
117117
| **Operation** | aggregate by `dstSubnet24, srcSubnet24` and `count` |
118118
| **Exposed as** | `flp_count_per_source_destination_subnet` of type `counter` |
119119
| **Visualized as** | "Connections rate of src / destination subnet occurences" on dashboard `details` |
@@ -125,7 +125,7 @@ and the transformation to generate the exported metric.
125125
|:---|:---|
126126
| **Details** | Sum egress bytes for all traffic per destination subnet |
127127
| **Usage** | Evaluate network usage breakdown per destination subnet |
128-
| **Labels** | bandwidth, graph, rate, subnet |
128+
| **Tags** | bandwidth, graph, rate, subnet |
129129
| **Operation** | aggregate by `dstSubnet` and `sum` field `bytes` |
130130
| **Exposed as** | `flp_egress_per_destination_subnet` of type `counter` |
131131
| **Visualized as** | "Bandwidth per destination subnet" on dashboard `details` |
@@ -138,7 +138,7 @@ and the transformation to generate the exported metric.
138138
|:---|:---|
139139
| **Details** | Sum egress bytes for all traffic per namespace |
140140
| **Usage** | Evaluate network usage breakdown per namespace |
141-
| **Labels** | kubernetes, bandwidth, graph |
141+
| **Tags** | kubernetes, bandwidth, graph |
142142
| **Operation** | aggregate by `srcK8S_Namespace, srcK8S_Type` and `sum` field `bytes` |
143143
| **Exposed as** | `flp_egress_per_namespace` of type `counter` |
144144
| **Visualized as** | "Bandwidth per namespace" on dashboard `details` |
@@ -150,7 +150,7 @@ and the transformation to generate the exported metric.
150150
|:---|:---|
151151
| **Details** | Flows length distribution over time |
152152
| **Usage** | Evaluate flows length behavior including mice/elephant use-case |
153-
| **Labels** | bandwidth, mice, elephant, rate |
153+
| **Tags** | bandwidth, mice, elephant, rate |
154154
| **Operation** | aggregate by `all_Evaluate` and `raw_values` field `bytes` |
155155
| **Exposed as** | `flp_flows_length_histogram` of type `histogram` |
156156
| **Visualized as** | "Flows length heatmap" on dashboard `details` |
@@ -163,7 +163,7 @@ and the transformation to generate the exported metric.
163163
|:---|:---|
164164
| **Details** | Counts the number of connections per geo-location based on destination IP |
165165
| **Usage** | Evaluate network connections geo-location |
166-
| **Labels** | rate, connections-count, geo-location, destinationIP |
166+
| **Tags** | rate, connections-count, geo-location, destinationIP |
167167
| **Operation** | aggregate by `dstLocation_CountryName` and `count` |
168168
| **Exposed as** | `flp_connections_per_destination_location` of type `counter` |
169169
| **Visualized as** | "Connections rate per destinationIP geo-location" on dashboard `details` |
@@ -175,7 +175,7 @@ and the transformation to generate the exported metric.
175175
|:---|:---|
176176
| **Details** | Sum bytes for all traffic per source namespace |
177177
| **Usage** | Evaluate network usage breakdown per source namespace |
178-
| **Labels** | loki, graph, rate, namespace |
178+
| **Tags** | loki, graph, rate, namespace |
179179
| **Visualized as** | "Bandwidth per source namespace" on dashboard `details` |
180180
|||
181181

@@ -185,7 +185,7 @@ and the transformation to generate the exported metric.
185185
|:---|:---|
186186
| **Details** | Rate of loki logs per sec |
187187
| **Usage** | Evaluate loki service usage |
188-
| **Labels** | loki, graph, rate |
188+
| **Tags** | loki, graph, rate |
189189
| **Visualized as** | "Loki logs rate" on dashboard `details` |
190190
|||
191191

@@ -195,7 +195,7 @@ and the transformation to generate the exported metric.
195195
|:---|:---|
196196
| **Details** | Counts the number of connections per network service based on destination port number and protocol |
197197
| **Usage** | Evaluate network services |
198-
| **Labels** | rate, network-services, destination-port, destination-protocol |
198+
| **Tags** | rate, network-services, destination-port, destination-protocol |
199199
| **Operation** | aggregate by `service` and `count` |
200200
| **Exposed as** | `flp_service_count` of type `counter` |
201201
| **Visualized as** | "Network services connections rate" on dashboard `details` |

pkg/confgen/confgen.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ func (cg *ConfGen) ParseDefinition(name string, bytes []byte) error {
145145

146146
// parse yaml
147147
var defFile DefFile
148-
err = yaml.Unmarshal(bytes, &defFile)
148+
err = yaml.UnmarshalStrict(bytes, &defFile)
149149
if err != nil {
150-
log.Debugf("%s yaml.Unmarshal err: %v ", name, err)
150+
log.Debugf("%s yaml.UnmarshalStrict err: %v ", name, err)
151151
return err
152152
}
153153

pkg/confgen/confgen_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func Test_RunShortConfGen(t *testing.T) {
192192
destCfgBytes, err := ioutil.ReadFile(configOut)
193193
require.NoError(t, err)
194194
var out config.ConfigFileStruct
195-
err = yaml.Unmarshal(destCfgBytes, &out)
195+
err = yaml.UnmarshalStrict(destCfgBytes, &out)
196196
require.NoError(t, err)
197197
require.Len(t, out.Pipeline, 4)
198198
require.Len(t, out.Parameters, 4)
@@ -280,7 +280,7 @@ func Test_RunLongConfGen(t *testing.T) {
280280
destCfgBytes, err := ioutil.ReadFile(configOut)
281281
require.NoError(t, err)
282282
var out config.ConfigFileStruct
283-
err = yaml.Unmarshal(destCfgBytes, &out)
283+
err = yaml.UnmarshalStrict(destCfgBytes, &out)
284284
require.NoError(t, err)
285285
require.Len(t, out.Parameters, 6)
286286
require.Len(t, out.Pipeline, 6)

pkg/confgen/encode.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
package confgen
1919

2020
import (
21-
"encoding/json"
22-
2321
jsoniter "github.com/json-iterator/go"
2422
"github.com/netobserv/flowlogs-pipeline/pkg/api"
23+
"github.com/netobserv/flowlogs-pipeline/pkg/config"
2524
log "github.com/sirupsen/logrus"
2625
)
2726

@@ -35,7 +34,7 @@ func (cg *ConfGen) parseEncode(encode *map[string]interface{}) (*api.PromEncode,
3534
}
3635

3736
var prom api.PromEncode
38-
err = json.Unmarshal(b, &prom)
37+
err = config.JsonUnmarshalStrict(b, &prom)
3938
if err != nil {
4039
log.Debugf("Unmarshal aggregate.Definitions err: %v ", err)
4140
return nil, err

pkg/confgen/extract.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
package confgen
1919

2020
import (
21-
"encoding/json"
22-
2321
jsoniter "github.com/json-iterator/go"
22+
"github.com/netobserv/flowlogs-pipeline/pkg/config"
2423
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/extract/aggregate"
2524
log "github.com/sirupsen/logrus"
2625
)
@@ -35,7 +34,7 @@ func (cg *ConfGen) parseExtract(extract *map[string]interface{}) (*aggregate.Def
3534
}
3635

3736
var jsonNetworkAggregate aggregate.Definitions
38-
err = json.Unmarshal(b, &jsonNetworkAggregate)
37+
err = config.JsonUnmarshalStrict(b, &jsonNetworkAggregate)
3938
if err != nil {
4039
log.Debugf("Unmarshal aggregate.Definitions err: %v ", err)
4140
return nil, err

pkg/confgen/transform.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
package confgen
1919

2020
import (
21-
"encoding/json"
22-
2321
jsoniter "github.com/json-iterator/go"
2422
"github.com/netobserv/flowlogs-pipeline/pkg/api"
23+
"github.com/netobserv/flowlogs-pipeline/pkg/config"
2524
log "github.com/sirupsen/logrus"
2625
)
2726

@@ -34,7 +33,7 @@ func (cg *ConfGen) parseTransport(transform *map[string]interface{}) (*api.Trans
3433
}
3534

3635
var jsonNetworkTransform api.TransformNetwork
37-
err = json.Unmarshal(b, &jsonNetworkTransform)
36+
err = config.JsonUnmarshalStrict(b, &jsonNetworkTransform)
3837
if err != nil {
3938
log.Debugf("Unmarshal transform.TransformNetwork err: %v ", err)
4039
return nil, err

pkg/confgen/visualization.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
package confgen
1919

2020
import (
21-
"encoding/json"
22-
2321
jsoniter "github.com/json-iterator/go"
22+
"github.com/netobserv/flowlogs-pipeline/pkg/config"
2423
log "github.com/sirupsen/logrus"
2524
)
2625

@@ -62,7 +61,7 @@ func (cg *ConfGen) parseVisualization(visualization *Visualization) (*Visualizat
6261
}
6362

6463
var jsonVisualization Visualization
65-
err = json.Unmarshal(b, &jsonVisualization)
64+
err = config.JsonUnmarshalStrict(b, &jsonVisualization)
6665
if err != nil {
6766
log.Debugf("Unmarshal aggregate.Definitions err: %v ", err)
6867
return nil, err

pkg/config/config.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package config
1919

2020
import (
21+
"bytes"
2122
"encoding/json"
2223

2324
"github.com/netobserv/flowlogs-pipeline/pkg/api"
@@ -99,18 +100,27 @@ func ParseConfig(opts Options) (ConfigFileStruct, error) {
99100
out := ConfigFileStruct{}
100101

101102
logrus.Debugf("opts.PipeLine = %v ", opts.PipeLine)
102-
err := json.Unmarshal([]byte(opts.PipeLine), &out.Pipeline)
103+
err := JsonUnmarshalStrict([]byte(opts.PipeLine), &out.Pipeline)
103104
if err != nil {
104105
logrus.Errorf("error when reading config file: %v", err)
105106
return out, err
106107
}
107108
logrus.Debugf("stages = %v ", out.Pipeline)
108109

109-
err = json.Unmarshal([]byte(opts.Parameters), &out.Parameters)
110+
err = JsonUnmarshalStrict([]byte(opts.Parameters), &out.Parameters)
110111
if err != nil {
111112
logrus.Errorf("error when reading config file: %v", err)
112113
return out, err
113114
}
114115
logrus.Debugf("params = %v ", out.Parameters)
115116
return out, nil
116117
}
118+
119+
// JsonUnmarshalStrict is like Unmarshal except that any fields that are found
120+
// in the data that do not have corresponding struct members, or mapping
121+
// keys that are duplicates, will result in an error.
122+
func JsonUnmarshalStrict(data []byte, v interface{}) error {
123+
dec := json.NewDecoder(bytes.NewReader(data))
124+
dec.DisallowUnknownFields()
125+
return dec.Decode(v)
126+
}

0 commit comments

Comments
 (0)