Skip to content

Commit 0392761

Browse files
committed
AuthPowerBI env vars v4 – keeping only workspace_id
1 parent 2d4134e commit 0392761

File tree

6 files changed

+38
-27
lines changed

6 files changed

+38
-27
lines changed

api/test_views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def test_authenticated_powerbi_values_when_configured(self):
8585
return_value=(expected["embed_url"], expected["report_id"], expected["embed_token"], expected["expires_at"]),
8686
) as p_info,
8787
):
88-
resp = self.client.get(self.url)
88+
# Pass report_id via query parameter per new behavior
89+
resp = self.client.get(f"{self.url}?report_id={expected['report_id']}")
8990

9091
self.assertEqual(resp.status_code, 200)
9192
data = resp.json()
@@ -95,7 +96,7 @@ def test_authenticated_powerbi_values_when_configured(self):
9596
self.assertEqual(data.get("user"), "carol")
9697
# helpers were called
9798
p_token.assert_called_once()
98-
p_info.assert_called_once()
99+
p_info.assert_called_once_with("access-token", "ws-abc", expected["report_id"])
99100

100101

101102
class SecureFileFieldTest(APITestCase):

api/views.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@
2222
from django.utils.http import url_has_allowed_host_and_scheme
2323
from django.views import View
2424
from django.views.generic.edit import FormView
25-
from drf_spectacular.utils import extend_schema, extend_schema_view
25+
from drf_spectacular.utils import (
26+
OpenApiParameter,
27+
OpenApiTypes,
28+
extend_schema,
29+
extend_schema_view,
30+
)
2631
from haystack.query import SQ, SearchQuerySet
2732
from rest_framework import authentication, permissions
2833
from rest_framework.authtoken.models import Token
@@ -1080,11 +1085,10 @@ def logout_user(request):
10801085
def _pbi_token_via_managed_identity() -> str | None:
10811086
"""
10821087
Acquire an AAD access token for Power BI using the AKS managed identity.
1083-
If POWERBI_AZURE_CLIENT_ID is provided, target that user-assigned MI.
1084-
(There is another AZURE_CLIENT_ID among env vars, that is why this distinctive name is used.)
1088+
If AZURE_CLIENT_ID is provided, target that user-assigned Managed Identity.
10851089
"""
10861090
try:
1087-
client_id = getattr(settings, "POWERBI_AZURE_CLIENT_ID", None) or os.getenv("POWERBI_AZURE_CLIENT_ID")
1091+
client_id = getattr(settings, "AZURE_CLIENT_ID", None) or os.getenv("AZURE_CLIENT_ID")
10881092
if client_id:
10891093
cred = ManagedIdentityCredential(client_id=client_id)
10901094
else:
@@ -1169,31 +1173,47 @@ class AuthPowerBI(APIView):
11691173
authentication_classes = (authentication.TokenAuthentication,) # later to SessionAuthentication
11701174
permission_classes = (permissions.IsAuthenticated,)
11711175

1176+
@extend_schema(
1177+
parameters=[
1178+
OpenApiParameter(
1179+
name="report_id",
1180+
type=OpenApiTypes.STR,
1181+
location=OpenApiParameter.QUERY,
1182+
required=False,
1183+
description=("Power BI report identifier. If omitted, the first report in the configured workspace is used."),
1184+
),
1185+
OpenApiParameter(
1186+
name="debug",
1187+
type=OpenApiTypes.BOOL,
1188+
location=OpenApiParameter.QUERY,
1189+
required=False,
1190+
description=("Enable debug-lite logging of safe token claim metadata (tid, appid, oid, aud, exp)."),
1191+
),
1192+
]
1193+
)
11721194
def get(self, request):
11731195
# Try real Power BI via managed identity
1174-
# Accept config from settings or environment for parity with diagnostics command
1196+
# Workspace can come from settings or environment; report_id must come from query param
11751197
workspace_id = getattr(settings, "POWERBI_WORKSPACE_ID", None) or os.getenv("POWERBI_WORKSPACE_ID")
1176-
# Support both POWERBI_REPORT_ID (preferred) and legacy REPORT_ID, plus env override
1177-
report_id_cfg = (
1178-
getattr(settings, "POWERBI_REPORT_ID", None) or getattr(settings, "REPORT_ID", None) or os.getenv("POWERBI_REPORT_ID")
1179-
)
1198+
# Receive report id from GET parameter only; do not use env/settings
1199+
report_id_param = request.query_params.get("report_id")
11801200
access_token = _pbi_token_via_managed_identity()
11811201

