Search in sources :

Example 1 with DataSource

use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.

the class LdapProvisioning method createDataSource.

private DataSource createDataSource(Account account, DataSourceType dsType, String dsName, Map<String, Object> dataSourceAttrs, boolean passwdAlreadyEncrypted, boolean restoring) throws ServiceException {
    removeAttrIgnoreCase("objectclass", dataSourceAttrs);
    LdapEntry ldapEntry = (LdapEntry) (account instanceof LdapEntry ? account : getAccountById(account.getId()));
    if (ldapEntry == null) {
        throw AccountServiceException.NO_SUCH_ACCOUNT(account.getName());
    }
    List<DataSource> existing = getAllDataSources(account);
    if (existing.size() >= account.getLongAttr(A_zimbraDataSourceMaxNumEntries, 20)) {
        throw AccountServiceException.TOO_MANY_DATA_SOURCES();
    }
    // must be the same
    dataSourceAttrs.put(A_zimbraDataSourceName, dsName);
    dataSourceAttrs.put(Provisioning.A_zimbraDataSourceType, dsType.toString());
    account.setCachedData(DATA_SOURCE_LIST_CACHE_KEY, null);
    boolean checkImmutable = !restoring;
    CallbackContext callbackContext = new CallbackContext(CallbackContext.Op.CREATE);
    AttributeManager.getInstance().preModify(dataSourceAttrs, null, callbackContext, checkImmutable);
    ZLdapContext zlc = null;
    try {
        zlc = LdapClient.getContext(LdapServerType.MASTER, LdapUsage.CREATE_DATASOURCE);
        String dn = getDataSourceDn(ldapEntry, dsName);
        ZMutableEntry entry = LdapClient.createMutableEntry();
        entry.setDN(dn);
        entry.mapToAttrs(dataSourceAttrs);
        entry.setAttr(A_objectClass, "zimbraDataSource");
        String extraOc = LdapDataSource.getObjectClass(dsType);
        if (extraOc != null) {
            entry.addAttr(A_objectClass, Sets.newHashSet(extraOc));
        }
        String dsId = entry.getAttrString(A_zimbraDataSourceId);
        if (dsId == null) {
            dsId = LdapUtil.generateUUID();
            entry.setAttr(A_zimbraDataSourceId, dsId);
        }
        String password = entry.getAttrString(A_zimbraDataSourcePassword);
        if (password != null) {
            String encrypted = passwdAlreadyEncrypted ? password : DataSource.encryptData(dsId, password);
            entry.setAttr(A_zimbraDataSourcePassword, encrypted);
        }
        String oauthToken = entry.getAttrString(A_zimbraDataSourceOAuthToken);
        if (oauthToken != null) {
            String encrypted = passwdAlreadyEncrypted ? oauthToken : DataSource.encryptData(dsId, oauthToken);
            entry.setAttr(A_zimbraDataSourceOAuthToken, encrypted);
        }
        String clientSecret = entry.getAttrString(A_zimbraDataSourceOAuthClientSecret);
        if (clientSecret != null) {
            String encrypted = passwdAlreadyEncrypted ? clientSecret : DataSource.encryptData(dsId, clientSecret);
            entry.setAttr(A_zimbraDataSourceOAuthClientSecret, encrypted);
        }
        String smtpPassword = entry.getAttrString(A_zimbraDataSourceSmtpAuthPassword);
        if (smtpPassword != null) {
            String encrypted = passwdAlreadyEncrypted ? smtpPassword : DataSource.encryptData(dsId, smtpPassword);
            entry.setAttr(A_zimbraDataSourceSmtpAuthPassword, encrypted);
        }
        entry.setAttr(Provisioning.A_zimbraCreateTimestamp, LdapDateUtil.toGeneralizedTime(new Date()));
        zlc.createEntry(entry);
        DataSource ds = getDataSourceById(ldapEntry, dsId, zlc);
        AttributeManager.getInstance().postModify(dataSourceAttrs, ds, callbackContext);
        return ds;
    } catch (LdapEntryAlreadyExistException nabe) {
        throw AccountServiceException.DATA_SOURCE_EXISTS(dsName);
    } catch (LdapException e) {
        throw e;
    } catch (AccountServiceException e) {
        throw e;
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to create data source: " + dsName, e);
    } finally {
        LdapClient.closeContext(zlc);
    }
}
Also used : ZMutableEntry(com.zimbra.cs.ldap.ZMutableEntry) LdapEntryAlreadyExistException(com.zimbra.cs.ldap.LdapException.LdapEntryAlreadyExistException) ZLdapContext(com.zimbra.cs.ldap.ZLdapContext) LdapEntry(com.zimbra.cs.account.ldap.entry.LdapEntry) Date(java.util.Date) DataSource(com.zimbra.cs.account.DataSource) LdapDataSource(com.zimbra.cs.account.ldap.entry.LdapDataSource) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) CallbackContext(com.zimbra.cs.account.callback.CallbackContext) LdapException(com.zimbra.cs.ldap.LdapException)

