Search in sources :

Example 1 with ZimbraMemcachedClient

use of com.zimbra.common.util.memcached.ZimbraMemcachedClient in project zm-mailbox by Zimbra.

the class MemcachedImapCacheTest method testInvalidObject.

@Test
public void testInvalidObject() {
    try {
        PowerMockito.mockStatic(MemcachedConnector.class);
        ZimbraMemcachedClient memcachedClient = new MockZimbraMemcachedClient();
        PowerMockito.when(MemcachedConnector.getClient()).thenReturn(memcachedClient);
        ImapFolder folder = PowerMockito.mock(ImapFolder.class);
        MemcachedImapCache imapCache = new MemcachedImapCache();
        imapCache.put("trash", folder);
        ImapFolder folderDeserz = imapCache.get("trash");
        assertNull(folderDeserz);
    } catch (Exception e) {
        fail("Exception should not be thrown");
    }
}
Also used : ZimbraMemcachedClient(com.zimbra.common.util.memcached.ZimbraMemcachedClient) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with ZimbraMemcachedClient

use of com.zimbra.common.util.memcached.ZimbraMemcachedClient in project zm-mailbox by Zimbra.

the class ProxyPurgeUtil method purgeAccounts.

/**
 * Purges or, prints all the routes for the accounts supplied.
 * @param servers list of memcached servers supplied, if null the function gets all the
 *                memcached servers from provisioning
 * @param accounts list of accounts (qualified or, unqualified)
 * @param purge true for the account routes purging, false for printing the routes
 * @param outputformat format of the output in case of printing
 * @throws ServiceException
 */
