-
Notifications
You must be signed in to change notification settings - Fork 214
/
TimeMeasuringAuthenticationProvider.java
194 lines (167 loc) · 8.52 KB
/
TimeMeasuringAuthenticationProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.gateway.service.security.authentication;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.CompletionStage;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.base.model.auth.AuthorizationContextType;
import org.eclipse.ditto.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.gateway.api.GatewayAuthenticationProviderUnavailableException;
import org.eclipse.ditto.internal.utils.pekko.logging.ThreadSafeDittoLogger;
import org.eclipse.ditto.internal.utils.metrics.instruments.timer.StartedTimer;
import org.eclipse.ditto.internal.utils.tracing.TraceUtils;
import org.eclipse.ditto.internal.utils.tracing.span.SpanTagKey;
import org.apache.pekko.http.javadsl.server.RequestContext;
/**
* An abstract class that measures the time it takes to authenticate a request.
*
* @param <R> the type of the AuthenticationResult implementation.
*/
@Immutable
public abstract class TimeMeasuringAuthenticationProvider<R extends AuthenticationResult>
implements AuthenticationProvider<R> {
private final ThreadSafeDittoLogger logger;
/**
* Constructs a new TimeMeasuringAuthenticationProvider object.
*
* @param logger the logger to be used.
* @throws NullPointerException if {@code logger} is {@code null}.
*/
protected TimeMeasuringAuthenticationProvider(final ThreadSafeDittoLogger logger) {
this.logger = checkNotNull(logger, "logger");
}
@Override
public final CompletableFuture<R> authenticate(final RequestContext requestContext,
final DittoHeaders dittoHeaders) {
final AuthorizationContextType authorizationContextType = getType(requestContext);
final StartedTimer timer = TraceUtils.newAuthFilterTimer(authorizationContextType).start();
CompletableFuture<R> resultFuture;
try {
resultFuture = tryToAuthenticate(requestContext, dittoHeaders);
} catch (final Throwable e) {
resultFuture = CompletableFuture.failedFuture(e);
}
resultFuture = resultFuture.thenApply(
authenticationResult -> {
timer.tag(SpanTagKey.AUTH_SUCCESS.getTagForValue(authenticationResult.isSuccess()));
return authenticationResult;
})
.exceptionally(error -> {
final Throwable rootCause = getRootCause(error);
if (rootCause instanceof DittoRuntimeException dittoRuntimeException) {
timer.tag(SpanTagKey.AUTH_SUCCESS.getTagForValue(false));
if (isInternalError(dittoRuntimeException.getHttpStatus())) {
logger.withCorrelationId(dittoHeaders)
.warn("An unexpected error occurred during authentication of type <{}>.",
authorizationContextType, dittoRuntimeException);
timer.tag(SpanTagKey.AUTH_ERROR.getTagForValue(true));
}
return toFailedAuthenticationResult(dittoRuntimeException, dittoHeaders);
} else {
timer.tag(SpanTagKey.AUTH_SUCCESS.getTagForValue(false));
timer.tag(SpanTagKey.AUTH_ERROR.getTagForValue(true));
return toFailedAuthenticationResult(rootCause, dittoHeaders);
}
});
resultFuture.whenComplete((result, error) -> timer.stop());
return resultFuture;
}
private static Throwable getRootCause(final Throwable error) {
if (error instanceof CompletionException && error.getCause() != null) {
return error.getCause();
} else {
return error;
}
}
private static boolean isInternalError(final HttpStatus httpStatusCode) {
return httpStatusCode.isServerError();
}
/**
* Used to identify the authentication provider in order to distinguish measured metrics for this authentication
* provider.
*
* @param requestContext the request context to authenticate.
* @return the type of this authentication provider.
*/
protected abstract AuthorizationContextType getType(RequestContext requestContext);
/**
* Authenticates the given {@link RequestContext request context}.
*
* @param requestContext the request context to authenticate.
* @param dittoHeaders the (potentially not completely set) DittoHeaders of the request.
* @return the authentication result.
*/
protected abstract CompletableFuture<R> tryToAuthenticate(RequestContext requestContext, DittoHeaders dittoHeaders);
/**
* Creates failed authentication result with a {@link AuthenticationResult#getReasonOfFailure() reason of failure}
* based on the given throwable.
*
* @param throwable the throwable that caused a failure.
* @param dittoHeaders the (potentially not completely set) DittoHeaders of the request.
* @return a failed authentication result holding the extracted reason of failure.
*/
protected abstract R toFailedAuthenticationResult(Throwable throwable, DittoHeaders dittoHeaders);
/**
* Converts the given {@link Throwable} to a {@link DittoRuntimeException} either by returning the
* ditto runtime exception hold as a cause or by building a
* {@link GatewayAuthenticationProviderUnavailableException} with the given throwable as cause (Unwrapped in case
* the throwable is of type {@link java.util.concurrent.CompletionException}).
*
* @param throwable the throwable to convert to a {@link org.eclipse.ditto.base.model.exceptions.DittoRuntimeException}.
* @param dittoHeaders the built DittoHeaders of the request that caused the given throwable.
* @return the converted exception.
*/
protected DittoRuntimeException toDittoRuntimeException(final Throwable throwable,
final DittoHeaders dittoHeaders) {
return DittoRuntimeException.asDittoRuntimeException(throwable,
cause -> {
final DittoRuntimeException dittoRuntimeException =
unwrapDittoRuntimeException(cause, dittoHeaders);
if (dittoRuntimeException == null) {
logger.withCorrelationId(dittoHeaders)
.warn("Failed to unwrap DittoRuntimeException from Throwable!", throwable);
return buildInternalErrorException(cause, dittoHeaders);
}
return dittoRuntimeException;
});
}
private static DittoRuntimeException unwrapDittoRuntimeException(final Throwable throwable,
final DittoHeaders dittoHeaders) {
if (null == throwable) {
return null;
}
if (throwable instanceof DittoRuntimeException dittoRuntimeException) {
if (dittoRuntimeException.getDittoHeaders().getCorrelationId().isPresent()) {
return dittoRuntimeException;
}
return dittoRuntimeException.setDittoHeaders(dittoHeaders);
}
return unwrapDittoRuntimeException(throwable.getCause(), dittoHeaders);
}
protected CompletableFuture<R> failOnTimeout(
final CompletionStage<R> authenticationResultFuture, final DittoHeaders dittoHeaders) {
return AuthenticationResultOrTimeout.of(authenticationResultFuture.toCompletableFuture(), dittoHeaders).get()
.exceptionally(e -> toFailedAuthenticationResult(e, dittoHeaders));
}
protected static DittoRuntimeException buildInternalErrorException(final Throwable cause,
final DittoHeaders dittoHeaders) {
return GatewayAuthenticationProviderUnavailableException.newBuilder()
.dittoHeaders(dittoHeaders)
.cause(cause)
.build();
}
}