11821202
# Optional debug-lite: log selected token claims and config when requested
11831203
debug_flag = str(request.query_params.get("debug", "")).lower() in {"1", "true", "yes", "on"}
11841204
if debug_flag:
11851205
logger.info(
1186-
"AuthPowerBI debug-lite enabled: workspace_id=%s report_id_cfg=%s has_token=%s",
1206+
"AuthPowerBI debug-lite enabled: workspace_id=%s report_id_param=%s has_token=%s",
11871207
workspace_id,
1188-
report_id_cfg,
1208+
report_id_param,
11891209
bool(access_token),
11901210
)
11911211
if access_token:
11921212
_log_token_claims_safe(access_token)
11931213

11941214
if access_token and workspace_id:
11951215
try:
1196-
embed_url, report_id, embed_token, expires_at = _pbi_get_embed_info(access_token, workspace_id, report_id_cfg)
1216+
embed_url, report_id, embed_token, expires_at = _pbi_get_embed_info(access_token, workspace_id, report_id_param)
11971217
return Response(
11981218
{
11991219
"detail": "ok",

deploy/helm/ifrcgo-helm/templates/config/secret.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ stringData:
7979
OIDC_RSA_PRIVATE_KEY_BASE64_ENCODED: "{{ .Values.env.OIDC_RSA_PRIVATE_KEY_BASE64_ENCODED }}"
8080
OIDC_RSA_PUBLIC_KEY_BASE64_ENCODED: "{{ .Values.env.OIDC_RSA_PUBLIC_KEY_BASE64_ENCODED }}"
8181
RELIEF_WEB_APP_NAME: "{{ .Values.env.RELIEF_WEB_APP_NAME}}"
82-
POWERBI_AZURE_CLIENT_ID: "{{ .Values.env.POWERBI_AZURE_CLIENT_ID}}"
83-
POWERBI_REPORT_ID: "{{ .Values.env.POWERBI_REPORT_ID}}"
8482
POWERBI_WORKSPACE_ID: "{{ .Values.env.POWERBI_WORKSPACE_ID}}"
8583

8684
# Additional secrets

deploy/helm/ifrcgo-helm/values.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ env:
6666
OIDC_RSA_PRIVATE_KEY_BASE64_ENCODED:
6767
OIDC_RSA_PUBLIC_KEY_BASE64_ENCODED:
6868
RELIEF_WEB_APP_NAME: ''
69-
POWERBI_AZURE_CLIENT_ID: ''
70-
POWERBI_REPORT_ID: ''
7169
POWERBI_WORKSPACE_ID: ''
7270

7371
# NOTE: Used to pass additional configs to api/worker containers

docker-compose.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ x-server: &base_server_setup
5252
IFRC_TRANSLATION_HEADER_API_KEY: ${IFRC_TRANSLATION_HEADER_API_KEY:-}
5353
# ReliefWeb appname
5454
RELIEF_WEB_APP_NAME: ${RELIEF_WEB_APP_NAME:-}
55-
# Azure CLIENT_ID (PrincipalId) + PowerBI IDs
56-
POWERBI_AZURE_CLIENT_ID: ${POWERBI_AZURE_CLIENT_ID:-}
57-
POWERBI_REPORT_ID: ${POWERBI_REPORT_ID:-}
55+
# PowerBI
5856
POWERBI_WORKSPACE_ID: ${POWERBI_WORKSPACE_ID:-}
5957

6058
extra_hosts:

main/settings.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@
147147
AZURE_OPENAI_DEPLOYMENT_NAME=(str, None),
148148
# ReliefWeb appname
149149
RELIEF_WEB_APP_NAME=(str, None),
150-
# Azure CLIENT_ID (PrincipalId) + PowerBI IDs
151-
POWERBI_AZURE_CLIENT_ID=(str, None),
152-
POWERBI_REPORT_ID=(str, None),
150+
# PowerBI
153151
POWERBI_WORKSPACE_ID=(str, None),
154152
)
155153

@@ -884,9 +882,7 @@ def decode_base64(env_key, fallback_env_key):
884882
# ReliefWeb (for databank cronjob)
885883
RELIEF_WEB_APP_NAME = env("RELIEF_WEB_APP_NAME")
886884

887-
# Azure CLIENT_ID (PrincipalId for Power BI usage)
888-
POWERBI_AZURE_CLIENT_ID = env("POWERBI_AZURE_CLIENT_ID")
889-
POWERBI_REPORT_ID = env("POWERBI_REPORT_ID")
885+
# PowerBI
890886
POWERBI_WORKSPACE_ID = env("POWERBI_WORKSPACE_ID")
891887

892888
# Manual checks

0 commit comments

Comments
 (0)