Skip to content

DEV-3258: OIDC Token Exchange - #1278

Merged
Matt Calhoun (mcalhoun) merged 13 commits into
mainfrom
DEV-3258/oidc-token-exchange
Jun 8, 2025
Merged

Matt Calhoun (mcalhoun) merged 13 commits into
mainfrom
DEV-3258/oidc-token-exchange

Conversation

@milldr

@milldr Dan Miller (milldr) commented Jun 5, 2025 •

Copy link
Copy Markdown
Contributor

what

This pull request introduces significant enhancements to the AtmosProAPIClient to support GitHub OIDC token-based authentication, improve error handling, and add corresponding unit tests. The changes include the addition of new constants, error definitions, helper functions for OIDC token management, and updates to existing methods to handle the new authentication flow.

Authentication Enhancements:

  • Added support for GitHub OIDC token-based authentication as a fallback when the API token is not provided. (pkg/pro/api_client.go, pkg/pro/requests.go) [1] [2] [3]
  • Introduced helper functions getGitHubOIDCToken and exchangeOIDCTokenForAtmosToken to retrieve and exchange OIDC tokens for Atmos Pro tokens. (pkg/pro/api_client.go)

Error Handling Improvements:

  • Centralized error definitions for better maintainability and consistency. (pkg/pro/api_client.go)
  • Updated error messages across multiple methods to use formatted error constants. (pkg/pro/api_client.go) [1] [2] [3] [4] [5] [6] [7]

Configuration Updates:

  • Replaced os.Getenv with viper.GetString for environment variable management, enabling better configuration handling. (pkg/pro/api_client.go)
  • Added a new environment variable ATMOS_PRO_WORKSPACE_ID for OIDC authentication. (pkg/config/const.go)

Unit Tests:

  • Added comprehensive unit tests for OIDC authentication, including scenarios for successful authentication, fallback to API token, and failure cases. (pkg/pro/api_client_test.go)

Struct Additions:

  • Defined new request and response structs for OIDC token exchange (GitHubOIDCAuthRequest, GitHubOIDCAuthResponse, GitHubOIDCResponse). (pkg/pro/requests.go)

why

Currently all the Atmos Pro functionality reads the API token out of the environment. We should leave that in place, but should use that token second only if we can't exchange an OIDC token for an Atmos token.

references

Summary by CodeRabbit

  • New Features

    • Added support for authentication using GitHub OIDC tokens as a fallback when an API token is not set.
    • Updated default API base URL and endpoint to new Atmos Pro service addresses.
    • Enhanced error reporting with more detailed and consistent error messages during authentication and API interactions.
  • Bug Fixes

    • Improved handling of missing authentication credentials, providing clearer error messages when both OIDC and API tokens are unavailable.
  • Tests

    • Introduced new tests to validate OIDC and API token authentication flows, including fallback scenarios and error handling.

@github-actions github-actions Bot added the size/m label Jun 5, 2025
Comment thread pkg/pro/api_client.go Fixed
Comment thread pkg/pro/api_client.go Fixed
Comment thread pkg/pro/api_client.go Fixed
Comment thread pkg/pro/api_client.go Fixed
Comment thread pkg/pro/api_client.go Fixed
Comment thread pkg/pro/api_client.go Fixed
Comment thread pkg/pro/api_client.go Fixed
Comment thread pkg/pro/api_client.go Fixed
Comment thread pkg/pro/api_client.go Fixed
Comment thread pkg/pro/api_client.go Fixed
@codecov

codecov Bot commented Jun 5, 2025 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 50.52632% with 47 lines in your changes missing coverage. Please review.

Project coverage is 50.27%. Comparing base (4e4d050) to head (2736282).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pkg/pro/api_client.go 50.52% 36 Missing and 11 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1278      +/-   ##
==========================================
+ Coverage   49.88%   50.27%   +0.39%     
==========================================
  Files         236      236              
  Lines       25621    25694      +73     