Example 2 with DataSource

use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.

the class LdapProvisioning method getDataSourcesByQuery.

private List<DataSource> getDataSourcesByQuery(LdapEntry entry, ZLdapFilter filter, ZLdapContext initZlc) throws ServiceException {
    List<DataSource> result = new ArrayList<DataSource>();
    try {
        String base = entry.getDN();
        ZSearchResultEnumeration ne = helper.searchDir(base, filter, ZSearchControls.SEARCH_CTLS_SUBTREE(), initZlc, LdapServerType.REPLICA);
        while (ne.hasMore()) {
            ZSearchResultEntry sr = ne.next();
            result.add(new LdapDataSource((Account) entry, sr.getDN(), sr.getAttributes(), this));
        }
        ne.close();
    } catch (ServiceException e) {
        throw ServiceException.FAILURE("unable to lookup data source via query: " + filter.toFilterString() + " message: " + e.getMessage(), e);
    }
    return result;
}
Also used : Account(com.zimbra.cs.account.Account) GuestAccount(com.zimbra.cs.account.GuestAccount) LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) LdapDataSource(com.zimbra.cs.account.ldap.entry.LdapDataSource) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) ZSearchResultEnumeration(com.zimbra.cs.ldap.ZSearchResultEnumeration) ArrayList(java.util.ArrayList) DataSource(com.zimbra.cs.account.DataSource) LdapDataSource(com.zimbra.cs.account.ldap.entry.LdapDataSource) ZSearchResultEntry(com.zimbra.cs.ldap.ZSearchResultEntry)

Example 3 with DataSource

use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.

the class LdapProvisioning method autoProvZMGProxyAccount.

