Fix remainingCount handling and related e2e tests.
Add secret rest api for e2e support.