Guide · updated

Service users and repoinit: replacing admin sessions properly

Calls such as getAdministrativeResourceResolver and loginAdministrative were deprecated in Sling long ago, and your code cannot use administrative sessions on AEM as a Cloud Service. The replacement has three parts that ship with your code: a service user created by repoinit, a mapping from your bundle to that user, and a call that asks for a resolver by sub-service name.

1. Decide what each service needs

List every place that used an admin session and write down the paths it reads or writes. One service user per purpose (for example, one that reads /content/dam statistics, another that writes reports under /var) keeps permissions small and audit logs readable.

2. Create the user with repoinit

Repoinit scripts run at startup and are deployed as an OSGi configuration of org.apache.sling.jcr.repoinit.RepositoryInitializer. Adobe's WKND example creates the user and grants read access like this:

  • create service user wknd-examples-statistics-service with forced path system/cq:services/wknd-examples
  • a set principal ACL for wknd-examples-statistics-service block containing allow jcr:read on /content/dam, closed with end

Adobe notes that not every repoinit directive is supported in the older .config format; use .cfg.json for advanced directives. Factory configuration files on Cloud Service follow the <factoryPID>-<name>.cfg.json naming convention.

3. Map your bundle to the user

Add an amended service user mapping, for example org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended-wknd-examples.cfg.json, with a user.mapping entry in the form bundle-symbolic-name:subservice=[service-user]. Adobe's example maps wknd-examples.core:wknd-examples-statistics to [wknd-examples-statistics-service].

4. Open the resolver by sub-service, and always close it

Pass ResourceResolverFactory.SUBSERVICE with your sub-service name to getServiceResourceResolver, inside try-with-resources so the resolver is closed even when an exception is thrown. Adobe's sample code makes the same point: every resolver you open must be closed.

Common mistakes

  • One all-powerful service user for everything: it recreates the admin session problem with extra steps.
  • Mapping the wrong bundle symbolic name: no user is found for the sub-service and getServiceResourceResolver throws a LoginException.
  • Forgetting write permissions on /var or /conf for code that writes there.
  • Leaving resolvers open in long-running jobs: open per unit of work, close it, repeat.

Our refactoring factory rewrites the Java call automatically and leaves a TODO naming the sub-service. A person approves each service user's permissions, because that is a security decision, not a code change.

Check your own site or code for free

This guide covers the AEM code and platform side of a move. The pages are a separate job, and that is the job the four steps do.

Surge Migrate moves a website from any CMS to any CMS in four steps: Scan, Convert, Verify, Install. Pages are converted automatically, and you check the result before you install anything.

Sources

Any CMS to any CMS

Scan, Convert, Verify, Install

This guide covers one part of a move. Surge Migrate moves the pages, from any CMS to any CMS. Converted automatically. Checked before you install.

  1. 1

    Scan

    Names the system behind your site, counts pages and templates, lists the blocks to build and the work no tool does, and gives an effort range with its working.

    Scan a site →
  2. 2

    Convert

    Each page becomes an Edge Delivery document, an AEM Core Component page and import files for 7 other systems. 10 pages free; 500 or 2,000 pages in one paid run.

    Convert your pages →
  3. 3

    Verify

    A download is offered only when its own format check passes. The report lists every field not found and every component kept as plain content. Compare old and new pages side by side.

    Parity check →
  4. 4

    Install

    You import with the target system's own tool, on your own environment. Each download's README gives the command. Your system changes only when you run it.

    How each import was tested →

No account needed. Public pages only, robots.txt obeyed. Nothing is installed in your system by these tools.