Skip to content

SnowflakeHook: extract OAuth token lifecycle management into dedicated helper#68549

Merged
jason810496 merged 2 commits into
apache:mainfrom
SameerMesiah97:SnowflakeHook-Add-SnowflakeOAuthManager
Jun 24, 2026
Merged

SnowflakeHook: extract OAuth token lifecycle management into dedicated helper#68549
jason810496 merged 2 commits into
apache:mainfrom
SameerMesiah97:SnowflakeHook-Add-SnowflakeOAuthManager

Conversation

@SameerMesiah97

Copy link
Copy Markdown
Contributor

Description

This change refactors OAuth token handling in SnowflakeHook by extracting token lifecycle responsibilities into a dedicated _SnowflakeOAuthManager helper.

OAuth-specific functionality previously implemented directly in SnowflakeHook, including grant type validation, token retrieval, token caching, and token expiration tracking, has been moved into the new helper class. SnowflakeHook continues to expose the existing get_oauth_token() interface, but now delegates OAuth token management to the helper.

Rationale

SnowflakeHook previously mixed connection management responsibilities with OAuth-specific concerns. Extracting OAuth token lifecycle management into _SnowflakeOAuthManager establishes a clearer separation of concerns and provides a dedicated owner for token acquisition, refresh, validation, and cache management.

This reduces the amount of authentication-specific state maintained by SnowflakeHook, simplifies the hook implementation, and makes OAuth functionality easier to reason about and maintain independently.

Tests

  • Updated OAuth-related tests to reflect the new ownership boundaries where appropriate.

Notes

  • Updated the _get_static_conn_params() docstring to reflect the new ownership boundaries introduced by _SnowflakeOAuthManager.

Backwards Compatibility

This change is a refactor only and does not modify public APIs or runtime behavior. SnowflakeHook continues to expose the existing get_oauth_token() interface, and existing authentication flows remain unchanged.

@boring-cyborg boring-cyborg Bot added area:providers provider:snowflake Issues related to Snowflake provider labels Jun 15, 2026

@phanikumv phanikumv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

still see some reference to _get_valid_oauth_token in the test_snowflake.py, could you please correct it

management.

Move grant type validation, token requests, token caching and token
expiration tracking out of SnowflakeHook while preserving the existing
public API.

SnowflakeHook continues to expose get_oauth_token(), but now delegates
OAuth token management to the dedicated helper.
@SameerMesiah97
SameerMesiah97 force-pushed the SnowflakeHook-Add-SnowflakeOAuthManager branch from 14b9f5d to e858cbd Compare June 16, 2026 17:28
@SameerMesiah97
SameerMesiah97 force-pushed the SnowflakeHook-Add-SnowflakeOAuthManager branch from e858cbd to 4ed50ee Compare June 16, 2026 17:29
@SameerMesiah97
SameerMesiah97 requested a review from phanikumv June 16, 2026 18:31
@potiuk potiuk added the ready for maintainer review Set after triaging when all criteria pass. label Jun 17, 2026
@phanikumv
phanikumv requested a review from jason810496 June 24, 2026 11:21

@jason810496 jason810496 left a comment

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.

Thanks for the PR. Sorry for pushing back, but I'm not sure the extraction earns its keep.

_SnowflakeOAuthManager has a single consumer. SnowflakeHook creates it once, it's private, and it isn't injected or swappable, so we pay for the indirection without the usual payoffs (reuse, isolation, substitutability).

The boundary is also leaky. Every method still takes conn_config and indexes into it (account, client_id, refresh_token...), and validate_grant_type still runs in _get_conn_params then again inside get_valid_oauth_token. That duplicate validation is relocated, not fixed.

It's behavior-preserving, so nothing breaks, but it's mostly lateral churn on recently landed code (#60027), and the tests now reach into hook._oauth.validate_grant_type, coupling to internals the PR is trying to hide.

If there's a concrete second consumer or isolation benefit in mind, I'd be glad to hear it. Otherwise I'd lean toward holding off.

@SameerMesiah97

Copy link
Copy Markdown
Contributor Author

Thanks for the PR. Sorry for pushing back, but I'm not sure the extraction earns its keep.

_SnowflakeOAuthManager has a single consumer. SnowflakeHook creates it once, it's private, and it isn't injected or swappable, so we pay for the indirection without the usual payoffs (reuse, isolation, substitutability).

The boundary is also leaky. Every method still takes conn_config and indexes into it (account, client_id, refresh_token...), and validate_grant_type still runs in _get_conn_params then again inside get_valid_oauth_token. That duplicate validation is relocated, not fixed.

It's behavior-preserving, so nothing breaks, but it's mostly lateral churn on recently landed code (#60027), and the tests now reach into hook._oauth.validate_grant_type, coupling to internals the PR is trying to hide.

If there's a concrete second consumer or isolation benefit in mind, I'd be glad to hear it. Otherwise I'd lean toward holding off.

After considering your argument, I have to agree now that adding a new internal helper class is difficult to justify considering that it is only used by one other class. The motivation for this PR was to group all Oauth related functionality in one part of the hook to improve maintainability and this naturally led me to consider creating _SnowflakeOAuthManager. What are your thoughts on simply grouping all Oauth related methods in one part of the hook? I would not open a PR just for something like that but since this has already been opened, are you fine with me changing this PR to group all the Oauth methods within SnowflakeHook?

@SameerMesiah97

Copy link
Copy Markdown
Contributor Author

@jason810496

Whilst your argument is valid considering the current state of the provider, you may have to keep in mind that I am currently in process of adding a new hook for Snowflake Cortex Agents (PR #68942). Would it be better to wait until that is reviewed as SnowflakeCortexAgentHook is currently reaching into SnowflakeHook internals? Perhaps, the utility of this new internal helper would be more readily apparent when there is another hook that needs it.

@jason810496 jason810496 left a comment

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.

you may have to keep in mind that I am currently in process of adding a new hook for Snowflake Cortex Agents (PR #68942).

Then it makes sense now, I will merge this one first so your new Snowflake Cortex Agents hook can rebase on top of this and reuse the _SnowflakeOAuthManager util. Thanks.

@jason810496
jason810496 merged commit 94405c4 into apache:main Jun 24, 2026
81 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:snowflake Issues related to Snowflake provider ready for maintainer review Set after triaging when all criteria pass.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants