How to Set Authentication For Long Running Tasks

Hi,

 

I've been implementing a bitbucket plugin in which I need to excute a long running indexing task. I've used atlassian scheduler library, registered the re-index code as a job. Unfortunately, I need to access services like RepositoryService, ProjectService etc. to execute my job. The code runs successfully when running serially, but when I run the same code as a scheduled job (or as a thread within servlet) "An Authentication object was not found in the SecurityContext" exception is thrown when I try to access methods of service classes (e.g. repositoryService.getByKey(String projectKey)). How can I bypass and / or provide authentication object to spring security context within my scheduled job ? Full exception from the debug log is pasted below. I could also provide source code if required.

-thank you very much in advance,

-yusuf

 

2018-01-22 16:15:21,521 DEBUG [http-nio-7990-exec-4] yusuft @EVDYXCx915x173x0 4uvx03 0:0:0:0:0:0:0:1 "POST /plugins/servlet/commit-linecount/admin HTTP/1.1" c.a.s.c.impl.CaesiumSchedulerService scheduleJob: CommitLineCountReIndexJob for id=1: JobConfig[jobRunnerKey=tr.gov.tubitak.bilgem.yte.bitbucket.plugin.commit.linecount.scheduler.impl.CommitLineCountJobRunnerImpl,runMode=RUN_LOCALLY,schedule=Schedule[type=INTERVAL,intervalScheduleInfo=IntervalScheduleInfo[firstRunTime=Mon Jan 22 16:15:22 EET 2018,intervalInMillis=0]],parameters={commitLineCountJobId=1, extraInfo=tr.gov.tubitak.bilgem.yte.bitbucket.plugin.commit.linecount.util.ReIndexUtils@2708f7d0}]
2018-01-22 16:15:21,528 DEBUG [Caesium-1-4]  c.a.s.c.impl.SchedulerQueueImpl take: null (loop)
2018-01-22 16:15:21,528 DEBUG [Caesium-1-1]  c.a.s.c.impl.SchedulerQueueImpl take: null (loop)
2018-01-22 16:15:21,528 DEBUG [http-nio-7990-exec-4] yusuft @EVDYXCx915x173x0 4uvx03 0:0:0:0:0:0:0:1 "POST /plugins/servlet/commit-linecount/admin HTTP/1.1" c.a.s.c.impl.SchedulerQueueImpl add job=QueuedJob[jobId=CommitLineCountReIndexJob for id=1,deadline=1516626922019] replaced=null
2018-01-22 16:15:21,528 DEBUG [Caesium-1-2]  c.a.s.c.impl.SchedulerQueueImpl take: null (loop)
2018-01-22 16:15:21,528 DEBUG [Caesium-1-3]  c.a.s.c.impl.SchedulerQueueImpl take: null (loop)
2018-01-22 16:15:21,533 DEBUG [http-nio-7990-exec-4] yusuft @EVDYXCx915x173x0 4uvx03 0:0:0:0:0:0:0:1 "POST /plugins/servlet/commit-linecount/admin HTTP/1.1" c.a.s.c.impl.CaesiumSchedulerService Enqueued job 'CommitLineCountReIndexJob for id=1' for Mon Jan 22 16:15:22 EET 2018
2018-01-22 16:15:22,020 DEBUG [Caesium-1-2]  c.a.s.c.impl.SchedulerQueueImpl take: null (loop)
2018-01-22 16:15:22,020 DEBUG [Caesium-1-1]  c.a.s.c.impl.SchedulerQueueImpl take: null (loop)
2018-01-22 16:15:22,020 DEBUG [Caesium-1-3]  c.a.s.c.impl.SchedulerQueueImpl take: null (loop)
2018-01-22 16:15:22,020 DEBUG [Caesium-1-2]  c.a.s.c.impl.SchedulerQueueImpl take: QueuedJob[jobId=CommitLineCountReIndexJob for id=1,deadline=1516626922019]
2018-01-22 16:15:22,023 DEBUG [Caesium-1-4]  c.a.s.c.impl.SchedulerQueueImpl take: null (loop)
2018-01-22 16:15:22,023 DEBUG [Caesium-1-2]  c.a.s.c.impl.SchedulerQueueImpl remove jobId=CommitLineCountReIndexJob for id=1 removed=null
2018-01-22 16:15:22,024 DEBUG [Caesium-1-2]  c.a.s.c.impl.CaesiumSchedulerService Job 'CommitLineCountReIndexJob for id=1' has a null nextRunTime, which means we never expect it to run again
2018-01-22 16:15:22,024 DEBUG [Caesium-1-2]  c.a.scheduler.core.JobLauncher launch: RUN_LOCALLY: CommitLineCountReIndexJob for id=1
2018-01-22 16:15:22,024 ERROR [Caesium-1-2]  t.g.t.b.y.b.p.c.l.s.i.CommitLineCountJobRunnerImpl Running job: CommitLineCountReIndexJob for id=1
2018-01-22 16:15:22,034 ERROR [Caesium-1-2]  c.a.scheduler.core.JobLauncher Scheduled job with ID 'CommitLineCountReIndexJob for id=1' failed
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
        at org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:379)
        at com.atlassian.plugin.util.ContextClassLoaderSettingInvocationHandler.invoke(ContextClassLoaderSettingInvocationHandler.java:26)
        at org.eclipse.gemini.blueprint.service.importer.support.internal.aop.ServiceInvoker.doInvoke(ServiceInvoker.java:56)
        at org.eclipse.gemini.blueprint.service.importer.support.internal.aop.ServiceInvoker.invoke(ServiceInvoker.java:60)
        at org.eclipse.gemini.blueprint.service.util.internal.aop.ServiceTCCLInterceptor.invokeUnprivileged(ServiceTCCLInterceptor.java:70)
        at org.eclipse.gemini.blueprint.service.util.internal.aop.ServiceTCCLInterceptor.invoke(ServiceTCCLInterceptor.java:53)
        at org.eclipse.gemini.blueprint.service.importer.support.LocalBundleContextAdvice.invoke(LocalBundleContextAdvice.java:57)
        at tr.gov.tubitak.bilgem.yte.bitbucket.plugin.commit.linecount.config.impl.SelectedProjectsCommitIndexerConfiguration.initTransientVariables(SelectedProjectsCommitIndexerConfiguration.java:77)
        at tr.gov.tubitak.bilgem.yte.bitbucket.plugin.commit.linecount.scheduler.impl.CommitLineCountJobRunnerImpl.runJob(CommitLineCountJobRunnerImpl.java:72)
        at com.atlassian.scheduler.core.JobLauncher.runJob(JobLauncher.java:153)
        at com.atlassian.scheduler.core.JobLauncher.launchAndBuildResponse(JobLauncher.java:118)
        at com.atlassian.scheduler.core.JobLauncher.launch(JobLauncher.java:97)
        at com.atlassian.scheduler.caesium.impl.CaesiumSchedulerService.launchJob(CaesiumSchedulerService.java:443)
        at com.atlassian.scheduler.caesium.impl.CaesiumSchedulerService.executeLocalJob(CaesiumSchedulerService.java:410)
        at com.atlassian.scheduler.caesium.impl.CaesiumSchedulerService.executeQueuedJob(CaesiumSchedulerService.java:388)
        at com.atlassian.scheduler.caesium.impl.CaesiumSchedulerService$1.consume(CaesiumSchedulerService.java:285)
        at com.atlassian.scheduler.caesium.impl.CaesiumSchedulerService$1.consume(CaesiumSchedulerService.java:282)
        at com.atlassian.scheduler.caesium.impl.SchedulerQueueWorker.executeJob(SchedulerQueueWorker.java:65)
        at com.atlassian.scheduler.caesium.impl.SchedulerQueueWorker.executeNextJob(SchedulerQueueWorker.java:59)
        at com.atlassian.scheduler.caesium.impl.SchedulerQueueWorker.run(SchedulerQueueWorker.java:34)
        at java.lang.Thread.run(Thread.java:745)
        ... 32 frames trimmed
