Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

fix(payg): bill the triggering user when an unowned policy runs (#7893)

ConnorYoh0cff6f14b154
diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/policy/engine/PolicyEngine.java b/app/proprietary/src/main/java/stirling/software/proprietary/policy/engine/PolicyEngine.java
index e9656e55d2..5368ba6c84 100644
--- a/app/proprietary/src/main/java/stirling/software/proprietary/policy/engine/PolicyEngine.java
+++ b/app/proprietary/src/main/java/stirling/software/proprietary/policy/engine/PolicyEngine.java
@@ -186,6 +186,10 @@ public class PolicyEngine {
// than the owner's problem. Three identities, deliberately not interchangeable.
String triggeringUser = currentActingPrincipal();
String fileOwner = triggeringUser != null ? triggeringUser : policy.owner();
+ // Unowned policies (the seeded Classification policy) have no owner to bill; fall back to
+ // the triggering user, or the run goes out principal-less and its tool sub-steps bill the
+ // team-less INTERNAL_API_USER, which PAYG refuses with a usage-limit 402.
+ String billingPrincipal = policy.owner() != null ? policy.owner() : triggeringUser;
// Stored supporting files (certificates, watermark images, ...) load here, before the
// async hop: worker threads have no principal, so assets bind by the policy's own team.
PolicyInputs resolved = assetResolver.resolve(policy, inputs);
@@ -196,7 +200,7 @@ public class PolicyEngine {
new PipelineDefinition(
policy.name(), policy.steps(), outputResolver.resolve(policy));
return submitForPrincipal(
- policy.owner(),
+ billingPrincipal,
fileOwner,
triggeringUser,
policy.id(),
diff --git a/app/proprietary/src/test/java/stirling/software/proprietary/policy/engine/PolicyEngineTest.java b/app/proprietary/src/test/java/stirling/software/proprietary/policy/engine/PolicyEngineTest.java
index ac1683935e..efc51c9487 100644
--- a/app/proprietary/src/test/java/stirling/software/proprietary/policy/engine/PolicyEngineTest.java
+++ b/app/proprietary/src/test/java/stirling/software/proprietary/policy/engine/PolicyEngineTest.java
@@ -531,6 +531,54 @@ class PolicyEngineTest {
assertEquals("alice", principalAtDispatch[0]);
}
+ @Test
+ void runPolicyBillsTheTriggeringUserWhenThePolicyIsUnowned() throws Exception {
+ // Regression: an unowned policy (the seeded Classification policy) must bill the triggering
+ // user, not a null principal that reverts its tool sub-steps to the team-less
+ // INTERNAL_API_USER and draws a usage-limit 402.
+ when(toolMetadataService.isMultiInput(anyString())).thenReturn(false);
+ when(toolMetadataService.shouldUnpackZipResponse(anyString())).thenReturn(false);
+ int[] counter = {0};
+ when(fileStorage.storeInputStream(any(InputStream.class), anyString()))
+ .thenAnswer(
+ inv ->
+ new StoredFile(
+ "file-" + ++counter[0],
+ ((InputStream) inv.getArgument(0)).readAllBytes().length));
+
+ String[] principalAtDispatch = {"<none>"};
+ when(internalApiClient.post(eq(ROTATE), any()))
+ .thenAnswer(
+ inv -> {
+ principalAtDispatch[0] = MDC.get("auditPrincipal");
+ return ResponseEntity.ok(pdf("rotated", "rotated.pdf"));
+ });
+
+ Policy unowned =
+ new Policy(
+ "p1",
+ "rotate",
+ null, // unowned, like the seeded Classification policy
+ true,
+ List.of(),
+ List.of(new PipelineStep(ROTATE, Map.of())),
+ OutputSpec.inline());
+
+ MDC.put("auditPrincipal", "bob"); // the user whose upload triggered the run
+ try {
+ engine.runPolicy(
+ unowned,
+ PolicyInputs.of(List.of(pdf("input", "input.pdf"))),
+ PolicyProgressListener.NOOP)
+ .completion()
+ .get(10, TimeUnit.SECONDS);
+ } finally {
+ MDC.remove("auditPrincipal");
+ }
+
+ assertEquals("bob", principalAtDispatch[0]);
+ }
+
@Test
void adHocRunDispatchesToolCallsAsTheSubmittingUser() throws Exception {
// Ad-hoc runs (no stored policy) bill whoever kicked them off; the principal is captured on