Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spike: Quicksight POC #98

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions controlpanel/core/auth/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"azure",
client_id=settings.AUTHLIB_OAUTH_CLIENTS["azure"]["client_id"],
# client_secret is not needed for PKCE flow
# TODO add this in?
server_metadata_url=settings.AUTHLIB_OAUTH_CLIENTS["azure"]["server_metadata_url"],
client_kwargs=settings.AUTHLIB_OAUTH_CLIENTS["azure"]["client_kwargs"],
)
Expand Down
4 changes: 2 additions & 2 deletions controlpanel/interfaces/web/auth/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class OIDCLoginRequiredMixin(LoginRequiredMixin):

def dispatch(self, request, *args, **kwargs):
if not request.user.is_authenticated:
return redirect(reverse("login"))
return redirect(reverse("login-prompt"))
if OIDCSessionValidator(request).expired():
return redirect(reverse("login"))
return redirect(reverse("login-prompt"))
return super().dispatch(request, *args, **kwargs)
14 changes: 8 additions & 6 deletions controlpanel/interfaces/web/context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@


def nav_items(request):
data_products_url = reverse("data-products")
if not request.user.is_authenticated:
return {}
quicksight_url = reverse("quicksight")
return {
"nav_items": [
{"name": "Home", "url": "/", "active": request.get_full_path() == "/"},
{
"name": "Data Products",
"url": data_products_url,
"active": request.get_full_path() == data_products_url,
"name": "Quicksight",
"url": quicksight_url,
"active": request.get_full_path() == quicksight_url,
},
]
}


def header_context(request):
is_logged_in = request.user.is_authenticated
is_logged_in = request.user and request.user.is_authenticated
return {
"header_nav_items": [
{
"name": request.user.name,
"name": request.user.name if is_logged_in else "",
"url": "",
},
{
Expand Down
26 changes: 26 additions & 0 deletions controlpanel/interfaces/web/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django import forms

from controlpanel.core.models import Datasource


class DatasourceFormView(forms.ModelForm):
class Meta:
model = Datasource
fields = ["name"]

def __init__(self, *args, **kwargs) -> None:
self.user = kwargs.pop("user")
super().__init__(*args, **kwargs)

def save(self, commit: bool = True) -> Datasource:
obj = super().save(commit=False)
obj.user = self.user
obj.save()
return obj


class DatasourceQuicksightForm(forms.ModelForm):
class Meta:
model = Datasource
fields = ["is_quicksight_enabled"]
labels = {"is_quicksight_enabled": "Enabled for use in Quicksight"}
2 changes: 1 addition & 1 deletion controlpanel/interfaces/web/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% extends "govuk_frontend_django/base.html" %}
{% load static govuk_frontend_django %}

{% block title %}Analytical Platform Dashboard{% endblock title %}
{% block title %}Analytical Platform{% endblock title %}

{% block head %}
<link href="{% static 'app.css' %}" rel="stylesheet" type="text/css" />
Expand Down
26 changes: 26 additions & 0 deletions controlpanel/interfaces/web/templates/datasources-list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% extends "base.html" %}

{% block content %}

<h1 class="govuk-heading-xl">S3 Datasources</h1>
<table class="govuk-table">
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th scope="col" class="govuk-table__header">Name</th>
<th scope="col" class="govuk-table__header">Owner</th>
</tr>
</thead>
<tbody class="govuk-table__body">
{% for datasource in datasources %}
<tr class="govuk-table__row">
<td class="govuk-table__cell"><a href="{{ datasource.get_absolute_url }}">{{ datasource.name }}</a></p></td>
<td class="govuk-table__cell"><a href="{{ datasource.get_absolute_url }}">{{ request.user.email }}</a></p></td>
</tr>
{% endfor %}

</tbody>
</table>

<br>
<p><a class="govuk-button" href="{% url 'datasources-create' %}">Create a new Datasource</a></p>
{% endblock %}
29 changes: 29 additions & 0 deletions controlpanel/interfaces/web/templates/datasources-manage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% extends "base.html" %}

{% block content %}

<h1 class="govuk-heading-xl">Manage datasource</h1>
<h2 class="govuk-heading-m">{{ form.instance.name }}</h2>


