Repository navigation
make sure pre saml login is enabled after saml is disabled #13953
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
base: main
Are you sure you want to change the base?
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 | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -54,6 +54,8 @@ | |||||||||||||||||
| import org.apache.cloudstack.framework.config.Configurable; | ||||||||||||||||||
| import org.apache.cloudstack.framework.security.keystore.KeystoreDao; | ||||||||||||||||||
| import org.apache.cloudstack.framework.security.keystore.KeystoreVO; | ||||||||||||||||||
| import org.apache.cloudstack.resourcedetail.UserDetailVO; | ||||||||||||||||||
| import org.apache.cloudstack.resourcedetail.dao.UserDetailsDao; | ||||||||||||||||||
| import org.apache.cloudstack.utils.security.CertUtils; | ||||||||||||||||||
| import org.apache.commons.codec.binary.Base64; | ||||||||||||||||||
| import org.apache.commons.httpclient.HttpClient; | ||||||||||||||||||
|
|
@@ -92,6 +94,10 @@ | |||||||||||||||||
| @Component | ||||||||||||||||||
| public class SAML2AuthManagerImpl extends AdapterBase implements SAML2AuthManager, Configurable { | ||||||||||||||||||
|
|
||||||||||||||||||
| /** Remembers the user's Source (e.g. LDAP) from before SAML was authorized, so disabling | ||||||||||||||||||
| * SAML can fall back to it instead of always defaulting to {@link User.Source#UNKNOWN}. */ | ||||||||||||||||||
| private static final String PRE_SAML_SOURCE_DETAIL_KEY = "PreSamlSource"; | ||||||||||||||||||
|
|
||||||||||||||||||
| private SAMLProviderMetadata _spMetadata = new SAMLProviderMetadata(); | ||||||||||||||||||
| private Map<String, SAMLProviderMetadata> _idpMetadataMap = new HashMap<String, SAMLProviderMetadata>(); | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
@@ -115,7 +121,10 @@ | |||||||||||||||||
| @Inject | ||||||||||||||||||
| private UserDao _userDao; | ||||||||||||||||||
|
|
||||||||||||||||||
| @Inject | ||||||||||||||||||
| private UserDetailsDao userDetailsDao; | ||||||||||||||||||
|
|
||||||||||||||||||
| @Inject | ||||||||||||||||||
|
Check warning on line 127 in plugins/user-authenticators/saml2/src/main/java/org/apache/cloudstack/saml/SAML2AuthManagerImpl.java
|
||||||||||||||||||
| DomainManager _domainMgr; | ||||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
|
|
@@ -446,26 +455,52 @@ | |||||||||||||||||
| @Override | ||||||||||||||||||
| public boolean authorizeUser(Long userId, String entityId, boolean enable) { | ||||||||||||||||||
| UserVO user = _userDao.getUser(userId); | ||||||||||||||||||
| if (user != null) { | ||||||||||||||||||
| if (enable) { | ||||||||||||||||||
| user.setExternalEntity(entityId); | ||||||||||||||||||
| user.setSource(User.Source.SAML2); | ||||||||||||||||||
| } else { | ||||||||||||||||||
| boolean enableLoginAfterSAMLDisable = SAML2AuthManager.EnableLoginAfterSAMLDisable.value(); | ||||||||||||||||||
| if (user.getSource().equals(User.Source.SAML2)) { | ||||||||||||||||||
| if(enableLoginAfterSAMLDisable) { | ||||||||||||||||||
| user.setSource(User.Source.UNKNOWN); | ||||||||||||||||||
| } else { | ||||||||||||||||||
| user.setSource(User.Source.SAML2DISABLED); | ||||||||||||||||||
| } | ||||||||||||||||||
| } else { | ||||||||||||||||||
| return false; | ||||||||||||||||||
| } | ||||||||||||||||||
| if (user == null) { | ||||||||||||||||||
| return false; | ||||||||||||||||||
| } | ||||||||||||||||||
| if (enable) { | ||||||||||||||||||
| enableSAMLForUser(user, entityId); | ||||||||||||||||||
| } else if (!disableSAMLForUser(user)) { | ||||||||||||||||||
| return false; | ||||||||||||||||||
| } | ||||||||||||||||||
| _userDao.update(user.getId(), user); | ||||||||||||||||||
| return true; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| private void enableSAMLForUser(UserVO user, String entityId) { | ||||||||||||||||||
| if (user.getSource() != null && !User.Source.SAML2.equals(user.getSource()) && !User.Source.SAML2DISABLED.equals(user.getSource())) { | ||||||||||||||||||
| userDetailsDao.addDetail(user.getId(), PRE_SAML_SOURCE_DETAIL_KEY, user.getSource().toString(), false); | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+471
to
+473
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.
Suggested change
|
||||||||||||||||||
| user.setExternalEntity(entityId); | ||||||||||||||||||
| user.setSource(User.Source.SAML2); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| private boolean disableSAMLForUser(UserVO user) { | ||||||||||||||||||
| if (!user.getSource().equals(User.Source.SAML2)) { | ||||||||||||||||||
| return false; | ||||||||||||||||||
| } | ||||||||||||||||||
|
Comment on lines
+478
to
+481
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.
Suggested change
|
||||||||||||||||||
| if (SAML2AuthManager.EnableLoginAfterSAMLDisable.value()) { | ||||||||||||||||||
|
Check warning on line 482 in plugins/user-authenticators/saml2/src/main/java/org/apache/cloudstack/saml/SAML2AuthManagerImpl.java
|
||||||||||||||||||
| user.setSource(getPreSamlSource(user.getId())); | ||||||||||||||||||
| } else { | ||||||||||||||||||
| user.setSource(User.Source.SAML2DISABLED); | ||||||||||||||||||
| } | ||||||||||||||||||
| return true; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * The Source (e.g. LDAP) the user had before SAML was authorized for them, so disabling | ||||||||||||||||||
| * SAML can restore it instead of always falling back to {@link User.Source#UNKNOWN}. | ||||||||||||||||||
| */ | ||||||||||||||||||
| private User.Source getPreSamlSource(long userId) { | ||||||||||||||||||
| UserDetailVO preSamlSource = userDetailsDao.findDetail(userId, PRE_SAML_SOURCE_DETAIL_KEY); | ||||||||||||||||||
| if (preSamlSource != null) { | ||||||||||||||||||
| try { | ||||||||||||||||||
| return User.Source.valueOf(preSamlSource.getValue()); | ||||||||||||||||||
| } catch (IllegalArgumentException e) { | ||||||||||||||||||
| logger.warn("Unrecognized pre-SAML source '{}' stored for user {}; falling back to UNKNOWN", preSamlSource.getValue(), userId); | ||||||||||||||||||
| } | ||||||||||||||||||
| _userDao.update(user.getId(), user); | ||||||||||||||||||
| return true; | ||||||||||||||||||
| } | ||||||||||||||||||
| return false; | ||||||||||||||||||
| return User.Source.UNKNOWN; | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| @Override | ||||||||||||||||||
|
|
||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.