Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HMS-2837 fix: middleware validation error response #23

Merged
merged 1 commit into from
Oct 26, 2023

Conversation

cryptomilk
Copy link
Contributor

We should not return a plaintext error response as the error won't be displayed in this case. By returning a new HTTP error the default error handler of the echo server will take care of it creating the error response in the correct format. As this is a REST API, the error will be returned in json format.

Pair-Programmed-With: Alejandro Visiedo

Steps to reproduce to check the errors:

$ make compose-down clean build compose-up run
...

$ ./test/scripts/local-domains-token.sh             
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Rh-Insights-Request-Id: test_52c262bd78e14daa835e
X-Xss-Protection: 1; mode=block
Date: Fri, 20 Oct 2023 11:15:10 GMT
Content-Length: 175

{"domain_id":"d4120943-fc98-5194-9d37-ac58dac9e199","domain_token":"F4_THf6XZy4.HJ_HIKZOtCupizBX8kQqIZiBaYKKwKP-xc9ydRvqoWI","domain_type":"rhel-idm","expiration":1697807710}


$ TOKEN="F4_THf6XZy4.HJ_HIKZOtCupizBX8kQqIZiBaYKKwKP-xc9ydRvqoWI"
$ UUID="d4120943-fc98-5194-9d37-ac58dac9e199"

$ ./test/scripts/local-domains-register.sh "$TOKEN" 
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Rh-Insights-Request-Id: test_acee9b7c694f48c68a2f
X-Xss-Protection: 1; mode=block
Date: Fri, 20 Oct 2023 11:16:00 GMT
Transfer-Encoding: chunked

{"auto_enrollment_enabled":true,"description":"","domain_id":"d4120943-fc98-5194-9d37-ac58dac9e199","domain_name":"mydomain.example","domain_type":"rhel-idm","rhel-idm":{"ca_certs":[{"issuer":"CN=Certificate Authority, O=MYDOMAIN.EXAMPLE","nickname":"MY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AQUFBwEB\nBDUwMzAxBggrBgEFBQcwAYYlaHR0cDovL2lwYS1jYS5obXNpZG0tZGV2LnRlc3Qv\nY2Evb2NzcDANBgkqhkiG9w0BAQsFAAOCAYEA6JDiMHd8aWSlyIQ8tg/mEH7mIvSz\niXWfygMcyXP5sGRvrE0yo2lbNfr8y3KnOGkNYMqrKJ28VBXAPjx5zLrooHynLYua\nLEsHw6XzvQWiWvcstSkKhcVOGdDqTMhl2XEGvx+LHZYBWKlb7i+L/0fDl0EUestS\ne4Shh63DLJ+7RaMFqoI/CHO/Jer5R4+dIMR8KSTTBhjEGLwN6rsRNI7D7vsyqDV8\ntZmhMHNEo9jtrPR8+tAzp6BaumioukI75nkAXrKiB0GRXI/jRp94VqEZstWcQPqc\nxzRRyR2Htet4AVbUWnSq2TRWIyeIecgPVmHXgDPpFWrwi/hpysXqT9sN/QOsCa3a\n2IpyGeuieProOeXb5lG4pbwePz5dRRlY3WRvhWdQm+dRGRErJt42KC7JAfiYoSmV\nDfJjQL2S11oYZt048ZQFIsUpiSJTmsCLXURIEuccrKT+WXR7D+WNkYm8aJ/4s8Ub\n+B8Vv5GjCTO5LrjgVWGZtxOttN/uJ1ecgZpW\n-----END CERTIFICATE-----\n","serial_number":"1","subject":"CN=My Domain, O=MYDOMAIN.EXAMPLE"}],"locations":[{"description":"Boston data center","name":"boston"},{"name":"europe"}],"realm_domains":["mydomain.example"],"realm_name":"MYDOMAIN.EXAMPLE","servers":[{"ca_server":true,"fqdn":"ipaserver.mydomain.example","hcc_enrollment_server":true,"hcc_update_server":true,"location":"boston","pkinit_server":true,"subscription_manager_id":"6f324116-b3d2-11ed-8a37-482ae3863d30"},{"ca_server":false,"fqdn":"server2.mydomain.example","hcc_enrollment_server":false,"hcc_update_server":false,"pkinit_server":false,"subscription_manager_id":"6f324116-b3d2-11ed-8a37-482ae3863d30"}]},"title":"mydomain.example"}

$ make db-cli                                 
podman exec --interactive --tty --env POSTGRES_DB=idmsvc-db --env POSTGRES_USER=idmsvc-user --env POSTGRES_PASSWORD=idmsvc-secret idmsvc_database_1 psql sslmode=disable dbname=idmsvc-db user=idmsvc-user host=localhost port=5432 password=idmsvc-secret
psql (13.9 (Debian 13.9-1.pgdg110+1))
Type "help" for help.

idmsvc-db=# UPDATE domains set title=E'my title\n';     
UPDATE 1
idmsvc-db=# \q
exit code: 0

Current main branch:

./test/scripts/local-domains-read.sh "$UUID"
HTTP/1.1 500 Internal Server Error
Content-Type: text/plain
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Rh-Insights-Request-Id: test_09578e93515741dba19b
X-Xss-Protection: 1; mode=block
Date: Fri, 20 Oct 2023 11:28:20 GMT
Content-Length: 117

With the commit from this PR:

./test/scripts/local-domains-read.sh "$UUID"
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=UTF-8
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Rh-Insights-Request-Id: test_9b103faea1404b6db74d
X-Xss-Protection: 1; mode=block
Date: Fri, 20 Oct 2023 11:27:37 GMT
Content-Length: 134

{"message":"response body doesn't match schema #/components/schemas/DomainResponse: Error at \"/title\": invalid code point: U+000A"}

@app-sre-bot
Copy link
Collaborator

Can one of the admins verify this patch?

@frasertweedale
Copy link
Contributor

reviewing this today!

Copy link
Contributor

@frasertweedale frasertweedale left a comment

Choose a reason for hiding this comment

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

I think the server should respond 500 for response validation failure. Besides that this PR is good! Thank you.

internal/infrastructure/middleware/validate_api.go Outdated Show resolved Hide resolved
We should not return a plaintext error response as the error won't be displayed
in this case. By returning a new HTTP error the default error handler of the
echo server will take care of it creating the error response in the correct
format. As this is a REST API, the error will be returned in json format.

Pair-Programmed-With: Alejandro Visiedo <[email protected]>
@frasertweedale
Copy link
Contributor

@cryptomilk thank you for the update. Merging.

@frasertweedale frasertweedale merged commit e7b9b6f into podengo-project:main Oct 26, 2023
1 check passed
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.

3 participants