Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Fixing issue where traceId was missing in the logs #35
Browse files Browse the repository at this point in the history
  • Loading branch information
tlisonbee committed May 10, 2017
1 parent 40b3c2e commit 575d888
Show file tree
Hide file tree
Showing 22 changed files with 163 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import com.nike.cerberus.service.MetadataService;
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.server.http.ResponseInfo;
import com.nike.riposte.server.http.impl.FullResponseInfo;
import com.nike.riposte.util.AsyncNettyHelper;
import com.nike.riposte.util.Matcher;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpMethod;
Expand Down Expand Up @@ -57,15 +59,20 @@ public CompletableFuture<ResponseInfo<SDBMetadataResult>> doExecute(final Reques
final Executor longRunningTaskExecutor,
final ChannelHandlerContext ctx,
final SecurityContext securityContext) {

return CompletableFuture.supplyAsync(
() -> ResponseInfo.newBuilder(metadataService.getSDBMetadata(getLimit(request), getOffset(request)))
.build(), longRunningTaskExecutor
AsyncNettyHelper.supplierWithTracingAndMdc(() -> getMetadata(request), ctx),
longRunningTaskExecutor
);
}

private FullResponseInfo<SDBMetadataResult> getMetadata(RequestInfo<Void> request) {
return ResponseInfo.newBuilder(metadataService.getSDBMetadata(getLimit(request), getOffset(request)))
.build();
}

