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);
}
}
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;
}
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);
}
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));
}
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);
}
Aggregations