Search in sources :

Example 1 with CacheKey

use of password.pwm.svc.cache.CacheKey in project pwm by pwm-project.

the class LdapProfile method readCanonicalDN.

public String readCanonicalDN(final PwmApplication pwmApplication, final String dnValue) throws PwmUnrecoverableException {
    {
        final boolean doCanonicalDnResolve = Boolean.parseBoolean(pwmApplication.getConfig().readAppProperty(AppProperty.LDAP_RESOLVE_CANONICAL_DN));
        if (!doCanonicalDnResolve) {
            return dnValue;
        }
    }
    final boolean enableCanonicalCache = Boolean.parseBoolean(pwmApplication.getConfig().readAppProperty(AppProperty.LDAP_CACHE_CANONICAL_ENABLE));
    String canonicalValue = null;
    final CacheKey cacheKey = CacheKey.makeCacheKey(LdapPermissionTester.class, null, "canonicalDN-" + this.getIdentifier() + "-" + dnValue);
    if (enableCanonicalCache) {
        final String cachedDN = pwmApplication.getCacheService().get(cacheKey);
        if (cachedDN != null) {
            canonicalValue = cachedDN;
        }
    }
    if (canonicalValue == null) {
        try {
            final ChaiProvider chaiProvider = this.getProxyChaiProvider(pwmApplication);
            final ChaiEntry chaiEntry = chaiProvider.getEntryFactory().newChaiEntry(dnValue);
            canonicalValue = chaiEntry.readCanonicalDN();
            if (enableCanonicalCache) {
                final long cacheSeconds = Long.parseLong(pwmApplication.getConfig().readAppProperty(AppProperty.LDAP_CACHE_CANONICAL_SECONDS));
                final CachePolicy cachePolicy = CachePolicy.makePolicyWithExpiration(new TimeDuration(cacheSeconds, TimeUnit.SECONDS));
                pwmApplication.getCacheService().put(cacheKey, cachePolicy, canonicalValue);
            }
            LOGGER.trace("read and cached canonical ldap DN value for input '" + dnValue + "' as '" + canonicalValue + "'");
        } catch (ChaiUnavailableException | ChaiOperationException e) {
            LOGGER.error("error while reading canonicalDN for dn value '" + dnValue + "', error: " + e.getMessage());
            return dnValue;
        }
    }
    return canonicalValue;
}
Also used : ChaiUnavailableException(com.novell.ldapchai.exception.ChaiUnavailableException) CachePolicy(password.pwm.svc.cache.CachePolicy) ChaiProvider(com.novell.ldapchai.provider.ChaiProvider) ChaiEntry(com.novell.ldapchai.ChaiEntry) TimeDuration(password.pwm.util.java.TimeDuration) ChaiOperationException(com.novell.ldapchai.exception.ChaiOperationException) CacheKey(password.pwm.svc.cache.CacheKey)

Example 2 with CacheKey

use of password.pwm.svc.cache.CacheKey in project pwm by pwm-project.

the class LdapOperationsHelper method readLdapGuidValue.

