Skip to content

Commit

Permalink
Add a logger for create-tables with unauthenticated or metacat user (#…
Browse files Browse the repository at this point in the history
…522)

Add a logger for create-tables with unauthenticated or metacat user
  • Loading branch information
swaranga-netflix authored Jan 17, 2023
1 parent 7319202 commit adfa117
Showing 1 changed file with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.netflix.metacat.main.api.v1;

import com.google.common.base.Preconditions;
import com.netflix.metacat.common.MetacatRequestContext;
import com.netflix.metacat.common.NameDateDto;
import com.netflix.metacat.common.QualifiedName;
import com.netflix.metacat.common.dto.CatalogDto;
Expand All @@ -38,11 +39,13 @@
import com.netflix.metacat.main.services.MViewService;
import com.netflix.metacat.main.services.MetacatServiceHelper;
import com.netflix.metacat.main.services.TableService;
import com.netflix.spectator.api.Registry;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -53,12 +56,19 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.net.HttpURLConnection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

Expand All @@ -76,13 +86,15 @@
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
@Slf4j
public class MetacatController implements MetacatV1 {
private final CatalogService catalogService;
private final DatabaseService databaseService;
private final MViewService mViewService;
private final TableService tableService;
private final RequestWrapper requestWrapper;
private final Config config;
private final Registry registry;

/**
* Constructor.
Expand All @@ -93,6 +105,7 @@ public class MetacatController implements MetacatV1 {
* @param tableService table service
* @param requestWrapper request wrapper obj
* @param config Config
* @param registry the spectator registry
*/
@Autowired
public MetacatController(
Expand All @@ -101,14 +114,16 @@ public MetacatController(
final MViewService mViewService,
final TableService tableService,
final RequestWrapper requestWrapper,
final Config config
final Config config,
final Registry registry
) {
this.catalogService = catalogService;
this.databaseService = databaseService;
this.mViewService = mViewService;
this.tableService = tableService;
this.requestWrapper = requestWrapper;
this.config = config;
this.registry = registry;
}

/**
Expand Down Expand Up @@ -250,6 +265,37 @@ public TableDto createTable(
@ApiParam(value = "The table information", required = true)
@Valid @RequestBody final TableDto table
) {
final MetacatRequestContext requestContext = MetacatContextManager.getContext();
if (requestContext.getUserName() == null || "metacat".equals(requestContext.getUserName())) {
registry.counter(
"unauth.user.create.table", "catalog", catalogName, "database", databaseName).increment();

final Map<String, String> requestHeaders = new HashMap<>();

final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

if (requestAttributes instanceof ServletRequestAttributes) {
final ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;

final HttpServletRequest servletRequest = servletRequestAttributes.getRequest();

if (servletRequest != null) {
final Enumeration<String> headerNames = servletRequest.getHeaderNames();

if (headerNames != null) {
while (headerNames.hasMoreElements()) {
final String header = headerNames.nextElement();
requestHeaders.put(header, servletRequest.getHeader(header));
}
}
}
}

log.info("Create table with unknown user. "
+ "catalog: {}, database: {}, table-name: {}, table-info: {}, context: {}, headers: {}",
catalogName, databaseName, tableName, table, requestContext, requestHeaders);
}

final QualifiedName name = this.requestWrapper.qualifyName(
() -> QualifiedName.ofTable(catalogName, databaseName, tableName)
);
Expand Down

0 comments on commit adfa117

Please sign in to comment.