Skip to content

feat(coderd_ai_provider): delete the stored API key with an empty api_key_wo - #451

Merged
ethanndickson merged 2 commits into
mainfrom
mike/ai-provider-clear-api-key
Oct 5, 2026
Merged

ethanndickson merged 2 commits into
mainfrom
mike/ai-provider-clear-api-key

Conversation

@ibetitsmike

Copy link
Copy Markdown
Contributor

Problem

coderd_ai_provider cannot delete a stored provider API key:

  • api_key_wo = "" fails validation (string length must be at least 1).
  • Bumping api_key_wo_version without api_key_wo fails with Missing API Key.
  • Removing api_key_wo from config keeps the stored key, by design.

The only way out was replacing the resource, which deletes and recreates the provider under a new ID. That blocks switching an existing anthropic provider from api.anthropic.com to settings.claude_platform_aws in place: Coder authenticates with a stored key before falling back to SigV4, so the old api.anthropic.com key keeps being sent to Claude Platform. Replacing the provider instead also churns everything that references it, such as coderd_agents_model.ai_provider_id.

Fix

Setting api_key_wo = "" together with a new api_key_wo_version now sends api_keys: []. The Coder API deletes every stored key that is not listed in the request, so the stored key goes away. On create, an empty key sends no keys. Without a version bump nothing changes, as before.

This mirrors the existing Bedrock behavior, where empty access_key_wo and access_key_secret_wo with a credentials_wo_version bump clear stored credentials. The acceptance test gains a step that clears the OpenAI key and checks that api_key_masked is gone.

Opened by Xum on behalf of @ibetitsmike.

…_key_wo

The resource had no way to delete a stored provider key: an empty
api_key_wo was rejected, a version bump without a key errored, and
removing the attribute kept the key. Accept api_key_wo = "" with a
version bump and send an empty api_keys list, which the Coder API treats
as deleting every stored key. Mirrors how empty Bedrock credentials
clear stored credentials.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-05T01:42:44.142747Z cf621f1 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Warn when an apply deletes the stored API key, and report a missing api_key_wo on a version bump at plan time instead of apply time. Drop tests that duplicate stronger coverage.
@ethanndickson

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: cf621f18ba

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/provider/ai_provider_resource.go
@ethanndickson

Copy link
Copy Markdown
Member

@codex review

re: Skip null destroy plans before reading attributes

Not an issue. ModifyPlan reads single attributes with GetAttribute, not the whole model with req.Plan.Get. In terraform-plugin-framework v1.19.0, ValueAtPath ignores tftypes.ErrInvalidStep and returns a null value without a diagnostic (internal/fwschemadata/data_value.go:66-67), and tftypes returns that error for any null value. On destroy the planned api_key_wo_version is therefore null, and the version.IsNull() check returns before any diagnostic is added. The other resources need the Plan.Raw.IsNull guard because req.Plan.Get into a struct fails on a null object. TestAccAIProviderResource destroys this resource at the end of the test and passes on this commit.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: cf621f18ba

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ethanndickson
ethanndickson merged commit 0afa68e into main Oct 5, 2026
13 checks passed
@ethanndickson
ethanndickson deleted the mike/ai-provider-clear-api-key branch October 5, 2026 01:44
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.

2 participants