-
Notifications
You must be signed in to change notification settings - Fork 1.4k
add test ldap test command and button #13954
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 | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,88 @@ | ||||||
| // 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.api.command; | ||||||
|
|
||||||
| import javax.inject.Inject; | ||||||
|
|
||||||
| import org.apache.cloudstack.api.APICommand; | ||||||
| import org.apache.cloudstack.api.ApiConstants; | ||||||
| import org.apache.cloudstack.api.BaseCmd; | ||||||
| import org.apache.cloudstack.api.Parameter; | ||||||
| import org.apache.cloudstack.api.ServerApiException; | ||||||
| import org.apache.cloudstack.api.response.DomainResponse; | ||||||
| import org.apache.cloudstack.api.response.SuccessResponse; | ||||||
| import org.apache.cloudstack.ldap.LdapManager; | ||||||
|
|
||||||
| import com.cloud.exception.InvalidParameterValueException; | ||||||
| import com.cloud.user.Account; | ||||||
|
|
||||||
| @APICommand(name = "testLdapConfiguration", description = "Tests connectivity to an LDAP server without saving a configuration", responseObject = SuccessResponse.class, | ||||||
| since = "4.23.0", requestHasSensitiveInfo = false, responseHasSensitiveInfo = false) | ||||||
|
Collaborator
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
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. awaiting 4.23 release |
||||||
| public class LdapTestConfigurationCmd extends BaseCmd { | ||||||
|
|
||||||
| @Inject | ||||||
|
Check warning on line 37 in plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapTestConfigurationCmd.java
|
||||||
| private LdapManager _ldapManager; | ||||||
|
Check warning on line 38 in plugins/user-authenticators/ldap/src/main/java/org/apache/cloudstack/api/command/LdapTestConfigurationCmd.java
|
||||||
|
|
||||||
| @Parameter(name = ApiConstants.HOST_NAME, type = CommandType.STRING, required = true, description = "Hostname") | ||||||
| private String hostname; | ||||||
|
|
||||||
| @Parameter(name = ApiConstants.PORT, type = CommandType.INTEGER, description = "Port") | ||||||
| private int port; | ||||||
|
|
||||||
| @Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "Linked Domain") | ||||||
| private Long domainId; | ||||||
|
|
||||||
| public LdapTestConfigurationCmd() { | ||||||
| super(); | ||||||
| } | ||||||
|
|
||||||
| public LdapTestConfigurationCmd(final LdapManager ldapManager) { | ||||||
| super(); | ||||||
| _ldapManager = ldapManager; | ||||||
| } | ||||||
|
|
||||||
| public String getHostname() { | ||||||
| return hostname; | ||||||
| } | ||||||
|
|
||||||
| public int getPort() { | ||||||
| return port; | ||||||
| } | ||||||
|
|
||||||
| public Long getDomainId() { | ||||||
| return domainId; | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public void execute() throws ServerApiException { | ||||||
| SuccessResponse response = new SuccessResponse(getCommandName()); | ||||||
| try { | ||||||
| _ldapManager.testConnection(this); | ||||||
| response.setSuccess(true); | ||||||
| response.setDisplayText("Successfully connected to the LDAP server"); | ||||||
| } catch (InvalidParameterValueException e) { | ||||||
| response.setSuccess(false); | ||||||
| response.setDisplayText(e.getMessage()); | ||||||
| } | ||||||
| setResponseObject(response); | ||||||
| } | ||||||
|
|
||||||
| @Override | ||||||
| public long getEntityOwnerId() { | ||||||
| return Account.ACCOUNT_ID_SYSTEM; | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // 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.ldap; | ||
|
|
||
| import com.cloud.domain.dao.DomainDao; | ||
| import com.cloud.exception.InvalidParameterValueException; | ||
| import com.cloud.user.AccountManager; | ||
| import org.apache.cloudstack.api.command.LdapTestConfigurationCmd; | ||
| import org.apache.cloudstack.ldap.dao.LdapConfigurationDao; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.MockitoJUnitRunner; | ||
| import org.springframework.test.util.ReflectionTestUtils; | ||
|
|
||
| import javax.naming.NamingException; | ||
|
|
||
| import static org.junit.Assert.assertThrows; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyLong; | ||
| import static org.mockito.Mockito.doThrow; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * Tests {@link LdapManagerImpl#testConnection} and {@link LdapManagerImpl#addConfiguration}: | ||
| * testing a connection binds to the LDAP server (defaulting the port to 389 when omitted) but | ||
| * never persists a configuration, whether the bind succeeds or fails; adding a configuration | ||
| * still binds before persisting, and does not persist when the bind fails. | ||
| */ | ||
| @RunWith(MockitoJUnitRunner.class) | ||
| public class LdapManagerImplTest { | ||
|
|
||
| private static final long DOMAIN_ID = 1L; | ||
|
|
||
| private LdapManagerImpl ldapManager; | ||
|
|
||
| @Mock | ||
| private LdapConfigurationDao ldapConfigurationDao; | ||
|
|
||
| @Mock | ||
| private LdapContextFactory ldapContextFactory; | ||
|
|
||
| @Mock | ||
| private DomainDao domainDao; | ||
|
|
||
| @Mock | ||
| private AccountManager accountManager; | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| ldapManager = new LdapManagerImpl(ldapConfigurationDao, ldapContextFactory, null, null); | ||
| ReflectionTestUtils.setField(ldapManager, "domainDao", domainDao); | ||
| ReflectionTestUtils.setField(ldapManager, "accountManager", accountManager); | ||
| } | ||
|
|
||
| @Test | ||
| public void testConnectionDoesNotPersistOnSuccess() throws Exception { | ||
| LdapTestConfigurationCmd cmd = buildCmd("ldap.example.com", 389, DOMAIN_ID); | ||
|
|
||
| ldapManager.testConnection(cmd); | ||
|
|
||
| verify(ldapContextFactory).createBindContext("ldap://ldap.example.com:389", DOMAIN_ID); | ||
| verify(ldapConfigurationDao, never()).persist(any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testConnectionDefaultsPortWhenNotGiven() throws Exception { | ||
| LdapTestConfigurationCmd cmd = buildCmd("ldap.example.com", 0, DOMAIN_ID); | ||
|
|
||
| ldapManager.testConnection(cmd); | ||
|
|
||
| verify(ldapContextFactory).createBindContext("ldap://ldap.example.com:389", DOMAIN_ID); | ||
| } | ||
|
|
||
| @Test | ||
| public void testConnectionThrowsOnBindFailure() throws Exception { | ||
| LdapTestConfigurationCmd cmd = buildCmd("ldap.example.com", 389, DOMAIN_ID); | ||
| doThrow(new NamingException("bind failed")).when(ldapContextFactory).createBindContext(any(), anyLong()); | ||
|
|
||
| assertThrows(InvalidParameterValueException.class, () -> ldapManager.testConnection(cmd)); | ||
|
|
||
| verify(ldapConfigurationDao, never()).persist(any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void addConfigurationStillBindsBeforePersisting() throws Exception { | ||
| when(ldapConfigurationDao.find("ldap.example.com", 389, DOMAIN_ID)).thenReturn(null); | ||
| when(ldapConfigurationDao.persist(any())).thenAnswer(invocation -> invocation.getArgument(0)); | ||
|
|
||
| ldapManager.addConfiguration("ldap.example.com", 389, DOMAIN_ID); | ||
|
|
||
| verify(ldapContextFactory).createBindContext("ldap://ldap.example.com:389", DOMAIN_ID); | ||
| verify(ldapConfigurationDao).persist(any()); | ||
| } | ||
|
|
||
| @Test | ||
| public void addConfigurationDoesNotPersistOnBindFailure() throws Exception { | ||
| when(ldapConfigurationDao.find("ldap.example.com", 389, DOMAIN_ID)).thenReturn(null); | ||
| doThrow(new NamingException("bind failed")).when(ldapContextFactory).createBindContext(any(), anyLong()); | ||
|
|
||
| assertThrows(InvalidParameterValueException.class, () -> ldapManager.addConfiguration("ldap.example.com", 389, DOMAIN_ID)); | ||
|
|
||
| verify(ldapConfigurationDao, never()).persist(any()); | ||
| } | ||
|
|
||
| private LdapTestConfigurationCmd buildCmd(String hostname, int port, long domainId) { | ||
| LdapTestConfigurationCmd cmd = new LdapTestConfigurationCmd(); | ||
| ReflectionTestUtils.setField(cmd, "hostname", hostname); | ||
| ReflectionTestUtils.setField(cmd, "port", port); | ||
| ReflectionTestUtils.setField(cmd, "domainId", domainId); | ||
| return cmd; | ||
| } | ||
| } |
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.
Why isn't the command name "ldapTestConfiguration" (same as class name)? Would keep consistency and group it with other Ldap APIs
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.
good point, mainly because I didn’t review the AI output enough, thanks. I will revisit later,