public static String readLdapGuidValue(final PwmApplication pwmApplication, final SessionLabel sessionLabel, final UserIdentity userIdentity, final boolean throwExceptionOnError) throws ChaiUnavailableException, PwmUnrecoverableException {
    final boolean enableCache = Boolean.parseBoolean(pwmApplication.getConfig().readAppProperty(AppProperty.LDAP_CACHE_USER_GUID_ENABLE));
    final CacheKey cacheKey = CacheKey.makeCacheKey(LdapOperationsHelper.class, null, "guidValue-" + userIdentity.toDelimitedKey());
    if (enableCache) {
        final String cachedValue = pwmApplication.getCacheService().get(cacheKey);
        if (cachedValue != null) {
            return NULL_CACHE_GUID.equals(cachedValue) ? null : cachedValue;
        }
    }
    final String existingValue = GUIDHelper.readExistingGuidValue(pwmApplication, sessionLabel, userIdentity, throwExceptionOnError);
    final LdapProfile ldapProfile = pwmApplication.getConfig().getLdapProfiles().get(userIdentity.getLdapProfileID());
    final String guidAttributeName = ldapProfile.readSettingAsString(PwmSetting.LDAP_GUID_ATTRIBUTE);
    if (StringUtil.isEmpty(existingValue)) {
        if (!"DN".equalsIgnoreCase(guidAttributeName) && !"VENDORGUID".equalsIgnoreCase(guidAttributeName)) {
            if (ldapProfile.readSettingAsBoolean(PwmSetting.LDAP_GUID_AUTO_ADD)) {
                LOGGER.trace("assigning new GUID to user " + userIdentity);
                return GUIDHelper.assignGuidToUser(pwmApplication, sessionLabel, userIdentity, guidAttributeName);
            }
        }
        final String errorMsg = "unable to resolve GUID value for user " + userIdentity.toString();
        GUIDHelper.processError(errorMsg, throwExceptionOnError);
    }
    if (enableCache) {
        final long cacheSeconds = Long.parseLong(pwmApplication.getConfig().readAppProperty(AppProperty.LDAP_CACHE_USER_GUID_SECONDS));
        final CachePolicy cachePolicy = CachePolicy.makePolicyWithExpiration(new TimeDuration(cacheSeconds, TimeUnit.SECONDS));
        final String cacheValue = existingValue == null ? NULL_CACHE_GUID : existingValue;
        pwmApplication.getCacheService().put(cacheKey, cachePolicy, cacheValue);
    }
    return existingValue;
}
Also used : CachePolicy(password.pwm.svc.cache.CachePolicy) TimeDuration(password.pwm.util.java.TimeDuration) LdapProfile(password.pwm.config.profile.LdapProfile) CacheKey(password.pwm.svc.cache.CacheKey)

Example 3 with CacheKey

use of password.pwm.svc.cache.CacheKey in project pwm by pwm-project.

the class UserIdentity method toObfuscatedKey.

public String toObfuscatedKey(final PwmApplication pwmApplication) throws PwmUnrecoverableException {
    // use local cache first.
    if (!StringUtil.isEmpty(obfuscatedValue)) {
        return obfuscatedValue;
    }
    // check app cache.  This is used primarily so that keys are static over some meaningful lifetime, allowing browser caching based on keys.
    final CacheService cacheService = pwmApplication.getCacheService();
    final CacheKey cacheKey = CacheKey.makeCacheKey(this.getClass(), null, "userKey" + "|" + this.toDelimitedKey());
    final String cachedValue = cacheService.get(cacheKey);
    if (!StringUtil.isEmpty(cachedValue)) {
        obfuscatedValue = cachedValue;
        return cachedValue;
    }
    // generate key
    try {
        final String jsonValue = JsonUtil.serialize(this);
        final String localValue = CRYPO_HEADER + pwmApplication.getSecureService().encryptToString(jsonValue);
        this.obfuscatedValue = localValue;
        cacheService.put(cacheKey, CachePolicy.makePolicyWithExpiration(TimeDuration.DAY), localValue);
        return localValue;
    } catch (Exception e) {
        throw new PwmUnrecoverableException(new ErrorInformation(PwmError.ERROR_UNKNOWN, "unexpected error making obfuscated user key: " + e.getMessage()));
    }
}
Also used : ErrorInformation(password.pwm.error.ErrorInformation) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) CacheKey(password.pwm.svc.cache.CacheKey) ChaiException(com.novell.ldapchai.exception.ChaiException) PwmUnrecoverableException(password.pwm.error.PwmUnrecoverableException) CacheService(password.pwm.svc.cache.CacheService)

Example 4 with CacheKey

use of password.pwm.svc.cache.CacheKey in project pwm by pwm-project.

the class PeopleSearchDataReader method makeOrgChartData.

