Skip to content

Conversation

@mateoCuervo
Copy link
Contributor

@mateoCuervo mateoCuervo commented Nov 26, 2025

Ticket: https://linear.app/onkernel/issue/KERNEL-487/cli-set-api-key-auth-priority-higher-than-oauth

Tested

Current state

# Using KERNEL_API_KEY it uses my current org I called it Nov 25 ORG notice last browser name
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel browsers list
Browser ID               | Created At    | Persisten... | Profile | CDP WS URL               | Live View URL           
ca3u5hijj15ifurm3ujxztml | 2025-11-25... | 9878979987   | -       | httof idle browser in... | https://apterygial-mu...
jo2n7c4zgl70zzlnzgmy3t8h | 2025-11-25... | 1223132213   | -       | httof idle browser in... | https://apterygial-mu...
hj5tjjncxfkewvor5fsprck0 | 2025-11-25... | Nov25-Org... | -       | httof idle browser in... | https://apterygial-mu...

# Log in with and select org "Mateos org"
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel login        
 INFO  Starting Kernel authentication...
 INFO  This will open your browser to complete the OAuth flow
 INFO  Authentication URL:                                                                                                 
  http://localhost:3002/authorize?client_id=J7i8BKwyFBoyPQN3&code_challenge=XPFk4ct2f3mLWg85mgKPZ01yeFdqQnSlR98Ew2pK87g&code_challenge_method=S256&redirect_uri=http%3A%2F%2Flocalhost%3A58432%2Fcallback&response_type=code&scope=openid+email&state=eyJjc3JmIjoiSGk0dWg0c0VBMTRFVzUwY3c1NjltUnZpZkx1S0tnSUsifQ%3D%3D

 SUCCESS  Authentication successful!                                                                                       
 SUCCESS  ✓ Successfully authenticated with Kernel!
 INFO  You can now use other Kernel CLI commands without setting KERNEL_API_KEY

# Now it should browsers from my other org "Mateos org"
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel browsers list
[DEBUG] Using OAuth token authentication (token length: 918 chars)
[DEBUG] Token preview: eyJhbGciOiJSUzI1NiIs...C87PgKujVC4nYMU8zDGA
Browser ID               | Created At    | Persisten... | Profile | CDP WS URL               | Live View URL           
tmww8k86b170jh8kqxjtzhqu | 2025-11-25... | mateos-Or... | -       | httof idle browser in... | https://apterygial-mu...

After changes

# Using KERNEL_API_KEY it uses my current org I called it Nov 25 ORG notice last browser name
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel browsers list
Browser ID               | Created At        | Persistent ID   | Profile | CDP WS URL                         | Live View URL                     
ca3u5hijj15ifurm3ujxztml | 2025-11-25 13:... | 9878979987      | -       | httof idle browser instances in... | https://apterygial-multiflorous...
jo2n7c4zgl70zzlnzgmy3t8h | 2025-11-25 14:... | 1223132213      | -       | httof idle browser instances in... | https://apterygial-multiflorous...
hj5tjjncxfkewvor5fsprck0 | 2025-11-25 14:... | Nov25-OrgPer... | -       | httof idle browser instances in... | https://apterygial-multiflorous...

# Log in with and select org "Mateos org"
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel login        
 INFO  Starting Kernel authentication...
 INFO  This will open your browser to complete the OAuth flow
 INFO  Authentication URL:                                                                                                                            
  http://localhost:3002/authorize?client_id=J7i8BKwyFBoyPQN3&code_challenge=qaTOaQ1yIwkNnde8QHJ2sBT4IKqjBly0EfXQ4Gqoe2c&code_challenge_method=S256&redirect_uri=http%3A%2F%2Flocalhost%3A58432%2Fcallback&response_type=code&scope=openid+email&state=eyJjc3JmIjoiaW8xVTluSzh5a0xXR0lkSXFjdnBvb20tc09nelEyZU4ifQ%3D%3D

 SUCCESS  Authentication successful!                                                                                                                  
 SUCCESS  ✓ Successfully authenticated with Kernel!
 INFO  You can now use other Kernel CLI commands without setting KERNEL_API_KEY

# Now it should browsers from my other org "Mateos org"
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel browsers list
[DEBUG] Using OAuth token authentication (token length: 918 chars)
[DEBUG] Token preview: eyJhbGciOiJSUzI1NiIs...pm2ig2L1X7nM9EASXU7Q
Browser ID               | Created At        | Persistent ID   | Profile | CDP WS URL                         | Live View URL                     
tmww8k86b170jh8kqxjtzhqu | 2025-11-25 14:... | mateos-OrgPe... | -       | httof idle browser instances in... | https://apterygial-multiflorous...

