Search in sources :

Example 1 with Caller

use of com.netflix.titus.api.model.callmetadata.Caller in project titus-control-plane by Netflix.

the class SimpleGrpcCallMetadataResolver method resolve.

@Override
public Optional<CallMetadata> resolve() {
    if (Context.current() == Context.ROOT) {
        // Not in GRPC server call.
        return Optional.empty();
    }
    CallMetadata forwardedCallMetadata = V3HeaderInterceptor.CALL_METADATA_CONTEXT_KEY.get();
    Caller directCaller = resolveDirectCallerInternal();
    // If we have CallMetadata instance, we can safely ignore other headers, except the direct caller.
    if (forwardedCallMetadata != null) {
        return Optional.of(forwardedCallMetadata.toBuilder().withCallPath(CollectionsExt.copyAndAdd(forwardedCallMetadata.getCallPath(), directCaller.getId())).withCallers(CollectionsExt.copyAndAdd(forwardedCallMetadata.getCallers(), directCaller)).build());
    }
    // No CellMetadata in header, so we must built it here.
    String callerId = V3HeaderInterceptor.CALLER_ID_CONTEXT_KEY.get();
    String callReason = getOrDefault(V3HeaderInterceptor.CALL_REASON_CONTEXT_KEY.get(), "reason not given");
    CallMetadata.Builder callMetadataBuilder = CallMetadata.newBuilder().withCallReason(callReason);
    if (callerId == null) {
        callMetadataBuilder.withCallerId(directCaller.getId()).withCallPath(Collections.singletonList(directCaller.getId())).withCallers(Collections.singletonList(directCaller));
    } else {
        Caller originalCaller = Caller.newBuilder().withId(callerId).withCallerType(CallerType.parseCallerType(callerId, V3HeaderInterceptor.CALLER_TYPE_CONTEXT_KEY.get())).build();
        callMetadataBuilder.withCallerId(callerId).withCallPath(asList(callerId, directCaller.getId())).withCallers(asList(originalCaller, directCaller));
    }
    return Optional.of(callMetadataBuilder.build());
}
Also used : CallMetadata(com.netflix.titus.api.model.callmetadata.CallMetadata) Caller(com.netflix.titus.api.model.callmetadata.Caller)

Example 2 with Caller

use of com.netflix.titus.api.model.callmetadata.Caller in project titus-control-plane by Netflix.

the class SimpleHttpCallMetadataResolver method interceptBefore.

private void interceptBefore(HttpServletRequest httpServletRequest) {
    Caller directCaller = resolveDirectCaller(httpServletRequest).orElseGet(() -> resolveDirectCallerFromServletRequest(httpServletRequest));
    String callReason = getOrDefault(httpServletRequest.getHeader(CallMetadataHeaders.CALL_REASON_HEADER), "reason not given");
    String originalCallerId = httpServletRequest.getHeader(CallMetadataHeaders.CALLER_ID_HEADER);
    CallMetadata.Builder callMetadataBuilder = CallMetadata.newBuilder().withCallReason(callReason);
    if (originalCallerId == null) {
        callMetadataBuilder.withCallerId(directCaller.getId()).withCallPath(Collections.singletonList(directCaller.getId())).withCallers(Collections.singletonList(directCaller));
    } else {
        CallerType originalCallerType = CallerType.parseCallerType(originalCallerId, httpServletRequest.getHeader(CallMetadataHeaders.CALLER_TYPE_HEADER));
        Caller originalCaller = Caller.newBuilder().withId(originalCallerId).withCallerType(originalCallerType).build();
        callMetadataBuilder.withCallerId(originalCallerId).withCallPath(asList(originalCallerId, directCaller.getId())).withCallers(asList(originalCaller, directCaller));
    }
    callMetadataThreadLocal.set(callMetadataBuilder.build());
}
Also used : Caller(com.netflix.titus.api.model.callmetadata.Caller) CallMetadata(com.netflix.titus.api.model.callmetadata.CallMetadata) CallerType(com.netflix.titus.api.model.callmetadata.CallerType)

Example 3 with Caller

use of com.netflix.titus.api.model.callmetadata.Caller in project titus-control-plane by Netflix.

the class SpringCallMetadataInterceptor method preHandle.

@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception {
    Authentication delegate = SecurityContextHolder.getContext().getAuthentication();
    if (delegate == null || delegate instanceof CallMetadataAuthentication) {
        return super.preHandle(httpServletRequest, httpServletResponse, handler);
    }
    String callReason = httpServletRequest.getHeader(CallMetadataHeaders.CALL_REASON_HEADER);
    String debugQueryParameter = httpServletRequest.getParameter(DEBUG_QUERY_PARAM);
    boolean debug = debugQueryParameter == null ? Boolean.parseBoolean(httpServletRequest.getHeader(CallMetadataHeaders.DEBUG_HEADER)) : Boolean.parseBoolean(debugQueryParameter);
    String originalCallerId = httpServletRequest.getHeader(CallMetadataHeaders.CALLER_ID_HEADER);
    Caller directCaller = getDirectCaller(httpServletRequest, delegate);
    CallMetadata.Builder callMetadataBuilder = CallMetadata.newBuilder().withCallReason(callReason).withDebug(debug);
    if (originalCallerId == null) {
        callMetadataBuilder.withCallers(Collections.singletonList(directCaller));
    } else {
        CallerType originalCallerType = CallerType.parseCallerType(originalCallerId, httpServletRequest.getHeader(CallMetadataHeaders.CALLER_TYPE_HEADER));
        Caller originalCaller = Caller.newBuilder().withId(originalCallerId).withCallerType(originalCallerType).build();
        callMetadataBuilder.withCallers(asList(originalCaller, directCaller));
    }
    CallMetadataAuthentication authentication = new CallMetadataAuthentication(callMetadataBuilder.build(), delegate);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    return super.preHandle(httpServletRequest, httpServletResponse, handler);
}
Also used : Caller(com.netflix.titus.api.model.callmetadata.Caller) CallMetadata(com.netflix.titus.api.model.callmetadata.CallMetadata) Authentication(org.springframework.security.core.Authentication) CallerType(com.netflix.titus.api.model.callmetadata.CallerType)

Aggregations

CallMetadata (com.netflix.titus.api.model.callmetadata.CallMetadata)3 Caller (com.netflix.titus.api.model.callmetadata.Caller)3 CallerType (com.netflix.titus.api.model.callmetadata.CallerType)2 Authentication (org.springframework.security.core.Authentication)1