-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Dereference NULL return value #618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
||
|
|
@@ -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 | ||
|
|
@@ -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 { | ||
|
|
@@ -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); | ||
|
|
@@ -358,7 +363,8 @@ private boolean setup() { | |
| } | ||
| _timer = new Timer(); | ||
| final HttpClient client = new HttpClient(); | ||
| final String idpMetaDataUrl = SAMLIdentityProviderMetadataURL.value(); | ||
| //final String idpMetaDataUrl = SAMLIdentityProviderMetadataURL.value(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove commented deadcode, or simply keep final String idpMetaDataUrl = SAMLIdentityProviderMetadataURL.value(); as not many consumers of the new getSAMLIdentityProviderMetadataURL() method.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bhaisaab Sure. Removing the comment. The function call is needed for mocking at the time of unit testing. |
||
| final String idpMetaDataUrl = getSAMLIdentityProviderMetadataURL(); | ||
| if (SAMLTimeout.value() != null && SAMLTimeout.value() > SAMLPluginConstants.SAML_REFRESH_INTERVAL) { | ||
| _refreshInterval = SAMLTimeout.value(); | ||
| } | ||
|
|
@@ -368,21 +374,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; | ||
| } | ||
|
|
||
| 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); | ||
|
|
||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can remove else {} here just keep;
if (condition){
return stuff();
}
return otherstuff();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bhaisaab I feel we can go with the if-else style because it anyhow is doing the same thing plus it give better code readability and logging. Your take?