forked from confluentinc/ksql
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Security plumbing (confluentinc#4778)
- Loading branch information
Showing
25 changed files
with
492 additions
and
229 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
43 changes: 43 additions & 0 deletions
43
ksqldb-rest-app/src/main/java/io/confluent/ksql/api/auth/AuthenticationPlugin.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,43 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.api.auth; | ||
|
||
import io.vertx.core.WorkerExecutor; | ||
import io.vertx.ext.web.RoutingContext; | ||
import java.security.Principal; | ||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
/** | ||
* Extension point for adding custom authentication. | ||
*/ | ||
public interface AuthenticationPlugin { | ||
|
||
void configure(Map<String, ?> map); | ||
|
||
/** | ||
* Handle authentication for the request. Please note that in the case of failure to authenticate | ||
* the plugin should end the response. This behaviour makes it compatible with existing auth | ||
* plugins which it wraps. | ||
* | ||
* @param routingContext The routing context | ||
* @param workerExecutor The worker executor | ||
* @return A CompletableFuture representing the result of the authentication | ||
*/ | ||
CompletableFuture<Principal> handleAuth(RoutingContext routingContext, | ||
WorkerExecutor workerExecutor); | ||
|
||
} |
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 |
---|---|---|
|
@@ -48,5 +48,4 @@ public Optional<String> getAuthToken() { | |
return authToken; | ||
} | ||
|
||
|
||
} |
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
65 changes: 0 additions & 65 deletions
65
ksqldb-rest-app/src/main/java/io/confluent/ksql/api/auth/KsqlAuthorizationFilter.java
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
...b-rest-app/src/main/java/io/confluent/ksql/api/auth/KsqlAuthorizationProviderHandler.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,85 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.api.auth; | ||
|
||
import com.google.common.collect.ImmutableSet; | ||
import io.confluent.ksql.security.KsqlAuthorizationProvider; | ||
import io.vertx.core.AsyncResult; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.Promise; | ||
import io.vertx.core.WorkerExecutor; | ||
import io.vertx.ext.auth.User; | ||
import io.vertx.ext.web.RoutingContext; | ||
import java.security.Principal; | ||
import java.util.Set; | ||
|
||
public class KsqlAuthorizationProviderHandler implements Handler<RoutingContext> { | ||
|
||
private static final Set<String> PATHS_WITHOUT_AUTHORIZATION = ImmutableSet | ||
.of("/v1/metadata", "/healthcheck"); | ||
|
||
private final WorkerExecutor workerExecutor; | ||
private final KsqlAuthorizationProvider ksqlAuthorizationProvider; | ||
|
||
public KsqlAuthorizationProviderHandler(final WorkerExecutor workerExecutor, | ||
final KsqlAuthorizationProvider ksqlAuthorizationProvider) { | ||
this.workerExecutor = workerExecutor; | ||
this.ksqlAuthorizationProvider = ksqlAuthorizationProvider; | ||
} | ||
|
||
@Override | ||
public void handle(final RoutingContext routingContext) { | ||
|
||
final String path = routingContext.normalisedPath(); | ||
|
||
if (PATHS_WITHOUT_AUTHORIZATION.contains(path)) { | ||
routingContext.next(); | ||
return; | ||
} | ||
|
||
workerExecutor.<Void>executeBlocking( | ||
promise -> authorize(promise, routingContext), | ||
ar -> handleAuthorizeResult(ar, routingContext)); | ||
} | ||
|
||
private static void handleAuthorizeResult(final AsyncResult<Void> ar, | ||
final RoutingContext routingContext) { | ||
if (ar.succeeded()) { | ||
routingContext.next(); | ||
} else { | ||
routingContext.fail(403, ar.cause()); | ||
} | ||
} | ||
|
||
private void authorize(final Promise<Void> promise, final RoutingContext routingContext) { | ||
final User user = routingContext.user(); | ||
if (user == null) { | ||
promise.fail( | ||
new IllegalStateException("Null user in " + KsqlAuthorizationProviderHandler.class)); | ||
return; | ||
} | ||
final Principal principal = new ApiPrincipal(user.principal().getString("username")); | ||
try { | ||
ksqlAuthorizationProvider | ||
.checkEndpointAccess(principal, routingContext.request().method().toString(), | ||
routingContext.normalisedPath()); | ||
} catch (Exception e) { | ||
promise.fail(e); | ||
return; | ||
} | ||
promise.complete(); | ||
} | ||
} |
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
Oops, something went wrong.