2018-01-22 16:15:22,045 DEBUG [Caesium-1-2]  c.a.s.core.AbstractSchedulerService addRunDetails: jobId=CommitLineCountReIndexJob for id=1 startedAt=Mon Jan 22 16:15:22 EET 2018 runOutcome=FAILED message=AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
2018-01-22 16:15:22,045 DEBUG [Caesium-1-2]  c.a.scheduler.core.JobLauncher deleteIfRunOnce: deleting completed job: CommitLineCountReIndexJob for id=1
2018-01-22 16:15:22,051 DEBUG [Caesium-1-2]  c.a.s.c.impl.SchedulerQueueImpl remove jobId=CommitLineCountReIndexJob for id=1 removed=null
2018-01-22 16:15:22,051 DEBUG [Caesium-1-2]  c.a.s.c.impl.CaesiumSchedulerService unscheduleJob: CommitLineCountReIndexJob for id=1

1 answer

Solved by using  following code snippet..

securityService.withPermission(Permission.ADMIN,"commitLineCountReIndexJob").call(new UncheckedOperation<JobRunnerResponse>() {
@Override
public JobRunnerResponse perform() {
// perform execution here...
});

-thank you very much

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 06, 2018 in Bitbucket

Upgrade Best Practices

Hello! My name is Mark Askew and I am a Premier Support Engineer for products Bitbucket Server/Data Center, Fisheye & Crucible. Today, I want to bring the discussion that Jennifer, Matt, and ...

252 views 3 7
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you