OrgChartDataBean makeOrgChartData(final UserIdentity userIdentity, final boolean noChildren) throws PwmUnrecoverableException {
    final Instant startTime = Instant.now();
    final CacheKey cacheKey = makeCacheKey(OrgChartDataBean.class.getSimpleName(), userIdentity.toDelimitedKey() + "|" + noChildren);
    {
        // if value is cached then return;
        final String cachedOutput = pwmRequest.getPwmApplication().getCacheService().get(cacheKey);
        if (cachedOutput != null) {
            StatisticsManager.incrementStat(pwmRequest, Statistic.PEOPLESEARCH_CACHE_HITS);
            LOGGER.trace(pwmRequest, "completed makeOrgChartData of " + userIdentity.toDisplayString() + " from cache");
            return JsonUtil.deserialize(cachedOutput, OrgChartDataBean.class);
        } else {
            StatisticsManager.incrementStat(pwmRequest, Statistic.PEOPLESEARCH_CACHE_MISSES);
        }
    }
    final OrgChartDataBean orgChartData = new OrgChartDataBean();
    // make self reference
    orgChartData.setSelf(makeOrgChartReferenceForIdentity(userIdentity));
    {
        // make parent reference
        final List<UserIdentity> parentIdentities = readUserDNAttributeValues(userIdentity, peopleSearchConfiguration.getOrgChartParentAttr());
        if (parentIdentities != null && !parentIdentities.isEmpty()) {
            final UserIdentity parentIdentity = parentIdentities.iterator().next();
            orgChartData.setParent(makeOrgChartReferenceForIdentity(parentIdentity));
        }
    }
    int childCount = 0;
    if (!noChildren) {
        // make children reference
        final Map<String, OrgChartReferenceBean> sortedChildren = new TreeMap<>();
        final List<UserIdentity> childIdentities = readUserDNAttributeValues(userIdentity, peopleSearchConfiguration.getOrgChartChildAttr());
        for (final UserIdentity childIdentity : childIdentities) {
            final OrgChartReferenceBean childReference = makeOrgChartReferenceForIdentity(childIdentity);
            if (childReference != null) {
                if (childReference.getDisplayNames() != null && !childReference.getDisplayNames().isEmpty()) {
                    final String firstDisplayName = childReference.getDisplayNames().iterator().next();
                    sortedChildren.put(firstDisplayName, childReference);
                } else {
                    sortedChildren.put(String.valueOf(childCount), childReference);
                }
                childCount++;
            }
        }
        orgChartData.setChildren(Collections.unmodifiableList(new ArrayList<>(sortedChildren.values())));
    }
    if (!StringUtil.isEmpty(peopleSearchConfiguration.getOrgChartAssistantAttr())) {
        final List<UserIdentity> assistantIdentities = readUserDNAttributeValues(userIdentity, peopleSearchConfiguration.getOrgChartAssistantAttr());
        if (assistantIdentities != null && !assistantIdentities.isEmpty()) {
            final UserIdentity assistantIdentity = assistantIdentities.iterator().next();
            final OrgChartReferenceBean assistantReference = makeOrgChartReferenceForIdentity(assistantIdentity);
            if (assistantReference != null) {
                orgChartData.setAssistant(assistantReference);
            }
        }
    }
    final TimeDuration totalTime = TimeDuration.fromCurrent(startTime);
    storeDataInCache(pwmRequest.getPwmApplication(), cacheKey, orgChartData);
    LOGGER.trace(pwmRequest, "completed makeOrgChartData in " + totalTime.asCompactString() + " with " + childCount + " children");
    return orgChartData;
}
Also used : Instant(java.time.Instant) UserIdentity(password.pwm.bean.UserIdentity) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) ArrayList(java.util.ArrayList) List(java.util.List) TimeDuration(password.pwm.util.java.TimeDuration) CacheKey(password.pwm.svc.cache.CacheKey)

Example 5 with CacheKey

use of password.pwm.svc.cache.CacheKey in project pwm by pwm-project.

the class PeopleSearchDataReader method makeUserDetailRequest.