# Now I trigger re build with my latest changes
➜  cli git:(feat-use-api-key-first) ✗ make build                
go build -o bin/kernel ./cmd/kernel

# It gives precedence to API KEY 
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel browsers list
Browser ID               | Created At        | Persistent ID   | Profile | CDP WS URL                         | Live View URL                     
ca3u5hijj15ifurm3ujxztml | 2025-11-25 13:... | 9878979987      | -       | httof idle browser instances in... | https://apterygial-multiflorous...
jo2n7c4zgl70zzlnzgmy3t8h | 2025-11-25 14:... | 1223132213      | -       | httof idle browser instances in... | https://apterygial-multiflorous...
hj5tjjncxfkewvor5fsprck0 | 2025-11-25 14:... | Nov25-OrgPer... | -       | httof idle browser instances in... | https://apterygial-multiflorous...

# I do logout
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel logout       
 INFO  Logging out...
 SUCCESS  ✓ Successfully logged out
 INFO  Run 'kernel login' to authenticate again

# Now login again to "Mateos org"
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel login 
 INFO  Starting Kernel authentication...
 INFO  This will open your browser to complete the OAuth flow
 INFO  Authentication URL:                                                                                                                                                                                   
  http://localhost:3002/authorize?client_id=J7i8BKwyFBoyPQN3&code_challenge=jQg_U5OE4av1FtB1dBwjibFnSLvSrM1jqXsl8DLa70E&code_challenge_method=S256&redirect_uri=http%3A%2F%2Flocalhost%3A58432%2Fcallback&response_type=code&scope=openid+email&state=eyJjc3JmIjoidEEwckg5UWh3cktILWFKNWpNaXctRUlWdFFyTE1ncEMifQ%3D%3D

 SUCCESS  Authentication successful!                                                                                                                                                                         
 SUCCESS  ✓ Successfully authenticated with Kernel!
 INFO  You can now use other Kernel CLI commands without setting KERNEL_API_KEY

# It still gives precedence to my org named "Nov 25" this is the one in API_KEY
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel browsers list
Browser ID               | Created At              | Persistent ID       | Profile | CDP WS URL                                         | Live View URL                                     
ca3u5hijj15ifurm3ujxztml | 2025-11-25 13:58:01 EST | 9878979987          | -       | httof idle browser instances in the pops://apte... | https://apterygial-multiflorous-magaly.ngrok-fr...
jo2n7c4zgl70zzlnzgmy3t8h | 2025-11-25 14:05:16 EST | 1223132213          | -       | httof idle browser instances in the pops://apte... | https://apterygial-multiflorous-magaly.ngrok-fr...
hj5tjjncxfkewvor5fsprck0 | 2025-11-25 14:08:57 EST | Nov25-OrgPersistent | -       | httof idle browser instances in the pops://apte... | https://apterygial-multiflorous-magaly.ngrok-fr...

@mesa-dot-dev
Copy link

mesa-dot-dev bot commented Nov 26, 2025

Mesa Description

TL;DR

Refactored authentication logic to prioritize the KERNEL_API_KEY environment variable over OAuth tokens.

Why we made these changes

To ensure that when KERNEL_API_KEY is set, it is consistently used for authentication, providing explicit control over the authentication method and predictable behavior in environments where both an API key and an OAuth token might be available.

What changed?

  • The GetAuthenticatedClient function in pkg/auth/client.go was updated.
  • It now attempts to authenticate using the KERNEL_API_KEY environment variable first.
  • If KERNEL_API_KEY is not found, it falls back to using the stored OAuth token for authentication.

Validation

Current state

# Using KERNEL_API_KEY it uses my current org I called it Nov 25 ORG notice last browser name
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel browsers list
Browser ID               | Created At    | Persisten... | Profile | CDP WS URL               | Live View URL           
ca3u5hijj15ifurm3ujxztml | 2025-11-25... | 9878979987   | -       | httof idle browser in... | https://apterygial-mu...
jo2n7c4zgl70zzlnzgmy3t8h | 2025-11-25... | 1223132213   | -       | httof idle browser in... | https://apterygial-mu...
hj5tjjncxfkewvor5fsprck0 | 2025-11-25... | Nov25-Org... | -       | httof idle browser in... | https://apterygial-mu...

