Search in sources :

Example 1 with AuditBigQueryResponse

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));
}
Also used : IntStream(java.util.stream.IntStream) FieldValue(com.google.cloud.bigquery.FieldValue) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ChronoField(java.time.temporal.ChronoField) AuditBigQueryResponse(org.pmiops.workbench.model.AuditBigQueryResponse) Autowired(org.springframework.beans.factory.annotation.Autowired) ImmutableList(com.google.common.collect.ImmutableList) CdrVersionDao(org.pmiops.workbench.db.dao.CdrVersionDao) Map(java.util.Map) UserDao(org.pmiops.workbench.db.dao.UserDao) Set(java.util.Set) QueryJobConfiguration(com.google.cloud.bigquery.QueryJobConfiguration) Instant(java.time.Instant) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) Sets(com.google.common.collect.Sets) ZoneId(java.time.ZoneId) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) DateTimeFormatter(java.time.format.DateTimeFormatter) Clock(java.time.Clock) ResponseEntity(org.springframework.http.ResponseEntity) VisibleForTesting(com.google.common.annotations.VisibleForTesting) QueryResult(com.google.cloud.bigquery.QueryResult) Instant(java.time.Instant) QueryResult(com.google.cloud.bigquery.QueryResult) AuditBigQueryResponse(org.pmiops.workbench.model.AuditBigQueryResponse) FieldValue(com.google.cloud.bigquery.FieldValue)

Aggregations

FieldValue (com.google.cloud.bigquery.FieldValue)1 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)1 QueryResult (com.google.cloud.bigquery.QueryResult)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 Sets (com.google.common.collect.Sets)1 Clock (java.time.Clock)1 Instant (java.time.Instant)1 ZoneId (java.time.ZoneId)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 DateTimeFormatterBuilder (java.time.format.DateTimeFormatterBuilder)1 ChronoField (java.time.temporal.ChronoField)1 ChronoUnit (java.time.temporal.ChronoUnit)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 CdrVersionDao (org.pmiops.workbench.db.dao.CdrVersionDao)1