diff --git a/api/src/com/cloud/user/UserContext.java b/api/src/com/cloud/user/UserContext.java index c4ec8ef7d93..17df27ba20d 100644 --- a/api/src/com/cloud/user/UserContext.java +++ b/api/src/com/cloud/user/UserContext.java @@ -20,7 +20,6 @@ package com.cloud.user; import com.cloud.server.ManagementService; import com.cloud.utils.component.ComponentLocator; - public class UserContext { @@ -81,11 +80,18 @@ public class UserContext { } public static UserContext current() { - UserContext context = s_currentContext.get(); - if (context == null) { + UserContext context = s_currentContext.get(); + if(context == null) { + // + // TODO: we should enforce explicit UserContext setup at major entry-points for security concerns, + // however, there are many places that run background jobs assume the system context. + // + // If there is a security concern, all entry points from user (including the front end that takes HTTP request in and + // the core async-job manager that runs commands from user) have explicitly setup the UserContext. + // return s_adminContext; - } - return context; + } + return context; } public static void updateContext(long userId, Account accountObject, String sessionId) { @@ -101,8 +107,8 @@ public class UserContext { s_currentContext.set(new UserContext(userId, accountObject, sessionId, apiServer)); } - public static void unregisterContext() { - s_currentContext.set(null); + public static void unregisterContext() { + s_currentContext.set(null); } public void setStartEventId(long startEventId) { @@ -120,5 +126,4 @@ public class UserContext { public void setAccountId(long accountId) { this.accountId = accountId; } - } diff --git a/server/src/com/cloud/async/AsyncJobManagerImpl.java b/server/src/com/cloud/async/AsyncJobManagerImpl.java index 3d075d3b524..6eea55dd2ed 100644 --- a/server/src/com/cloud/async/AsyncJobManagerImpl.java +++ b/server/src/com/cloud/async/AsyncJobManagerImpl.java @@ -380,12 +380,15 @@ public class AsyncJobManagerImpl implements AsyncJobManager, ClusterManagerListe } UserContext.registerContext(userId, accountObject, null, false); - - // dispatch could ultimately queue the job - _dispatcher.dispatch(cmdObj, params); - - // serialize this to the async job table - completeAsyncJob(jobId, AsyncJobResult.STATUS_SUCCEEDED, 0, cmdObj.getResponseObject()); + try { + // dispatch could ultimately queue the job + _dispatcher.dispatch(cmdObj, params); + + // serialize this to the async job table + completeAsyncJob(jobId, AsyncJobResult.STATUS_SUCCEEDED, 0, cmdObj.getResponseObject()); + } finally { + UserContext.unregisterContext(); + } // commands might need to be queued as part of synchronization here, so they just have to be re-dispatched from the queue mechanism... if (job.getSyncSource() != null) {