Sfoglia il codice sorgente

AMBARI-7968. Views : Document HttpImpersonator (alejandro)

Alejandro Fernandez 10 anni fa
parent
commit
c0c7380722

+ 21 - 13
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; }
 }

+ 1 - 3
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);

+ 11 - 0
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. 

+ 0 - 12
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);
 }

+ 1 - 1
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();