Skip to content

Commit eef2077

Browse files
authored
fix(logs): add actor urn on unauthorised (#12030)
1 parent 2fe2132 commit eef2077

File tree

5 files changed

+21
-15
lines changed

5 files changed

+21
-15
lines changed

metadata-service/auth-filter/src/main/java/com/datahub/auth/authentication/filter/AuthenticationFilter.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,22 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
9898
}
9999

100100
if (authentication != null) {
101+
String actorUrnStr = authentication.getActor().toUrnStr();
101102
// Successfully authenticated.
102103
log.debug(
103-
String.format(
104-
"Successfully authenticated request for Actor with type: %s, id: %s",
105-
authentication.getActor().getType(), authentication.getActor().getId()));
104+
"Successfully authenticated request for Actor with type: {}, id: {}",
105+
authentication.getActor().getType(),
106+
authentication.getActor().getId());
106107
AuthenticationContext.setAuthentication(authentication);
107108
chain.doFilter(request, response);
108109
} else {
109110
// Reject request
110111
log.debug(
111112
"Failed to authenticate request. Received 'null' Authentication value from authenticator chain.");
112113
((HttpServletResponse) response)
113-
.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized to perform this action.");
114+
.sendError(
115+
HttpServletResponse.SC_UNAUTHORIZED,
116+
"Unauthorized to perform this action due to expired auth.");
114117
return;
115118
}
116119
AuthenticationContext.remove();

metadata-service/auth-servlet-impl/src/main/java/com/datahub/auth/authentication/AuthServiceController.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ CompletableFuture<ResponseEntity<String>> generateSessionTokenForUser(
138138
}
139139