@Override
public Pair<Account, Boolean> autoProvZMGProxyAccount(String emailAddr, String password) throws ServiceException {
    Account acct = getAccountByForeignPrincipal(ZMG_PROXY_ACCT_FOREIGN_PRINCIPAL_PREFIX + emailAddr);
    if (acct != null) {
        return new Pair<>(acct, false);
    }
    String domainId = getConfig().getMobileGatewayDefaultProxyAccountDomainId();
    if (domainId == null) {
        return new Pair<>(null, false);
    }
    Domain domain = getDomainById(domainId);
    if (domain == null) {
        ZimbraLog.account.error("Domain corresponding to" + A_zimbraMobileGatewayDefaultProxyAccountDomainId + "not found ");
        throw ServiceException.FAILURE("Missing server configuration", null);
    }
    String testDsId = "TestId";
    Map<String, Object> testAttrs = getZMGProxyDataSourceAttrs(emailAddr, password, true, testDsId);
    DataSource dsToTest = new DataSource(null, DataSourceType.imap, "Test", testDsId, testAttrs, this);
    try {
        DataSourceManager.test(dsToTest);
    } catch (ServiceException e) {
        ZimbraLog.account.debug("ZMG Proxy account auto provisioning failed", e);
        throw AuthFailedServiceException.AUTH_FAILED(emailAddr, SystemUtil.getInnermostException(e).getMessage(), e);
    }
    Map<String, Object> acctAttrs = new HashMap<String, Object>();
    acctAttrs.put(Provisioning.A_zimbraIsMobileGatewayProxyAccount, ProvisioningConstants.TRUE);
    acctAttrs.put(Provisioning.A_zimbraForeignPrincipal, ZMG_PROXY_ACCT_FOREIGN_PRINCIPAL_PREFIX + emailAddr);
    acctAttrs.put(Provisioning.A_zimbraHideInGal, ProvisioningConstants.TRUE);
    acctAttrs.put(Provisioning.A_zimbraMailStatus, Provisioning.MailStatus.disabled.toString());
    acctAttrs.put(Provisioning.A_zimbraMailHost, getLocalServer().getServiceHostname());
    acct = createDummyAccount(acctAttrs, password, domain);
    DataSource ds;
    try {
        Map<String, Object> dsAttrs = getZMGProxyDataSourceAttrs(emailAddr, password, false, null);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
        dsAttrs.put(Provisioning.A_zimbraDataSourceFolderId, Integer.toString(mbox.createFolder(null, "/" + emailAddr, new Folder.FolderOptions()).getId()));
        ds = createDataSource(acct, DataSourceType.imap, emailAddr, dsAttrs);
    } catch (ServiceException e) {
        try {
            deleteAccount(acct.getId());
        } catch (ServiceException e1) {
            if (!AccountServiceException.NO_SUCH_ACCOUNT.equals(e1.getCode())) {
                throw e1;
            }
        }
        throw e;
    }
    DataSourceManager.asyncImportData(ds);
    return new Pair<>(acct, true);
}
Also used : Account(com.zimbra.cs.account.Account) GuestAccount(com.zimbra.cs.account.GuestAccount) LdapAccount(com.zimbra.cs.account.ldap.entry.LdapAccount) AccountServiceException(com.zimbra.cs.account.AccountServiceException) AuthFailedServiceException(com.zimbra.cs.account.AccountServiceException.AuthFailedServiceException) ServiceException(com.zimbra.common.service.ServiceException) Mailbox(com.zimbra.cs.mailbox.Mailbox) HashMap(java.util.HashMap) LdapDomain(com.zimbra.cs.account.ldap.entry.LdapDomain) Domain(com.zimbra.cs.account.Domain) Pair(com.zimbra.common.util.Pair) DataSource(com.zimbra.cs.account.DataSource) LdapDataSource(com.zimbra.cs.account.ldap.entry.LdapDataSource)

Example 4 with DataSource

use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.

the class DataSourcePurge method dataSourcePurgeQueueKey.

private String dataSourcePurgeQueueKey(List<DataSource> dataSources) {
    String accountId = mbox.getAccountId();
    if (dataSources.size() == 1) {
        return String.format("%s:%s", accountId, dataSources.get(0).getId());
    }
    List<String> dsIds = new ArrayList<String>();
    for (DataSource ds : dataSources) {
        dsIds.add(ds.getId());
    }
    Collections.sort(dsIds);
    return String.format("%s:%s", accountId, Joiner.on(",").join(dsIds));
}
Also used : ArrayList(java.util.ArrayList) DataSource(com.zimbra.cs.account.DataSource) DbDataSource(com.zimbra.cs.db.DbDataSource)

Example 5 with DataSource

use of com.zimbra.cs.account.DataSource in project zm-mailbox by Zimbra.

the class DataSourceManagerTest method testGetDataImportWithDefaultClass.

