Skip to content

Commit 8996385

Browse files
committed
Added more audit logs
1 parent 21cb8be commit 8996385

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

Issues.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
* When sending null in ServiceMessage.data - now it's throwing BadRequest, but before it was
22
security-exception. Must be changed back to how it was.
3-
* Add debug logs for Auth/Authz error situations.
3+

services-api/src/main/java/io/scalecube/services/RequestContext.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
import java.util.Objects;
1515
import java.util.StringJoiner;
1616
import java.util.stream.Stream;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
1719
import reactor.core.publisher.Mono;
1820
import reactor.util.context.Context;
1921

2022
public class RequestContext implements Context {
2123

24+
private static final Logger LOGGER = LoggerFactory.getLogger(RequestContext.class);
25+
2226
private final Context source;
2327

2428
public RequestContext() {
@@ -178,17 +182,39 @@ public static Mono<RequestContext> deferSecured() {
178182
.doOnNext(
179183
context -> {
180184
if (!context.hasPrincipal()) {
185+
if (LOGGER.isDebugEnabled()) {
186+
LOGGER.debug(
187+
"Insufficient permissions for secured method ({}): "
188+
+ "request context does not have principal",
189+
context.methodInfo());
190+
}
181191
throw new ForbiddenException("Insufficient permissions");
182192
}
183193

184194
final var principal = context.principal();
185195
final var methodInfo = context.methodInfo();
186196

187197
if (!methodInfo.allowedRoles().contains(principal.role())) {
198+
if (LOGGER.isDebugEnabled()) {
199+
LOGGER.debug(
200+
"Insufficient permissions for secured method ({}): "
201+
+ "principal role is not allowed (principal: {})",
202+
context.methodInfo(),
203+
principal);
204+
}
188205
throw new ForbiddenException("Insufficient permissions");
189206
}
207+
190208
for (var allowedPermission : methodInfo.allowedPermissions()) {
191209
if (!principal.hasPermission(allowedPermission)) {
210+
if (LOGGER.isDebugEnabled()) {
211+
LOGGER.debug(
212+
"Insufficient permissions for secured method ({}): "
213+
+ "allowed permission: {} is missing (principal: {})",
214+
context.methodInfo(),
215+
allowedPermission,
216+
principal.role());
217+
}
192218
throw new ForbiddenException("Insufficient permissions");
193219
}
194220
}

services-api/src/main/java/io/scalecube/services/methods/ServiceMethodInvoker.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
import java.util.Objects;
1717
import org.reactivestreams.Publisher;
1818
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
1920
import reactor.core.publisher.Flux;
2021
import reactor.core.publisher.Mono;
2122
import reactor.util.context.Context;
2223

2324
public class ServiceMethodInvoker {
2425

26+
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceMethodInvoker.class);
27+
2528
private final Method method;
2629
private final Object service;
2730
private final MethodInfo methodInfo;
@@ -243,6 +246,12 @@ private Mono<Principal> mapPrincipal(RequestContext context) {
243246
if (context.hasPrincipal()) {
244247
return Mono.just(context.principal());
245248
} else {
249+
if (LOGGER.isDebugEnabled()) {
250+
LOGGER.debug(
251+
"Insufficient permissions for secured method ({}): "
252+
+ "request context does not have principal and principalMapper is also not set",
253+
methodInfo);
254+
}
246255
throw new ForbiddenException("Insufficient permissions");
247256
}
248257
}

services-transport-parent/services-transport-rsocket/src/main/java/io/scalecube/services/transport/rsocket/RSocketServiceAcceptor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ private Mono<Principal> authenticate(ByteBuf connectionSetup) {
5050
final var credentials = new byte[connectionSetup.readableBytes()];
5151
connectionSetup.getBytes(connectionSetup.readerIndex(), credentials);
5252

53-
return authenticator
54-
.authenticate(credentials)
53+
return Mono.defer(() -> authenticator.authenticate(credentials))
5554
.switchIfEmpty(Mono.just(NULL_PRINCIPAL))
5655
.doOnSuccess(principal -> LOGGER.debug("Authenticated successfully: {}", principal))
5756
.doOnError(ex -> LOGGER.error("Authentication failed", ex))

0 commit comments

Comments
 (0)