Skip to content

Commit

Permalink
fix(CO-1133): fix amavis ldap attrs definition (#533)
Browse files Browse the repository at this point in the history
* fix: do not include amavis attributes in zimbraAccount schema

* chore: re-generate ZAttrAccount

* fix(test): do not close the soap client after execute so we can read big body

* test: add GetInfo handler tests

* refactor: simplify SoapUtils

* refactor: use HttpClient instead of CloseableHttpClient
  • Loading branch information
matteobaglini authored Jul 1, 2024
1 parent 2708e4d commit 5f75ce3
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 144 deletions.
114 changes: 0 additions & 114 deletions store/src/main/java/com/zimbra/cs/account/ZAttrAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,120 +31,6 @@ protected ZAttrAccount(

///// BEGIN-AUTO-GEN-REPLACE

/**
*
* @return amavisBlacklistSender, or null if unset
*/
@ZAttr(id=-1)
public String getAmavisBlacklistSender() {
return getAttr(ZAttrProvisioning.A_amavisBlacklistSender, null, true);
}

/**
*
* @param amavisBlacklistSender new value
* @throws com.zimbra.common.service.ServiceException if error during update
*/
@ZAttr(id=-1)
public void setAmavisBlacklistSender(String amavisBlacklistSender) throws com.zimbra.common.service.ServiceException {
HashMap<String,Object> attrs = new HashMap<>();
attrs.put(ZAttrProvisioning.A_amavisBlacklistSender, amavisBlacklistSender);
getProvisioning().modifyAttrs(this, attrs);
}

/**
*
* @param amavisBlacklistSender new value
* @param attrs existing map to populate, or null to create a new map
* @return populated map to pass into Provisioning.modifyAttrs
*/
@ZAttr(id=-1)
public Map<String,Object> setAmavisBlacklistSender(String amavisBlacklistSender, Map<String,Object> attrs) {
if (attrs == null) attrs = new HashMap<>();
attrs.put(ZAttrProvisioning.A_amavisBlacklistSender, amavisBlacklistSender);
return attrs;
}

/**
*
* @throws com.zimbra.common.service.ServiceException if error during update
*/
@ZAttr(id=-1)
public void unsetAmavisBlacklistSender() throws com.zimbra.common.service.ServiceException {
HashMap<String,Object> attrs = new HashMap<>();
attrs.put(ZAttrProvisioning.A_amavisBlacklistSender, "");
getProvisioning().modifyAttrs(this, attrs);
}

/**
*
* @param attrs existing map to populate, or null to create a new map
* @return populated map to pass into Provisioning.modifyAttrs
*/
@ZAttr(id=-1)
public Map<String,Object> unsetAmavisBlacklistSender(Map<String,Object> attrs) {
if (attrs == null) attrs = new HashMap<>();
attrs.put(ZAttrProvisioning.A_amavisBlacklistSender, "");
return attrs;
}

/**
*
* @return amavisWhitelistSender, or null if unset
*/
@ZAttr(id=-1)
public String getAmavisWhitelistSender() {
return getAttr(ZAttrProvisioning.A_amavisWhitelistSender, null, true);
}

/**
*
* @param amavisWhitelistSender new value
* @throws com.zimbra.common.service.ServiceException if error during update
*/
@ZAttr(id=-1)
public void setAmavisWhitelistSender(String amavisWhitelistSender) throws com.zimbra.common.service.ServiceException {
HashMap<String,Object> attrs = new HashMap<>();
attrs.put(ZAttrProvisioning.A_amavisWhitelistSender, amavisWhitelistSender);
getProvisioning().modifyAttrs(this, attrs);
}

/**
*
* @param amavisWhitelistSender new value
* @param attrs existing map to populate, or null to create a new map
* @return populated map to pass into Provisioning.modifyAttrs
*/
@ZAttr(id=-1)
public Map<String,Object> setAmavisWhitelistSender(String amavisWhitelistSender, Map<String,Object> attrs) {
if (attrs == null) attrs = new HashMap<>();
attrs.put(ZAttrProvisioning.A_amavisWhitelistSender, amavisWhitelistSender);
return attrs;
}

/**
*
* @throws com.zimbra.common.service.ServiceException if error during update
*/
@ZAttr(id=-1)
public void unsetAmavisWhitelistSender() throws com.zimbra.common.service.ServiceException {
HashMap<String,Object> attrs = new HashMap<>();
attrs.put(ZAttrProvisioning.A_amavisWhitelistSender, "");
getProvisioning().modifyAttrs(this, attrs);
}

/**
*
* @param attrs existing map to populate, or null to create a new map
* @return populated map to pass into Provisioning.modifyAttrs
*/
@ZAttr(id=-1)
public Map<String,Object> unsetAmavisWhitelistSender(Map<String,Object> attrs) {
if (attrs == null) attrs = new HashMap<>();
attrs.put(ZAttrProvisioning.A_amavisWhitelistSender, "");
return attrs;
}

/**
* RFC2256: ISO-3166 country 2-letter code
*
Expand Down
4 changes: 2 additions & 2 deletions store/src/main/resources/conf/attrs/amavisd-new-attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

<attr name="amavisSpamModifiesSubj" type="boolean"/>

<attr name="amavisWhitelistSender" type="astring" max="256" optionalIn="account" flags="accountInfo" callback="WhiteBlackList"/>
<attr name="amavisWhitelistSender" type="astring" max="256" flags="accountInfo" callback="WhiteBlackList"/>

<attr name="amavisBlacklistSender" type="astring" max="256" optionalIn="account" flags="accountInfo" callback="WhiteBlackList"/>
<attr name="amavisBlacklistSender" type="astring" max="256" flags="accountInfo" callback="WhiteBlackList"/>

<attr name="amavisSpamQuarantineTo" type="astring" max="256"/>

Expand Down
3 changes: 2 additions & 1 deletion store/src/test/java/com/zextras/mailbox/soap/SoapUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
package com.zextras.mailbox.soap;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

public class SoapUtils {

public static String getResponse(HttpResponse response) throws Exception {
return new String (response.getEntity().getContent().readAllBytes());
return EntityUtils.toString(response.getEntity());
}
}
53 changes: 26 additions & 27 deletions store/src/test/java/com/zextras/mailbox/util/SoapClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,23 @@
import com.zimbra.common.util.ZimbraCookie;
import com.zimbra.cs.account.Account;
import com.zimbra.cs.account.AuthToken;
import com.zimbra.cs.account.AuthTokenException;
import com.zimbra.cs.service.AuthProvider;
import com.zimbra.soap.JaxbUtil;
import java.net.URI;
import java.util.Objects;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.cookie.BasicClientCookie;

/**
* A SoapClient that wraps the Body in an Envelope.
*
*/
/** A SoapClient that wraps the Body in an Envelope. */
public class SoapClient {

/**
* Endpoint in form of http://<host>:<port>/<basePath>
*/
/** Endpoint in form of http://<host>:<port>/<basePath> */
private String endpoint;

public SoapClient(String endpoint) {
Expand Down Expand Up @@ -70,29 +66,34 @@ public Request setRequestedAccount(Account requestedAccount) {
private String url = "/";

public HttpResponse execute() throws Exception {
final var cookieStore = createCookieAuthToken();
HttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore).build();
final HttpPost httpPost = new HttpPost();
Element envelope;
if (Objects.isNull(requestedAccount)) {
envelope = SoapProtocol.Soap12.soapEnvelope(soapBody);
} else {
final Element headerXml =
Element.parseXML(
String.format(
"<context xmlns=\"urn:zimbra\"><account by=\"id\">%s</account></context>",
requestedAccount.getId()));
envelope = SoapProtocol.Soap12.soapEnvelope(soapBody, headerXml);
}
httpPost.setURI(URI.create(this.url));
httpPost.setEntity(new StringEntity(envelope.toString()));
return client.execute(httpPost);
}

private BasicCookieStore createCookieAuthToken() throws AuthTokenException, ServiceException {
AuthToken authToken = AuthProvider.getAuthToken(caller, isAdminAccount());
BasicCookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie =
new BasicClientCookie(ZimbraCookie.authTokenCookieName(false), authToken.getEncoded());
cookie.setDomain(caller.getServerName());
cookie.setPath("/");
cookieStore.addCookie(cookie);
try (CloseableHttpClient client = HttpClientBuilder.create().setDefaultCookieStore(cookieStore)
.build()) {
final HttpPost httpPost = new HttpPost();
Element envelope;
if (Objects.isNull(requestedAccount)) {
envelope = SoapProtocol.Soap12.soapEnvelope(soapBody);
} else {
final Element headerXml = Element.parseXML(String.format(
"<context xmlns=\"urn:zimbra\"><account by=\"id\">%s</account></context>",
requestedAccount.getId()));
envelope = SoapProtocol.Soap12.soapEnvelope(soapBody, headerXml);
}
httpPost.setURI(URI.create(this.url));
httpPost.setEntity(new StringEntity(envelope.toString()));
return client.execute(httpPost);
}
return cookieStore;
}

private boolean isAdminAccount() {
Expand All @@ -113,7 +114,7 @@ public Request newRequest() {
* @throws Exception
*/
public HttpResponse executeSoap(Account account, Element soapBody) throws Exception {
return newRequest().setCaller(account).setSoapBody(soapBody).execute();
return newRequest().setCaller(account).setSoapBody(soapBody).execute();
}

/**
Expand All @@ -125,6 +126,4 @@ public HttpResponse executeSoap(Account account, Element soapBody) throws Except
public HttpResponse executeSoap(Account account, Object soapBodyPOJO) throws Exception {
return executeSoap(account, JaxbUtil.jaxbToElement(soapBodyPOJO));
}


}
85 changes: 85 additions & 0 deletions store/src/test/java/com/zimbra/cs/service/account/GetInfoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.zimbra.cs.service.account;

import static org.junit.jupiter.api.Assertions.*;

import com.zextras.mailbox.soap.SoapTestSuite;
import com.zextras.mailbox.util.MailboxTestUtil;
import com.zimbra.common.account.ZAttrProvisioning;
import com.zimbra.cs.account.Account;
import com.zimbra.cs.account.Provisioning;
import com.zimbra.soap.account.message.GetInfoRequest;
import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@Tag("api")
class GetInfoTest extends SoapTestSuite {

private static MailboxTestUtil.AccountCreator.Factory accountCreatorFactory;
private static Provisioning provisioning;
private Account account;

@BeforeAll
static void init() throws Exception {
provisioning = Provisioning.getInstance();
accountCreatorFactory = new MailboxTestUtil.AccountCreator.Factory(provisioning);
}

@BeforeEach
void setUp() throws Exception {
account = accountCreatorFactory.get().create();
}

@Test
void getAllSections() throws Exception {
final var request = new GetInfoRequest();

final var response = getSoapClient().executeSoap(account, request);

assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
}

@ParameterizedTest
@ValueSource(
strings = {
"mbox",
"prefs",
"attrs",
"zimlets",
"props",
"idents",
"sigs",
"dsrcs",
"children"
})
void getSpecificSection(String section) throws Exception {
final var request = new GetInfoRequest().addSection(section);

final var response = getSoapClient().executeSoap(account, request);

assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
}

@Test
void attributesSectionProvidesAmavisLists() throws Exception {
final var account =
accountCreatorFactory
.get()
.withAttribute(ZAttrProvisioning.A_amavisWhitelistSender, "foo1@bar.com")
.withAttribute(ZAttrProvisioning.A_amavisBlacklistSender, "foo2@bar.com")
.create();
final var request = new GetInfoRequest().addSection("attrs");

final var response = getSoapClient().executeSoap(account, request);

assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
final var body = EntityUtils.toString(response.getEntity());
assertTrue(body.contains("amavisWhitelistSender"));
assertTrue(body.contains("amavisBlacklistSender"));
}
}

0 comments on commit 5f75ce3

Please sign in to comment.