public static void purgeAccounts(List<String> servers, List<String> accounts, boolean purge, String outputformat) throws ServiceException {
    Provisioning prov = Provisioning.getInstance();
    // Some sanity checks.
    if (accounts == null || accounts.isEmpty()) {
        System.err.println("No account supplied");
        System.exit(1);
    }
    if (!purge) {
        // the outputformat must be supplied.
        if (outputformat == null || outputformat.length() == 0) {
            System.err.println("outputformat must be supplied for info");
            System.exit(1);
        }
    }
    if (servers == null) {
        List<Server> memcachedServers = prov.getAllServers(Provisioning.SERVICE_MEMCACHED);
        servers = new ArrayList<String>();
        for (Iterator<Server> it = memcachedServers.iterator(); it.hasNext(); ) {
            Server s = it.next();
            String serverName = s.getAttr(Provisioning.A_zimbraServiceHostname, "localhost");
            String servicePort = s.getAttr(Provisioning.A_zimbraMemcachedBindPort, memcachedPort);
            servers.add(serverName + ":" + servicePort);
        }
    }
    // Connect to all memcached servers.
    int numServers = servers.size();
    ArrayList<ZimbraMemcachedClient> zmcs = new ArrayList<ZimbraMemcachedClient>();
    for (int i = 0; i < numServers; ++i) {
        ZimbraMemcachedClient zmc = new ZimbraMemcachedClient();
        zmc.connect(new String[] { servers.get(i) }, false, null, 0, 5000);
        zmcs.add(zmc);
    }
    for (String a : accounts) {
        // Bug 24463
        // The route keying in memcached is governed by the following rules:
        // 1. if login name is fully qualified, use that as the route key
        // 2. otherwise, if memcache_entry_allow_unqualified is true, then use the bare login as the route key
        // 3. else, append the IP address of the proxy interface to the login and use that as the route key
        // 4. for the login store all the user's alias, append the ip address of the proxy interface.
        // 
        // For accounts authenticating without domain, NGINX internally suffixes @domain
        // to the login name, by first looking up an existing domain by the IP address of
        // the proxy interface where the connection came in. If no such domain is found,
        // then NGINX falls back to the default domain name specified by the config
        // attribute zimbraDefaultDomainName.
        // The IP to domain mapping is done based on the zimbraVirtualIPAddress attribute
        // of the domain (The IP-to-domain mapping is a many-to-one relationship.)
        // 
        // For the zmproxypurge utility if the account supplied (-a option) is:
        // 1. For fully qualified account with @domain; it will find all the virtual IP
        // addresses for that domain and will delete all the entries on all memcached servers:
        // i) with the user@domain (case 1 as described above)
        // ii) with just the user (case 2 as described above)
        // iii) with all the virtual IP addresses configured for the domain
        // iv) find all the alias for the account and repeat (i) to (iii)
        // 2. For the account supplied with the IP address; the utility will only try to
        // purge the entries with the user@IP.
        // 3. If there is a single domain and the account supplied is not fully qualified;
        // the utility will append the default domain to that entry and will execute step 1.
        // (In this case the provisioning lookup will return the correct domain)
        ArrayList<String> routes = new ArrayList<String>();
        // Lookup the account; at this point we don't whether the user is fully qualified.
        Account account = prov.get(Key.AccountBy.name, a);
        if (account == null) {
            // In this case just purge the entries with the given account name as supplied.
            System.out.println("error looking up accout: " + a);
            routes.add("route:proto=http;user=" + a);
            routes.add("route:proto=imap;user=" + a);
            routes.add("route:proto=pop3;user=" + a);
            routes.add("route:proto=httpssl;user=" + a);
            routes.add("route:proto=imapssl;user=" + a);
            routes.add("route:proto=pop3ssl;user=" + a);
        } else {
            String uid = account.getUid();
            routes.add("route:proto=http;id=" + account.getId());
            routes.add("route:proto=http;user=" + uid);
            routes.add("route:proto=imap;user=" + uid);
            routes.add("route:proto=pop3;user=" + uid);
            routes.add("route:proto=httpssl;id=" + account.getId());
            routes.add("route:proto=httpssl;user=" + uid);
            routes.add("route:proto=imapssl;user=" + uid);
            routes.add("route:proto=pop3ssl;user=" + uid);
            routes.add("route:proto=httpssl;admin=1;id=" + account.getId());
            String domain = account.getDomainName();
            routes.add("route:proto=http;user=" + uid + "@" + domain);
            routes.add("route:proto=imap;user=" + uid + "@" + domain);
            routes.add("route:proto=pop3;user=" + uid + "@" + domain);
            routes.add("route:proto=httpssl;user=" + uid + "@" + domain);
            routes.add("route:proto=imapssl;user=" + uid + "@" + domain);
            routes.add("route:proto=pop3ssl;user=" + uid + "@" + domain);
            routes.add("alias:user=" + uid + ";ip=" + domain);
            Domain d = prov.get(Key.DomainBy.name, domain);
            String[] vips = d.getVirtualIPAddress();
            for (String vip : vips) {
                // for each virtual ip add the routes to the list.
                routes.add("route:proto=http;user=" + uid + "@" + vip);
                routes.add("route:proto=imap;user=" + uid + "@" + vip);
                routes.add("route:proto=pop3;user=" + uid + "@" + vip);
                routes.add("route:proto=httpssl;user=" + uid + "@" + vip);
                routes.add("route:proto=imapssl;user=" + uid + "@" + vip);
                routes.add("route:proto=pop3ssl;user=" + uid + "@" + vip);
                routes.add("alias:user=" + uid + ";ip=" + vip);
            }
            String[] vhostnames = d.getVirtualHostname();
            for (String vhost : vhostnames) {
                // for each virtual host name add the alias to the list
                routes.add("alias:user=" + uid + ";vhost=" + vhost);
            }
            String[] aliases = account.getMailAlias();
            List<String> uids = new ArrayList<String>();
            uids.add(uid);
            for (String alias : aliases) {
                if (alias.indexOf('@') != -1 && alias.substring(alias.indexOf('@') + 1).equals(domain)) {
                    uids.add(alias.substring(0, alias.indexOf('@')));
                }
            }
            // this logic works for for all cases account=addr@<alias domain> or alias-name@<alias domain>
            if (prov instanceof LdapProvisioning) {
                ZLdapContext ldpCtx = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.GET_DOMAIN);
                try {
                    List<String> aliasDomainIds = ((LdapProvisioning) prov).getEmptyAliasDomainIds(ldpCtx, d, false);
                    if (aliasDomainIds != null) {
                        for (String aliasDomainId : aliasDomainIds) {
                            String aliasDomain = prov.getDomainById(aliasDomainId).getDomainName();
                            for (String userName : uids) {
                                routes.add("route:proto=http;user=" + userName + "@" + aliasDomain);
                                routes.add("route:proto=imap;user=" + userName + "@" + aliasDomain);
                                routes.add("route:proto=pop3;user=" + userName + "@" + aliasDomain);
                                routes.add("route:proto=httpssl;user=" + userName + "@" + aliasDomain);
                                routes.add("route:proto=imapssl;user=" + userName + "@" + aliasDomain);
                                routes.add("route:proto=pop3ssl;user=" + userName + "@" + aliasDomain);
                                routes.add("alias:user=" + userName + ";ip=" + aliasDomain);
                            }
                        }
                    }
                } finally {
                    LdapClient.closeContext(ldpCtx);
                }
            }
            // bug:79940 says Active Sync routes are stored as http/https - alias@domain.com
            for (String alias : aliases) {
                routes.add("route:proto=http;user=" + alias);
                routes.add("route:proto=imap;user=" + alias);
                routes.add("route:proto=pop3;user=" + alias);
                routes.add("route:proto=httpssl;user=" + alias);
                routes.add("route:proto=imapssl;user=" + alias);
                routes.add("route:proto=pop3ssl;user=" + alias);
                if (alias.indexOf('@') != -1) {
                    alias = alias.substring(0, alias.indexOf('@'));
                }
                for (String vhost : vhostnames) {
                    // for each virtual host name add the alias to the alias user
                    routes.add("alias:user=" + alias + ";vhost=" + vhost);
                }
                for (String vip : vips) {
                    // for each virtual ip add the routes to the list.
                    routes.add("route:proto=http;user=" + alias + "@" + vip);
                    routes.add("route:proto=imap;user=" + alias + "@" + vip);
                    routes.add("route:proto=pop3;user=" + alias + "@" + vip);
                    routes.add("route:proto=httpssl;user=" + alias + "@" + vip);
                    routes.add("route:proto=imapssl;user=" + alias + "@" + vip);
                    routes.add("route:proto=pop3ssl;user=" + alias + "@" + vip);
                    routes.add("alias:user=" + alias + ";ip=" + vip);
                }
            }
        }
        for (int i = 0; i < numServers; ++i) {
            ZimbraMemcachedClient zmc = zmcs.get(i);
            for (String route : routes) {
                if (purge) {
                    // Note: there is no guarantee that all the routes will be present.
                    // We just try to purge all of them without waiting on ack.
                    System.out.println("Purging " + route + " on server " + servers.get(i));
                    zmc.remove(route, false);
                } else {
                    String output = String.format(outputformat, servers.get(i), route, zmc.get(route));
                    System.out.println(output);
                }
            }
        }
    }
    for (ZimbraMemcachedClient zmc : zmcs) {
        zmc.disconnect(ZimbraMemcachedClient.DEFAULT_TIMEOUT);
    }
}
Also used : Account(com.zimbra.cs.account.Account) Server(com.zimbra.cs.account.Server) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) ZimbraMemcachedClient(com.zimbra.common.util.memcached.ZimbraMemcachedClient) Provisioning(com.zimbra.cs.account.Provisioning) LdapProvisioning(com.zimbra.cs.account.ldap.LdapProvisioning) LdapProvisioning(com.zimbra.cs.account.ldap.LdapProvisioning) Domain(com.zimbra.cs.account.Domain)

