Skip to content

Commit 0522baf

Browse files
committed
review: improv logging of cache invalidation
1 parent b1cfc49 commit 0522baf

12 files changed

+78
-49
lines changed

core/src/main/java/org/pentaho/platform/engine/security/authorization/PentahoAuthorizationRuleLevel.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.pentaho.platform.api.engine.security.authorization.IAuthorizationRule;
2323
import org.pentaho.platform.api.engine.security.authorization.decisions.IAuthorizationDecision;
2424
import org.pentaho.platform.engine.core.system.PentahoSystem;
25+
import org.pentaho.platform.engine.security.authorization.core.rules.AbstractAuthorizationRule;
26+
import org.pentaho.platform.engine.security.authorization.core.rules.AbstractCompositeAuthorizationRule;
2527
import org.pentaho.platform.engine.security.authorization.core.rules.AllAuthorizationRule;
2628
import org.pentaho.platform.engine.security.authorization.core.rules.AnyAuthorizationRule;
2729
import org.springframework.util.Assert;
@@ -33,7 +35,7 @@
3335
import java.util.function.Supplier;
3436
import java.util.stream.Collectors;
3537

36-
public class PentahoAuthorizationRuleLevel implements IAuthorizationRule<IAuthorizationRequest> {
38+
public class PentahoAuthorizationRuleLevel extends AbstractAuthorizationRule<IAuthorizationRequest> {
3739
private static final String RULE_LEVEL_ATTRIBUTE = "ruleLevel";
3840

3941
public enum RuleLevelType {
@@ -58,7 +60,7 @@ public enum RuleLevelType {
5860
private final Supplier<List<IPentahoObjectReference<IAuthorizationRule>>> authorizationRuleReferencesSupplier;
5961

6062
@NonNull
61-
private IAuthorizationRule<IAuthorizationRequest> delegateRule;
63+
private AbstractCompositeAuthorizationRule delegateRule;
6264

6365
public PentahoAuthorizationRuleLevel( @NonNull IPluginManager pluginManager,
6466
@NonNull RuleLevelType ruleLevelType,
@@ -119,12 +121,12 @@ private void updateDelegateRule() {
119121

120122
@VisibleForTesting
121123
@NonNull
122-
protected IAuthorizationRule<IAuthorizationRequest> getDelegateRule() {
124+
protected AbstractCompositeAuthorizationRule getDelegateRule() {
123125
return this.delegateRule;
124126
}
125127

126128
@NonNull
127-
private IAuthorizationRule<IAuthorizationRequest> buildDelegateRule() {
129+
private AbstractCompositeAuthorizationRule buildDelegateRule() {
128130
var rules = getLevelAuthorizationRules( levelRulePredicate );
129131
rules.addAll( postRules );
130132

@@ -174,12 +176,20 @@ protected static Predicate<IPentahoObjectReference<?>> buildLevelRulePredicate(
174176
// normalize null to ""
175177
final String normalizedRuleLevel = getDefaultValue( ruleLevel, "" );
176178

177-
return ruleRef -> {
178-
String value = getDefaultValue(
179-
ruleRef.getAttributes().get( RULE_LEVEL_ATTRIBUTE ),
180-
isDefaultRuleLevel ? normalizedRuleLevel : "" );
179+
return new Predicate<>() {
180+
@Override
181+
public boolean test( IPentahoObjectReference<?> ruleRef ) {
182+
String value = getDefaultValue(
183+
ruleRef.getAttributes().get( RULE_LEVEL_ATTRIBUTE ),
184+
isDefaultRuleLevel ? normalizedRuleLevel : "" );
181185

182-
return value.equals( normalizedRuleLevel );
186+
return value.equals( normalizedRuleLevel );
187+
}
188+
189+
@Override
190+
public String toString() {
191+
return "Rules[level=" + normalizedRuleLevel + " default=" + isDefaultRuleLevel + "]";
192+
}
183193
};
184194
}
185195

@@ -194,4 +204,13 @@ private static String getDefaultValue( @Nullable Object value, @NonNull String d
194204
private static Supplier<List<IPentahoObjectReference<IAuthorizationRule>>> getPentahoSystemAuthorizationRuleReferencesSupplier() {
195205
return () -> PentahoSystem.getObjectReferences( IAuthorizationRule.class, null );
196206
}
207+
208+
@Override
209+
public String toString() {
210+
return String.format(
211+
"PentahoAuthorizationRuleLevel[%s, %s, count=%s]",
212+
ruleLevelType,
213+
levelRulePredicate,
214+
getDelegateRule().getRules().size() );
215+
}
197216
}

core/src/main/java/org/pentaho/platform/engine/security/authorization/core/AbstractAuthorizationAction.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,9 @@ public boolean equals( Object obj ) {
3030
return true;
3131
}
3232

33-
if ( !( obj instanceof IAuthorizationAction ) ) {
34-
return false;
35-
}
36-
37-
IAuthorizationAction other = (IAuthorizationAction) obj;
33+
return obj instanceof IAuthorizationAction other
34+
&& Objects.equals( getName(), other.getName() );
3835

39-
return Objects.equals( getName(), other.getName() );
4036
}
4137

4238
@Override

core/src/main/java/org/pentaho/platform/engine/security/authorization/core/AbstractAuthorizationUser.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ public abstract class AbstractAuthorizationUser extends AbstractAuthorizationPri
2121

2222
@Override
2323
public boolean equals( Object o ) {
24-
if ( !( o instanceof IAuthorizationUser ) ) {
25-
return false;
26-
}
24+
return o instanceof IAuthorizationUser that
25+
&& Objects.equals( getName(), that.getName() );
2726

28-
IAuthorizationUser that = (IAuthorizationUser) o;
29-
return Objects.equals( getName(), that.getName() );
3027
}
3128

3229
// have hash code be sensitive to the interface type, so that it is

core/src/main/java/org/pentaho/platform/engine/security/authorization/core/AuthorizationOptions.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,9 @@ public AuthorizationDecisionReportingMode getDecisionReportingMode() {
5858

5959
@Override
6060
public boolean equals( Object o ) {
61-
if ( !( o instanceof IAuthorizationOptions ) ) {
62-
return false;
63-
}
61+
return o instanceof IAuthorizationOptions that
62+
&& getDecisionReportingMode() == that.getDecisionReportingMode();
6463

65-
IAuthorizationOptions that = (IAuthorizationOptions) o;
66-
return getDecisionReportingMode() == that.getDecisionReportingMode();
6764
}
6865

6966
@Override

core/src/main/java/org/pentaho/platform/engine/security/authorization/core/AuthorizationRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ public int hashCode() {
8585
@Override
8686
public String toString() {
8787
return String.format(
88-
"%s [principal=`%s`, action='%s']",
88+
"%s [principal=%s, action=%s]",
8989
getClass().getSimpleName(),
90-
principal.getName(),
90+
principal,
9191
action );
9292
}
9393
}

core/src/main/java/org/pentaho/platform/engine/security/authorization/core/AuthorizationRole.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,9 @@ public String getName() {
3737

3838
@Override
3939
public boolean equals( Object o ) {
40-
if ( !( o instanceof IAuthorizationRole ) ) {
41-
return false;
42-
}
40+
return o instanceof IAuthorizationRole that
41+
&& Objects.equals( getName(), that.getName() );
4342

44-
IAuthorizationRole that = (IAuthorizationRole) o;
45-
return Objects.equals( getName(), that.getName() );
4643
}
4744

4845
// have hash code be sensitive to the interface type, so that it is

core/src/main/java/org/pentaho/platform/engine/security/authorization/core/AuthorizationService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,13 @@ public Optional<IAuthorizationDecision> authorizeRule(
162162

163163
try {
164164
if ( !rule.getRequestType().isAssignableFrom( request.getClass() ) ) {
165+
if ( logger.isDebugEnabled() ) {
166+
logger.debug( String.format(
167+
"AuthorizeRule END - SUCCESS - request: %s rule: %s result: Abstained for request type",
168+
request,
169+
rule ) );
170+
}
171+
165172
return Optional.empty();
166173
}
167174

core/src/main/java/org/pentaho/platform/engine/security/authorization/core/caching/MemoryAuthorizationDecisionCache.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ private class SessionCacheData {
7474

7575
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
7676

77+
@NonNull
78+
private final String sessionKey;
79+
7780
/**
7881
* Stores the set of sessions associated with this session cache data.
7982
* <p>
@@ -91,8 +94,10 @@ private class SessionCacheData {
9194
@NonNull
9295
private final Cache<IAuthorizationDecisionCacheKey, IAuthorizationDecision> cache;
9396

94-
public SessionCacheData( @NonNull Cache<IAuthorizationDecisionCacheKey, IAuthorizationDecision> cache,
97+
public SessionCacheData( @NonNull String sessionKey,
98+
@NonNull Cache<IAuthorizationDecisionCacheKey, IAuthorizationDecision> cache,
9599
@NonNull IPentahoSession session ) {
100+
this.sessionKey = sessionKey;
96101
this.cache = cache;
97102

98103
addSessionCore( session );
@@ -174,6 +179,13 @@ public boolean isStale() {
174179
}
175180

176181
public void invalidate( @NonNull IAuthorizationDecisionCacheKey key ) {
182+
if ( logger.isTraceEnabled() ) {
183+
logger.trace(
184+
String.format(
185+
"Invalidating cache entry for key '%s' in session cache for '%s'",
186+
key, sessionKey ) );
187+
}
188+
177189
cache.invalidate( key );
178190
}
179191

@@ -195,6 +207,15 @@ public void invalidateAll( @NonNull Predicate<IAuthorizationDecisionCacheKey> pr
195207
.filter( predicate )
196208
.toList();
197209

210+
if ( logger.isTraceEnabled() ) {
211+
for ( var key : invalidateRequests ) {
212+
logger.trace(
213+
String.format(
214+
"Invalidating cache entry for key '%s' in session cache for '%s'",
215+
key, sessionKey ) );
216+
}
217+
}
218+
198219
cache.invalidateAll( invalidateRequests );
199220
}
200221

@@ -210,6 +231,10 @@ public void dispose() {
210231
} finally {
211232
lock.writeLock().unlock();
212233
}
234+
235+
if ( logger.isTraceEnabled() ) {
236+
logger.trace( String.format( "Session cache disposed for '%s'", sessionKey ) );
237+
}
213238
}
214239
}
215240

@@ -513,7 +538,7 @@ protected Cache<IAuthorizationDecisionCacheKey, IAuthorizationDecision> getSessi
513538
}
514539

515540
var cache = createSessionCache();
516-
cacheData = new SessionCacheData( cache, session );
541+
cacheData = new SessionCacheData( sessionKey, cache, session );
517542
cacheBySessionKey.put( sessionKey, cacheData );
518543
return cache;
519544
} finally {

core/src/main/java/org/pentaho/platform/engine/security/authorization/core/resources/ResourceAuthorizationRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ public int hashCode() {
100100
@Override
101101
public String toString() {
102102
return String.format(
103-
"%s [principal: `%s`, action: '%s', resource: %s]",
103+
"%s [principal=%s, action=%s, resource=%s]",
104104
getClass().getSimpleName(),
105-
getPrincipal().getName(),
105+
getPrincipal(),
106106
getAction(),
107107
getResource() );
108108
}

core/src/main/java/org/pentaho/platform/engine/security/authorization/core/rules/AbstractAuthorizationRule.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
package org.pentaho.platform.engine.security.authorization.core.rules;
1414

1515
import edu.umd.cs.findbugs.annotations.NonNull;
16-
import org.pentaho.platform.api.engine.security.authorization.IAuthorizationContext;
1716
import org.pentaho.platform.api.engine.security.authorization.IAuthorizationRequest;
1817
import org.pentaho.platform.api.engine.security.authorization.IAuthorizationRule;
1918
import org.pentaho.platform.api.engine.security.authorization.decisions.IAuthorizationDecision;
@@ -29,22 +28,14 @@
2928
* <p>
3029
* It provides a default implementation of the {@link #toString()} method, which returns the class's simple name.
3130
*
32-
* @param <T> The specific type of authorization request this rule can handle, must extend {@link IAuthorizationRequest}.
31+
* @param <T> The specific type of authorization request this rule can handle, must extend
32+
* {@link IAuthorizationRequest}.
3333
*/
3434
public abstract class AbstractAuthorizationRule<T extends IAuthorizationRequest> implements IAuthorizationRule<T> {
3535

3636
protected static final String LIST_SEPARATOR =
3737
Messages.getInstance().getString( "AbstractAuthorizationDecision.LIST_SEPARATOR" );
3838

39-
@NonNull
40-
@Override
41-
public abstract Class<T> getRequestType();
42-
43-
@NonNull
44-
@Override
45-
public abstract Optional<IAuthorizationDecision> authorize( @NonNull T request,
46-
@NonNull IAuthorizationContext context );
47-
4839
@NonNull
4940
protected final Optional<IAuthorizationDecision> abstain() {
5041
return Optional.empty();

0 commit comments

Comments
 (0)