Skip to content

fix(cli): add --cdf-url option to support private link base URLs#633

Merged
doctrino merged 3 commits intomainfrom
fix/private-link-base-url-support
Mar 28, 2026
Merged

fix(cli): add --cdf-url option to support private link base URLs#633
doctrino merged 3 commits intomainfrom
fix/private-link-base-url-support

Conversation

@gaetan-h
Copy link
Copy Markdown
Contributor

@gaetan-h gaetan-h commented Mar 28, 2026

Problem

The --tenant-id flow in the CLI calls CogniteClient.default_oauth_client_credentials(), which hardcodes base_url as https://{cdf_cluster}.cognitedata.com. This makes it impossible to connect to CDF via private link endpoints (e.g. https://<prefix>.plink.<cluster>.cognitedata.com), resulting in a 403 error:

Traffic from this source is forbidden for this endpoint

load_cognite_client_from_toml had the same issue — passing base_url from the toml file to default_oauth_client_credentials() caused a TypeError.

Solution

  • Add optional --cdf-url parameter to both generate() functions in the CLI. When provided, it overrides base_url in ClientConfig.
  • Fix load_cognite_client_from_toml to pop base_url from the toml content and apply it after client creation.
  • OAuth token URL and scopes continue to derive from --cdf-cluster (the public cluster name) — Azure AD resource principals are registered against the public URL, not the private link URL.

Usage with private link:

pygen generate --space my_space \
    --external-id MyModel \
    --version 1 \
    --tenant-id <tenant-id> \
    --client-id <client-id> \
    --client-secret <client-secret> \
    --cdf-cluster <cluster> \
    --cdf-url https://<prefix>.plink.<cluster>.cognitedata.com \
    --cdf-project my-project

Or via config.toml:

[cognite]
project = "my-project"
tenant_id = "<tenant-id>"
cdf_cluster = "<cluster>"
client_id = "<client-id>"
client_secret = "<client-secret>"
base_url = "https://<prefix>.plink.<cluster>.cognitedata.com"

Backward compatible: omitting --cdf-url / base_url falls back to the existing behavior.

Bump

  • Patch
  • Minor
  • Skip

Changelog

Fixed

  • Add --cdf-url CLI option and base_url toml support to allow connecting to CDF via private link endpoints.

The --tenant-id flow previously called default_oauth_client_credentials()
which hardcodes base_url as https://{cdf_cluster}.cognitedata.com, making
it impossible to use private link endpoints.

Adds an optional --cdf-url parameter to both generate() functions. When
provided, it overrides the base_url used in ClientConfig. The OAuth token
URL and scopes continue to derive from --cdf-cluster (the public cluster
name), which is correct since Azure AD does not know about private link URLs.

Backward compatible: omitting --cdf-url falls back to the previous behavior.
@gaetan-h gaetan-h requested review from a team as code owners March 28, 2026 12:42
@gemini-code-assist
Copy link
Copy Markdown

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 28, 2026

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
4947 3560 72% 60% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
cognite/pygen/_settings.py 60% 🟢
cognite/pygen/cli.py 0% 🟢
cognite/pygen/utils/cdf.py 28% 🟢
TOTAL 29% 🟢

updated for commit: ada2d57 by action🐍

…vate link

The function passed all toml fields directly to default_oauth_client_credentials()
which does not accept a base_url parameter, causing a TypeError when base_url
is set in the toml file.

Now pops base_url from the toml content before client creation and applies it
to client.config.base_url after construction. This preserves correct OAuth
scopes (derived from cdf_cluster) while routing API traffic to the private
link endpoint.
@doctrino doctrino enabled auto-merge (squash) March 28, 2026 13:09
@doctrino doctrino added the waiting-for-risk-review Waiting for a member of the risk review team to take an action label Mar 28, 2026
Copy link
Copy Markdown
Contributor

@doctrino doctrino left a comment

Choose a reason for hiding this comment

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

Good addition.

Risk reviewer, note the failing PR description is not a problem as it is fixed now. Rerunning the test will not work, as that always fetches the description of the time of the last commit.

@haakonvt
Copy link
Copy Markdown
Contributor

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a cdf_url parameter to allow overriding the CDF base URL for private link support across the CLI, settings, and TOML configuration. Feedback indicates significant code duplication in the client instantiation logic within cli.py, which also resulted in the token_url authentication flow being overlooked for the cdf_url update.

Comment on lines 188 to +198
elif tenant_id:
client = CogniteClient.default_oauth_client_credentials(
cdf_project, cdf_cluster, tenant_id, client_id, client_secret
credentials = OAuthClientCredentials.default_for_azure_ad(
tenant_id, client_id, client_secret, cdf_cluster
)
clientConfig = ClientConfig(
client_name="pygen",
project=cdf_project,
credentials=credentials,
base_url=cdf_url or f"https://{cdf_cluster}.cognitedata.com",
)
client = CogniteClient(clientConfig)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

This client creation logic is duplicated from lines 103-113. This violates the style guide's principles of maintainability and consistency. To improve this, consider extracting the logic into a helper function.

This duplication appears to have led to an issue: the token_url authentication flow (lines 173-187) doesn't use the new cdf_url parameter for the base_url. This will likely prevent private link from working with that authentication method.

A single helper function for client creation would resolve the duplication and provide a single place to fix the base_url handling for all authentication flows.

References
  1. The style guide emphasizes maintainability and consistency (lines 10-11). Code duplication, as seen here, makes the code harder to modify and extend, and can lead to inconsistencies and bugs. (link)

@haakonvt haakonvt self-assigned this Mar 28, 2026
@haakonvt haakonvt added risk-review-ongoing Risk review is in progress waiting-for-team Waiting for the submitter or reviewer of the PR to take an action and removed waiting-for-risk-review Waiting for a member of the risk review team to take an action labels Mar 28, 2026
@haakonvt
Copy link
Copy Markdown
Contributor

🦄 - 1 optional comment from Gemini. God påske! 🐣

@doctrino doctrino merged commit e639b63 into main Mar 28, 2026
16 of 18 checks passed
@doctrino doctrino deleted the fix/private-link-base-url-support branch March 28, 2026 13:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk-review-ongoing Risk review is in progress waiting-for-team Waiting for the submitter or reviewer of the PR to take an action

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants