use of org.pmiops.workbench.model.AuditBigQueryResponse in project workbench by all-of-us.
the class AuditController method auditBigQuery.
@Override
public ResponseEntity<AuditBigQueryResponse> auditBigQuery() {
// We expect to only see queries run within Firecloud AoU projects, or for administrative
// purposes within the CDR project itself.
Set<String> cdrProjects = ImmutableList.copyOf(cdrVersionDao.findAll()).stream().map(v -> v.getBigqueryProject()).collect(Collectors.toSet());
Set<String> whitelist = Sets.union(userDao.getAllUserProjects(), cdrProjects);
Instant now = clock.instant();
List<String> suffixes = IntStream.range(0, AUDIT_DAY_RANGE).mapToObj(i -> auditTableSuffix(now, i)).collect(Collectors.toList());
int numBad = 0;
int numQueries = 0;
for (String cdrProjectId : cdrProjects) {
QueryResult result = bigQueryService.executeQuery(QueryJobConfiguration.of(auditSql(cdrProjectId, suffixes)));
Map<String, Integer> rm = bigQueryService.getResultMapper(result);
for (List<FieldValue> row : result.iterateAll()) {
String project_id = bigQueryService.getString(row, rm.get("client_project_id"));
String email = bigQueryService.getString(row, rm.get("user_email"));
long total = bigQueryService.getLong(row, rm.get("total"));
if (bigQueryService.isNull(row, rm.get("client_project_id"))) {
log.severe(String.format("AUDIT: (CDR project '%s') %d queries with missing project ID from user '%s'; " + "indicates an ACL misconfiguration, this user can access the CDR but is not a " + "project jobUser", cdrProjectId, total, email));
numBad += total;
} else if (!whitelist.contains(project_id)) {
log.severe(String.format("AUDIT: (CDR project '%s') %d queries in unrecognized project '%s' from user '%s'", cdrProjectId, total, project_id, email));
numBad += total;
}
numQueries += total;
}
}
log.info(String.format("AUDIT: found audit issues with %d/%d BigQuery queries", numBad, numQueries));
return ResponseEntity.ok(new AuditBigQueryResponse().numQueryIssues(numBad));
}
Aggregations