140140
log.info("Attempting to generate session token for user {}", userId.asText());
141-
final String actorId = AuthenticationContext.getAuthentication().getActor().getId();
141+
Authentication authentication = AuthenticationContext.getAuthentication();
142+
final String actorId = authentication.getActor().getId();
143+
final String actorUrn = authentication.getActor().toUrnStr();
142144
return CompletableFuture.supplyAsync(
143145
() -> {
144146
// 1. Verify that only those authorized to generate a token (datahub system) are able to.
@@ -164,7 +166,7 @@ CompletableFuture<ResponseEntity<String>> generateSessionTokenForUser(
164166
}
165167
throw HttpClientErrorException.create(
166168
HttpStatus.UNAUTHORIZED,
167-
"Unauthorized to perform this action.",
169+
actorUrn + " unauthorized to perform this action.",
168170
new HttpHeaders(),
169171
null,
170172
null);

metadata-service/restli-servlet-impl/src/main/java/com/linkedin/metadata/resources/entity/AspectResource.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,13 @@ private Task<String> ingestProposals(
281281
boolean asyncBool)
282282
throws URISyntaxException {
283283
Authentication authentication = AuthenticationContext.getAuthentication();
284+
String actorUrnStr = authentication.getActor().toUrnStr();
284285

285286
Set<String> entityTypes = metadataChangeProposals.stream()
286287
.map(MetadataChangeProposal::getEntityType)
287288
.collect(Collectors.toSet());
288289
final OperationContext opContext = OperationContext.asSession(
289-
systemOperationContext, RequestContext.builder().buildRestli(authentication.getActor().toUrnStr(), getContext(),
290+
systemOperationContext, RequestContext.builder().buildRestli(actorUrnStr, getContext(),
290291
ACTION_INGEST_PROPOSAL, entityTypes), _authorizer, authentication, true);
291292

292293
// Ingest Authorization Checks
@@ -299,9 +300,8 @@ private Task<String> ingestProposals(
299300
.map(ex -> String.format("HttpStatus: %s Urn: %s", ex.getSecond(), ex.getFirst().getEntityUrn()))
300301
.collect(Collectors.joining(", "));
301302
throw new RestLiServiceException(
302-
HttpStatus.S_403_FORBIDDEN, "User is unauthorized to modify entity: " + errorMessages);
303+
HttpStatus.S_403_FORBIDDEN, "User " + actorUrnStr + " is unauthorized to modify entity: " + errorMessages);
303304
}
304-
String actorUrnStr = authentication.getActor().toUrnStr();
305305
final AuditStamp auditStamp =
306306
new AuditStamp().setTime(_clock.millis()).setActor(Urn.createFromString(actorUrnStr));
307307

metadata-service/restli-servlet-impl/src/main/java/com/linkedin/metadata/resources/entity/EntityResource.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,15 @@ public Task<Void> ingest(
274274
String actorUrnStr = authentication.getActor().toUrnStr();
275275
final Urn urn = com.datahub.util.ModelUtils.getUrnFromSnapshotUnion(entity.getValue());
276276
final OperationContext opContext = OperationContext.asSession(
277-
systemOperationContext, RequestContext.builder().buildRestli(authentication.getActor().toUrnStr(), getContext(),
277+
systemOperationContext, RequestContext.builder().buildRestli(actorUrnStr, getContext(),
278278
ACTION_INGEST, urn.getEntityType()), authorizer, authentication, true);
279279

280280
if (!isAPIAuthorizedEntityUrns(
281281
opContext,
282282
CREATE,
283283
List.of(urn))) {
284284
throw new RestLiServiceException(
285-
HttpStatus.S_403_FORBIDDEN, "User is unauthorized to edit entity " + urn);
285+
HttpStatus.S_403_FORBIDDEN, "User " + actorUrnStr + " is unauthorized to edit entity " + urn);
286286
}
287287

288288
try {
@@ -320,15 +320,15 @@ public Task<Void> batchIngest(
320320
.map(Entity::getValue)
321321
.map(com.datahub.util.ModelUtils::getUrnFromSnapshotUnion).collect(Collectors.toList());
322322
final OperationContext opContext = OperationContext.asSession(
323-
systemOperationContext, RequestContext.builder().buildRestli(authentication.getActor().toUrnStr(),
323+
systemOperationContext, RequestContext.builder().buildRestli(actorUrnStr,
324324
getContext(), ACTION_BATCH_INGEST, urns.stream().map(Urn::getEntityType).collect(Collectors.toList())),
325325
authorizer, authentication, true);
326326

327327
if (!isAPIAuthorizedEntityUrns(
328328
opContext,
329329
CREATE, urns)) {
330330
throw new RestLiServiceException(
331-
HttpStatus.S_403_FORBIDDEN, "User is unauthorized to edit entities.");
331+
HttpStatus.S_403_FORBIDDEN, "User " + actorUrnStr + " is unauthorized to edit entities.");
332332
}
333333

334334
for (Entity entity : entities) {

metadata-service/restli-servlet-impl/src/main/java/com/linkedin/metadata/resources/usage/UsageStats.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ public Task<Void> batchIngest(@ActionParam(PARAM_BUCKETS) @Nonnull UsageAggregat
104104
() -> {
105105

106106
final Authentication auth = AuthenticationContext.getAuthentication();
107+
String actorUrnStr = auth.getActor().toUrnStr();
107108
Set<Urn> urns = Arrays.stream(buckets).sequential().map(UsageAggregation::getResource).collect(Collectors.toSet());
108109
final OperationContext opContext = OperationContext.asSession(
109-
systemOperationContext, RequestContext.builder().buildRestli(auth.getActor().toUrnStr(), getContext(),
110+
systemOperationContext, RequestContext.builder().buildRestli(actorUrnStr, getContext(),
110111
ACTION_BATCH_INGEST, urns.stream().map(Urn::getEntityType).collect(Collectors.toList())), _authorizer,
111112
auth, true);
112113

@@ -115,7 +116,7 @@ public Task<Void> batchIngest(@ActionParam(PARAM_BUCKETS) @Nonnull UsageAggregat
115116
UPDATE,
116117
urns)) {
117118
throw new RestLiServiceException(
118-
HttpStatus.S_403_FORBIDDEN, "User is unauthorized to edit entities.");
119+
HttpStatus.S_403_FORBIDDEN, "User " + actorUrnStr + " is unauthorized to edit entities.");
119120
}
120121

121122
for (UsageAggregation agg : buckets) {

0 commit comments

Comments
 (0)