soarkit opened a new issue, #14775:
URL: https://github.com/apache/gravitino/issues/14775

   
   ## Repository
   
   `apache/gravitino`
   
   ## Target commit
   
   `9cc9084c14629af3f563b35a6482dbd678213430` — *[#13857] fix(server): Mask 
secrets in --updater-options and --spark-conf job arguments (#13858)*
   
   ## File
   
   
`core/src/main/java/org/apache/gravitino/job/JobRuntimeTemplateMaskUtils.java:86-102`
 (the producer that returns `null`), called from 
`core/src/main/java/org/apache/gravitino/listener/JobEventDispatcher.java:230-233`
 (which forwards the result), consumed by 
`core/src/main/java/org/apache/gravitino/listener/api/event/job/RunJobPreEvent.java:43-48`
 (the unconditional copy)
   
   ## Summary
   
   This commit put a masked copy of `jobConf` on the run-job events. The helper 
that builds that copy is explicitly nullable: `maskSensitiveJobConf(@Nullable 
Map)` is annotated `@Nullable` and returns `null` when its input is `null` 
(`JobRuntimeTemplateMaskUtils.java:86-90`, Javadoc at `:83-84` says "may be 
null" and "or null when `jobConf` is null"). `JobEventDispatcher.runJob` takes 
the result and hands it straight to `new RunJobPreEvent(...)` 
(`JobEventDispatcher.java:230-233`), whose constructor calls 
`ImmutableMap.copyOf(jobConf)` (`RunJobPreEvent.java:47`). 
`ImmutableMap.copyOf(null)` throws, so a run-job call made with no 
configuration aborts while the pre-event is being built — before 
`jobOperationDispatcher.runJob` is reached, so the job is never created and the 
caller sees an internal failure instead of a run.
   
   That the null case is real is visible one layer up: the REST handler already 
normalizes a missing `jobConf` with `request.getJobConf() != null ? 
request.getJobConf() : Collections.emptyMap()` 
(`server/.../JobOperations.java:436-437`). The event layer has no equivalent 
guard, and the two other run-job events have the same shape: `RunJobEvent` also 
calls `ImmutableMap.copyOf(jobConf)` (`RunJobEvent.java:54`), while 
`RunJobFailureEvent` stores the reference as-is (`RunJobFailureEvent.java:52`).
   
   ## Actual behavior
   
   **Expected:** a run-job request without a configuration creates the job and 
the events carry an empty `jobConf` (or the events carry no `jobConf`), exactly 
as the REST layer's empty-map substitution intends. No event constructor should 
throw on a value the producer declares nullable.
   
   **Actual:** masked result is `null` and event construction fails with
   
   ```
   maskedJobConf=null
   java.lang.NullPointerException: Cannot invoke "java.util.Map.entrySet()" 
because "map" is null
   ```
   
   so the dispatch that precedes job execution dies, and the caller gets a 
failed run instead of a job handle.
   
   ### Reproduction
   
   Compile the target's real `JobRuntimeTemplateMaskUtils`, `RunJobPreEvent`, 
`RunJobEvent` and their event base chain (Guava `ImmutableMap` is the only 
non-JDK dependency on this path), then call the two methods in sequence:
   
   ```java
   Map<String, String> masked = 
JobRuntimeTemplateMaskUtils.maskSensitiveJobConf(null);
   System.out.println("maskedJobConf=" + masked);
   new RunJobPreEvent("user", "metalake", "job_template", masked);   // line 
47: ImmutableMap.copyOf(masked)
   ```
   
   The first line prints `maskedJobConf=null`; the second throws the 
`NullPointerException` shown above. The same shape is reachable from the 
dispatcher itself:
   
   ```java
   new JobEventDispatcher(eventBus, jobOperationDispatcher)
       .runJob("metalake", "job_template", null);
   ```
   
   which fails while building the pre-event, before the delegate's `runJob` 
runs. The repository's own 
`core/src/test/java/org/apache/gravitino/listener/api/event/TestJobEventDispatcher.java`
 exercises this dispatcher with a non-null map only 
(`testRunJobEventMasksSensitiveJobConf`, 
`testRunJobFailureEventMasksSensitiveJobConf`), which is why the null input is 
not covered.
   
   ### Relevant code
   
   The nullable producer, `JobRuntimeTemplateMaskUtils.java:77-102`:
   
   ```java
     /**
      * Returns a copy of {@code jobConf} for run-job events. Only the Spark 
placeholder values {@code
      * updater_options} and {@code spark_conf} are masked. ...
      *
      * @param jobConf runtime configuration from the runJob caller; may be null
      * @return a masked copy, or null when {@code jobConf} is null
      */
     @Nullable
     public static Map<String, String> maskSensitiveJobConf(@Nullable 
Map<String, String> jobConf) {
       if (jobConf == null) {
         return null;
       }
       Map<String, String> masked = new LinkedHashMap<>();
       for (Map.Entry<String, String> entry : jobConf.entrySet()) {
         ...
       }
       return masked;
     }
   ```
   
   The unguarded forward, `JobEventDispatcher.java:225-244`:
   
   ```java
     public JobEntity runJob(String metalake, String jobTemplateName, 
Map<String, String> jobConf)
         throws NoSuchJobTemplateException {
       // Events carry a masked copy of the Spark updater_options / spark_conf 
JSON. Execution below
       // still uses the caller's plaintext jobConf.
       Map<String, String> maskedJobConf = 
JobRuntimeTemplateMaskUtils.maskSensitiveJobConf(jobConf);
       eventBus.dispatchEvent(
           new RunJobPreEvent(
               PrincipalUtils.getCurrentUserName(), metalake, jobTemplateName, 
maskedJobConf));
   
       try {
         JobEntity job = jobOperationDispatcher.runJob(metalake, 
jobTemplateName, jobConf);
         eventBus.dispatchEvent(
             new RunJobEvent(
                 PrincipalUtils.getCurrentUserName(),
                 metalake,
                 jobTemplateName,
                 maskedJobConf,
                 JobInfo.fromJobEntity(job)));
         return job;
   ```
   
   The non-null consumer, `RunJobPreEvent.java:43-48`:
   
   ```java
     public RunJobPreEvent(
         String user, String metalake, String jobTemplateName, Map<String, 
String> jobConf) {
       super(user, NameIdentifierUtil.ofJobTemplate(metalake, jobTemplateName));
       this.jobTemplateName = jobTemplateName;
       this.jobConf = ImmutableMap.copyOf(jobConf);
     }
   ```
   
   The guard the REST layer already applies, `JobOperations.java:435-440`:
   
   ```java
               request.validate();
               Map<String, String> jobConf =
                   request.getJobConf() != null ? request.getJobConf() : 
Collections.emptyMap();
   
               JobEntity jobEntity =
                   jobOperationDispatcher.runJob(metalake, 
request.getJobTemplateName(), jobConf);
   ```
   
   ## Root cause
   
   The commit introduced a producer whose declared contract includes `null` 
(`@Nullable` parameter, `@Nullable` return, Javadoc "may be null") and wired it 
directly into consumers that copy their argument unconditionally: 
`RunJobPreEvent` (`ImmutableMap.copyOf`, line 47) and `RunJobEvent` (line 54). 
`ImmutableMap.copyOf` dereferences the map's `entrySet`, so the null the 
producer is allowed to return is a hard failure at the event boundary. The REST 
layer anticipates the same null input and substitutes an empty map, but that 
normalization lives only in the REST handler; the dispatcher, which is also a 
public entry point of the Java surface, has no such normalization and no null 
check in either event constructor.
   
   ## Suggested fix
   
   Normalize once, at the producer, so every consumer of the masked copy sees a 
map — keep `@Nullable` on the parameter, drop it from the return, and return an 
empty map:
   
   ```java
     /**
      * @param jobConf runtime configuration from the runJob caller; may be null
      * @return a masked copy, or an empty map when {@code jobConf} is null
      */
     public static Map<String, String> maskSensitiveJobConf(@Nullable 
Map<String, String> jobConf) {
       if (jobConf == null) {
         return ImmutableMap.of();
       }
       ...
     }
   ```
   
   (`com.google.common.collect.ImmutableMap` is not imported by the file today 
— `ImmutableSet` is — so add the import, or use `Collections.emptyMap()`.)
   
   If keeping the nullable contract is intentional (for example to distinguish 
"no configuration supplied" from "empty configuration" on the event), then the 
event constructors must tolerate it:
   
   ```java
       this.jobConf = jobConf == null ? ImmutableMap.of() : 
ImmutableMap.copyOf(jobConf);
   ```
   
   in both `RunJobPreEvent` and `RunJobEvent`, and `RunJobFailureEvent` should 
expose the same normalized value instead of the raw reference.
   
   Add regression coverage next to the existing run-job event tests in 
`TestJobEventDispatcher`: call `runJob(..., null)` on a dispatcher backed by a 
mock delegate, and assert that the pre-event and post-event carry an empty 
`jobConf`, the delegate received the caller's argument unchanged, and no 
exception escapes event construction.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to