Skip to content

Conversation

qqqttt123
Copy link
Contributor

@qqqttt123 qqqttt123 commented Dec 22, 2023

What changes were proposed in this pull request?

I wrap the cors filter of Jetty as our system internal filter. Most default values respect the default values of the cors filter of Jetty. I only change the default value of AllowMethods.The origin default value is PUT,GET,POST,HEAD. Because we usually use DELETE method, too. I change it to PUT,GET,POST,HEAD,DELETE.

Why are the changes needed?

Fix: #42

Does this PR introduce any user-facing change?

yes, I have added the document.

How was this patch tested?

UT + manual test

var http = new XMLHttpRequest();
var url = 'http://localhost:8090/api/version';
http.onreadystatechange = (e) => {
    console.log(http.responseText)
}
http.open("GET", url);
http.send();

docs/security.md Outdated
| `gravitino.auxService.iceberg-rest.allowedHeaders` | A comma separated list of HTTP headers that are allowed to be specified when accessing the resources. Default value is X-Requested-With,Content-Type,Accept,Origin. If the value is a single *, this means that any headers will be accepted. | `X-Requested-With,Content-Type,Accept,Origin` | No | 0.4.0 |
| `gravitino.auxService.iceberg-rest.preflightMaxAge` | The number of seconds that preflight requests can be cached by the client. Default value is 1800 seconds, or 30 minutes. | `1800` | No | 0.4.0 |
| `gravitino.auxService.iceberg-rest.allowCredentials` | A boolean indicating if the resource allows requests with credentials. Default value is true. | `true` | No | 0.4.0 |
| `gravitino.auxService.iceberg-rest.exposedHeaders` | A comma separated list of HTTP headers that are allowed to be exposed on the client. Default value is the empty list. | `` | No | 0.4.0 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that Server configuration and Iceberg REST service's configuration are almost the same, can we merge them into one and say 'Iceberg rest are the same, the difference is that the prefix of configuration key is ....'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's will be difficult for users.

docs/security.md Outdated
| `gravitino.server.webserver.allowedTimingOrigins` | A comma separated list of origins that are allowed to time the resource. Default value is the empty string, meaning no origins. | `` | No | 0.4.0 |
| `gravitino.server.webserver.allowedMethods` | a comma separated list of HTTP methods that are allowed to be used when accessing the resources. Default value is GET,POST,HEAD,DELETE. | `GET,POST,HEAD,DELETE` | No | 0.4.0 |
| `gravitino.server.webserver.allowedHeaders` | A comma separated list of HTTP headers that are allowed to be specified when accessing the resources. Default value is X-Requested-With,Content-Type,Accept,Origin. If the value is a single *, this means that any headers will be accepted. | `X-Requested-With,Content-Type,Accept,Origin` | No | 0.4.0 |
| `gravitino.server.webserver.preflightMaxAge` | The number of seconds that preflight requests can be cached by the client. Default value is 1800 seconds, or 30 minutes. | `1800` | No | 0.4.0 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preflight is pre + flight or perf + light? The meaning of preflight is?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre + flight. This word is from the Jetty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to change to "gravitino.server.webserver.preflightMaxAgeInSecs"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

.createWithDefault("X-Requested-With,Content-Type,Accept,Origin");

public static final ConfigEntry<Integer> PREFLIGHT_MAX_AGE =
new ConfigBuilder("preflightMaxAge")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preflightMaxAge -> preflightMaxAgeInSecs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

docs/security.md Outdated
|---------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------|----------|---------------|
| `gravitino.server.webserver.enableCorsFilter` | Enable cross origin resource share filter. | false | No | 0.4.0 |
| `gravitino.server.webserver.allowedOrigins` | A comma separated list of origins that are allowed to access the resources. Default value is *, meaning all origins. | `*` | No | 0.4.0 |
| `gravitino.server.webserver.allowedTimingOrigins` | A comma separated list of origins that are allowed to time the resource. Default value is the empty string, meaning no origins. | `` | No | 0.4.0 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What' the meaning of "time the resource"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should a term of HTTP. time the resource should mean that get metrics time from the server.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Timing-Allow-Origin