/**
* Parses and validates limit query param
*
* @param request The request
* @return default or parsed vaule
*/
Expand All @@ -83,10 +90,11 @@ protected int getLimit(RequestInfo<Void> request) {

/**
* validates that the limit query is a valid number >= 1
*
* @param limitQueryValue
*/
protected void validateLimitQuery(String limitQueryValue) {
if (! StringUtils.isNumeric(limitQueryValue) || Integer.parseInt(limitQueryValue) < 1) {
if (!StringUtils.isNumeric(limitQueryValue) || Integer.parseInt(limitQueryValue) < 1) {
throw ApiException.newBuilder()
.withApiErrors(new ApiErrorBase(
DefaultApiError.INVALID_QUERY_PARAMS.getName(),
Expand All @@ -99,6 +107,7 @@ protected void validateLimitQuery(String limitQueryValue) {

/**
* Parses and validates offset query param
*
* @param request The request
* @return default or parsed vaule
*/
Expand All @@ -116,10 +125,11 @@ protected int getOffset(RequestInfo<Void> request) {

/**
* Validates that the offset value is a number that is >= 0
*
* @param offsetQueryValue
*/
protected void validateOffsetQuery(String offsetQueryValue) {
if (! StringUtils.isNumeric(offsetQueryValue)) {
if (!StringUtils.isNumeric(offsetQueryValue)) {
throw ApiException.newBuilder()
.withApiErrors(new ApiErrorBase(
DefaultApiError.INVALID_QUERY_PARAMS.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.nike.cerberus.service.MetadataService;
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.server.http.ResponseInfo;
import com.nike.riposte.util.AsyncNettyHelper;
import com.nike.riposte.util.Matcher;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpMethod;
Expand Down Expand Up @@ -54,19 +55,24 @@ public CompletableFuture<ResponseInfo<Void>> doExecute(RequestInfo<SDBMetadata>
ChannelHandlerContext ctx,
SecurityContext securityContext) {

return CompletableFuture.supplyAsync(() -> {
VaultAuthPrincipal vaultAuthPrincipal = (VaultAuthPrincipal) securityContext.getUserPrincipal();
return CompletableFuture.supplyAsync(
AsyncNettyHelper.supplierWithTracingAndMdc(() -> restoreSdbMetadata(request, securityContext), ctx),
longRunningTaskExecutor
);
}

private ResponseInfo<Void> restoreSdbMetadata(RequestInfo<SDBMetadata> request, SecurityContext securityContext) {
VaultAuthPrincipal vaultAuthPrincipal = (VaultAuthPrincipal) securityContext.getUserPrincipal();

String principal = vaultAuthPrincipal.getName();
String principal = vaultAuthPrincipal.getName();

log.info("Metadata Restore Event: the principal {} is attempting to restore sdb name: '{}'", principal, request.getContent().getName());
log.info("Metadata Restore Event: the principal {} is attempting to restore sdb name: '{}'", principal, request.getContent().getName());

metadataService.restoreMetadata(request.getContent(), principal);
metadataService.restoreMetadata(request.getContent(), principal);

return ResponseInfo.<Void>newBuilder()
.withHttpStatusCode(HttpResponseStatus.NO_CONTENT.code())
.build();
}, longRunningTaskExecutor);
return ResponseInfo.<Void>newBuilder()
.withHttpStatusCode(HttpResponseStatus.NO_CONTENT.code())
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@

package com.nike.cerberus.endpoints.authentication;

import com.nike.cerberus.domain.IamRoleAuthResponse;
import com.nike.cerberus.domain.IamPrincipalCredentials;
import com.nike.cerberus.domain.IamRoleAuthResponse;
import com.nike.cerberus.service.AuthenticationService;
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.server.http.ResponseInfo;
import com.nike.riposte.server.http.StandardEndpoint;
import com.nike.riposte.util.AsyncNettyHelper;
import com.nike.riposte.util.Matcher;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpMethod;
Expand Down Expand Up @@ -52,13 +53,18 @@ public AuthenticateIamPrincipal(final AuthenticationService authenticationServic
public CompletableFuture<ResponseInfo<IamRoleAuthResponse>> execute(final RequestInfo<IamPrincipalCredentials> request,
final Executor longRunningTaskExecutor,
final ChannelHandlerContext ctx) {
return CompletableFuture.supplyAsync(() -> {
IamPrincipalCredentials credentials = request.getContent();
log.info("IAM Auth Event: the IAM principal {} in attempting to authenticate in region {}",
credentials.getIamPrincipalArn(), credentials.getRegion());
return CompletableFuture.supplyAsync(
AsyncNettyHelper.supplierWithTracingAndMdc(() -> authenticate(request), ctx),
longRunningTaskExecutor
);
}

private ResponseInfo<IamRoleAuthResponse> authenticate(RequestInfo<IamPrincipalCredentials> request) {
IamPrincipalCredentials credentials = request.getContent();
log.info("IAM Auth Event: the IAM principal {} in attempting to authenticate in region {}",
credentials.getIamPrincipalArn(), credentials.getRegion());

return ResponseInfo.newBuilder(authenticationService.authenticate(request.getContent())).build();
}, longRunningTaskExecutor);
return ResponseInfo.newBuilder(authenticationService.authenticate(request.getContent())).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.server.http.ResponseInfo;
import com.nike.riposte.server.http.StandardEndpoint;
import com.nike.riposte.util.AsyncNettyHelper;
import com.nike.riposte.util.Matcher;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpMethod;
Expand Down Expand Up @@ -53,15 +54,20 @@ public AuthenticateIamRole(final AuthenticationService authenticationService) {
public CompletableFuture<ResponseInfo<IamRoleAuthResponse>> execute(final RequestInfo<IamRoleCredentials> request,
final Executor longRunningTaskExecutor,
final ChannelHandlerContext ctx) {
return CompletableFuture.supplyAsync(() -> {
IamRoleCredentials credentials = request.getContent();
log.info("IAM Auth Event: the IAM principal {} in attempting to authenticate in region {}",
String.format(AwsIamRoleArnParser.AWS_IAM_ROLE_ARN_TEMPLATE,
credentials.getAccountId(), credentials.getRoleName()), credentials.getRegion());
return CompletableFuture.supplyAsync(
AsyncNettyHelper.supplierWithTracingAndMdc(() -> authenticate(request), ctx),
longRunningTaskExecutor
);

return ResponseInfo.newBuilder(authenticationService.authenticate(request.getContent())).build();
}, longRunningTaskExecutor);
}

private ResponseInfo<IamRoleAuthResponse> authenticate(RequestInfo<IamRoleCredentials> request) {
IamRoleCredentials credentials = request.getContent();
log.info("IAM Auth Event: the IAM principal {} in attempting to authenticate in region {}",
String.format(AwsIamRoleArnParser.AWS_IAM_ROLE_ARN_TEMPLATE,
credentials.getAccountId(), credentials.getRoleName()), credentials.getRegion());

return ResponseInfo.newBuilder(authenticationService.authenticate(request.getContent())).build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.server.http.ResponseInfo;
import com.nike.riposte.server.http.StandardEndpoint;
import com.nike.riposte.util.AsyncNettyHelper;
import com.nike.riposte.util.Matcher;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpMethod;
Expand Down Expand Up @@ -57,18 +58,19 @@ public CompletableFuture<ResponseInfo<AuthResponse>> execute(final RequestInfo<V
final Executor longRunningTaskExecutor,
final ChannelHandlerContext ctx) {
return CompletableFuture.supplyAsync(
() -> {
final UserCredentials credentials =
extractCredentials(request.getHeaders().get(HttpHeaders.AUTHORIZATION));

log.info("User Auth Event: the principal: {} is attempting to authenticate", credentials.getUsername());

return ResponseInfo.newBuilder(authenticationService.authenticate(credentials)).build();
},
AsyncNettyHelper.supplierWithTracingAndMdc(() -> authenticate(request), ctx),
longRunningTaskExecutor
);
}

private ResponseInfo<AuthResponse> authenticate(RequestInfo<Void> request) {
final UserCredentials credentials = extractCredentials(request.getHeaders().get(HttpHeaders.AUTHORIZATION));

log.info("User Auth Event: the principal: {} is attempting to authenticate", credentials.getUsername());

return ResponseInfo.newBuilder(authenticationService.authenticate(credentials)).build();
}

@Override
public Matcher requestMatcher() {
return Matcher.match("/v2/auth/user", HttpMethod.GET);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.server.http.ResponseInfo;
import com.nike.riposte.server.http.StandardEndpoint;
import com.nike.riposte.util.AsyncNettyHelper;
import com.nike.riposte.util.Matcher;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpMethod;
Expand All @@ -47,7 +48,10 @@ public CompletableFuture<ResponseInfo<AuthResponse>> execute(final RequestInfo<M
final Executor longRunningTaskExecutor,
final ChannelHandlerContext ctx) {
return CompletableFuture.supplyAsync(
() -> ResponseInfo.newBuilder(authenticationService.mfaCheck(request.getContent())).build(),
AsyncNettyHelper.supplierWithTracingAndMdc(
() -> ResponseInfo.newBuilder(authenticationService.mfaCheck(request.getContent())).build(),
ctx
),
longRunningTaskExecutor
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.server.http.ResponseInfo;
import com.nike.riposte.server.http.StandardEndpoint;
import com.nike.riposte.util.AsyncNettyHelper;
import com.nike.riposte.util.Matcher;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpMethod;
Expand Down Expand Up @@ -53,10 +54,10 @@ public RefreshUserToken(final AuthenticationService authenticationService) {

@Override
public CompletableFuture<ResponseInfo<AuthResponse>> execute(final RequestInfo<Void> request,
final Executor longRunningTaskExecutor,
final ChannelHandlerContext ctx) {
final Executor longRunningTaskExecutor,
final ChannelHandlerContext ctx) {
return CompletableFuture.supplyAsync(
() -> getRefreshedUserToken(request),
AsyncNettyHelper.supplierWithTracingAndMdc(() -> getRefreshedUserToken(request), ctx),
longRunningTaskExecutor
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.server.http.ResponseInfo;
import com.nike.riposte.server.http.StandardEndpoint;
import com.nike.riposte.util.AsyncNettyHelper;
import com.nike.riposte.util.Matcher;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpMethod;
Expand Down Expand Up @@ -55,7 +56,10 @@ public RevokeToken(final AuthenticationService authenticationService) {
public CompletableFuture<ResponseInfo<Void>> execute(final RequestInfo<Void> request,
final Executor longRunningTaskExecutor,
final ChannelHandlerContext ctx) {
return CompletableFuture.supplyAsync(() -> revokeToken(request), longRunningTaskExecutor);
return CompletableFuture.supplyAsync(
AsyncNettyHelper.supplierWithTracingAndMdc(() -> revokeToken(request), ctx),
longRunningTaskExecutor
);
}

public ResponseInfo<Void> revokeToken(RequestInfo<Void> request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.nike.cerberus.service.CategoryService;
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.server.http.ResponseInfo;
import com.nike.riposte.util.AsyncNettyHelper;
import com.nike.riposte.util.Matcher;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultHttpHeaders;
Expand Down Expand Up @@ -53,11 +54,10 @@ public CreateCategory(final CategoryService categoryService) {
public CompletableFuture<ResponseInfo<Void>> doExecute(final RequestInfo<Category> request,
final Executor longRunningTaskExecutor,
final ChannelHandlerContext ctx,
final SecurityContext securityContext
) {
final SecurityContext securityContext) {
return CompletableFuture.supplyAsync(
() -> createCategory(request.getContent(), securityContext, CATEGORY_PATH),
longRunningTaskExecutor
AsyncNettyHelper.supplierWithTracingAndMdc(() -> createCategory(request.getContent(), securityContext, CATEGORY_PATH), ctx),
longRunningTaskExecutor
);
}

Expand All @@ -71,8 +71,8 @@ public ResponseInfo<Void> createCategory(final Category category,
final String id = categoryService.createCategory(category, securityContext.getUserPrincipal().getName());
final String location = basePathNoId + "/" + id;
return ResponseInfo.<Void>newBuilder().withHeaders(new DefaultHttpHeaders().set(LOCATION, location))
.withHttpStatusCode(201)
.build();
.withHttpStatusCode(201)
.build();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.nike.cerberus.service.CategoryService;
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.server.http.ResponseInfo;
import com.nike.riposte.util.AsyncNettyHelper;
import com.nike.riposte.util.Matcher;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpMethod;
Expand Down Expand Up @@ -48,11 +49,10 @@ public DeleteCategory(final CategoryService categoryService) {
public CompletableFuture<ResponseInfo<Void>> doExecute(final RequestInfo<Void> request,
final Executor longRunningTaskExecutor,
final ChannelHandlerContext ctx,
final SecurityContext securityContext
) {
final SecurityContext securityContext) {
return CompletableFuture.supplyAsync(
() -> deleteCategory(request.getPathParam(PATH_PARAM_ID)),
longRunningTaskExecutor
AsyncNettyHelper.supplierWithTracingAndMdc(() -> deleteCategory(request.getPathParam(PATH_PARAM_ID)), ctx),
longRunningTaskExecutor
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.nike.riposte.server.http.RequestInfo;
import com.nike.riposte.server.http.ResponseInfo;
import com.nike.riposte.server.http.StandardEndpoint;
import com.nike.riposte.util.AsyncNettyHelper;
import com.nike.riposte.util.Matcher;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpMethod;
Expand All @@ -43,12 +44,12 @@ public GetAllCategories(final CategoryService categoryService) {
}

@Override
public CompletableFuture<ResponseInfo<List<Category>>> execute(
RequestInfo<Void> request, Executor longRunningTaskExecutor, ChannelHandlerContext ctx
) {
public CompletableFuture<ResponseInfo<List<Category>>> execute(RequestInfo<Void> request,
Executor longRunningTaskExecutor,
ChannelHandlerContext ctx) {
return CompletableFuture.supplyAsync(
this::getAllCategories,
longRunningTaskExecutor
AsyncNettyHelper.supplierWithTracingAndMdc(this::getAllCategories, ctx),
longRunningTaskExecutor
);
}

Expand Down
Loading

0 comments on commit 575d888

Please sign in to comment.