Skip to content
Closed
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 @@ -104,6 +104,10 @@ public class SAML2AuthManagerImpl extends AdapterBase implements SAML2AuthManage
private int _refreshInterval = SAMLPluginConstants.SAML_REFRESH_INTERVAL;
private AbstractReloadingMetadataProvider _idpMetaDataProvider;

public String getSAMLIdentityProviderMetadataURL(){
return SAMLIdentityProviderMetadataURL.value();
}

@Inject
private KeystoreDao _ksDao;

Expand All @@ -119,12 +123,12 @@ public class SAML2AuthManagerImpl extends AdapterBase implements SAML2AuthManage
@Override
public boolean start() {
if (isSAMLPluginEnabled()) {
setup();
s_logger.info("SAML auth plugin loaded");
return setup();
} else {
s_logger.info("SAML auth plugin not enabled so not loading");
return super.start();
}
return super.start();
}

@Override
Expand All @@ -135,7 +139,7 @@ public boolean stop() {
return super.stop();
}

private boolean initSP() {
protected boolean initSP() {
KeystoreVO keyStoreVO = _ksDao.findByName(SAMLPluginConstants.SAMLSP_KEYPAIR);
if (keyStoreVO == null) {
try {
Expand Down Expand Up @@ -338,6 +342,7 @@ public void run() {
return;
}
s_logger.debug("Starting SAML IDP Metadata Refresh Task");

Map <String, SAMLProviderMetadata> metadataMap = new HashMap<String, SAMLProviderMetadata>();
try {
discoverAndAddIdp(_idpMetaDataProvider.getMetadata(), metadataMap);
Expand All @@ -358,7 +363,7 @@ private boolean setup() {
}
_timer = new Timer();
final HttpClient client = new HttpClient();
final String idpMetaDataUrl = SAMLIdentityProviderMetadataURL.value();
final String idpMetaDataUrl = getSAMLIdentityProviderMetadataURL();
if (SAMLTimeout.value() != null && SAMLTimeout.value() > SAMLPluginConstants.SAML_REFRESH_INTERVAL) {
_refreshInterval = SAMLTimeout.value();
}
Expand All @@ -368,21 +373,31 @@ private boolean setup() {
_idpMetaDataProvider = new HTTPMetadataProvider(_timer, client, idpMetaDataUrl);
} else {
File metadataFile = PropertiesUtil.findConfigFile(idpMetaDataUrl);
s_logger.debug("Provided Metadata is not a URL, trying to read metadata file from local path: " + metadataFile.getAbsolutePath());
_idpMetaDataProvider = new FilesystemMetadataProvider(_timer, metadataFile);
if (metadataFile == null) {
s_logger.error("Provided Metadata is not a URL, Unable to locate metadata file from local path: " + idpMetaDataUrl);
return false;
}
else{
s_logger.debug("Provided Metadata is not a URL, trying to read metadata file from local path: " + metadataFile.getAbsolutePath());
_idpMetaDataProvider = new FilesystemMetadataProvider(_timer, metadataFile);
}
}
_idpMetaDataProvider.setRequireValidMetadata(true);
_idpMetaDataProvider.setParserPool(new BasicParserPool());
_idpMetaDataProvider.initialize();
_timer.scheduleAtFixedRate(new MetadataRefreshTask(), 0, _refreshInterval * 1000);

} catch (MetadataProviderException e) {
s_logger.error("Unable to read SAML2 IDP MetaData URL, error:" + e.getMessage());
s_logger.error("SAML2 Authentication may be unavailable");
return false;
} catch (ConfigurationException | FactoryConfigurationError e) {
s_logger.error("OpenSAML bootstrapping failed: error: " + e.getMessage());
return false;
} catch (NullPointerException e) {
s_logger.error("Unable to setup SAML Auth Plugin due to NullPointerException" +
" please check the SAML global settings: " + e.getMessage());
return false;
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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.saml;

import com.cloud.user.DomainManager;
import com.cloud.user.dao.UserDao;
import org.apache.cloudstack.framework.security.keystore.KeystoreDao;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;


@RunWith(MockitoJUnitRunner.class)
public class SAML2AuthManagerImplTest {

@Mock
private KeystoreDao _ksDao;

@Mock
private SAMLTokenDao _samlTokenDao;

@Mock
private UserDao _userDao;

@Mock
DomainManager _domainMgr;

@InjectMocks
@Spy
SAML2AuthManagerImpl saml2AuthManager = new SAML2AuthManagerImpl();

@Before
public void setUp() {
doReturn(true).when(saml2AuthManager).isSAMLPluginEnabled();
doReturn(true).when(saml2AuthManager).initSP();
}

@Test
public void testStart() {
when(saml2AuthManager.getSAMLIdentityProviderMetadataURL()).thenReturn("file://does/not/exist");
boolean started = saml2AuthManager.start();
assertFalse("saml2authmanager should not start as the file doesnt exist", started);

when(saml2AuthManager.getSAMLIdentityProviderMetadataURL()).thenReturn(" ");
started = saml2AuthManager.start();
assertFalse("saml2authmanager should not start as the file doesnt exist", started);

when(saml2AuthManager.getSAMLIdentityProviderMetadataURL()).thenReturn("");
started = saml2AuthManager.start();
assertFalse("saml2authmanager should not start as the file doesnt exist", started);

}
}