Repository navigation
cmd: fix signature algorithm detection error shadowing and login fallback - #230
Open
Paramveersingh-S wants to merge 1 commit into
Open
Paramveersingh-S wants to merge 1 commit into
Paramveersingh-S wants to merge 1 commit into
Conversation
Author
|
cc @sureshanaparti @weizhouapache @borisstoyanov for review whenever you get a chance — fixes #226 ( |
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.
Fixes #226
Problem
Commit
c3c3377(Support API signing with HmacSHA512 (#213)) introduced two regressions incmd/network.gowhen a profile hassignaturealgorithm = auto(the default for new and migrated profiles) alongside invalid/staleapikey/secretkeyand validusername/password:Error variable shadowing in
detectSignatureAlgorithm(cmd/network.go:385-396):Because
if _, err := parseAPIResponse(body); err == nilshadows the outererr(returned byexecuteSignedAPIRequest, which isnilonce an HTTP response is received),errafter theifblock is alwaysnil:lastErr = erroverwriteslastErrwithnilon every failed HTTP probe, causingdetectSignatureAlgorithmto discard the parsed CloudStack API error and fall through to the genericerrors.New("failed to detect signature algorithm").return "", errreturns"", nil, causingsignRequestto fail downstream withsignature algorithm must be concrete.Missing fallback to
username/passwordlogin when profileapikey/secretkeyfail (cmd/network.go:481-495):#213(and preserved by#174when!r.CredentialsSupplied), if a profile in.cmk/configcontained bothapikey/secretkeyandusername/passwordand the API key was stale or invalid (returning HTTP 401),NewAPIRequestfell back toLogin(r)withusername/passwordwhen credentials were not supplied on the command line (!r.CredentialsSupplied).#213,NewAPIRequestreturned immediately inside theif len(r.Config.ActiveProfile.APIKey) > 0 && len(r.Config.ActiveProfile.SecretKey) > 0branch on both signature detection failure (signaturealgorithm = auto) and HTTP 401 response (signaturealgorithm = HmacSHA1/HmacSHA512), never falling back toLogin(r).Solution
detectSignatureAlgorithm:parseAPIResponse(body)toparseErr(avoiding shadowing the outererr) and closeresponse.Bodyimmediately after reading inside the probe loop.lastErr = parseErr, passparseErrtoisAuthenticationFailure(response.StatusCode, parseErr), and return"", parseErron non-authentication probe errors.NewAPIRequest:paramsbefore attachingapiKeyso the base request parameters stay clean if API-key auth falls back to session login.!r.CredentialsSuppliedandusername/passwordare configured in the active profile:activeSignatureAlgorithm(r)fails with an authentication error (isAuthenticationFailure(0, err)), fall through toLogin(r)session authentication.executeSignedAPIRequestreturnshttp.StatusUnauthorized(401), close the response body and fall through toLogin(r)session authentication.r.CredentialsSuppliedistrue(CLI-k/-sor environment variables) orusername/passwordare not configured, preserve#174behavior and return the error directly without falling back toLogin(r).Verification / Solution Proof
Added four unit tests in
cmd/network_test.go:TestAutoFallsBackToLoginWhenProfileAPIKeysAreInvalid: verifies that when a profile hassignaturealgorithm = auto, invalidapikey/secretkey, and validusername/password(CredentialsSupplied = false),NewAPIRequestprobesHmacSHA512andHmacSHA1, falls back toLogin(command=login), completes the command withsessionkey(without sendingapiKey), and keepssignaturealgorithm = auto.TestExplicitSignatureAlgorithmFallsBackToLoginWhenProfileAPIKeysAreInvalid: verifies that an explicitHmacSHA1profile with invalidapikey/secretkeyand validusername/password(CredentialsSupplied = false) falls back toLoginon HTTP 401.TestAutoDoesNotFallBackToLoginWhenCredentialsSuppliedOnCLI: verifies that whenCredentialsSupplied = true,NewAPIRequestdoes not fall back toLoginand surfaces the server's parsed authentication error ((HTTP 401, error code 9999) unable to verify user credentials and/or request signature) instead of"failed to detect signature algorithm".TestAutoReturnsNonAuthProbeErrorImmediately: verifies that a non-authentication error during probing (HTTP 500) is returned immediately on the first probe instead of returning"", nil.