-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tenant-manager: audit logging (#1891)
* tenant-manager: audit logging * configure access log * tenant manager: audit logs for authentication errors
- Loading branch information
Fabian Martinez
authored
Oct 5, 2021
1 parent
d2df3ba
commit c37a5a7
Showing
14 changed files
with
675 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
...manager-api/src/main/java/io/apicurio/multitenant/auth/CustomAuthenticationMechanism.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2021 Red Hat | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.apicurio.multitenant.auth; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.BiConsumer; | ||
|
||
import javax.annotation.Priority; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.inject.Alternative; | ||
import javax.inject.Inject; | ||
|
||
import org.slf4j.Logger; | ||
|
||
import io.apicurio.multitenant.logging.audit.AuditHttpRequestContext; | ||
import io.apicurio.multitenant.logging.audit.AuditHttpRequestInfo; | ||
import io.apicurio.multitenant.logging.audit.AuditLogService; | ||
import io.quarkus.oidc.runtime.BearerAuthenticationMechanism; | ||
import io.quarkus.oidc.runtime.OidcAuthenticationMechanism; | ||
import io.quarkus.security.identity.IdentityProviderManager; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.quarkus.security.identity.request.AuthenticationRequest; | ||
import io.quarkus.security.identity.request.TokenAuthenticationRequest; | ||
import io.quarkus.vertx.http.runtime.security.ChallengeData; | ||
import io.quarkus.vertx.http.runtime.security.HttpAuthenticationMechanism; | ||
import io.quarkus.vertx.http.runtime.security.HttpCredentialTransport; | ||
import io.quarkus.vertx.http.runtime.security.QuarkusHttpUser; | ||
import io.smallrye.mutiny.Uni; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* Custom HttpAuthenticationMechanism that simply wraps OidcAuthenticationMechanism. | ||
* The only purpose of this HttpAuthenticationMechanism is to handle authentication errors in order to generate audit logs. | ||
* | ||
* @author Fabian Martinez | ||
*/ | ||
@Alternative | ||
@Priority(1) | ||
@ApplicationScoped | ||
public class CustomAuthenticationMechanism implements HttpAuthenticationMechanism { | ||
|
||
@Inject | ||
Logger log; | ||
|
||
@Inject | ||
OidcAuthenticationMechanism oidcAuthenticationMechanism; | ||
|
||
@Inject | ||
AuditHttpRequestContext auditContext; | ||
@Inject | ||
AuditLogService auditLog; | ||
|
||
private final BearerAuthenticationMechanism bearerAuth = new BearerAuthenticationMechanism();; | ||
|
||
@Override | ||
public Uni<SecurityIdentity> authenticate(RoutingContext context, IdentityProviderManager identityProviderManager) { | ||
|
||
|
||
BiConsumer<RoutingContext, Throwable> failureHandler = context.get(QuarkusHttpUser.AUTH_FAILURE_HANDLER); | ||
BiConsumer<RoutingContext, Throwable> auditWrapper = (ctx, ex) -> { | ||
//this sends the http response | ||
failureHandler.accept(ctx, ex); | ||
//if it was an error response log it | ||
if (ctx.response().getStatusCode() >= 400) { | ||
Map<String, String> metadata = new HashMap<>(); | ||
metadata.put("method", ctx.request().method().name()); | ||
metadata.put("path", ctx.request().path()); | ||
metadata.put("response_code", String.valueOf(ctx.response().getStatusCode())); | ||
if (ex != null) { | ||
metadata.put("error_msg", ex.getMessage()); | ||
} | ||
|
||
//request context for AuditHttpRequestContext does not exist at this point | ||
auditLog.log("authenticate", AuditHttpRequestContext.FAILURE, metadata, new AuditHttpRequestInfo() { | ||
@Override | ||
public String getSourceIp() { | ||
return ctx.request().remoteAddress().toString(); | ||
} | ||
@Override | ||
public String getForwardedFor() { | ||
return ctx.request().getHeader(AuditHttpRequestContext.X_FORWARDED_FOR_HEADER); | ||
} | ||
}); | ||
} | ||
}; | ||
log.info("Setting audit wrapper {}", context.statusCode()); | ||
context.put(QuarkusHttpUser.AUTH_FAILURE_HANDLER, auditWrapper); | ||
|
||
return oidcAuthenticationMechanism.authenticate(context, identityProviderManager); | ||
} | ||
|
||
@Override | ||
public Uni<ChallengeData> getChallenge(RoutingContext context) { | ||
return bearerAuth.getChallenge(context); | ||
} | ||
|
||
@Override | ||
public Set<Class<? extends AuthenticationRequest>> getCredentialTypes() { | ||
return Collections.singleton(TokenAuthenticationRequest.class); | ||
} | ||
|
||
@Override | ||
public HttpCredentialTransport getCredentialTransport() { | ||
return new HttpCredentialTransport(HttpCredentialTransport.Type.AUTHORIZATION, "bearer"); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...ager-api/src/main/java/io/apicurio/multitenant/logging/audit/AuditHttpRequestContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2021 Red Hat | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.apicurio.multitenant.logging.audit; | ||
|
||
import javax.enterprise.context.RequestScoped; | ||
|
||
/** | ||
* @author Fabian Martinez | ||
*/ | ||
@RequestScoped | ||
public class AuditHttpRequestContext implements AuditHttpRequestInfo { | ||
|
||
public static final String X_FORWARDED_FOR_HEADER = "x-forwarded-for"; | ||
public static final String FAILURE = "failure"; | ||
public static final String SUCCESS = "success"; | ||
|
||
private String sourceIp; | ||
private String forwardedFor; | ||
private boolean auditEntryGenerated = false; | ||
|
||
@Override | ||
public String getSourceIp() { | ||
return sourceIp; | ||
} | ||
|
||
public void setSourceIp(String sourceIp) { | ||
this.sourceIp = sourceIp; | ||
} | ||
|
||
@Override | ||
public String getForwardedFor() { | ||
return forwardedFor; | ||
} | ||
|
||
public void setForwardedFor(String forwardedFor) { | ||
this.forwardedFor = forwardedFor; | ||
} | ||
|
||
public boolean isAuditEntryGenerated() { | ||
return auditEntryGenerated; | ||
} | ||
|
||
public void setAuditEntryGenerated(boolean auditEntryGenerated) { | ||
this.auditEntryGenerated = auditEntryGenerated; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...manager-api/src/main/java/io/apicurio/multitenant/logging/audit/AuditHttpRequestInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2021 Red Hat | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.apicurio.multitenant.logging.audit; | ||
|
||
/** | ||
* @author Fabian Martinez | ||
*/ | ||
public interface AuditHttpRequestInfo { | ||
|
||
/** | ||
* @return the sourceIp | ||
*/ | ||
String getSourceIp(); | ||
|
||
/** | ||
* @return the forwardedFor | ||
*/ | ||
String getForwardedFor(); | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
...nant-manager-api/src/main/java/io/apicurio/multitenant/logging/audit/AuditLogService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2021 Red Hat | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.apicurio.multitenant.logging.audit; | ||
|
||
import java.util.Map; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.context.control.ActivateRequestContext; | ||
import javax.inject.Inject; | ||
import org.slf4j.Logger; | ||
|
||
/** | ||
* @author Fabian Martinez | ||
*/ | ||
@ApplicationScoped | ||
public class AuditLogService { | ||
|
||
@Inject | ||
Logger log; | ||
|
||
@Inject | ||
AuditHttpRequestContext context; | ||
|
||
@ActivateRequestContext | ||
public void log(String action, String result, Map<String, String> metadata, AuditHttpRequestInfo requestInfo) { | ||
|
||
String remoteAddress; | ||
String forwardedRemoteAddress; | ||
if (requestInfo != null) { | ||
remoteAddress = requestInfo.getSourceIp(); | ||
forwardedRemoteAddress = requestInfo.getForwardedFor(); | ||
} else { | ||
remoteAddress = context.getSourceIp(); | ||
forwardedRemoteAddress = context.getForwardedFor(); | ||
} | ||
|
||
StringBuilder m = new StringBuilder(); | ||
m.append("tenant-manager.audit") | ||
.append(" ") | ||
.append("action=\"").append(action).append("\" ") | ||
.append("result=\"").append(result).append("\" ") | ||
.append("src_ip=\"").append(remoteAddress).append("\" "); | ||
if (forwardedRemoteAddress != null) { | ||
m.append("x_forwarded_for=\"").append(forwardedRemoteAddress).append("\" "); | ||
} | ||
for (Map.Entry<String, String> e : metadata.entrySet()) { | ||
m.append(e.getKey()).append("=\"").append(e.getValue()).append("\" "); | ||
} | ||
log.info(m.toString()); | ||
//mark in the context that we already generated an audit entry for this request | ||
context.setAuditEntryGenerated(true); | ||
} | ||
|
||
} |
Oops, something went wrong.