Repository navigation
Hand the token to the upstream provider through credentials_file - #51
Conversation
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]>
|
/test-examples="examples/cluster/vpc/v1beta1/network.yaml" |
jonasz-lasut
left a comment
There was a problem hiding this comment.
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
| // 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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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]>
|
/test-examples="examples/cluster/vpc/v1beta1/network.yaml" |
jonasz-lasut
left a comment
There was a problem hiding this comment.
Thank you for your contribution @blacksoxx LGTM!
Description of your changes
Implements the proposal in #2. The token still comes from the
tokenkey of the credentials Secret and the ProviderConfig APIs do not change; only the handoff to the in-process Terraform provider moves from the deprecatedtokenattribute tocredentials_file.SetupFnwrites the token in the upstream credentials file format (apikeysplusmeta.current_apikey) to a0600file created withos.CreateTempunder the credentials directory, pointscredentials_fileat it, configures the provider and removes the file on every path. The file only has to exist untilConfigurereturns; the token lives in the*civogo.Clientstored as provider meta after that, and upjet's SDK connector only readsMetafrom the Setup.--credentials-dirflag (CREDENTIALS_DIR, default/tmp/provider-civo). At startup the provider creates it with0700and removes leftovercivo-credentials-*.jsonfiles.CIVO_TOKENis set, since upstream reads that variable beforecredentials_file.emptyDirmedium: Memorymount.Fixes #2
I have:
make reviewable testto ensure this PR is ready for review. (go build ./...,go test ./internal/clients/...and golangci-lint v2.15.0 over./...are clean;make generatewas not needed, no API change.)How has this code been tested
internal/clientsunit tests: file content matches the upstream parser's format, mode0600, two calls with the same UID get distinct files, no file is left on the success path or on a failedConfigure, the configuration carriescredentials_fileand notoken, the startup sweep removes onlycivo-credentials-*.jsonfiles.TestConfiguredrives the real upstreamConfigureagainst anhttptestserver with the token coming from the file only (allCIVO_*variables cleared): the regions request carriesAuthorization: bearer <token>and the client gets the region and endpoint.--helpshows the flag;CIVO_TOKEN=xmakes it exit with the message; a stalecivo-credentials-old-1.jsonin a custom directory is removed at startup while other files stay.make e2eagainst a Civo account, I have no token.