Repository: ambari Updated Branches: refs/heads/trunk e0b17bf68 -> c0c738072
AMBARI-7968. Views : Document HttpImpersonator (alejandro) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/c0c73807 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/c0c73807 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/c0c73807 Branch: refs/heads/trunk Commit: c0c738072218a74dd83554f5b0c86b742280091f Parents: e0b17bf Author: Alejandro Fernandez <afernan...@hortonworks.com> Authored: Fri Oct 24 17:09:18 2014 -0700 Committer: Alejandro Fernandez <afernan...@hortonworks.com> Committed: Mon Oct 27 12:36:30 2014 -0700 ---------------------------------------------------------------------- .../server/view/ImpersonatorSettingImpl.java | 34 ++++++++++++-------- .../server/view/HttpImpersonatorImplTest.java | 4 +-- ambari-views/docs/index.md | 11 +++++++ .../apache/ambari/view/ImpersonatorSetting.java | 12 ------- .../apache/ambari/view/jobs/ProxyServlet.java | 2 +- 5 files changed, 34 insertions(+), 29 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/c0c73807/ambari-server/src/main/java/org/apache/ambari/server/view/ImpersonatorSettingImpl.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/view/ImpersonatorSettingImpl.java b/ambari-server/src/main/java/org/apache/ambari/server/view/ImpersonatorSettingImpl.java index 1f9c2b2..bb75533 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/view/ImpersonatorSettingImpl.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/view/ImpersonatorSettingImpl.java @@ -29,35 +29,43 @@ public class ImpersonatorSettingImpl implements ImpersonatorSetting { public static final String DEFAULT_DO_AS_PARAM = "doAs"; + /** + * This constructor will use DEFAULT_DO_AS_PARAM and currently logged on user. + * @param context View context object that will provide the currently logged on user. + */ public ImpersonatorSettingImpl(ViewContext context) { // Default values - this.doAsParamName = DEFAULT_DO_AS_PARAM; this.username = context.getUsername(); + this.doAsParamName = DEFAULT_DO_AS_PARAM; } /** - * @return The parameter name used for "doAs" impersonation. + * @param context View context object that will provide the currently logged on user. + * @param doAsParamName The parameter name used for "doAs" impersonation. */ - @Override - public String getDoAsParamName() { return this.doAsParamName; } + public ImpersonatorSettingImpl(ViewContext context, String doAsParamName) { + this.username = context.getUsername(); + this.doAsParamName = doAsParamName; + } /** - * @return The username value that will be used for "doAs" impersonation. + * @param username Username value that will be used for "doAs" impersonation. + * @param doAsParamName The parameter name used for "doAs" impersonation. */ - @Override - public String getUsername() { return this.username; } + public ImpersonatorSettingImpl(String username, String doAsParamName) { + this.username = username; + this.doAsParamName = doAsParamName; + } /** - * Set the parameter name used for "doAs" impersonation. - * @param doAsParamName Query parameter name + * @return The parameter name used for "doAs" impersonation. */ @Override - public void setDoAsParamName(String doAsParamName) { this.doAsParamName = doAsParamName; } + public String getDoAsParamName() { return this.doAsParamName; } /** - * Set the username value that will be used for "doAs" impersonation. - * @param username Username to impersonate as + * @return The username value that will be used for "doAs" impersonation. */ @Override - public void setUsername(String username) { this.username = username; } + public String getUsername() { return this.username; } } http://git-wip-us.apache.org/repos/asf/ambari/blob/c0c73807/ambari-server/src/test/java/org/apache/ambari/server/view/HttpImpersonatorImplTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/view/HttpImpersonatorImplTest.java b/ambari-server/src/test/java/org/apache/ambari/server/view/HttpImpersonatorImplTest.java index 5526592..5a86c23 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/view/HttpImpersonatorImplTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/view/HttpImpersonatorImplTest.java @@ -103,9 +103,7 @@ public class HttpImpersonatorImplTest extends TestCase { String requestMethod = "GET"; // Test custom params - ImpersonatorSetting impersonatorSetting = new ImpersonatorSettingImpl(this.viewContext); - impersonatorSetting.setDoAsParamName("impersonate"); - impersonatorSetting.setUsername("hive"); + ImpersonatorSetting impersonatorSetting = new ImpersonatorSettingImpl("hive", "impersonate"); when(this.viewContext.getImpersonatorSetting()).thenReturn(impersonatorSetting); String actualResult = this.viewContext.getHttpImpersonator().requestURL(urlToRead, requestMethod, impersonatorSetting); Assert.assertEquals(this.expectedResult, actualResult); http://git-wip-us.apache.org/repos/asf/ambari/blob/c0c73807/ambari-views/docs/index.md ---------------------------------------------------------------------- diff --git a/ambari-views/docs/index.md b/ambari-views/docs/index.md index 4897aca..18f1fb3 100644 --- a/ambari-views/docs/index.md +++ b/ambari-views/docs/index.md @@ -428,6 +428,17 @@ The view implementation code can use the view context to check custom permission return Response.ok("<b>You have accessed a restricted resource.</b>").type("text/html").build(); } +###Impersonation + +Views can utilize the viewContext to facilitate calls that require impersonating a user. For example, a service may expose an endpoint that accepts parameters like "doAs=johndoe" to perform some action on behalf of that user. +The HttpImpersonator Interface provides a contract for how to perform an HTTP GET request on a URL that supports some type of "doAs" parameter, and the username. + + HttpImpersonator impersonator = viewContext.getHttpImpersonator(); + ImpersonatorSetting impersonatorSetting = viewContext.getImpersonatorSetting(); + String result = impersonator.requestURL(urlToRead, "GET", impersonatorSetting); + +The ImpersonatorSetting class contains the variables that are added to the URL params. Its default constructor sets "doAs" as the default query parameter name, and the currently logged on user as its value; both of these can be changed with the overloaded constructors. + ###Persistence The application data map is different than the view instance properties that are specified to instantiate the view. The application data map may contain any arbitrary string values that the view needs to persist and may be updated or queried at any point during the life of the view instance by the application logic. http://git-wip-us.apache.org/repos/asf/ambari/blob/c0c73807/ambari-views/src/main/java/org/apache/ambari/view/ImpersonatorSetting.java ---------------------------------------------------------------------- diff --git a/ambari-views/src/main/java/org/apache/ambari/view/ImpersonatorSetting.java b/ambari-views/src/main/java/org/apache/ambari/view/ImpersonatorSetting.java index b52cc90..53f6189 100644 --- a/ambari-views/src/main/java/org/apache/ambari/view/ImpersonatorSetting.java +++ b/ambari-views/src/main/java/org/apache/ambari/view/ImpersonatorSetting.java @@ -31,16 +31,4 @@ public interface ImpersonatorSetting { * @return The username value that will be used for "doAs" impersonation. */ public String getUsername(); - - /** - * Set the parameter name used for "doAs" impersonation. - * @param doAsParamName Query parameter name - */ - public void setDoAsParamName(String doAsParamName); - - /** - * Set the username value that will be used for "doAs" impersonation. - * @param username Username to impersonate as - */ - public void setUsername(String username); } http://git-wip-us.apache.org/repos/asf/ambari/blob/c0c73807/contrib/views/jobs/src/main/java/org/apache/ambari/view/jobs/ProxyServlet.java ---------------------------------------------------------------------- diff --git a/contrib/views/jobs/src/main/java/org/apache/ambari/view/jobs/ProxyServlet.java b/contrib/views/jobs/src/main/java/org/apache/ambari/view/jobs/ProxyServlet.java index f02844e..3336004 100644 --- a/contrib/views/jobs/src/main/java/org/apache/ambari/view/jobs/ProxyServlet.java +++ b/contrib/views/jobs/src/main/java/org/apache/ambari/view/jobs/ProxyServlet.java @@ -58,7 +58,7 @@ public class ProxyServlet extends HttpServlet { response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); - // Getting the result is super simply by using the impersonator and the default values in the factory. + // Getting the result is super simple by using the impersonator and the default values in the factory. String result = this.impersonator.requestURL(urlToRead, "GET", this.impersonatorSetting); PrintWriter writer = response.getWriter();