==========================================
+ Hits        12780    12917     +137     
+ Misses      11222    11127      -95     
- Partials     1619     1650      +31     
Flag Coverage Δ
unittests 50.27% <50.52%> (+0.39%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread pkg/pro/api_client.go Fixed
Comment thread pkg/pro/api_client.go Fixed
Comment thread pkg/pro/api_client.go Fixed
@milldr Dan Miller (milldr) added the minor New features that do not break anything label Jun 6, 2025
@milldr
Dan Miller (milldr) marked this pull request as ready for review June 6, 2025 20:33
@milldr
Dan Miller (milldr) requested a review from a team as a code owner June 6, 2025 20:33
@coderabbitai

coderabbitai Bot commented Jun 6, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The changes introduce support for exchanging a GitHub Actions OIDC token for an Atmos Pro API token during authentication, falling back to the existing API token method if OIDC is unavailable. This includes new error handling, helper functions, configuration constants, request/response structs, and comprehensive tests for the new authentication flow.

Changes

File(s) Change Summary
pkg/config/const.go Added AtmosProWorkspaceIDEnvVarName constant for workspace ID environment variable; updated default URLs.
pkg/pro/api_client.go Refactored authentication: added OIDC token exchange, unified error handling, used Viper for config.
pkg/pro/api_client_test.go Added tests for OIDC authentication, fallback to API token, and failure cases.
pkg/pro/requests.go Added structs for GitHub OIDC and Atmos Pro authentication request/response payloads.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant APIClient
    participant GitHubOIDC
    participant AtmosProAPI

    User->>APIClient: NewAtmosProAPIClientFromEnv()
    APIClient->>APIClient: Check for API token in env
    alt API token not found
        APIClient->>APIClient: Check for GitHub OIDC env vars
        APIClient->>GitHubOIDC: GET OIDC token (with audience)
        GitHubOIDC-->>APIClient: OIDC token (.value)
        APIClient->>APIClient: Get workspace ID from env
        APIClient->>AtmosProAPI: POST /auth/github-oidc (token, workspaceId)
        AtmosProAPI-->>APIClient: Atmos API token
    else API token found
        APIClient->>APIClient: Use API token
    end
    APIClient-->>User: Initialized client or error
Loading

Assessment against linked issues

Objective Addressed Explanation
Implement OIDC authentication using GitHub Actions OIDC token (DEV-3258) ✅
Fallback to existing API token if OIDC is unavailable (DEV-3258) ✅
Correctly use environment variables for OIDC and workspace ID (DEV-3258) ✅
Provide error handling for both OIDC and API token authentication failures (DEV-3258) ✅

Suggested reviewers

  • osterman
  • aknysh
  • mcalhoun

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a281bdf and f35bc3d.

📒 Files selected for processing (1)
  • pkg/config/const.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • pkg/config/const.go
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: Acceptance Tests (macos-latest, macos)
  • GitHub Check: Acceptance Tests (windows-latest, windows)
  • GitHub Check: Acceptance Tests (ubuntu-latest, linux)
  • GitHub Check: [localstack] demo-localstack
  • GitHub Check: Summary
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary or @auto-summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @auto-title anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 0

🧹 Nitpick comments (2)
pkg/pro/api_client.go (2)

257-257: Use HTTP client with timeout.

Consider using an HTTP client with a timeout instead of http.DefaultClient to prevent hanging requests.

-	resp, err := http.DefaultClient.Do(req)
+	client := &http.Client{Timeout: 10 * time.Second}
+	resp, err := client.Do(req)

296-296: Use HTTP client with timeout.

For consistency and to prevent hanging requests, use an HTTP client with a timeout.

-	resp, err := http.DefaultClient.Do(req)
+	client := &http.Client{Timeout: 10 * time.Second}
+	resp, err := client.Do(req)
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8957ff6 and a281bdf.

📒 Files selected for processing (4)
  • pkg/config/const.go (1 hunks)
  • pkg/pro/api_client.go (10 hunks)
  • pkg/pro/api_client_test.go (2 hunks)
  • pkg/pro/requests.go (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
pkg/pro/api_client.go (1)
Learnt from: osterman
PR: cloudposse/atmos#1202
File: pkg/utils/yaml_func_exec.go:104-104
Timestamp: 2025-04-23T15:02:50.246Z
Learning: In the Atmos codebase, direct calls to `os.Getenv` should be avoided. Instead, use `viper.BindEnv` for environment variable access. This provides a consistent approach to configuration management across the codebase.
🪛 GitHub Check: codecov/patch
pkg/pro/api_client.go

[warning] 90-91: pkg/pro/api_client.go#L90-L91
Added lines #L90 - L91 were not covered by tests


[warning] 96-96: pkg/pro/api_client.go#L96
Added line #L96 was not covered by tests


[warning] 105-105: pkg/pro/api_client.go#L105
Added line #L105 was not covered by tests


[warning] 120-120: pkg/pro/api_client.go#L120
Added line #L120 was not covered by tests


[warning] 125-125: pkg/pro/api_client.go#L125
Added line #L125 was not covered by tests


[warning] 132-132: pkg/pro/api_client.go#L132
Added line #L132 was not covered by tests


[warning] 137-137: pkg/pro/api_client.go#L137
Added line #L137 was not covered by tests


[warning] 151-151: pkg/pro/api_client.go#L151
Added line #L151 was not covered by tests


[warning] 156-156: pkg/pro/api_client.go#L156
Added line #L156 was not covered by tests


[warning] 161-161: pkg/pro/api_client.go#L161
Added line #L161 was not covered by tests


[warning] 167-167: pkg/pro/api_client.go#L167
Added line #L167 was not covered by tests


[warning] 176-176: pkg/pro/api_client.go#L176
Added line #L176 was not covered by tests


[warning] 198-198: pkg/pro/api_client.go#L198
Added line #L198 was not covered by tests


[warning] 203-203: pkg/pro/api_client.go#L203
Added line #L203 was not covered by tests


[warning] 208-208: pkg/pro/api_client.go#L208
Added line #L208 was not covered by tests


[warning] 214-214: pkg/pro/api_client.go#L214
Added line #L214 was not covered by tests


[warning] 223-223: pkg/pro/api_client.go#L223
Added line #L223 was not covered by tests


[warning] 244-245: pkg/pro/api_client.go#L244-L245
Added lines #L244 - L245 were not covered by tests


[warning] 252-253: pkg/pro/api_client.go#L252-L253
Added lines #L252 - L253 were not covered by tests


[warning] 259-260: pkg/pro/api_client.go#L259-L260
Added lines #L259 - L260 were not covered by tests


[warning] 269-270: pkg/pro/api_client.go#L269-L270
Added lines #L269 - L270 were not covered by tests


[warning] 286-287: pkg/pro/api_client.go#L286-L287
Added lines #L286 - L287 were not covered by tests


[warning] 291-292: pkg/pro/api_client.go#L291-L292
Added lines #L291 - L292 were not covered by tests


[warning] 298-299: pkg/pro/api_client.go#L298-L299
Added lines #L298 - L299 were not covered by tests


[warning] 303-304: pkg/pro/api_client.go#L303-L304
Added lines #L303 - L304 were not covered by tests


[warning] 308-309: pkg/pro/api_client.go#L308-L309
Added lines #L308 - L309 were not covered by tests

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary
🔇 Additional comments (6)
pkg/config/const.go (1)

92-92: LGTM!

The new constant follows the established naming pattern and integrates well with the existing Atmos Pro configuration constants.

pkg/pro/requests.go (1)

9-23: Well-structured OIDC data types.

The new structs are properly defined with clear JSON tags and descriptive comments. They provide a clean interface for the OIDC token exchange flow.

pkg/pro/api_client_test.go (1)

150-266: Excellent test coverage for OIDC authentication.

The test comprehensively covers all authentication scenarios with proper environment variable management and viper configuration reset between test cases. Good use of httptest servers for mocking external dependencies.

pkg/pro/api_client.go (3)

18-42: Good error handling standardization.

The predefined error variables and format string constant improve error consistency across the package. This makes error handling more maintainable and debugging easier.


64-100: Good implementation of OIDC authentication with proper fallback.

The authentication flow correctly prioritizes API token and falls back to OIDC when needed. Excellent use of viper for environment variable access, which addresses the previous review feedback about avoiding direct os.Getenv calls.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 90-91: pkg/pro/api_client.go#L90-L91
Added lines #L90 - L91 were not covered by tests


[warning] 96-96: pkg/pro/api_client.go#L96
Added line #L96 was not covered by tests


105-105: Consistent error handling improvements.

The migration to predefined error variables and consistent error formatting enhances maintainability and debugging capabilities across all API client methods.

Also applies to: 120-121, 125-126, 132-133, 137-138, 151-152, 156-157, 161-162, 167-168, 176-177, 185-186, 198-199, 203-204, 208-209, 214-215, 223-224, 232-233

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 105-105: pkg/pro/api_client.go#L105
Added line #L105 was not covered by tests

coderabbitai[bot]
coderabbitai Bot previously approved these changes Jun 6, 2025
Comment thread pkg/config/const.go Resolved
Comment thread pkg/config/const.go Resolved
@mcalhoun
Matt Calhoun (mcalhoun) merged commit 435cefb into main Jun 8, 2025
@mcalhoun
Matt Calhoun (mcalhoun) deleted the DEV-3258/oidc-token-exchange branch June 8, 2025 16:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor New features that do not break anything

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants