apolloconfig/apollo · error · BadRequestException

Current user not found

Error message

Current user not found

What it means

Thrown by ItemController.resolveOperator when authType is USER or USER_TOKEN but userInfoHolder.getUser() returns null or a UserInfo with a blank userId. The resolveOperator method (two-arg variant for ItemController) determines the acting user for item write operations; for interactive and user-token identities it expects a valid session user. Maps to HTTP 400 BadRequestException.

Source

Thrown at apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/ItemController.java:282

      this.itemOpenApiService.updateItem(appId, env, clusterName, namespaceName, item,
          resolvedOperator);
    }
  }

  private void checkCommentLength(String comment) {
    if (!StringUtils.isEmpty(comment) && comment.length() > ITEM_COMMENT_MAX_LENGTH) {
      throw new BadRequestException("Comment length should not exceed %s characters",
          ITEM_COMMENT_MAX_LENGTH);
    }
  }

  private String resolveOperator(String queryOperator, String payloadOperator) {
    String authType = UserIdentityContextHolder.getAuthType();
    if (UserIdentityConstants.USER.equals(authType)
        || UserIdentityConstants.USER_TOKEN.equals(authType)) {
      UserInfo loginUser = userInfoHolder.getUser();
      if (loginUser == null || StringUtils.isBlank(loginUser.getUserId())) {
        throw new BadRequestException("Current user not found");
      }
      return loginUser.getUserId();
    }

    if (UserIdentityConstants.CONSUMER.equals(authType)) {
      String operator = StringUtils.isBlank(queryOperator) ? payloadOperator : queryOperator;
      RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(operator),
          "operator should not be null or empty");
      if (userService.findByUserId(operator) == null) {
        throw BadRequestException.userNotExists(operator);
      }
      return operator;
    }

    throw new BadRequestException("Unsupported auth type: %s", authType);
  }

  private boolean shouldHideConfigToPortalUser(String appId, String env, String clusterName,

View on GitHub (pinned to d95fc18d11)

Solutions

  1. For USER auth: re-authenticate through the Portal to establish a fresh session.
  2. For USER_TOKEN: verify the token's associated user account is active and not deleted in the Portal user management page.
  3. In tests: configure userInfoHolder.getUser() to return a valid UserInfo with a non-blank userId before invoking the controller.

Example fix

// before — test mock returns null user
when(userInfoHolder.getUser()).thenReturn(null);
controller.updateItem("appA", "DEV", "default", "application", item, false); // throws

// after — provide valid UserInfo
UserInfo user = new UserInfo();
user.setUserId("testUser");
when(userInfoHolder.getUser()).thenReturn(user);
Defensive patterns

Strategy: try-catch

Validate before calling

// Before calling item write, verify the user session is valid
UserInfo user = userInfoHolder.getUser();
if (user == null || StringUtils.isBlank(user.getUserId())) {
    throw new IllegalStateException("No active user session. Please re-authenticate.");
}

Try / catch

try {
    controller.updateItem(appId, env, clusterName, namespaceName, item, createIfNotExists);
} catch (BadRequestException e) {
    if ("Current user not found".equals(e.getMessage())) {
        redirectToLogin(); // or refresh the token
    } else {
        throw e;
    }
}

Prevention

When it happens

Trigger: Any item write endpoint (createItem, updateItem, createOrUpdateItem) called with USER or USER_TOKEN auth where the user session is absent, expired, or the UserInfo object has a null/blank userId.

Common situations: A Portal browser session expired before the item save completed (USER path). A USER_TOKEN references a deactivated user account. Integration tests with an improperly mocked UserInfoHolder. Thread-local context pollution in async request handling where the security context is cleared before the controller runs.

Related errors


AI-assisted analysis of apolloconfig/apollo@d95fc18d11 (2026-08-14). Data as JSON: /api/errors/cc0e4d4b8c11d531. Report an issue: GitHub.