This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
81 additions
and
13 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
67 changes: 67 additions & 0 deletions
67
server/src/main/java/keywhiz/service/permissions/OwnershipPermissionCheck.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,67 @@ | ||
package keywhiz.service.permissions; | ||
|
||
import com.codahale.metrics.MetricRegistry; | ||
import com.google.inject.Inject; | ||
import java.util.Set; | ||
import keywhiz.api.model.Client; | ||
import keywhiz.api.model.Group; | ||
import keywhiz.api.model.Secret; | ||
import keywhiz.service.daos.AclDAO; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class OwnershipPermissionCheck implements PermissionCheck{ | ||
private static final Logger logger = LoggerFactory.getLogger(OwnershipPermissionCheck.class); | ||
|
||
private static final String SUCCESS_METRIC_NAME = MetricRegistry.name(OwnershipPermissionCheck.class, "success", "histogram"); | ||
private static final String FAILURE_METRIC_NAME = MetricRegistry.name(OwnershipPermissionCheck.class, "failure", "histogram"); | ||
|
||
private final MetricRegistry metricRegistry; | ||
private final AclDAO aclDAO; | ||
|
||
@Inject | ||
public OwnershipPermissionCheck(MetricRegistry metricRegistry, AclDAO.AclDAOFactory aclDAOFactory) { | ||
this.metricRegistry = metricRegistry; | ||
this.aclDAO = aclDAOFactory.readwrite(); | ||
} | ||
|
||
public boolean isAllowed(Object source, String action, Object target) { | ||
boolean hasPermission = false; | ||
|
||
if (isClient(source) && isSecret(target)) { | ||
Set<Group> clientGroups = aclDAO.getGroupsFor((Client) source); | ||
String secretOwner = ((Secret) target).getOwner(); | ||
|
||
for (Group group : clientGroups) { | ||
if (group.getName().equals(secretOwner)) { | ||
hasPermission = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
emitHistogramMetrics(hasPermission); | ||
|
||
logger.info( | ||
String.format("isAllowed Actor: %s, Action: %s, Target: %s, Result: %s", source, action, target, | ||
hasPermission)); | ||
|
||
return hasPermission; | ||
} | ||
|
||
private boolean isClient(Object source) { | ||
return source instanceof Client; | ||
} | ||
|
||
private boolean isSecret(Object target) { | ||
return target instanceof Secret; | ||
} | ||
|
||
private void emitHistogramMetrics(Boolean isPermitted) { | ||
int hasPermissionSuccessMetricInt = isPermitted ? 1 : 0; | ||
metricRegistry.histogram(SUCCESS_METRIC_NAME).update(hasPermissionSuccessMetricInt); | ||
|
||
int hasPermissionFailureMetricInt = isPermitted ? 0 : 1; | ||
metricRegistry.histogram(FAILURE_METRIC_NAME).update(hasPermissionFailureMetricInt); | ||
} | ||
} |