diff --git a/src/main/java/org/zalando/nakadi/service/AdminService.java b/src/main/java/org/zalando/nakadi/service/AdminService.java index f7b7592863..77da0f5f95 100644 --- a/src/main/java/org/zalando/nakadi/service/AdminService.java +++ b/src/main/java/org/zalando/nakadi/service/AdminService.java @@ -40,7 +40,7 @@ public class AdminService { private final AuthorizationService authorizationService; private final FeatureToggleService featureToggleService; private final NakadiSettings nakadiSettings; - private Cache> resourceCache; + private Cache> resourceCache; private final NakadiAuditLogPublisher auditLogPublisher; @Autowired @@ -58,11 +58,7 @@ public AdminService(final AuthorizationDbRepository authorizationDbRepository, } public List getAdmins() { - try { - return addDefaultAdmin(resourceCache.get(ADMIN_RESOURCE, authorizationDbRepository::listAdmins)); - } catch (ExecutionException e) { return addDefaultAdmin(authorizationDbRepository.listAdmins()); - } } public void updateAdmins(final List newAdmins) @@ -87,20 +83,33 @@ public void updateAdmins(final List newAdmins) "-"); } - public boolean isAdmin(final AuthorizationService.Operation operation) throws PluginException { + private Resource getAdminResource() { final List permissions = getAdmins(); - final Resource resource = new ResourceImpl<>(ADMIN_RESOURCE, ADMIN_RESOURCE, + return new ResourceImpl<>(ADMIN_RESOURCE, ADMIN_RESOURCE, + ResourceAuthorization.fromPermissionsList(permissions), null); + } + + private Resource getAllDataAccessResource() { + final List permissions = authorizationDbRepository.listAllDataAccess(); + return new ResourceImpl<>(ALL_DATA_ACCESS_RESOURCE, + ALL_DATA_ACCESS_RESOURCE, ResourceAuthorization.fromPermissionsList(permissions), null); + } + + public boolean isAdmin(final AuthorizationService.Operation operation) throws PluginException { + Resource resource; + try { + resource = resourceCache.get(ADMIN_RESOURCE, () -> getAdminResource()); + } catch (ExecutionException e) { + resource = getAdminResource(); + } return authorizationService.isAuthorized(operation, resource); } public boolean hasAllDataAccess(final AuthorizationService.Operation operation) throws PluginException { try { - final List permissions = resourceCache.get(ALL_DATA_ACCESS_RESOURCE, - authorizationDbRepository::listAllDataAccess); - final Resource resource = new ResourceImpl<>(ALL_DATA_ACCESS_RESOURCE, - ALL_DATA_ACCESS_RESOURCE, - ResourceAuthorization.fromPermissionsList(permissions), null); + final Resource resource = resourceCache.get(ALL_DATA_ACCESS_RESOURCE, + () -> getAllDataAccessResource()); return authorizationService.isAuthorized(operation, resource); } catch (ExecutionException e) { LOG.error("Could not determine whether this application has all data access", e);