Skip to content

Commit c57b407

Browse files
committed
Run Jobs and Reports as the calling User (#1495)
1 parent 8b32a34 commit c57b407

30 files changed

Lines changed: 178 additions & 182 deletions

File tree

‎core/idrepo/logic/src/main/java/org/apache/syncope/core/logic/GroupLogic.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Comparator;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.Optional;
2728
import java.util.Set;
2829
import org.apache.commons.lang3.ArrayUtils;
2930
import org.apache.syncope.common.lib.SyncopeClientException;
@@ -406,12 +407,13 @@ public ProvisioningResult<GroupTO> provision(
406407
+ "and hasRole('" + IdRepoEntitlement.TASK_EXECUTE + "')")
407408
@Transactional
408409
public ExecTO provisionMembers(final String key, final ProvisionAction action) {
409-
Group group = groupDAO.findById(key).orElseThrow(() -> new NotFoundException("Group " + key));
410+
Group group = Optional.ofNullable(groupDAO.authFind(key)).
411+
orElseThrow(() -> new NotFoundException("Group " + key));
410412

411413
Implementation jobDelegate = implementationDAO.findById(
412414
GroupMemberProvisionTaskJobDelegate.class.getSimpleName()).
413415
orElseThrow(() -> new NotFoundException(
414-
"Implementation " + GroupMemberProvisionTaskJobDelegate.class.getSimpleName()));
416+
"Implementation " + GroupMemberProvisionTaskJobDelegate.class.getSimpleName()));
415417

416418
String name = (action == ProvisionAction.DEPROVISION ? "de" : "")
417419
+ "provision members of group " + group.getName();

