Skip to content

Hand the token to the upstream provider through credentials_file - #51

Merged
jonasz-lasut merged 2 commits into
upbound:mainfrom
blacksoxx:credentials-file
Nov 13, 2026
Merged

jonasz-lasut merged 2 commits into
upbound:mainfrom
blacksoxx:credentials-file

Conversation

@blacksoxx

Copy link
Copy Markdown
Contributor

Description of your changes

Implements the proposal in #2. The token still comes from the token key of the credentials Secret and the ProviderConfig APIs do not change; only the handoff to the in-process Terraform provider moves from the deprecated token attribute to credentials_file.

  • SetupFn writes the token in the upstream credentials file format (apikeys plus meta.current_apikey) to a 0600 file created with os.CreateTemp under the credentials directory, points credentials_file at it, configures the provider and removes the file on every path. The file only has to exist until Configure returns; the token lives in the *civogo.Client stored as provider meta after that, and upjet's SDK connector only reads Meta from the Setup.
  • New --credentials-dir flag (CREDENTIALS_DIR, default /tmp/provider-civo). At startup the provider creates it with 0700 and removes leftover civo-credentials-*.json files.
  • The provider refuses to start when CIVO_TOKEN is set, since upstream reads that variable before credentials_file.
  • README gets a "Credentials on disk" section with the emptyDir medium: Memory mount.

Fixes #2

I have:

  • Read and followed Crossplane's contribution process.
  • Run make reviewable test to ensure this PR is ready for review. (go build ./..., go test ./internal/clients/... and golangci-lint v2.15.0 over ./... are clean; make generate was not needed, no API change.)

How has this code been tested

  • internal/clients unit tests: file content matches the upstream parser's format, mode 0600, two calls with the same UID get distinct files, no file is left on the success path or on a failed Configure, the configuration carries credentials_file and no token, the startup sweep removes only civo-credentials-*.json files.
  • TestConfigure drives the real upstream Configure against an httptest server with the token coming from the file only (all CIVO_* variables cleared): the regions request carries Authorization: bearer <token> and the client gets the region and endpoint.
  • Built binary: --help shows the flag; CIVO_TOKEN=x makes it exit with the message; a stale civo-credentials-old-1.json in a custom directory is removed at startup while other files stay.
  • Not run: make e2e against a Civo account, I have no token.

Upstream deprecated the token attribute in favour of credentials_file.
The SetupFn now writes the token to a short-lived 0600 file under
--credentials-dir (default /tmp/provider-civo), points credentials_file
at it, configures the provider and removes the file. The provider
sweeps leftover files at startup and refuses to start when CIVO_TOKEN
is set, since upstream reads that before the file.

The Secret format and the ProviderConfig APIs do not change.

Signed-off-by: Youssef Omar Bouden <[email protected]>
@jonasz-lasut

Copy link
Copy Markdown
Collaborator

/test-examples="examples/cluster/vpc/v1beta1/network.yaml"

@jonasz-lasut jonasz-lasut left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hi @blacksoxx we really appreciate your contribution! I've left one small comment for your consideration and started E2E run with real credentials, which looks green. Let me know what you think about the comment in the client configure func

Comment thread internal/clients/civo.go
// file, configures a provider instance with it and removes the file again
// before returning. Nothing reads the file after Configure: the token lives
// in the Civo API client stored as provider meta from then on.
func configure(ctx context.Context, credentialsDir, uid, token string, region, apiEndpoint *string) (terraform.Setup, error) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The credentials file can outlive the "few milliseconds plus the API round trip" the README promises. It's removed only when Configure
returns, and in civo/civo v1.3.2 providerConfigure calls client.ListRegions() on a civogo client built as &http.Client{Transport: httpTransport}, with no timeout. If that request stalls, the token remains on disk for as long as the connection hangs.

A shorter context doesn't help. Upstream registers a plain ConfigureFunc, which takes no context, and ListRegions() takes none either.

The file's lifetime can be bounded without touching the call. getToken reads the file before the client is created, so the file isn't
needed once the request is in flight. A timer next to the deferred remove covers the stalled case:

defer func() { _ = os.Remove(path) }()
watchdog := time.AfterFunc(credentialsFileTTL, func() { _ = os.Remove(path) })
defer watchdog.Stop()

Something like 30 seconds for credentialsFileTTL would do. A Configure that hasn't read the file by then fails with upstream's error reading from credentials_file and is retried on the next reconcile.

Could you add the timer, a test that the file is gone after the TTL while Configure is still blocked, and change the README sentence to
state the TTL as the upper bound?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review and for running the e2e, and good catch on the stalled request. Added in the second commit, the way you sketched it: a time.AfterFunc watchdog next to the deferred remove with credentialsFileTTL at 30 seconds, stopped when Configure returns. TestConfigureStalledRemovesTheFile blocks the regions request in the test server, waits until the file is gone while Configure is still inside the call, then releases the request and checks the configure still succeeds with the token in the client. The README sentence now gives the 30 seconds as the upper bound.

The upstream provider reads the file before it validates the token with
an API call that has no timeout, so a stalled call kept the token on
disk for as long as the connection hung. A timer next to the deferred
remove deletes the file after 30 seconds; a configure call that has not
read it by then fails and is retried on the next reconcile.

Signed-off-by: Youssef Omar Bouden <[email protected]>
@jonasz-lasut

Copy link
Copy Markdown
Collaborator

/test-examples="examples/cluster/vpc/v1beta1/network.yaml"

@jonasz-lasut jonasz-lasut left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for your contribution @blacksoxx LGTM!

@jonasz-lasut
jonasz-lasut merged commit cb73e40 into upbound:main Nov 13, 2026
6 checks passed
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.

Switch provider authentication from the deprecated token attribute to credentials_file

2 participants