forked from knative-extensions/eventing-kafka-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Receiver reject requests for wrong audience (knative-extensions#3675)
* Receiver: reject request for wrong audience * Switch to AuthenticationHandler * Fix "Request has already been read" issue * Change TokenVerifier to an interface * Initialize TokenVerifier in main * Add test for AuthenticationHandler * Only initialize OIDC discovery config in main and create a TokenVerifier per verticle instance. * Rerun hack/update-codegen.sh * Move TokenVerifier setup into setup() to prevent null pointer exception when vertx is null * Update KafkaChannel OIDC e2e tests, to run OIDC conformance tests so the receiver is tested too. * Run OIDC e2e tests as part of the reconciler suite * Fix KafkaChannelOIDC e2e test * Fix lint issue * Address review comments
- Loading branch information
Showing
20 changed files
with
404 additions
and
83 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
77 changes: 77 additions & 0 deletions
77
...ane/core/src/main/java/dev/knative/eventing/kafka/broker/core/oidc/TokenVerifierImpl.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,77 @@ | ||
/* | ||
* Copyright © 2018 Knative Authors (knative-dev@googlegroups.com) | ||
* | ||
* 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 dev.knative.eventing.kafka.broker.core.oidc; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.http.HttpServerRequest; | ||
import org.jose4j.jwt.JwtClaims; | ||
import org.jose4j.jwt.consumer.InvalidJwtException; | ||
import org.jose4j.jwt.consumer.JwtConsumer; | ||
import org.jose4j.jwt.consumer.JwtConsumerBuilder; | ||
import org.jose4j.jwt.consumer.JwtContext; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class TokenVerifierImpl implements TokenVerifier { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(TokenVerifierImpl.class); | ||
|
||
private final Vertx vertx; | ||
|
||
private final OIDCDiscoveryConfig oidcDiscoveryConfig; | ||
|
||
public TokenVerifierImpl(Vertx vertx, OIDCDiscoveryConfig oidcDiscoveryConfig) { | ||
this.vertx = vertx; | ||
this.oidcDiscoveryConfig = oidcDiscoveryConfig; | ||
} | ||
|
||
public Future<JwtClaims> verify(String token, String expectedAudience) { | ||
return this.vertx.<JwtClaims>executeBlocking(promise -> { | ||
// execute blocking, as jose .process() is blocking | ||
|
||
JwtConsumer jwtConsumer = new JwtConsumerBuilder() | ||
.setVerificationKeyResolver(this.oidcDiscoveryConfig.getJwksVerificationKeyResolver()) | ||
.setExpectedAudience(expectedAudience) | ||
.setExpectedIssuer(this.oidcDiscoveryConfig.getIssuer()) | ||
.build(); | ||
|
||
try { | ||
JwtContext jwtContext = jwtConsumer.process(token); | ||
|
||
promise.complete(jwtContext.getJwtClaims()); | ||
} catch (InvalidJwtException e) { | ||
promise.fail(e); | ||
} | ||
}); | ||
} | ||
|
||
public Future<JwtClaims> verify(final HttpServerRequest request, String expectedAudience) { | ||
String authHeader = request.getHeader("Authorization"); | ||
if (authHeader == null || authHeader.isEmpty()) { | ||
return Future.failedFuture("Request didn't contain Authorization header"); | ||
} | ||
|
||
if (!authHeader.startsWith("Bearer ") && authHeader.length() <= "Bearer ".length()) { | ||
return Future.failedFuture("Authorization header didn't contain Bearer token"); | ||
} | ||
|
||
String token = authHeader.substring("Bearer ".length()); | ||
|
||
request.pause(); | ||
return verify(token, expectedAudience).onSuccess(v -> request.resume()); | ||
} | ||
} |
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
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
61 changes: 61 additions & 0 deletions
61
...n/java/dev/knative/eventing/kafka/broker/receiver/impl/handler/AuthenticationHandler.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 © 2018 Knative Authors (knative-dev@googlegroups.com) | ||
* | ||
* 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 dev.knative.eventing.kafka.broker.receiver.impl.handler; | ||
|
||
import static dev.knative.eventing.kafka.broker.core.utils.Logging.keyValue; | ||
|
||
import dev.knative.eventing.kafka.broker.core.oidc.TokenVerifier; | ||
import dev.knative.eventing.kafka.broker.receiver.IngressProducer; | ||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.http.HttpServerRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Handler checking that the provided request contained a valid JWT. | ||
*/ | ||
public class AuthenticationHandler { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(AuthenticationHandler.class); | ||
private final TokenVerifier tokenVerifier; | ||
|
||
public AuthenticationHandler(final TokenVerifier tokenVerifier) { | ||
this.tokenVerifier = tokenVerifier; | ||
} | ||
|
||
public void handle( | ||
final HttpServerRequest request, final IngressProducer ingressInfo, final Handler<HttpServerRequest> next) { | ||
if (ingressInfo.getAudience().isEmpty()) { | ||
logger.debug("No audience for ingress set. Continue without authentication check..."); | ||
next.handle(request); | ||
return; | ||
} | ||
|
||
tokenVerifier | ||
.verify(request, ingressInfo.getAudience()) | ||
.onFailure(e -> { | ||
logger.debug("Failed to verify authentication of request: {}", keyValue("error", e.getMessage())); | ||
request.response() | ||
.setStatusCode(HttpResponseStatus.UNAUTHORIZED.code()) | ||
.end(); | ||
}) | ||
.onSuccess(jwtClaims -> { | ||
logger.debug("Request contained valid JWT. Continuing..."); | ||
next.handle(request); | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.