pulumi: Dynamic providers cannot capture secrets

Hello!

  • Vote on this issue by adding a 👍 reaction
  • To contribute a fix for this issue, leave a comment (and link to your pull request, if you’ve opened one already)

Issue details

Handling secrets in dynamic resources the same way as in the GitHub example here results in the secrets being stored in plain text in the state file due to serialisation of the provider (in the GitHub example the new Ocktokit({auth}) expressions in the dynamic provider cause the auth variable to be serialised as a string containing the auth token).

As far as I can tell, the only way to use secrets in dynamic providers is to add them as inputs (not ideal because an auth token should really be config, will fail to update an existing resource if the token is invalidated).

Relates to #4512 #2718

Steps to reproduce

  1. Follow the GitHub example steps to create a dynamic provider
  2. Run pulumi up
  3. Search the stack’s state file for the auth token’s value

Expected: no matches for the auth token in the state file Actual: auth token is found in the serialised provider string

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 17
  • Comments: 18 (10 by maintainers)

Commits related to this issue

Most upvoted comments

It seems like the cleanest solution is to allow dynamic providers to tap into secrets in the same way as other providers, e.g. cloudflare:apiToken

+1 on this, not clear what to do here. and seems like any time you’re hitting an external API (one of the obvious use-cases for dynamic providers) you’re going to need some credentials you want encrypted

Scrapped together some time to play around with function serialization and found a way to manage secrets, or any value that should not be hard-coded into the __provider property. Values that are baked during provider serialization can cause many issues when trying to work with existing resources

If you read from process.env inside your functions, they won’t be serialized when the resource is first created. Here is an example of how to avoid dynamic values ending up in your provider:

const provider: pulumi.dynamic.ResourceProvider<Inputs, Outputs> = {
  async create() {
    const mySecret = process.env['PROVIDER_SECRET']
    const endpoint = process.env['REMOTE_ENDPOINT']
    const res = await fetch(endpoint, { headers: { 'x-api-key': mySecret // ...
  }
}

Using this approach, the next time you run your program it will always use the most up-to-date values from process.env for both new and existing resources

By comparison, reading from process.env outside the serialized functions will bake in the values from process.env during serialization, which will brick existing resources when those values change

const mySecret = process.env['PROVIDER_SECRET']

class MyProvider implements pulumi.dynamic.ResourceProvider<Inputs, Outputs> {
  constructor {
    this.endpoint = process.env['REMOTE_ENDPOINT']
  }

  async create() {
    const res = await fetch(this.endpoint, { headers: { 'x-api-key': mySecret // ...
  }
}

Both of these approaches (root scope, constructor) should be avoided. in fact, after looking at the serialized output of a class, the constructor is not run at all again after serialization because the provider is serialized into an object, not a class

Going back to OPs example github provider, the solution there is to read the auth token from process.env in each of the CRUD functions

I opened a new issue for a couple of related serialization problems here - https://github.com/pulumi/pulumi/issues/15485

The combination of #13315 (Python) and #13329 (Node.js) addresses the core issue here, in that dynamic providers can now capture secrets and when they do their code is marked as a secret in the state.

This does not though allow the provider itself to receive and return secret values from the provider inputs/outputs to create/update/delete/etc. calls. That part is a fundamental limitation of the dynamic provider model today, and would require a very significant overhaul of the approach to dynamic providers. In cases where that is needed, full static providers are still the best bet. But for common cases of passing credentials into dynamic providers, the new support will ensure those can be accessed as secrets and the sensitive values consumed during deployments, without leaking the secrets into Pulumi state.

Do we just not pass and hydrate the configuration within the dynamic provider process? That wouldn’t seem terribly difficult to do unless I’m missing something?

Dynamic providers are an oddity because the provider code is part of the resource not it’s own thing. As such there is no ProviderResource that can be separately configured. I did look at changing this but it’s a large change to the interface for what dynamic providers/resources look like. So I don’t think we can do config until we do a major version change of the dynamic interfaces.

Feels like the best short term option is to just always mark __provider as secret.