Impersonation with webapps#

By default, webapps require users to be authenticated. For more details and options, please see Public webapps.

It is essential to consider the security and permission aspects of impersonation while using webapps. You can refer to this documentation to learn more about these aspects.

Each time a user uses a webapp, the code is executed on behalf of a user, defined by the “Run backend as” parameter configured in the webapp settings, as illustrated in Figure 1. Therefore, the actions performed by the user are attributed to the user mentioned in the “Run backend as” parameter.

Figure 1: Backend configuration.

Figure 1: Backend configuration.#

For example, if user U1 runs a webapp with the “Run backend as” parameter set to A1, the actions taken by U1 will be identified as actions taken by the user A1. If you need the actions being identified as actions taken by user U1, you must make an impersonation call.

Webapps can be accessed by any user with the necessary permissions, such as “Read dashboards” or “Read project content” after successfully logging in. You can refer to the Public webapps page if you require further information.

Impersonation usage#

Impersonation calls work on any web framework so that you can use your preferred web framework. Creating a simple webapp allows you to test, evaluate, and understand how impersonation works. When a job starts, Dataiku logs it. Then, you can observe impersonation by examining a Dataiku project’s interface and jobs section.

To test impersonation, you will create a web application that builds a dataset (i.e., starts a job). You can observe impersonation by examining a Dataiku project’s interface or the jobs section. To carry out this task, you will use the Dataiku TShirts project. To do so:

  1. On the top left corner, click + New project > Learning projects > Dataiku TShirts.

  2. On the top navigation bar, navigate to the </> > Webapp section.

  3. Click on + New Webapp and select Code Webapp.

  4. Select the library with which you want to build your webapp. For standard webapps, you can choose either Flask or FastAPI as backend framework. Examples are provided for both.

The webapp aims to build a dataset present in the project; for example, you will build the web_history_prepared dataset.

Implementation#

The code presented demonstrates how to access user information and retrieve their name. The button action will launch a build of a Dataset, using impersonation so that the Dataset build is done as the end-user.

HTML Code#
1<h1>Impersonation Demo</h1>
2
3<h2>Welcome: <span id="identified_user"></span></h2>
4
5<div class="build_dataset">
6    <form id="form-dataset" novalidate>
7        <button type="button" class="main-blue-button" id="build-button">Build the dataset</button>
8    </form>
9</div>
Javascript code#
 1/*
 2 * For more information, refer to the "Javascript API" documentation:
 3 * https://doc.dataiku.com/dss/latest/api/js/index.html
 4 */
 5
 6let buildButton = document.getElementById('build-button');
 7let identifiedUser = document.getElementById('identified_user')
 8
 9buildButton.addEventListener('click', function (event) {
10    datasetToBuild = "web_history_prepared"
11    $.get(getWebAppBackendUrl("/build_dataset"), {datasetToBuild: datasetToBuild});
12});
13
14// When loading, get the user information
15$.getJSON(getWebAppBackendUrl('/get_user_name'), function (data) {
16    identifiedUser.textContent = data;
17});
Backend code#
 1import dataiku
 2from flask import request, jsonify
 3
 4import logging
 5
 6logger = logging.getLogger(__name__)
 7
 8
 9# Example:
10# As the Python webapp backend is a Flask app, refer to the Flask
11# documentation for more information about how to adapt this
12# example to your needs.
13# From JavaScript, you can access the defined endpoints using
14# getWebAppBackendUrl('get_user_name')
15
16@app.route('/get_user_name')
17def get_user_name():
18    logger.info("In it")
19    logger.info(request)
20    # Get user information from the request
21    headers = dict(request.headers)
22    auth_info = dataiku.api_client().get_auth_info_from_browser_headers(headers)
23    return (json.dumps(auth_info.get("associatedDSSUser")))
24
25@app.route('/build_dataset')
26def build_dataset():
27    dataset = request.args.get('datasetToBuild')
28    logger.info("Impersonation begins...")
29    # Launch the build of a Dataset using impersonation.
30    # This Dataset build will be done as the end-user.
31    with dataiku.WebappImpersonationContext() as context:
32        # Each time you need to do impersonation, you need to obtain a client.
33        local_client = dataiku.api_client()
34        project = local_client.get_default_project()
35        outdataset = project.get_dataset(dataset)
36        outdataset.build()
37
38    logger.info("Impersonation ends...")
39    resp = jsonify(success=True)
40    return resp

Wrapping Up#

Congratulations! You know how to use and implement impersonation for web applications. Permissions and impersonation are critical points for web application security.