docs/security.md Outdated
| `gravitino.server.webserver.allowedTimingOrigins` | A comma separated list of origins that are allowed to time the resource. Default value is the empty string, meaning no origins. | `` | No | 0.4.0 |
| `gravitino.server.webserver.allowedMethods` | a comma separated list of HTTP methods that are allowed to be used when accessing the resources. Default value is GET,POST,HEAD,DELETE. | `GET,POST,HEAD,DELETE` | No | 0.4.0 |
| `gravitino.server.webserver.allowedHeaders` | A comma separated list of HTTP headers that are allowed to be specified when accessing the resources. Default value is X-Requested-With,Content-Type,Accept,Origin. If the value is a single *, this means that any headers will be accepted. | `X-Requested-With,Content-Type,Accept,Origin` | No | 0.4.0 |
| `gravitino.server.webserver.preflightMaxAge` | The number of seconds that preflight requests can be cached by the client. Default value is 1800 seconds, or 30 minutes. | `1800` | No | 0.4.0 |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to change to "gravitino.server.webserver.preflightMaxAgeInSecs"

public static final ConfigEntry<String> ALLOWED_HEADERS =
new ConfigBuilder("allowedHeaders")
.doc(
"A comma separated list of HTTP headers that are allowed to be specified when accessing the resources. Default value is X-Requested-With,Content-Type,Accept,Origin. If the value is a single *, this means that any headers will be accepted.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code line seems too long, is it ok for spotless check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I used spotless to format the file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to limit the line length to 100 words.

public static final ConfigEntry<String> ALLOWED_TIMING_ORIGINS =
new ConfigBuilder("allowedTimingOrigins")
.doc(
"A comma separated list of origins that are allowed to time the resource. Default value is the empty string, meaning no origins.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

means no origins.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

new ConfigBuilder("enableCorsFilter")
.doc("Enable cross origin resource share filter")
.version("0.4.0")
.booleanConf()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you have checkValue here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@jerryshao jerryshao changed the title [ISSUE-42] feat(server-common): Support cors filter [#42] feat(server-common): Support cors filter Dec 22, 2023
public static final ConfigEntry<String> ALLOWED_HEADERS =
new ConfigBuilder("allowedHeaders")
.doc(
"A comma separated list of HTTP headers that are allowed to be specified when accessing the resources. Default value is X-Requested-With,Content-Type,Accept,Origin. If the value is a single *, this means that any headers will be accepted.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to limit the line length to 100 words.

FilterHolder filterHolder = new FilterHolder();
filterHolder.setClassName(CrossOriginFilter.class.getName());
filterHolder.setInitParameter(
JettyServerConfig.ALLOWED_ORIGINS.getKey(), config.getAllowedOrigins());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this cors filter check the validity of the configuration? For example, if the configuration is not legal or have additional/trailing whitespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this cors filter check less things. Only check int type whether can be parsed to Integer.

import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlets.CrossOriginFilter;

class CorsFilterHelper {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest renaming this class to CorsFilterHolder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.


private CorsFilterHelper() {}

public static FilterHolder createCorsFilterHolder(JettyServerConfig config) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you can simplify the method name to create to remove duplication with class name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -253,15 +259,17 @@ public final class JettyServerConfig {
public static final ConfigEntry<String> EXPOSED_HEADERS =
new ConfigBuilder("exposedHeaders")
.doc(
"A comma separated list of HTTP headers that are allowed to be exposed on the client. Default value is the empty list")
"A comma separated list of HTTP headers that are allowed to be exposed on the client."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you miss a whitespace when you fold the sentence into two lines. Not just here, but also in the above code.

Heng Qin added 2 commits December 22, 2023 20:26
@qqqttt123 qqqttt123 merged commit fbbb7d6 into apache:main Dec 22, 2023
@qqqttt123 qqqttt123 deleted the ISSUE-42-2 branch December 22, 2023 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Improvement] Add cors filter for Jetty server
3 participants