Repository navigation
Conversation
…size HttpURLConnection never follows redirects that change the protocol, so a direct download QCOW2 template registered with an http:// URL that redirects to https:// failed the virtual size check with "Failed to connect URL". When redirects are enabled, follow them in QCOW2Utils with a limit of 10 hops, resolving relative Location headers against the current URL. Only http and https targets are followed and https is never downgraded to http. Fixes apache#14506 Assisted-by: Claude Opus 5.5 (Claude Code)
|
Congratulations on your first Pull Request and welcome to the Apache CloudStack community! If you have any issues or are unsure about any anything please check our Contribution Guide (https://github.com/apache/cloudstack/blob/main/CONTRIBUTING.md)
|
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes the QCOW2 virtual size check for KVM direct download templates whose
http://URL redirects tohttps://.QCOW2Utils.getVirtualSizeFromUrl()usedHttpURLConnection.setInstanceFollowRedirects(followRedirects).HttpURLConnectionnever follows a redirect that changes the protocol, so withstore.download.follow.redirects=trueanhttp://tohttps://redirect returned the 301 response itself. Reading the header from the empty body then failed with "Unable to skip to the virtual size header", reported as "Failed to connect URL", so the template could not be registered.Changes in
QCOW2Utils:followRedirectsis true, redirects (301, 302, 303, 307, 308) are followed in the method itself, up to 10 hops, and theLocationheader is resolved against the current URL so relative locations work.httpandhttpstargets are followed. AnhttpsURL is never redirected tohttp: the downgrade is refused, logged and fails the check, the same as any other connection error. Same-scheme redirects andhttptohttpsupgrades are allowed.followRedirectsis false the behaviour is unchanged: the redirect is not followed.Not covered here: #14505 (trust store used by the commons-httpclient based HEAD/GET checks) and the
HttpsDirectTemplateDownloader.getRemoteFileSize()trust store point mentioned in the issue. Those are separate code paths.Fixes: #14506
Types of changes
Feature/Enhancement Scale or Bug Severity
Feature/Enhancement Scale
Bug Severity
Screenshots (if appropriate):
How Has This Been Tested?
New unit tests in
QCOW2UtilsTest, using a localcom.sun.net.httpserver.HttpServer:getRedirectUrl: relativeLocationresolution,httptohttpsallowed,httpstohttprefused, non-HTTP scheme (file:) refused, missingLocationrefused.getVirtualSizeFromUrl: follows a relative redirect and returns the virtual size; does not follow a redirect whenfollowRedirectsis false; stops after 10 redirects (11 requests); follows anhttptohttpsredirect (the test checks that the https target port receives a connection; the TLS handshake then fails because the target is a plain socket).Results (JDK 17, Maven 3.9):
mvn -pl utils test -Dtest=QCOW2UtilsTest: 11 tests pass, checkstyle passes.getVirtualSizeFromUrlbody,getVirtualSizeFromUrlFollowsHttpToHttpsRedirectfails (the https target is never contacted) andgetVirtualSizeFromUrlStopsAfterTooManyRedirectsfails (expected:<11> but was:<20>).mvn -pl utils test: 2405 tests, 0 failures, 14 skipped.QCOW2Utils.getVirtualSizeFromUrl(url, true)from a small Java program:http://github.com/cirros-dev/cirros/releases/download/0.6.2/cirros-0.6.2-x86_64-disk.imgreturns117440512with this change, and throwsIllegalArgumentException: Failed to connect URLwith the current4.22class.Not tested: a full template registration on a KVM host and management server.
How did you try to break this feature and the system with this change?
httpstohttpdowngrades and redirects to other schemes such asfile:are refused.HttpDirectTemplateDownloader.getRemoteFileSize(); the method signature is unchanged.Assisted-by: Claude Opus 5.5 (Claude Code)