Example 3 with ZimbraMemcachedClient

use of com.zimbra.common.util.memcached.ZimbraMemcachedClient in project zm-mailbox by Zimbra.

the class GetMemcachedClientConfig method handle.

@Override
public Element handle(Element request, Map<String, Object> context) throws ServiceException {
    ZimbraSoapContext zsc = getZimbraSoapContext(context);
    Element response = zsc.createElement(AdminConstants.GET_MEMCACHED_CLIENT_CONFIG_RESPONSE);
    ZimbraMemcachedClient zmcd = MemcachedConnector.getClient();
    if (zmcd != null) {
        response.addAttribute(AdminConstants.A_MEMCACHED_CLIENT_CONFIG_SERVER_LIST, zmcd.getServerList());
        response.addAttribute(AdminConstants.A_MEMCACHED_CLIENT_CONFIG_HASH_ALGORITHM, zmcd.getHashAlgorithm());
        response.addAttribute(AdminConstants.A_MEMCACHED_CLIENT_CONFIG_BINARY_PROTOCOL, zmcd.getBinaryProtocolEnabled());
        response.addAttribute(AdminConstants.A_MEMCACHED_CLIENT_CONFIG_DEFAULT_EXPIRY_SECONDS, zmcd.getDefaultExpirySeconds());
        response.addAttribute(AdminConstants.A_MEMCACHED_CLIENT_CONFIG_DEFAULT_TIMEOUT_MILLIS, zmcd.getDefaultTimeoutMillis());
    }
    return response;
}
Also used : ZimbraMemcachedClient(com.zimbra.common.util.memcached.ZimbraMemcachedClient) ZimbraSoapContext(com.zimbra.soap.ZimbraSoapContext) Element(com.zimbra.common.soap.Element)

Aggregations

ZimbraMemcachedClient (com.zimbra.common.util.memcached.ZimbraMemcachedClient)3 Element (com.zimbra.common.soap.Element)1 Account (com.zimbra.cs.account.Account)1 Domain (com.zimbra.cs.account.Domain)1 Provisioning (com.zimbra.cs.account.Provisioning)1 Server (com.zimbra.cs.account.Server)1 LdapProvisioning (com.zimbra.cs.account.ldap.LdapProvisioning)1 ZLdapContext (com.zimbra.cs.ldap.ZLdapContext)1 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1