From e076115ed0574e9ddef296ec02427b4324e8f3dd Mon Sep 17 00:00:00 2001 From: Daman Arora Date: Wed, 25 Nov 2026 13:49:00 -0500 Subject: [PATCH] flasharray: say the login failed when the array rejects the password --- .../adapter/flasharray/FlashArrayAdapter.java | 26 +++++++++-- .../FlashArrayAdapterLoginTest.java | 45 +++++++++++++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) create mode 100644 plugins/storage/volume/flasharray/src/test/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapterLoginTest.java diff --git a/plugins/storage/volume/flasharray/src/main/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapter.java b/plugins/storage/volume/flasharray/src/main/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapter.java index fae94bb1bea0..e57bcba0521f 100644 --- a/plugins/storage/volume/flasharray/src/main/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapter.java +++ b/plugins/storage/volume/flasharray/src/main/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapter.java @@ -61,6 +61,7 @@ import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.ssl.SSLContextBuilder; +import org.apache.http.util.EntityUtils; import com.cloud.utils.exception.CloudRuntimeException; import com.fasterxml.jackson.core.JsonProcessingException; @@ -661,10 +662,9 @@ private String getApiTokenUsingUserPass(CloseableHttpClient client) throws IOExc "Authentication responded successfully but no api token was returned"); } return legacyToken.getApiToken(); - } else if (statusCode == 401 || statusCode == 403) { - throw new CloudRuntimeException( - "Authentication or Authorization to FlashArray [" + url + "] with user [" + username - + "] failed, unable to retrieve session token"); + } else if (statusCode == 400 || statusCode == 401 || statusCode == 403) { + // the legacy login endpoint answers 400 {"msg": "invalid credentials"} for a wrong username or password + throw new CloudRuntimeException(legacyLoginFailureMessage(statusCode, readBody(response))); } else { throw new CloudRuntimeException( "Unexpected HTTP response code from FlashArray [" + url + "] - [" + statusCode @@ -681,6 +681,24 @@ private String getApiTokenUsingUserPass(CloseableHttpClient client) throws IOExc } } + String legacyLoginFailureMessage(int statusCode, String arrayMessage) { + String msg = "Authentication to FlashArray [" + url + "] with user [" + username + + "] failed, check the username and password configured for this storage pool (HTTP " + statusCode; + if (arrayMessage != null && !arrayMessage.isBlank()) { + msg += ": " + arrayMessage.trim(); + } + return msg + ")"; + } + + private String readBody(CloseableHttpResponse response) { + try { + return response.getEntity() == null ? null + : EntityUtils.toString(response.getEntity(), "UTF-8"); + } catch (IOException e) { + return null; + } + } + private synchronized void refreshSession(boolean force) { try { if (force || keyExpiration < System.currentTimeMillis()) { diff --git a/plugins/storage/volume/flasharray/src/test/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapterLoginTest.java b/plugins/storage/volume/flasharray/src/test/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapterLoginTest.java new file mode 100644 index 000000000000..2c8cf635f2c7 --- /dev/null +++ b/plugins/storage/volume/flasharray/src/test/java/org/apache/cloudstack/storage/datastore/adapter/flasharray/FlashArrayAdapterLoginTest.java @@ -0,0 +1,45 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.storage.datastore.adapter.flasharray; + +import static org.mockito.Mockito.mock; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +public class FlashArrayAdapterLoginTest { + + private final FlashArrayAdapter adapter = mock(FlashArrayAdapter.class, Mockito.CALLS_REAL_METHODS); + + @Test + public void wrongPasswordSaysToCheckCredentials() { + String msg = adapter.legacyLoginFailureMessage(400, "{\"msg\": \"invalid credentials\"}"); + + Assert.assertTrue(msg.contains("check the username and password")); + Assert.assertTrue(msg.contains("HTTP 400")); + Assert.assertTrue(msg.contains("invalid credentials")); + } + + @Test + public void missingArrayMessageStillReadsCleanly() { + String msg = adapter.legacyLoginFailureMessage(401, null); + + Assert.assertTrue(msg.contains("check the username and password")); + Assert.assertTrue(msg.endsWith("(HTTP 401)")); + } +}