<form method="POST">
{% csrf_token %}
<div class="govuk-form-group">
<fieldset class="govuk-fieldset" aria-describedby="manage-datasource">
<div id="manage-datasource" class="govuk-hint">
Access settings
</div>
<div class="govuk-checkboxes" data-module="govuk-checkboxes">
<div class="govuk-checkboxes__item">
<input class="govuk-checkboxes__input" id="{{ form.is_quicksight_enabled.id_for_label }}" name="{{ form.is_quicksight_enabled.name }}" type="checkbox" {% if form.instance.is_quicksight_enabled %}checked{% endif%}>
<label class="govuk-label govuk-checkboxes__label" for="{{ form.is_quicksight_enabled.id_for_label }}">
{{ form.is_quicksight_enabled.label }}
</label>
</div>
</div>
</fieldset>
</div>
<button type="submit" class="govuk-button" data-module="govuk-button">Save</button>
</form>

{% endblock content %}
18 changes: 18 additions & 0 deletions controlpanel/interfaces/web/templates/datasources.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends "base.html" %}

{% block content %}

<form method="POST">
{% csrf_token %}
<div class="govuk-form-group">
<h1 class="govuk-label-wrapper">
<label class="govuk-label govuk-label--l" for="{{ form.name.id_for_label }}">
Datasource name
</label>
</h1>
<input class="govuk-input" id="{{ form.name.id_for_label }}" name="{{ form.name.name }}" type="text">
</div>
<button type="submit" class="govuk-button" data-module="govuk-button">Save</button>
</form>

{% endblock content %}
6 changes: 1 addition & 5 deletions controlpanel/interfaces/web/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% extends "base.html" %}

{% block content %}
<h1 class="govuk-heading-xl">Welcome</h1>

{% if user.is_authenticated %}
<pre>
Expand All @@ -11,12 +12,7 @@
{{ user.user_id }}
</pre>

<h1 class="govuk-heading-xl">User list</h1>
{% for user in users %}
<li>{{ user.name }}: {{ user.nickname }}, {{ user.email }} </li>
{% endfor %}
<hr>
<a href="{% url 'logout' %}">logout</a>
<br/>
{% else %}
<a href="{% url 'login' %}">login</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{% include "includes/moj_header.svg" %}
<a class="moj-header__link moj-header__link--organisation-name" href="{{ header_organisation_url }}">Ministry of Justice</a>

<a class="moj-header__link moj-header__link--service-name" href="{{ header_service_url}}">Data Platform</a>
<a class="moj-header__link moj-header__link--service-name" href="{{ header_service_url}}">Analytical Platform</a>
</div>
<div class="moj-header__content">

Expand Down
7 changes: 7 additions & 0 deletions controlpanel/interfaces/web/templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "base.html" %}

{% block content %}
<h1 class="govuk-heading-xl">Analytical Platform Dashboard</h1>

<p><a class="govuk-button" href="{% url 'login' %}">Sign in</a></p>
{% endblock content %}
59 changes: 59 additions & 0 deletions controlpanel/interfaces/web/templates/quicksight.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{% extends "base.html" %}

{% block container_class_names %}govuk-grid-column-full{% endblock container_class_names %}

{% block content %}
<body onload="embedConsole()">
<div id="experience-container"></div>
</body>

{% endblock content %}

{% block body_end %}
{{ block.super }}
<script src="https://unpkg.com/[email protected]/dist/quicksight-embedding-js-sdk.min.js"></script>
<script type="text/javascript">
const embedConsole = async() => {
const {
createEmbeddingContext,
} = QuickSightEmbedding;

const embeddingContext = await createEmbeddingContext({
onChange: (changeEvent, metadata) => {
console.log('Context received a change', changeEvent, metadata);
},
});

const frameOptions = {
url: "{{ embed_url|safe }}", // replace this value with the url generated via embedding API
container: '#experience-container',
height: "700px",
width: "100%",
onChange: (changeEvent, metadata) => {
switch (changeEvent.eventName) {
case 'FRAME_MOUNTED': {
console.log("Do something when the experience frame is mounted.");
break;
}
case 'FRAME_LOADED': {
console.log("Do something when the experience frame is loaded.");
break;
}
}
},
};

const contentOptions = {
onMessage: async (messageEvent, experienceMetadata) => {
switch (messageEvent.eventName) {
case 'ERROR_OCCURRED': {
console.log("Do something when the embedded experience fails loading.");
break;
}
}
}
};
const embeddedConsoleExperience = await embeddingContext.embedConsole(frameOptions, contentOptions);
};
</script>
{% endblock body_end %}
4 changes: 3 additions & 1 deletion controlpanel/interfaces/web/urls.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from django.urls import path