@Test
public void testGetDataImportWithDefaultClass() throws ServiceException {
    Map<String, Object> testAttrs = new HashMap<String, Object>();
    testAttrs.put(Provisioning.A_zimbraDataSourceDomain, "zimbra.com");
    testAttrs.put(Provisioning.A_zimbraDataSourcePort, "1234");
    testAttrs.put(Provisioning.A_zimbraDataSourceHost, "localhost");
    testAttrs.put(Provisioning.A_zimbraDataSourceUsername, "test");
    testAttrs.put(Provisioning.A_zimbraDataSourcePassword, "test");
    DataSource ds = new DataSource(testAccount, DataSourceType.pop3, POP3_DS_NAME, POP3_DS_ID, testAttrs, null);
    DataImport di = DataSourceManager.getInstance().getDataImport(ds);
    assertNotNull("DataImport should not be NULL", di);
    assertTrue("DataImport for 'pop3' should be Pop3Sync", di instanceof Pop3Sync);
    ds = new DataSource(testAccount, DataSourceType.imap, IMAP_DS_NAME, IMAP_DS_ID, testAttrs, null);
    di = DataSourceManager.getInstance().getDataImport(ds);
    assertNotNull("DataImport should not be NULL", di);
    assertTrue("DataImport for 'imap' should be ImapSync", di instanceof ImapSync);
    ds = new DataSource(testAccount, DataSourceType.caldav, CALDAV_DS_NAME, CALDAV_DS_ID, testAttrs, null);
    di = DataSourceManager.getInstance().getDataImport(ds);
    assertNotNull("DataImport should not be NULL", di);
    assertTrue("DataImport for 'caldav' should be CalDavDataImport", di instanceof CalDavDataImport);
    ds = new DataSource(testAccount, DataSourceType.rss, RSS_DS_NAME, RSS_DS_ID, testAttrs, null);
    di = DataSourceManager.getInstance().getDataImport(ds);
    assertNotNull("DataImport should not be NULL", di);
    assertTrue("DataImport for 'rss' should be RssImport", di instanceof RssImport);
    ds = new DataSource(testAccount, DataSourceType.cal, CAL_DS_NAME, CAL_DS_ID, testAttrs, null);
    di = DataSourceManager.getInstance().getDataImport(ds);
    assertNotNull("DataImport should not be NULL", di);
    assertTrue("DataImport for 'cal' should be RssImport", di instanceof RssImport);
    ds = new DataSource(testAccount, DataSourceType.gal, GAL_DS_NAME, GAL_DS_ID, testAttrs, null);
    di = DataSourceManager.getInstance().getDataImport(ds);
    assertNotNull("DataImport should not be NULL", di);
    assertTrue("DataImport for 'gal' should be GalImport", di instanceof GalImport);
}
Also used : GalImport(com.zimbra.cs.gal.GalImport) DataImport(com.zimbra.cs.account.DataSource.DataImport) HashMap(java.util.HashMap) ImapSync(com.zimbra.cs.datasource.imap.ImapSync) DataSource(com.zimbra.cs.account.DataSource) Test(org.junit.Test)

Aggregations

DataSource (com.zimbra.cs.account.DataSource)71 Account (com.zimbra.cs.account.Account)29 HashMap (java.util.HashMap)21 Provisioning (com.zimbra.cs.account.Provisioning)18 DbDataSource (com.zimbra.cs.db.DbDataSource)18 ServiceException (com.zimbra.common.service.ServiceException)15 Element (com.zimbra.common.soap.Element)14 Mailbox (com.zimbra.cs.mailbox.Mailbox)11 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)11 ArrayList (java.util.ArrayList)11 ZDataSource (com.zimbra.client.ZDataSource)8 AccountServiceException (com.zimbra.cs.account.AccountServiceException)7 Folder (com.zimbra.cs.mailbox.Folder)7 DataSourceType (com.zimbra.soap.admin.type.DataSourceType)7 HashSet (java.util.HashSet)7 Test (org.junit.Test)7 DataSourceItem (com.zimbra.cs.db.DbDataSource.DataSourceItem)6 ZMailbox (com.zimbra.client.ZMailbox)5 LdapDataSource (com.zimbra.cs.account.ldap.entry.LdapDataSource)5 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)5