‎core/idrepo/logic/src/test/java/org/apache/syncope/core/logic/ReportLogicTest.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ public void executeAndExport() throws Exception {
111111
assertTrue(report.getExecutions().isEmpty());
112112

113113
JobExecutionContext ctx = mock(JobExecutionContext.class);
114-
when(ctx.getExecutor()).thenReturn("test");
115-
when(ctx.isDryRun()).thenReturn(false);
114+
when(ctx.executor()).thenReturn("test");
115+
when(ctx.dryRun()).thenReturn(false);
116116

117117
ReportJobDelegate delegate =
118118
ApplicationContextProvider.getBeanFactory().createBean(TestReportJobDelegate.class);

‎core/provisioning-api/src/main/java/org/apache/syncope/core/provisioning/api/job/JobExecutionContext.java‎

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,42 +21,19 @@
2121
import java.util.HashMap;
2222
import java.util.Map;
2323

24-
public class JobExecutionContext {
25-
26-
private final String domain;
27-
28-
private final String jobName;
29-
30-
private final String executor;
31-
32-
private final boolean dryRun;
33-
34-
private final Map<String, Object> data = new HashMap<>();
35-
36-
public JobExecutionContext(final String domain, final String jobName, final String executor, final boolean dryRun) {
37-
this.domain = domain;
38-
this.jobName = jobName;
39-
this.executor = executor;
40-
this.dryRun = dryRun;
41-
}
42-
43-
public String getDomain() {
44-
return domain;
45-
}
46-
47-
public String getJobName() {
48-
return jobName;
49-
}
50-
51-
public String getExecutor() {
52-
return executor;
53-
}
54-
55-
public boolean isDryRun() {
56-
return dryRun;
57-
}
58-
59-
public Map<String, Object> getData() {
60-
return data;
24+
public record JobExecutionContext(
25+
String domain,
26+
String jobName,
27+
String executor,
28+
boolean dryRun,
29+
Map<String, Object> data) {
30+
31+
public JobExecutionContext(
32+
final String domain,
33+
final String jobName,
34+
final String executor,
35+
final boolean dryRun) {
36+
37+
this(domain, jobName, executor, dryRun, new HashMap<>());
6138
}
6239
}

‎core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/AbstractSchedTaskJobDelegate.java‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ public abstract class AbstractSchedTaskJobDelegate<T extends SchedTask> implemen
9191

9292
protected boolean manageOperationId;
9393

94-
protected String executor;
95-
9694
protected void setStatus(final String status) {
9795
publisher.publishEvent(new JobStatusEvent(
9896
this, AuthContextUtils.getDomain(), JobNamer.getJobName(task), status));
@@ -115,15 +113,13 @@ protected void init(
115113
MDC.put(Job.OPERATION_ID, SecureRandomUtils.generateRandomUUID().toString());
116114
return true;
117115
});
118-
119-
executor = Optional.ofNullable(context.getExecutor()).orElseGet(() -> securityProperties.getAdminUser());
120116
}
121117

122118
protected TaskExec<SchedTask> initExecution() {
123119
TaskExec<SchedTask> execution = taskUtilsFactory.getInstance(taskType).newTaskExec();
124120
execution.setStart(OffsetDateTime.now());
125121
execution.setTask(task);
126-
execution.setExecutor(executor);
122+
execution.setExecutor(AuthContextUtils.getUsername());
127123

128124
return execution;
129125
}
@@ -144,7 +140,7 @@ protected void endExecution(
144140
task = taskDAO.save(task);
145141

146142
notificationManager.createTasks(
147-
executor,
143+
AuthContextUtils.getUsername(),
148144
OpEvent.CategoryType.TASK,
149145
this.getClass().getSimpleName(),
150146
null,
@@ -155,7 +151,7 @@ protected void endExecution(
155151

156152
auditManager.audit(
157153
AuthContextUtils.getDomain(),
158-
executor,
154+
AuthContextUtils.getUsername(),
159155
OpEvent.CategoryType.TASK,
160156
task.getClass().getSimpleName(),
161157
null,

‎core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/AfterHandlingJob.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static void schedule(final SyncopeTaskScheduler scheduler, final Map<Stri
5050
AfterHandlingJob.class.getSimpleName() + "_" + SecureRandomUtils.generateRandomUUID(),
5151
AuthContextUtils.getWho(),
5252
false);
53-
context.getData().putAll(jobMap);
53+
context.data().putAll(jobMap);
5454

5555
try {
5656
AfterHandlingJob job = ApplicationContextProvider.getBeanFactory().createBean(AfterHandlingJob.class);
@@ -75,14 +75,14 @@ protected JobDelegate getDelegate() {
7575
@Override
7676
protected void execute(final JobExecutionContext context) throws JobExecutionException {
7777
Optional<AfterHandlingEvent> event = Optional.ofNullable(
78-
context.getData().get(AfterHandlingEvent.JOBMAP_KEY)).map(AfterHandlingEvent.class::cast);
78+
context.data().get(AfterHandlingEvent.JOBMAP_KEY)).map(AfterHandlingEvent.class::cast);
7979
if (event.isEmpty()) {
8080
LOG.debug("No event to process, aborting");
8181
return;
8282
}
8383

8484
try {
85-
AuthContextUtils.runAsAdmin(context.getDomain(), () -> {
85+
AuthContextUtils.runAsAdmin(context.domain(), () -> {
8686
notificationManager.createTasks(event.get());
8787
auditManager.audit(event.get());
8888
});

‎core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/DefaultJobManager.java‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ protected void registerJob(
117117
final String cronExpression,
118118
final OffsetDateTime startAt) {
119119

120-
if (isRunning(context.getJobName())) {
121-
LOG.debug("Job {} already running, cancel", context.getJobName());
120+
if (isRunning(context.jobName())) {
121+
LOG.debug("Job {} already running, cancel", context.jobName());
122122
return;
123123
}
124124

@@ -175,10 +175,10 @@ protected void register(
175175
JobNamer.getJobName(task),
176176
executor,
177177
dryRun);
178-
context.getData().put(JobManager.TASK_TYPE, type);
179-
context.getData().put(JobManager.TASK_KEY, task.getKey());
180-
context.getData().put(JobManager.DELEGATE_IMPLEMENTATION, jobDelegate.getKey());
181-
context.getData().putAll(jobData);
178+
context.data().put(JobManager.TASK_TYPE, type);
179+
context.data().put(JobManager.TASK_KEY, task.getKey());
180+
context.data().put(JobManager.DELEGATE_IMPLEMENTATION, jobDelegate.getKey());
181+
context.data().putAll(jobData);
182182

183183
registerJob(
184184
context,
@@ -238,8 +238,8 @@ protected void register(
238238
JobNamer.getJobName(report),
239239
executor,
240240
dryRun);
241-
context.getData().put(JobManager.REPORT_KEY, report.getKey());
242-
context.getData().put(JobManager.DELEGATE_IMPLEMENTATION, report.getJobDelegate().getKey());
241+
context.data().put(JobManager.REPORT_KEY, report.getKey());
242+
context.data().put(JobManager.DELEGATE_IMPLEMENTATION, report.getJobDelegate().getKey());
243243

244244
registerJob(
245245
context,

‎core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/ExpiredAccessTokenCleanup.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class ExpiredAccessTokenCleanup extends AbstractSchedTaskJobDelegate<Sche
3131

3232
@Override
3333
protected String doExecute(final JobExecutionContext context) {
34-
if (!context.isDryRun()) {
34+
if (!context.dryRun()) {
3535
int deleted = accessTokenDAO.deleteExpired(OffsetDateTime.now());
3636
LOG.debug("Successfully deleted {} expired access tokens", deleted);
3737
}

‎core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/ExpiredBatchCleanup.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class ExpiredBatchCleanup extends AbstractSchedTaskJobDelegate<SchedTask>
3030

3131
@Override
3232
protected String doExecute(final JobExecutionContext context) {
33-
if (!context.isDryRun()) {
33+
if (!context.dryRun()) {
3434
long deleted = batchDAO.deleteExpired();
3535
LOG.debug("Successfully deleted {} expired batch requests", deleted);
3636
}

‎core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/GroupMemberProvisionTaskJobDelegate.java‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.syncope.core.provisioning.api.job.JobExecutionContext;
4343
import org.apache.syncope.core.provisioning.api.job.JobExecutionException;
4444
import org.apache.syncope.core.provisioning.api.job.StoppableJobDelegate;
45+
import org.apache.syncope.core.spring.security.AuthContextUtils;
4546
import org.springframework.beans.factory.annotation.Autowired;
4647
import org.springframework.data.domain.PageRequest;
4748
import org.springframework.data.domain.Sort;
@@ -89,8 +90,8 @@ public void execute(
8990
final JobExecutionContext context)
9091
throws JobExecutionException {
9192

92-
groupKey = (String) context.getData().get(GROUP_KEY_JOBDETAIL_KEY);
93-
action = (ProvisionAction) context.getData().get(ACTION_JOBDETAIL_KEY);
93+
groupKey = (String) context.data().get(GROUP_KEY_JOBDETAIL_KEY);
94+
action = (ProvisionAction) context.data().get(ACTION_JOBDETAIL_KEY);
9495

9596
super.execute(taskType, taskKey, context);
9697
}
@@ -143,9 +144,9 @@ protected String doExecute(final JobExecutionContext context) {
143144

144145
List<PropagationStatus> statuses = action == ProvisionAction.DEPROVISION
145146
? userProvisioningManager.deprovision(
146-
user.getKey(), gResources, false, executor)
147+
user.getKey(), gResources, false, AuthContextUtils.getUsername())
147148
: userProvisioningManager.provision(
148-
user.getKey(), true, null, gResources, false, executor);
149+
user.getKey(), true, null, gResources, false, AuthContextUtils.getUsername());
149150
for (PropagationStatus propagationStatus : statuses) {
150151
result.append("User ").append(user.getKey()).append('\t').
151152
append("Resource ").append(propagationStatus.getResource()).append('\t').
@@ -192,9 +193,9 @@ protected String doExecute(final JobExecutionContext context) {
192193

193194
List<PropagationStatus> statuses = action == ProvisionAction.DEPROVISION
194195
? anyObjectProvisioningManager.deprovision(
195-
anyObject.getKey(), gResources, false, executor)
196+
anyObject.getKey(), gResources, false, AuthContextUtils.getUsername())
196197
: anyObjectProvisioningManager.provision(
197-
anyObject.getKey(), gResources, false, executor);
198+
anyObject.getKey(), gResources, false, AuthContextUtils.getUsername());
198199

199200
for (PropagationStatus propagationStatus : statuses) {
200201
result.append(anyObject.getType().getKey()).append(' ').

‎core/provisioning-java/src/main/java/org/apache/syncope/core/provisioning/java/job/Job.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,25 @@ public void run() {
6262

6363
boolean locked = false;
6464
try {
65-
locked = AuthContextUtils.callAsAdmin(context.getDomain(), () -> jobStatusDAO.lock(context.getJobName()));
65+
locked = AuthContextUtils.callAsAdmin(context.domain(), () -> jobStatusDAO.lock(context.jobName()));
6666
} catch (Exception e) {
67-
LOG.debug("While attempting to lock job {}", context.getJobName(), e);
67+
LOG.debug("While attempting to lock job {}", context.jobName(), e);
6868
}
6969
if (!locked) {
70-
LOG.debug("Could not lock job {}, skipping execution", context.getJobName());
70+
LOG.debug("Could not lock job {}, skipping execution", context.jobName());
7171
return;
7272
}
7373

74-
LOG.debug("Job {} locked, starting execution", context.getJobName());
74+
LOG.debug("Job {} locked, starting execution", context.jobName());
7575

7676
try {
7777
execute(context);
7878
} catch (JobExecutionException e) {
79-
LOG.error("While executing job {}", context.getJobName(), e);
79+
LOG.error("While executing job {}", context.jobName(), e);
8080
} finally {
81-
LOG.debug("Job {} execution completed", context.getJobName());
81+
LOG.debug("Job {} execution completed", context.jobName());
8282

83-
AuthContextUtils.runAsAdmin(context.getDomain(), () -> jobStatusDAO.unlock(context.getJobName()));
83+
AuthContextUtils.runAsAdmin(context.domain(), () -> jobStatusDAO.unlock(context.jobName()));
8484
}
8585
}
8686
}

0 commit comments

Comments
 (0)