Skip to content

Commit

Permalink
fix: do not use Marshmallow validation in partial params validation
Browse files Browse the repository at this point in the history
  • Loading branch information
betodealmeida committed Jun 17, 2021
1 parent 243af7f commit 7b370b1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 17 deletions.
8 changes: 8 additions & 0 deletions docs/src/pages/docs/Miscellaneous/issue_codes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,11 @@ The submitted payload has the incorrect schema.
```

Please check that the request payload has the expected schema.

## Issue 1021

```
The database port provided is invalid.
```

Please check that the provided database port is an integer between 0 and 65535 (inclusive).
1 change: 1 addition & 0 deletions superset-frontend/src/components/ErrorMessage/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const ErrorTypeEnum = {
CONNECTION_INVALID_PASSWORD_ERROR: 'CONNECTION_INVALID_PASSWORD_ERROR',
CONNECTION_INVALID_HOSTNAME_ERROR: 'CONNECTION_INVALID_HOSTNAME_ERROR',
CONNECTION_PORT_CLOSED_ERROR: 'CONNECTION_PORT_CLOSED_ERROR',
CONNECTION_INVALID_PORT_ERROR: 'CONNECTION_INVALID_PORT_ERROR',
CONNECTION_HOST_DOWN_ERROR: 'CONNECTION_HOST_DOWN_ERROR',
CONNECTION_ACCESS_DENIED_ERROR: 'CONNECTION_ACCESS_DENIED_ERROR',
CONNECTION_UNKNOWN_DATABASE_ERROR: 'CONNECTION_UNKNOWN_DATABASE_ERROR',
Expand Down
11 changes: 0 additions & 11 deletions superset/databases/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,17 +346,6 @@ class Meta: # pylint: disable=too-few-public-methods
description=configuration_method_description,
)

@validates_schema
def validate_parameters( # pylint: disable=no-self-use
self, data: Dict[str, Any], **kwargs: Any # pylint: disable=unused-argument
) -> None:
"""
Validate the DB engine spec specific parameters schema.
"""
# TODO (aafghahi): Move this onCreate instead of validation
engine_spec = get_engine_spec(data.get("engine") or data.get("backend"))
engine_spec.parameters_schema.load(data["parameters"]) # type: ignore


class DatabasePostSchema(Schema, DatabaseParametersSchemaMixin):
class Meta: # pylint: disable=too-few-public-methods
Expand Down
11 changes: 10 additions & 1 deletion superset/db_engine_specs/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,16 @@ def validate_parameters(
port = parameters.get("port", None)
if not port:
return errors
if not is_port_open(host, port):
if not (isinstance(port, int) and 0 <= port < 2 ** 16):
errors.append(
SupersetError(
message="The port must be an integer between 0 and 65535 (inclusive).",
error_type=SupersetErrorType.CONNECTION_INVALID_PORT_ERROR,
level=ErrorLevel.ERROR,
extra={"invalid": ["port"]},
),
)
elif not is_port_open(host, port):
errors.append(
SupersetError(
message="The port is closed.",
Expand Down
4 changes: 4 additions & 0 deletions superset/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class SupersetErrorType(str, Enum):
CONNECTION_INVALID_PASSWORD_ERROR = "CONNECTION_INVALID_PASSWORD_ERROR"
CONNECTION_INVALID_HOSTNAME_ERROR = "CONNECTION_INVALID_HOSTNAME_ERROR"
CONNECTION_PORT_CLOSED_ERROR = "CONNECTION_PORT_CLOSED_ERROR"
CONNECTION_INVALID_PORT_ERROR = "CONNECTION_INVALID_PORT_ERROR"
CONNECTION_HOST_DOWN_ERROR = "CONNECTION_HOST_DOWN_ERROR"
CONNECTION_ACCESS_DENIED_ERROR = "CONNECTION_ACCESS_DENIED_ERROR"
CONNECTION_UNKNOWN_DATABASE_ERROR = "CONNECTION_UNKNOWN_DATABASE_ERROR"
Expand Down Expand Up @@ -156,6 +157,9 @@ class SupersetErrorType(str, Enum):
SupersetErrorType.CONNECTION_PORT_CLOSED_ERROR: [
{"code": 1008, "message": _("Issue 1008 - The port is closed.")},
],
SupersetErrorType.CONNECTION_INVALID_PORT_ERROR: [
{"code": 1021, "message": _("Issue 1021 - Port number is invalid.")},
],
SupersetErrorType.CONNECTION_HOST_DOWN_ERROR: [
{
"code": 1009,
Expand Down
10 changes: 5 additions & 5 deletions tests/databases/api_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1807,18 +1807,18 @@ def test_validate_parameters_invalid_port(self):
assert response == {
"errors": [
{
"message": "Not a valid integer.",
"error_type": "INVALID_PAYLOAD_SCHEMA_ERROR",
"level": "error",
"error_type": "CONNECTION_INVALID_PORT_ERROR",
"extra": {
"invalid": ["port"],
"issue_codes": [
{
"code": 1020,
"message": "Issue 1020 - The submitted payload has the incorrect schema.",
"code": 1021,
"message": "Issue 1021 - Port number is invalid.",
}
],
},
"level": "error",
"message": "The port must be an integer between 0 and 65535 (inclusive).",
}
]
}
Expand Down

0 comments on commit 7b370b1

Please sign in to comment.