Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)"));
}
}
Loading