from controlpanel.interfaces.web import auth, data_products
from controlpanel.interfaces.web.views import IndexView
from controlpanel.interfaces.web.views import IndexView, LoginPromptView, QuicksightView

urlpatterns = [
path("", IndexView.as_view(), name="index"),
path("login/prompt/", LoginPromptView.as_view(), name="login-prompt"),
path("login/", auth.OIDCLoginView.as_view(), name="login"),
path("authenticate/", auth.OIDCAuthenticationView.as_view(), name="authenticate"),
path("logout/", auth.LogoutView.as_view(), name="logout"),
path("login-fail/", auth.LoginFail.as_view(), name="login-fail"),
path("data-products/", data_products.DataProductsView.as_view(), name="data-products"),
path("quicksight/", QuicksightView.as_view(), name="quicksight"),
]
56 changes: 56 additions & 0 deletions controlpanel/interfaces/web/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import os
from typing import Any

from django.views.generic import TemplateView

import boto3

from controlpanel.core.models import User
from controlpanel.interfaces.web.auth.mixins import OIDCLoginRequiredMixin

Expand All @@ -17,3 +20,56 @@ def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
}
)
return context


class LoginPromptView(TemplateView):
template_name = "login.html"


class QuicksightView(OIDCLoginRequiredMixin, TemplateView):
template_name = "quicksight.html"

def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
context = super().get_context_data(**kwargs)
qs = boto3.client("quicksight", region_name="eu-west-1")
rolename = "dev_user_michaeljcollinsuk"
session_name = "michaeljcollinsuk"
try:
response = qs.register_user(
IdentityType="IAM",
IamArn=f"arn:aws:iam::525294151996:role/{rolename}",
SessionName=session_name,
Email="[email protected]",
UserRole="AUTHOR",
AwsAccountId=os.environ.get("QUICKSIGHT_ACCOUNT_ID"),
Namespace="default",
# UserName=user_name
)
except Exception as error:
print(f"User probably already registered {error}")
pass

response = qs.generate_embed_url_for_registered_user(
**{
"AwsAccountId": os.environ.get("QUICKSIGHT_ACCOUNT_ID"),
"UserArn": f"arn:aws:quicksight:eu-west-1:525294151996:user/default/{rolename}/{session_name}", # noqa
"ExperienceConfiguration": {"QuickSightConsole": {"InitialPath": "/start"}},
}
)

context["embed_url"] = response["EmbedUrl"]
return context

def describe_user(self, qs, rolename, session_name):
return qs.describe_user(
AwsAccountId=os.environ.get("QUICKSIGHT_ACCOUNT_ID"),
Namespace="default",
UserName=f"{rolename}/{session_name}",
)

def describe_policy_assignment(self, qs, name):
return qs.describe_iam_policy_assignment(
AwsAccountId=os.environ.get("QUICKSIGHT_ACCOUNT_ID"),
Namespace="default",
AssignmentName="michael-test-1",
)
1 change: 0 additions & 1 deletion controlpanel/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@
)
AZURE_LOGOUT_URL = f"https://login.microsoftonline.com/{AZURE_TENANT_ID}/oauth2/v2.0/logout"
AZURE_CODE_CHALLENGE_METHOD = os.environ.get("AZURE_CODE_CHALLENGE_METHOD", "S256")

AUTHLIB_OAUTH_CLIENTS = {
"azure": {
"client_id": AZURE_CLIENT_ID,
Expand Down
14 changes: 14 additions & 0 deletions manifest_bucket.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"fileLocations": [
{
"URIPrefixes": [
"https://s3.amazonaws.com/michael-quicksight-embed-poc/"
]
}
],
"globalUploadSettings": {
"format": "CSV",
"delimiter": ",",
"containsHeader": "true"
}
}
14 changes: 14 additions & 0 deletions manifest_data_dir.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"fileLocations": [
{
"URIPrefixes": [
"https://s3.amazonaws.com/michael-quicksight-embed-poc/data/"
]
}
],
"globalUploadSettings": {
"format": "CSV",
"delimiter": ",",
"containsHeader": "false"
}
}
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

asgiref==3.8.1
Authlib==1.3.0
boto3==1.34.50
certifi==2024.2.2
cfgv==3.4.0
Django==4.2.11
Expand Down
Loading
Loading