Skip to content

Commit c22455d

Browse files
author
Aaron Meihm
committed
dynamically create BigQuery metrics view
Closes #392
1 parent 5ab2c66 commit c22455d

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

Diff for: bin/generate_meta.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
f=`mktemp`
88
outpath=contrib/common/alertmeta.go
99

10-
java com.mozilla.secops.alert.AlertMeta "${f}"
10+
java com.mozilla.secops.alert.AlertMeta gometa "${f}"
1111
gofmt $f > $outpath

Diff for: bin/generate_view.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
# Generate BigQuery metrics view
4+
#
5+
# Called by maven and should not be run directly
6+
7+
f=./target/metrics-view.sql
8+
java com.mozilla.secops.alert.AlertMeta metricsview "${f}"
9+
cat $f

Diff for: pom.xml

+13-1
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,22 @@
7676
<goals>
7777
<goal>exec</goal>
7878
</goals>
79+
<configuration>
80+
<executable>bin/generate_meta.sh</executable>
81+
</configuration>
82+
</execution>
83+
<execution>
84+
<id>generate-metrics-view</id>
85+
<phase>package</phase>
86+
<goals>
87+
<goal>exec</goal>
88+
</goals>
89+
<configuration>
90+
<executable>bin/generate_view.sh</executable>
91+
</configuration>
7992
</execution>
8093
</executions>
8194
<configuration>
82-
<executable>bin/generate_meta.sh</executable>
8395
<environmentVariables>
8496
<CLASSPATH>
8597
${maven.compile.classpath}:target/classes

Diff for: src/main/java/com/mozilla/secops/alert/AlertMeta.java

+38-7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.io.FileWriter;
77
import java.io.IOException;
88
import java.io.Serializable;
9+
import java.util.ArrayList;
910
import org.slf4j.Logger;
1011
import org.slf4j.LoggerFactory;
1112

@@ -199,6 +200,15 @@ public String getKey() {
199200
return key;
200201
}
201202

203+
/**
204+
* Return if key is considered sensitive
205+
*
206+
* @return boolean
207+
*/
208+
public boolean getIsSensitive() {
209+
return isSensitive;
210+
}
211+
202212
/**
203213
* Obtain given associated key type
204214
*
@@ -324,17 +334,38 @@ public AlertMeta(@JsonProperty("key") String key, @JsonProperty("value") String
324334
}
325335

326336
/**
327-
* Convert metadata to Golang
337+
* Convert metadata to various formats
338+
*
339+
* <p>This main function can be used to export metadata keys in golang format, and to export a
340+
* BigQuery view query that is suitable for metrics usage.
328341
*
329342
* @param args Arguments
330343
*/
331344
public static void main(String[] args) throws IOException {
332-
BufferedWriter w = new BufferedWriter(new FileWriter(args[0]));
333-
w.write("package common\n\n// This file is automatically generated.\n\nconst (\n");
334-
for (Key k : Key.values()) {
335-
w.write(String.format("META_%s = \"%s\"\n", k.name(), k.getKey()));
345+
if (args[0].equals("gometa")) {
346+
BufferedWriter w = new BufferedWriter(new FileWriter(args[1]));
347+
w.write("package common\n\n// This file is automatically generated.\n\nconst (\n");
348+
for (Key k : Key.values()) {
349+
w.write(String.format("META_%s = \"%s\"\n", k.name(), k.getKey()));
350+
}
351+
w.write(")\n");
352+
w.close();
353+
} else if (args[0].equals("metricsview")) {
354+
BufferedWriter w = new BufferedWriter(new FileWriter(args[1]));
355+
ArrayList<String> sFields = new ArrayList<>();
356+
for (Key k : Key.values()) {
357+
if (k.getIsSensitive()) {
358+
sFields.add("'" + k.getKey() + "'");
359+
}
360+
}
361+
w.write(
362+
String.format(
363+
"SELECT EXTRACT(DATETIME FROM _PARTITIONTIME) AS partitiontime, "
364+
+ "id, timestamp, severity, category, ARRAY(\nSELECT AS STRUCT key, "
365+
+ "value FROM UNNEST(metadata) WHERE\nKEY NOT IN (%s)\n) AS metadata\n"
366+
+ "FROM <<table>>\n",
367+
String.join(", ", sFields)));
368+
w.close();
336369
}
337-
w.write(")\n");
338-
w.close();
339370
}
340371
}

0 commit comments

Comments
 (0)