# Log in with and select org "Mateos org"
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel login        
 INFO  Starting Kernel authentication...
 INFO  This will open your browser to complete the OAuth flow
 INFO  Authentication URL:                                                                                                 
  http://localhost:3002/authorize?client_id=J7i8BKwyFBoyPQN3&code_challenge=XPFk4ct2f3mLWg85mgKPZ01yeFdqQnSlR98Ew2pK87g&code_challenge_method=S256&redirect_uri=http%3A%2F%2Flocalhost%3A58432%2Fcallback&response_type=code&scope=openid+email&state=eyJjc3JmIjoiSGk0dWg0c0VBMTRFVzUwY3c1NjltUnZpZkx1S0tnSUsifQ%3D%3D

 SUCCESS  Authentication successful!                                                                                       
 SUCCESS  ✓ Successfully authenticated with Kernel!
 INFO  You can now use other Kernel CLI commands without setting KERNEL_API_KEY

# Now it should browsers from my other org "Mateos org"
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel browsers list
[DEBUG] Using OAuth token authentication (token length: 918 chars)
[DEBUG] Token preview: eyJhbGciOiJSUzI1NiIs...C87PgKujVC4nYMU8zDGA
Browser ID               | Created At    | Persisten... | Profile | CDP WS URL               | Live View URL           
tmww8k86b170jh8kqxjtzhqu | 2025-11-25... | mateos-Or... | -       | httof idle browser in... | https://apterygial-mu...

After changes

# Using KERNEL_API_KEY it uses my current org I called it Nov 25 ORG notice last browser name
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel browsers list
Browser ID               | Created At        | Persistent ID   | Profile | CDP WS URL                         | Live View URL                     
ca3u5hijj15ifurm3ujxztml | 2025-11-25 13:... | 9878979987      | -       | httof idle browser instances in... | https://apterygial-multiflorous...
jo2n7c4zgl70zzlnzgmy3t8h | 2025-11-25 14:... | 1223132213      | -       | httof idle browser instances in... | https://apterygial-multiflorous...
hj5tjjncxfkewvor5fsprck0 | 2025-11-25 14:... | Nov25-OrgPer... | -       | httof idle browser instances in... | https://apterygial-multiflorous...

# Log in with and select org "Mateos org"
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel login        
 INFO  Starting Kernel authentication...
 INFO  This will open your browser to complete the OAuth flow
 INFO  Authentication URL:                                                                                                                            
  http://localhost:3002/authorize?client_id=J7i8BKwyFBoyPQN3&code_challenge=qaTOaQ1yIwkNnde8QHJ2sBT4IKqjBly0EfXQ4Gqoe2c&code_challenge_method=S256&redirect_uri=http%3A%2F%2Flocalhost%3A58432%2Fcallback&response_type=code&scope=openid+email&state=eyJjc3JmIjoiaW8xVTluSzh5a0xXR0lkSXFjdnBvb20tc09nelEyZU4ifQ%3D%3D

 SUCCESS  Authentication successful!                                                                                                                  
 SUCCESS  ✓ Successfully authenticated with Kernel!
 INFO  You can now use other Kernel CLI commands without setting KERNEL_API_KEY

# Now it should browsers from my other org "Mateos org"
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel browsers list
[DEBUG] Using OAuth token authentication (token length: 918 chars)
[DEBUG] Token preview: eyJhbGciOiJSUzI1NiIs...pm2ig2L1X7nM9EASXU7Q
Browser ID               | Created At        | Persistent ID   | Profile | CDP WS URL                         | Live View URL                     
tmww8k86b170jh8kqxjtzhqu | 2025-11-25 14:... | mateos-OrgPe... | -       | httof idle browser instances in... | https://apterygial-multiflorous...

# Now I trigger re build with my latest changes
➜  cli git:(feat-use-api-key-first) ✗ make build                
go build -o bin/kernel ./cmd/kernel

# It gives precedence to API KEY 
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel browsers list
Browser ID               | Created At        | Persistent ID   | Profile | CDP WS URL                         | Live View URL                     
ca3u5hijj15ifurm3ujxztml | 2025-11-25 13:... | 9878979987      | -       | httof idle browser instances in... | https://apterygial-multiflorous...
jo2n7c4zgl70zzlnzgmy3t8h | 2025-11-25 14:... | 1223132213      | -       | httof idle browser instances in... | https://apterygial-multiflorous...
hj5tjjncxfkewvor5fsprck0 | 2025-11-25 14:... | Nov25-OrgPer... | -       | httof idle browser instances in... | https://apterygial-multiflorous...

