@Override public void handleGet(Operation get) { if (isSecurityContextRequest(get)) { handleGetSecurityContext(get); } else if (isRolesRequest(get)) { handleGetPrincipalRoles(get); } else if (isPrincipalByIdRequest(get)) { handleSearchById(get); } else if (isPrincipalByCriteriaRequest(get)) { Map<String, String> queryParams = UriUtils.parseUriQueryParams(get.getUri()); String roleQueryValue = queryParams.getOrDefault(ROLES_QUERY, null); handleSearchByCriteria(queryParams.get(CRITERIA_QUERY), roleQueryValue, get); } else { get.fail(new IllegalArgumentException( "Provide either criteria or principalId to search for.")); } }
@Override public void handlePost(Operation post) { if (isSecurityContextRequest(post)) { handlePostSecurityContext(post); return; } super.handlePost(post); }
private void handleGetSecurityContext(Operation get) { String principalId = extractPropertyFromPath(PATTERN_PRINCIPAL_SECURITY_CONTEXT, PRINCIPAL_ID_PATH_SEGMENT, get.getUri().getPath()); if (principalId == null) { get.fail(new LocalizableValidationException("Principal ID is required in URI path.", "auth.principalId.required")); return; } SecurityContextUtil.getSecurityContext(this, get, principalId) .thenAccept((context) -> { get.setBody(context); get.complete(); }).exceptionally((ex) -> { if (ex.getCause() instanceof ServiceNotFoundException) { logWarning( "Failed to retrieve security context for user %s: user does not exist", principalId); // hide stacktrace from response ServiceErrorResponse rsp = Utils.toServiceErrorResponse(ex); rsp.stackTrace = null; get.fail(Operation.STATUS_CODE_NOT_FOUND, ex, rsp); } else { logWarning("Failed to retrieve security context for user %s: %s", principalId, Utils.toString(ex)); get.fail(ex); } return null; }); }
UriUtils.buildUri(getHost(), Harbor.DEFAULT_REGISTRY_LINK), UriUtils.URI_PARAM_ODATA_EXPAND, Boolean.toString(true)); }); sendRequest(op);
@Override public void handlePatch(Operation patch) { if (!isRolesRequest(patch)) { String principalId = extractPropertyFromPath(PATTERN_PRINCIPAL_ROLES, PRINCIPAL_ID_PATH_SEGMENT, patch.getUri().getPath());
private void handleSearchById(Operation get) { String principalId = getIdFromPrincipalByIdRequest(get); DeferredResult<Principal> result = provider.getPrincipal(get, encode(principalId)); result.whenComplete((principal, ex) -> { if (ex != null) { if (ex.getCause() instanceof ServiceNotFoundException) { get.fail(Operation.STATUS_CODE_NOT_FOUND, ex.getCause(), ex.getCause()); return; } get.fail(ex); return; } if (principal == null) { get.fail(new Throwable("Principal does not exist!")); return; } get.setBody(principal).complete(); }); }
private void handleGetPrincipalRoles(Operation get) { String principalId = extractPropertyFromPath(PATTERN_PRINCIPAL_ROLES, PRINCIPAL_ID_PATH_SEGMENT, get.getUri().getPath()); if (principalId == null) { get.fail(new LocalizableValidationException("Principal ID is required in URI path.", "auth.principalId.required")); return; } PrincipalRoles rolesResponse = new PrincipalRoles(); PrincipalUtil.getPrincipal(this, get, principalId) .thenAccept(principal -> copyPrincipalData(principal, rolesResponse)) .thenCompose(ignore -> { if (rolesResponse.type == PrincipalType.GROUP) { return getDirectlyAssignedProjectRolesForGroup(this, rolesResponse); } return getDirectlyAssignedProjectRolesForUser(this, rolesResponse); }) .thenAccept(projectEntries -> rolesResponse.projects = projectEntries) .thenCompose(ignore -> { if (rolesResponse.type == PrincipalType.GROUP) { return getDirectlyAssignedSystemRolesForGroup(this, rolesResponse); } return getDirectlyAssignedSystemRolesForUser(this, rolesResponse); }) .thenAccept(systemRoles -> rolesResponse.roles = systemRoles) .thenAccept(ignore -> get.setBody(rolesResponse)) .whenCompleteNotify(get); }