-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Security Manager
- Loading branch information
Showing
647 changed files
with
5,742 additions
and
1,210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
...pi/src/main/java/com/refinedmods/refinedstorage2/api/network/security/SecurityPolicy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.refinedmods.refinedstorage2.api.network.security; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import org.apiguardian.api.API; | ||
|
||
@API(status = API.Status.STABLE, since = "2.0.0-milestone.3.5") | ||
public record SecurityPolicy(Set<Permission> allowedPermissions) { | ||
public static final SecurityPolicy EMPTY = new SecurityPolicy(Collections.emptySet()); | ||
|
||
public boolean isAllowed(final Permission permission) { | ||
return allowedPermissions.contains(permission); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...inedstorage2/api/network/impl/node/security/SecurityDecisionProviderProxyNetworkNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.refinedmods.refinedstorage2.api.network.impl.node.security; | ||
|
||
import com.refinedmods.refinedstorage2.api.network.impl.storage.AbstractNetworkNode; | ||
import com.refinedmods.refinedstorage2.api.network.security.Permission; | ||
import com.refinedmods.refinedstorage2.api.network.security.SecurityActor; | ||
import com.refinedmods.refinedstorage2.api.network.security.SecurityDecision; | ||
import com.refinedmods.refinedstorage2.api.network.security.SecurityDecisionProvider; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public class SecurityDecisionProviderProxyNetworkNode extends AbstractNetworkNode implements SecurityDecisionProvider { | ||
private long energyUsage; | ||
@Nullable | ||
private SecurityDecisionProvider delegate; | ||
|
||
public SecurityDecisionProviderProxyNetworkNode(final long energyUsage) { | ||
this.energyUsage = energyUsage; | ||
} | ||
|
||
public SecurityDecisionProviderProxyNetworkNode(final long energyUsage, final SecurityDecisionProvider delegate) { | ||
this(energyUsage); | ||
this.delegate = delegate; | ||
} | ||
|
||
public void setDelegate(@Nullable final SecurityDecisionProvider delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public void setEnergyUsage(final long energyUsage) { | ||
this.energyUsage = energyUsage; | ||
} | ||
|
||
@Override | ||
public long getEnergyUsage() { | ||
return energyUsage; | ||
} | ||
|
||
@Override | ||
public SecurityDecision isAllowed(final Permission permission, final SecurityActor actor) { | ||
if (delegate == null) { | ||
return SecurityDecision.PASS; | ||
} | ||
return delegate.isAllowed(permission, actor); | ||
} | ||
|
||
@Override | ||
public SecurityDecision isAllowed(final Permission permission) { | ||
if (delegate == null) { | ||
return SecurityDecision.PASS; | ||
} | ||
return delegate.isAllowed(permission); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...ain/java/com/refinedmods/refinedstorage2/api/network/impl/node/security/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@ParametersAreNonnullByDefault | ||
@FieldsAndMethodsAreNonnullByDefault | ||
package com.refinedmods.refinedstorage2.api.network.impl.node.security; | ||
|
||
import com.refinedmods.refinedstorage2.api.core.FieldsAndMethodsAreNonnullByDefault; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; |
52 changes: 52 additions & 0 deletions
52
...m/refinedmods/refinedstorage2/api/network/impl/security/SecurityDecisionProviderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.refinedmods.refinedstorage2.api.network.impl.security; | ||
|
||
import com.refinedmods.refinedstorage2.api.network.security.Permission; | ||
import com.refinedmods.refinedstorage2.api.network.security.SecurityActor; | ||
import com.refinedmods.refinedstorage2.api.network.security.SecurityDecision; | ||
import com.refinedmods.refinedstorage2.api.network.security.SecurityDecisionProvider; | ||
import com.refinedmods.refinedstorage2.api.network.security.SecurityPolicy; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
|
||
public class SecurityDecisionProviderImpl implements SecurityDecisionProvider { | ||
private final Map<SecurityActor, SecurityPolicy> policyByActor = new HashMap<>(); | ||
@Nullable | ||
private SecurityPolicy defaultPolicy; | ||
|
||
public SecurityDecisionProviderImpl setPolicy(final SecurityActor actor, final SecurityPolicy policy) { | ||
policyByActor.put(actor, policy); | ||
return this; | ||
} | ||
|
||
public SecurityDecisionProviderImpl setDefaultPolicy(@Nullable final SecurityPolicy policy) { | ||
this.defaultPolicy = policy; | ||
return this; | ||
} | ||
|
||
public void clearPolicies() { | ||
policyByActor.clear(); | ||
} | ||
|
||
@Override | ||
public SecurityDecision isAllowed(final Permission permission, final SecurityActor actor) { | ||
final SecurityPolicy policy = policyByActor.get(actor); | ||
if (policy == null) { | ||
return SecurityDecision.PASS; | ||
} | ||
return allowOrDeny(policy.isAllowed(permission)); | ||
} | ||
|
||
@Override | ||
public SecurityDecision isAllowed(final Permission permission) { | ||
if (defaultPolicy == null) { | ||
return SecurityDecision.PASS; | ||
} | ||
return allowOrDeny(defaultPolicy.isAllowed(permission)); | ||
} | ||
|
||
private static SecurityDecision allowOrDeny(final boolean allowed) { | ||
return allowed ? SecurityDecision.ALLOW : SecurityDecision.DENY; | ||
} | ||
} |
51 changes: 39 additions & 12 deletions
51
...m/refinedmods/refinedstorage2/api/network/impl/security/SecurityNetworkComponentImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,69 @@ | ||
package com.refinedmods.refinedstorage2.api.network.impl.security; | ||
|
||
import com.refinedmods.refinedstorage2.api.core.CoreValidations; | ||
import com.refinedmods.refinedstorage2.api.network.node.container.NetworkNodeContainer; | ||
import com.refinedmods.refinedstorage2.api.network.security.Permission; | ||
import com.refinedmods.refinedstorage2.api.network.security.SecurityActor; | ||
import com.refinedmods.refinedstorage2.api.network.security.SecurityDecision; | ||
import com.refinedmods.refinedstorage2.api.network.security.SecurityDecisionProvider; | ||
import com.refinedmods.refinedstorage2.api.network.security.SecurityNetworkComponent; | ||
import com.refinedmods.refinedstorage2.api.network.security.SecurityPolicy; | ||
|
||
import java.util.HashSet; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class SecurityNetworkComponentImpl implements SecurityNetworkComponent { | ||
private final Set<SecurityDecisionProvider> providers = new HashSet<>(); | ||
private final Set<SecurityDecisionProvider> providers = new LinkedHashSet<>(); | ||
private final SecurityPolicy defaultPolicy; | ||
|
||
public SecurityNetworkComponentImpl(final SecurityPolicy defaultPolicy) { | ||
this.defaultPolicy = defaultPolicy; | ||
} | ||
|
||
@Override | ||
public void onContainerAdded(final NetworkNodeContainer container) { | ||
if (container instanceof SecurityDecisionProvider provider) { | ||
if (container.getNode() instanceof SecurityDecisionProvider provider) { | ||
providers.add(provider); | ||
} | ||
} | ||
|
||
@Override | ||
public void onContainerRemoved(final NetworkNodeContainer container) { | ||
if (container instanceof SecurityDecisionProvider provider) { | ||
if (container.getNode() instanceof SecurityDecisionProvider provider) { | ||
providers.remove(provider); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isAllowed(final Permission permission, final SecurityActor actor) { | ||
for (final SecurityDecisionProvider provider : providers) { | ||
final SecurityDecision decision = provider.isAllowed(permission, actor); | ||
if (decision == SecurityDecision.DENY) { | ||
return false; | ||
} else if (decision == SecurityDecision.ALLOW) { | ||
return true; | ||
} | ||
if (providers.isEmpty()) { | ||
return defaultPolicy.isAllowed(permission); | ||
} | ||
final Set<SecurityDecision> decisions = providers.stream().map(provider -> CoreValidations.validateNotNull( | ||
provider.isAllowed(permission, actor), | ||
"Decision cannot be null" | ||
)).collect(Collectors.toSet()); | ||
final boolean anyDenied = decisions.stream().anyMatch(decision -> decision == SecurityDecision.DENY); | ||
if (anyDenied) { | ||
return false; | ||
} | ||
final boolean anyAllowed = decisions.stream().anyMatch(decision -> decision == SecurityDecision.ALLOW); | ||
if (anyAllowed) { | ||
return true; | ||
} | ||
return tryFallback(permission); | ||
} | ||
|
||
private boolean tryFallback(final Permission permission) { | ||
final Set<SecurityDecision> decisions = providers.stream().map(provider -> CoreValidations.validateNotNull( | ||
provider.isAllowed(permission), | ||
"Decision cannot be null" | ||
)).collect(Collectors.toSet()); | ||
final boolean anyDenied = decisions.stream().anyMatch(decision -> decision == SecurityDecision.DENY); | ||
if (anyDenied) { | ||
return false; | ||
} | ||
return true; | ||
return decisions.stream().anyMatch(decision -> decision == SecurityDecision.ALLOW); | ||
} | ||
} |
Oops, something went wrong.