# I do logout
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel logout       
 INFO  Logging out...
 SUCCESS  ✓ Successfully logged out
 INFO  Run 'kernel login' to authenticate again

# Now login again to "Mateos org"
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel login 
 INFO  Starting Kernel authentication...
 INFO  This will open your browser to complete the OAuth flow
 INFO  Authentication URL:                                                                                                                                                                                   
  http://localhost:3002/authorize?client_id=J7i8BKwyFBoyPQN3&code_challenge=jQg_U5OE4av1FtB1dBwjibFnSLvSrM1jqXsl8DLa70E&code_challenge_method=S256&redirect_uri=http%3A%2F%2Flocalhost%3A58432%2Fcallback&response_type=code&scope=openid+email&state=eyJjc3JmIjoidEEwckg5UWh3cktILWFKNWpNaXctRUlWdFFyTE1ncEMifQ%3D%3D

 SUCCESS  Authentication successful!                                                                                                                                                                         
 SUCCESS  ✓ Successfully authenticated with Kernel!
 INFO  You can now use other Kernel CLI commands without setting KERNEL_API_KEY

# It still gives precedence to my org named "Nov 25" this is the one in API_KEY
➜  cli git:(feat-use-api-key-first) ✗ ./bin/kernel browsers list
Browser ID               | Created At              | Persistent ID       | Profile | CDP WS URL                                         | Live View URL                                     
ca3u5hijj15ifurm3ujxztml | 2025-11-25 13:58:01 EST | 9878979987          | -       | httof idle browser instances in the pops://apte... | https://apterygial-multiflorous-magaly.ngrok-fr...
jo2n7c4zgl70zzlnzgmy3t8h | 2025-11-25 14:05:16 EST | 1223132213          | -       | httof idle browser instances in the pops://apte... | https://apterygial-multiflorous-magaly.ngrok-fr...
hj5tjjncxfkewvor5fsprck0 | 2025-11-25 14:08:57 EST | Nov25-OrgPersistent | -       | httof idle browser instances in the pops://apte... | https://apterygial-multiflorous-magaly.ngrok-fr...

Description generated by Mesa. Update settings

Copy link

@mesa-dot-dev mesa-dot-dev bot left a comment

Choose a reason for hiding this comment

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

Performed full review of 01dc4cd...f17b89f

Analysis

  1. Authentication priority inversion from OAuth-first to API-key-first fundamentally changes security model, with environment variables now taking precedence over user login sessions.

  2. Potential for unexpected authentication behavior where users with both OAuth login and environment variables set will silently use API keys instead of their OAuth credentials.

  3. Documentation update required to communicate this significant change to users, particularly for developers who work in both local development and CI/CD environments.

Tip

Help

Slash Commands:

  • /review - Request a full code review
  • /review latest - Review only changes since the last review
  • /describe - Generate PR description. This will update the PR body or issue comment depending on your configuration
  • /help - Get help with Mesa commands and configuration options

1 files reviewed | 0 comments | Edit Agent SettingsRead Docs

Copy link

@mesa-dot-dev mesa-dot-dev bot left a comment

Choose a reason for hiding this comment

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

Performed full review of 01dc4cd...f17b89f

Analysis

  1. The PR reverses authentication priority, making KERNEL_API_KEY the primary method over OAuth tokens, which could silently change behavior for users with both configured.

  2. By prioritizing stateless API keys over stateful OAuth tokens, the system will bypass OAuth's automatic refresh mechanism when both are present, potentially leading to unexpected authentication failures when API keys expire.

  3. This authentication strategy change lacks explicit configuration options, forcing environment-based priority that may not align with all user expectations or workflows.

  4. The change lacks sufficient documentation or user notifications to alert users about this potentially breaking change in authentication behavior.

Tip

Help

Slash Commands:

  • /review - Request a full code review
  • /review latest - Review only changes since the last review
  • /describe - Generate PR description. This will update the PR body or issue comment depending on your configuration
  • /help - Get help with Mesa commands and configuration options

1 files reviewed | 0 comments | Edit Agent SettingsRead Docs

@mateoCuervo mateoCuervo merged commit cfa1f71 into main Nov 26, 2025
2 checks passed
@mateoCuervo mateoCuervo deleted the feat-use-api-key-first branch November 26, 2025 18:29
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.

3 participants