UserDetailBean makeUserDetailRequest(final String userKey) throws PwmUnrecoverableException, PwmOperationalException, ChaiUnavailableException {
    final Instant startTime = Instant.now();
    final UserIdentity userIdentity = UserIdentity.fromKey(userKey, pwmRequest.getPwmApplication());
    final CacheKey cacheKey = makeCacheKey(UserDetailBean.class.getSimpleName(), userIdentity.toDelimitedKey());
    {
        final String cachedOutput = pwmRequest.getPwmApplication().getCacheService().get(cacheKey);
        if (cachedOutput != null) {
            StatisticsManager.incrementStat(pwmRequest, Statistic.PEOPLESEARCH_CACHE_HITS);
            return JsonUtil.deserialize(cachedOutput, UserDetailBean.class);
        } else {
            StatisticsManager.incrementStat(pwmRequest, Statistic.PEOPLESEARCH_CACHE_MISSES);
        }
    }
    try {
        checkIfUserIdentityViewable(userIdentity);
    } catch (PwmOperationalException e) {
        LOGGER.error(pwmRequest.getPwmSession(), "error during detail results request while checking if requested userIdentity is within search scope: " + e.getMessage());
        throw e;
    }
    final UserSearchResults detailResults = doDetailLookup(userIdentity);
    final Map<String, String> searchResults = detailResults.getResults().get(userIdentity);
    final UserDetailBean userDetailBean = new UserDetailBean();
    userDetailBean.setUserKey(userKey);
    final List<FormConfiguration> detailFormConfig = pwmRequest.getConfig().readSettingAsForm(PwmSetting.PEOPLE_SEARCH_DETAIL_FORM);
    final Map<String, AttributeDetailBean> attributeBeans = convertResultMapToBeans(pwmRequest, userIdentity, detailFormConfig, searchResults);
    userDetailBean.setDetail(attributeBeans);
    final String photoURL = figurePhotoURL(pwmRequest, userIdentity);
    if (photoURL != null) {
        userDetailBean.setPhotoURL(photoURL);
    }
    final List<String> displayName = figureDisplaynames(pwmRequest, userIdentity);
    if (displayName != null) {
        userDetailBean.setDisplayNames(displayName);
    }
    userDetailBean.setLinks(makeUserDetailLinks(userIdentity));
    LOGGER.trace(pwmRequest.getPwmSession(), "finished building userDetail result in " + TimeDuration.fromCurrent(startTime).asCompactString());
    storeDataInCache(pwmRequest.getPwmApplication(), cacheKey, userDetailBean);
    return userDetailBean;
}
Also used : Instant(java.time.Instant) UserIdentity(password.pwm.bean.UserIdentity) UserSearchResults(password.pwm.ldap.search.UserSearchResults) PwmOperationalException(password.pwm.error.PwmOperationalException) FormConfiguration(password.pwm.config.value.data.FormConfiguration) CacheKey(password.pwm.svc.cache.CacheKey)

Aggregations

CacheKey (password.pwm.svc.cache.CacheKey)8 CachePolicy (password.pwm.svc.cache.CachePolicy)4 UserIdentity (password.pwm.bean.UserIdentity)3 ErrorInformation (password.pwm.error.ErrorInformation)3 CacheService (password.pwm.svc.cache.CacheService)3 TimeDuration (password.pwm.util.java.TimeDuration)3 ChaiOperationException (com.novell.ldapchai.exception.ChaiOperationException)2 ChaiUnavailableException (com.novell.ldapchai.exception.ChaiUnavailableException)2 Instant (java.time.Instant)2 FormConfiguration (password.pwm.config.value.data.FormConfiguration)2 PwmDataValidationException (password.pwm.error.PwmDataValidationException)2 PwmOperationalException (password.pwm.error.PwmOperationalException)2 PwmUnrecoverableException (password.pwm.error.PwmUnrecoverableException)2 ChaiEntry (com.novell.ldapchai.ChaiEntry)1 ChaiUser (com.novell.ldapchai.ChaiUser)1 ChaiException (com.novell.ldapchai.exception.ChaiException)1 ChaiProvider (com.novell.ldapchai.provider.ChaiProvider)1 SearchHelper (com.novell.ldapchai.util.SearchHelper)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1