Skip to content

feat(portal): read the AI Gateway usage from ADX with a workload identity - #690

Open
Zhupku wants to merge 5 commits into
mainfrom
feat/portal-usage-adx
Open

Zhupku wants to merge 5 commits into
mainfrom
feat/portal-usage-adx

Conversation

@Zhupku

@Zhupku Zhupku commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The usage report of the Portal reads the AI Gateway usage of its cluster from the logs database in Azure Data Explorer (the proposal's phase 2). This pull request gives the Portal that access and checks it. The usage API and the pages follow in their own pull requests.

  • Cluster template. adxPortalUsage creates the managed identity <clusterName>-portal-usage. It has a federated credential for the Portal's service account cetirun-portal in platformNamespace, and the Viewer role on the logs database only. The new output adxPortalUsageClientId gives it to make deploy. The template creates it only with adxMode: create, and only while the logs database holds the data of this CetiRun cluster alone: Kusto grants a role on a whole database, and the daily usage views keep their rows longer than the request lines. Another installation can still use a database of adxMode: create, so the docs give the steps to remove the role before it does; adxPortalUsage: false does not remove it. adxPortalUsage is off by default, like adxAlerter and adxContent.
  • make deploy. The optional observability.portal block, with clientId, sets the Portal's usage values from the collection's clusterName, Kusto cluster and logs database. A value that it sets conflicts with the same value under platform.values, as for the other blocks. An infrastructure upgrade with a standalone Portal release refuses the block, which would set the values of the platform release's disabled Portal. With the Portal of the platform release, an infrastructure upgrade refuses a usage value under platform.values that differs from the block, like a complete deployment.
  • Portal chart. New values usage.enabled, usage.clusterName, usage.adx.endpoint, usage.adx.database (CetiRunLogs) and usage.adx.clientId. With usage.enabled, the chart checks them, names the identity on the ServiceAccount (azure.workload.identity/client-id), labels the Pod for the workload identity webhook, and passes the settings as USAGE_* variables. The webhook sets AZURE_CLIENT_ID only when it creates a Pod, so the Pod template also names the client ID (cetirun.microsoft.com/usage-client-id): a new client ID rolls the Portal. With the defaults, the chart renders the same objects as before, so an upgrade without the block restarts no Portal Pod.
  • Portal. A new package internal/usage, without an Azure SDK:
    • The token exchange of the service account token that the webhook projects, with the webhook's AZURE_* variables. It keeps a token until 5 minutes before it expires. An Entra error names its code, such as AADSTS700213, without the trace and correlation IDs.
    • A query client of the Kusto REST API (v2). A query takes its values as query parameters (declare query_parameters), never as text in the query. Each request is read only (request_readonly), has a server timeout of 30 seconds, and a response limit of 16 MiB. It reports the errors of a refused request, of a row, and of the end of a result.
    • The check at start, from the proposal's "Read access": one row of another cluster in the Gateway table or in the daily usage views GatewayDailyUsage and GatewayDailyUsageV2, which keep their rows longer. A fuzzy union leaves out a view that the database lacks. If the database holds AI Gateway rows of another cluster, the Portal turns the report off and logs why, with the advice to remove the identity's role. The check does not limit the identity. If it cannot read the database, it logs the cause and checks again after 30 seconds, then after a doubled wait, up to 10 minutes. An incomplete configuration turns the report off, not the Portal.
  • Docs. Observability (prerequisites, make deploy, a new section "The Portal's usage identity" with the log messages), infrastructure, platform, Helm values and the generated chart values, the platform README, the example environment file, and PRIVACY.md: the role covers every table of the logs database, so the database must hold the data of this cluster alone, also no older data of another cluster. "The Portal's usage identity" gives the steps to remove the role from the configured logs database before another cluster writes to it, and a command that shows that no role is left.

Related issue

Part of the usage report proposal (phase 2: "the identity, the template output and the make deploy input"). There is no issue.

Validation

Offline:

  • Portal: gofmt, go vet ./... and go test ./... -count=1 (Windows, Go 1.27.4, with the web/dist placeholder of portal-ci.yml); go test -race ./internal/usage/. The chart tests of internal/config, with Helm 3.22, in a Linux container (Go 1.27.4). New tests: the token exchange and its cache, the query client against recorded frames of a Kusto cluster, the check, its retries, and the wiring from the chart to the configuration.
  • scripts/tests in a Linux container with Helm 3.22 and Bicep 0.47.16: test_portal_usage_chart (new, 4), test_cluster_deploy (41), test_full_deployment (120), test_portal_chat_chart, test_portal_ownership_chart, test_portal_sandbox_storage, test_public_ingress_chart, test_skill_marketplace_platform: OK. test_infrastructure_configuration (37) with Python 3.12, which its tar filters need: OK.
  • The Portal chart with the defaults renders the same objects as on main; with usage.enabled, only the ServiceAccount annotation, the Pod label and annotation, and the four variables change.
  • Mutations: 22 of 22 in the Go code and 25 of 25 in the chart, make deploy and the template fail a test.
  • scripts/ci/validate.py python-check, license headers, site/tests (157), the generated reference tables, source-level site checks (0 content errors, no new findings in the changed lines), and bicep build of infra/cluster/main.bicep: OK.

Live, on a test cluster with CetiRun v0.2.2 and its ADX database. The Portal ran an image of this branch merged with #689 and #681, set with kubectl together with the values that the chart renders:

  • The template's identity module, with the parameters that main.bicep gives it, made the identity and its federated credential. The workload identity webhook gave the Portal Pods their AZURE_* variables, and the token exchange worked.
  • Before the role existed, each Pod logged Usage report: cannot check the logs database yet with Kusto's HTTP 403 ("not authorized to read database"). It checked again after 30 seconds and after 1 minute.
  • The ADX module of this branch then gave the identity the Viewer role on the logs database, and no role on the other databases. Kusto lists the role with the principal aadapp=<client ID>;<tenant ID>, the form of the .drop command in the docs. The check command of the docs returned that role, and no row for an unknown client ID.
  • Run read only on that database, the start check of the second review found the second test cluster in both daily usage views, which still hold 3 rows of it each. The first check, over the Gateway table only, found none, because the rows of that cluster had been deleted there.
  • That logs database also held AI Gateway rows of a second test cluster, from an earlier comparison. At its next check, each Pod logged Usage report is off, with the name of that cluster.
  • After a soft delete of the second cluster's rows from the Gateway table, the restarted Pods logged Usage report reads the AI Gateway usage from Azure Data Explorer within a second of their start.
  • No Agent Pod restarted.

Not run: make docs-check (Hugo), the image scans of CI, and a cluster template deployment with adxMode: create.

Compatibility and operational impact

  • Off by default in the template, in make deploy and in the chart. Without the observability.portal block, the Portal renders and runs as before.
  • With the block, and with a new client ID, the Portal Pods restart once, and the Portal reads Azure Data Explorer and Microsoft Entra ID over HTTPS from the platform namespace. The check is one small query at each start.
  • The identity can read every table of the logs database, also the inventory with owner aliases, emails and Entra IDs. Give the role only while the logs database holds the data of this CetiRun cluster alone, and remove it before another cluster writes to it: the Portal's check turns the report off, but does not remove the role. Phase 3 of the proposal covers shared databases.

Checklist

  • The change is focused and preserves unrelated work.
  • Tests cover the changed behavior, or I explained why no new test is necessary.
  • Documentation and examples match any public behavior changes.
  • Generated artifacts were updated from source, or none are affected.
  • Dependency changes include lockfiles, source attribution, and license information, or none are affected.
  • I examined the diff and attachments for secrets, private endpoints or identifiers, customer data, agent conversations, and internal information.
  • I followed the applicable CLA instructions, or asked a maintainer about an unclear check.
  • I identified checks that must have credentials or a live environment and did not run them without authorization.

…tity

The usage report reads the AI Gateway usage of its cluster from the logs
database. This adds the access and checks it; the usage API follows.

- Cluster template: adxPortalUsage creates <clusterName>-portal-usage with a
  federated credential for the Portal's service account and the Viewer role
  on the logs database, only with adxMode create. Kusto grants a role on a
  whole database, and only a database that the template makes for this
  cluster holds its rows alone. Output adxPortalUsageClientId.
- make deploy: the optional observability.portal block sets the Portal's
  usage values from the collection's cluster name, Kusto cluster and logs
  database.
- Portal chart: usage.* values, checked; the identity's client ID on the
  ServiceAccount and the workload identity label on the Pod. The defaults
  render the same objects as before.
- Portal: a token exchange of the projected service account token and a
  Kusto query client, without an Azure SDK. Queries take their values as
  query parameters. At start, the Portal looks for AI Gateway rows of another
  cluster in the database and turns the report off if it finds one; when it
  cannot read the database, it checks again after a growing delay.
- Docs: observability, infrastructure, platform, Helm values, PRIVACY.md.
@Zhupku Zhupku self-assigned this Nov 27, 2026
Comment thread scripts/cetirun_platform_deploy.py Resolved
…s Viewer role

Review of #690: adxMode create says who made the logs database, not who writes to it. Another installation can use the same database, and the Viewer role then reads its logs too, even when the Portal turns the report off. The docs, the template parameters, the chart values and the Portal's log now state that no other CetiRun cluster may write to the logs database while the identity has the role, and give the steps to remove the role before one does.
…fuse the portal block with a standalone Portal

The workload identity webhook sets AZURE_CLIENT_ID only when it creates a Pod, so a new client ID on the ServiceAccount reached no running Portal: the Pod template now names the client ID. An infrastructure upgrade with a standalone Portal release set the usage values of the platform release's disabled Portal; it now refuses observability.portal there, and still sets the values of the Portal of the platform release.
the `Viewer` role, the identity can read every table of the logs database.
These tables include the platform logs, the ingress access log, and the
inventory. So the role has a prerequisite: no other CetiRun cluster writes to
the logs database while the identity has the role. `adxMode: create` makes the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make the prerequisite about retained data as well as active writers. Daily usage can outlive the raw Gateway rows, so a database that no other cluster writes to today can still contain its usage. Use a fresh dedicated database if its history is unknown.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, agreed; fixed in 6e93e39. The prerequisite is now that the logs database holds the data of this CetiRun cluster alone: no other cluster writes to it while the identity has the role, and it keeps no data that another cluster wrote earlier. The docs name the daily usage views, which keep their rows for 400 days by default, longer than the request lines, and say to use a new logs database when its history is unknown. The same condition is in the template parameters, the chart value, the example environment file, and PRIVACY.md.

The Portal's start check now also reads GatewayDailyUsage and GatewayDailyUsageV2, in a fuzzy union, so a database without one of the views still gets checked. On the test cluster, this matters: the request lines of a second cluster were deleted from Gateway earlier, so the old check passed, but each daily view still holds 3 rows of that cluster. The new query finds that cluster there (a read-only run).

database, run:

```kusto
.drop database CetiRunLogs viewers ('aadapp=<portal-usage-client-id>;<tenant-id>')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This command targets CetiRunLogs even when observability.adx.databases.logs selects a different database. Use the configured logs database here and verify that the identity no longer has Viewer there before sharing it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed in 6e93e39. The steps now use <logs-database>, the database of observability.adx.databases.logs (by default CetiRunLogs), and end with a check that must return no rows:

.show database <logs-database> principals
| where PrincipalFQN startswith "aadapp=<portal-usage-client-id>"

On the test cluster, this command returned the identity's Viewer row, and no row for an unknown client ID.

if not re.fullmatch(GUID, portal_client):
raise ValueError("observability.portal.clientId must be the client ID of the managed identity with which the "
"Portal reads the usage of this cluster")
values.update({"cetirun-portal.usage.enabled": True, "cetirun-portal.usage.clusterName": cluster,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On an infrastructure upgrade with the bundled Portal, this mapping silently overrides an explicit usage.enabled: false or a different client ID in platform.values. The complete deployer rejects the same conflict. Check the explicitly supplied values before applying this mapping so the two deployment paths agree.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed in 7cf650f. The infrastructure deployer now checks each usage value of observability.portal against platform.values before it applies it, like the complete deployer: an explicit usage.enabled: false or another client ID fails with "platform.values.cetirun-portal.usage. conflicts with observability.portal". Unset values and values that agree pass.

test_infrastructure_configuration covers both conflicts and both passing cases. Mutations that drop the check, compare with the chart defaults, or check every value of the block fail it. I kept the check to the usage values: the collection, alerter and content values keep their current behavior in this path, because an existing environment may set them in both places today.

@AaronLiChen
Li Chen (AaronLiChen) dismissed their stale review November 27, 2026 06:24

Withdraw approval pending repair of the deployment-input conflict in discussion_r4716198773.

…e with data of another cluster

Review of #690: the daily usage views keep their rows longer than the raw Gateway rows, so a logs database that no other cluster writes to today can still hold the usage of one, and the removal command named CetiRunLogs although observability.adx.databases.logs can select another database. The prerequisite now covers retained data (use a new logs database when its history is unknown), the removal names the configured database and ends with a check that the identity has no role left, and the Portal's start check also reads GatewayDailyUsage and GatewayDailyUsageV2 (a union that leaves out a missing view).
…nst platform.values

Review of #690: with the Portal of the platform release, an infrastructure upgrade let observability.portal override an explicit usage value under platform.values, such as usage.enabled false or another client ID, which a complete deployment refuses. It now refuses such a conflict the same way. The other blocks keep their current behavior in this path.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants