Search in sources :

Example 1 with IntegrationException

use of com.netflix.spinnaker.kork.exceptions.IntegrationException in project fiat by spinnaker.

the class RedisPermissionsRepository method getUnrestrictedUserPermission.

private UserPermission getUnrestrictedUserPermission() {
    String serverLastModified = NO_LAST_MODIFIED;
    byte[] bServerLastModified = redisRead(new TimeoutContext("checkLastModified", clock, configProps.getRepository().getCheckLastModifiedTimeout()), c -> c.get(SafeEncoder.encode(unrestrictedLastModifiedKey())));
    if (bServerLastModified == null || bServerLastModified.length == 0) {
        log.debug("no last modified time available in redis for user {} using default of {}", UNRESTRICTED, NO_LAST_MODIFIED);
    } else {
        serverLastModified = SafeEncoder.encode(bServerLastModified);
    }
    try {
        UserPermission userPermission = unrestrictedPermission.get(serverLastModified);
        if (userPermission != null && !serverLastModified.equals(NO_LAST_MODIFIED)) {
            fallbackLastModified.set(serverLastModified);
        }
        return userPermission;
    } catch (Throwable ex) {
        log.error("failed reading user {} from cache for key {}", UNRESTRICTED, serverLastModified, ex);
        String fallback = fallbackLastModified.get();
        if (fallback != null) {
            UserPermission fallbackPermission = unrestrictedPermission.getIfPresent(fallback);
            if (fallbackPermission != null) {
                log.warn("serving fallback permission for user {} from key {} as {}", UNRESTRICTED, fallback, fallbackPermission);
                return fallbackPermission;
            }
            log.warn("no fallback entry remaining in cache for key {}", fallback);
        }
        if (ex instanceof RuntimeException) {
            throw (RuntimeException) ex;
        }
        throw new IntegrationException(ex);
    }
}
Also used : IntegrationException(com.netflix.spinnaker.kork.exceptions.IntegrationException) UserPermission(com.netflix.spinnaker.fiat.model.UserPermission)

Example 2 with IntegrationException

use of com.netflix.spinnaker.kork.exceptions.IntegrationException in project fiat by spinnaker.

the class FiatPermissionEvaluator method canCreate.

public boolean canCreate(String resourceType, Object resource) {
    if (!fiatStatus.isEnabled()) {
        return true;
    }
    String username = getUsername(SecurityContextHolder.getContext().getAuthentication());
    try {
        return AuthenticatedRequest.propagate(() -> {
            return retryHandler.retry("determine whether " + username + " can create resource " + resource, () -> {
                try {
                    fiatService.canCreate(username, resourceType, resource);
                    return true;
                } catch (RetrofitError re) {
                    boolean shouldRetry = true;
                    if (re.getKind() == RetrofitError.Kind.HTTP) {
                        switch(HttpStatus.valueOf(re.getResponse().getStatus())) {
                            case NOT_FOUND:
                                return false;
                            case BAD_REQUEST:
                                shouldRetry = false;
                        }
                    }
                    IntegrationException ie = new IntegrationException(re);
                    ie.setRetryable(shouldRetry);
                    throw ie;
                }
            });
        }).call();
    } catch (Exception e) {
        log.info(e.toString());
        return false;
    }
}
Also used : IntegrationException(com.netflix.spinnaker.kork.exceptions.IntegrationException) IntegrationException(com.netflix.spinnaker.kork.exceptions.IntegrationException) RetrofitError(retrofit.RetrofitError)

Aggregations

IntegrationException (com.netflix.spinnaker.kork.exceptions.IntegrationException)2 UserPermission (com.netflix.spinnaker.fiat.model.UserPermission)1 RetrofitError (retrofit.RetrofitError)1