-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add additional headers to the client request.
Add ClientRequestFilter.java interface in Presto-spi. Improve Request Headers in the Authentication Filter Class.
- Loading branch information
1 parent
4479bcc
commit 28c5aa4
Showing
16 changed files
with
511 additions
and
4 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
presto-docs/src/main/sphinx/develop/client-request-filter.rst
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,22 @@ | ||
|
||
====================== | ||
Client Request Filter | ||
====================== | ||
|
||
Presto allows operators to customize the headers used by the Presto runtime to process queries. Some example use cases include customized authentication workflows, or enriching query attributes such as the query source. This can be achieved using the Client Request Filter plugin, which allows control over header customization during query execution. | ||
|
||
Implementation | ||
-------------- | ||
|
||
The ``ClientRequestFilterFactory`` is responsible for creating instances of ``ClientRequestFilter``. It also defines | ||
the name of the filter. | ||
|
||
The ``ClientRequestFilter`` interface provides two methods: ``getExtraHeaders()``, which allows the runtime to quickly check if it needs to apply a more expensive call to enrich the headers, and ``getHeaderNames()``, which returns a list of header names used as the header names in client requests. | ||
|
||
The implementation of ``ClientRequestFilterFactory`` must be wrapped as a plugin and installed on the Presto cluster. | ||
|
||
After installing a plugin that implements ``ClientRequestFilterFactory`` on the coordinator, the ``AuthenticationFilter`` class passes the ``principal`` object to the request filter, which returns the header values as a map. | ||
|
||
Presto uses the request filter to determine whether a header is present in the blocklist. The blocklist includes headers such as ``X-Presto-Transaction-Id``, ``X-Presto-Started-Transaction-Id``, ``X-Presto-Clear-Transaction-Id``, and ``X-Presto-Trace-Token``, which are not allowed to be overridden. For a complete list of headers that may be overridden, please refer to the `Java source`_ (note that the blocklist headers are also listed there, but they are not eligible for overriding). | ||
|
||
.. _Java source: https://github.com/prestodb/presto/blob/master/presto-client/src/main/java/com/facebook/presto/client/PrestoHeaders.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
72 changes: 72 additions & 0 deletions
72
presto-main/src/main/java/com/facebook/presto/ClientRequestFilterManager.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,72 @@ | ||
/* | ||
* 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 com.facebook.presto; | ||
|
||
import com.facebook.presto.spi.ClientRequestFilter; | ||
import com.facebook.presto.spi.ClientRequestFilterFactory; | ||
import com.facebook.presto.spi.PrestoException; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import javax.annotation.concurrent.GuardedBy; | ||
import javax.annotation.concurrent.ThreadSafe; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import static com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS; | ||
import static com.facebook.presto.spi.StandardErrorCode.CONFIGURATION_INVALID; | ||
import static com.facebook.presto.spi.StandardErrorCode.INVALID_ARGUMENTS; | ||
import static com.google.common.collect.ImmutableList.toImmutableList; | ||
|
||
@ThreadSafe | ||
public class ClientRequestFilterManager | ||
{ | ||
private Map<String, ClientRequestFilterFactory> factories = new ConcurrentHashMap<>(); | ||
|
||
@GuardedBy("this") | ||
private volatile List<ClientRequestFilter> filters = ImmutableList.of(); | ||
private final AtomicBoolean loaded = new AtomicBoolean(); | ||
|
||
public void registerClientRequestFilterFactory(ClientRequestFilterFactory factory) | ||
{ | ||
if (loaded.get()) { | ||
throw new PrestoException(INVALID_ARGUMENTS, "Cannot register factories after filters are loaded."); | ||
} | ||
|
||
String name = factory.getName(); | ||
if (factories.putIfAbsent(name, factory) != null) { | ||
throw new PrestoException(ALREADY_EXISTS, "A factory with the name '" + name + "' is already registered."); | ||
} | ||
} | ||
|
||
public void loadClientRequestFilters() | ||
{ | ||
if (!loaded.compareAndSet(false, true)) { | ||
throw new PrestoException(CONFIGURATION_INVALID, "loadClientRequestFilters can only be called once."); | ||
} | ||
|
||
filters = factories.values().stream() | ||
.map(factory -> factory.create(factory.getName())) | ||
.collect(toImmutableList()); | ||
factories = null; | ||
} | ||
|
||
public List<ClientRequestFilter> getClientRequestFilters() | ||
{ | ||
return filters; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
presto-main/src/main/java/com/facebook/presto/ClientRequestFilterModule.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,28 @@ | ||
/* | ||
* 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 com.facebook.presto; | ||
|
||
import com.google.inject.Binder; | ||
import com.google.inject.Module; | ||
import com.google.inject.Scopes; | ||
|
||
public class ClientRequestFilterModule | ||
implements Module | ||
{ | ||
@Override | ||
public void configure(Binder binder) | ||
{ | ||
binder.bind(ClientRequestFilterManager.class).in(Scopes.SINGLETON); | ||
} | ||
} |
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
Oops, something went wrong.