Skip to content

Proposal: in-memory secret store for dynamic secrets #3442

Closed as not planned
Closed as not planned

Description

@jlongtine

I need the ability to create new Secrets in the v0.3 API for dagger-classic.

I suggest this change (or similar) to the API:

query {
  secret { // make the id: SecretID optional
    new(contents: String!) Secret!
  }
}

Another possibility:

query {
  secret(contents: String) Secret! // make the id: SecretID optional
}

Activity

  1. jlongtine commented on Oct 26, 2022

    @jlongtine
    ContributorAuthor

    @vito mentioned #3124

  2. samalba commented on Nov 2, 2022

    @samalba
    Contributor

    Discussed this briefly with @aluzzardi today to gather context and unblock @jlongtine.

    Looks like there are two options:

    • Option 1: (long-term), bigger effort: some sort of secret extension API to plug things like Vault, etc... as I understand that the ability to add secrets from the API (like in 0.2) is not gonna happen (for valid reasons). Also this API needs to be designed, reviewed and implemented. We know what it takes.
    • Option 2: (short-term): have a private API (under engine, hidden from the Go SDK) to allow for creating secrets. This will be intended to disappear (risk to leak, and to stay forever).

    I guess option 2 is more viable to unblock dagger-cue, depending on the effort for option 1. If we go option 2, we should find a way to derisk, for instance hiding the call behind a feature flag (eg. if this weird env var isn't set, the code path is disabled).

    Would it be possible for @sipsma or @vito to own this one? Note that I am not hinting any option here, the priority is to remove the blocker. There might be an option 3 that I don't see.

  3. sipsma commented on Nov 2, 2022

    @sipsma
    Contributor

    @jlongtine how absurd and awful of an idea is this: you currently can load secrets from env vars, so whenever you need to load a secret in dagger-cue you export an env var and then use that to load the secret in? Would need to be careful if dagger-cue ever forks off child processes of course. But could be a path to avoiding any changes to the API if it's plausible.

  4. shykes commented on Nov 2, 2022

    @shykes
    Contributor

    Maybe we should revisit the “no direct creation of secrets” rule, in case the circumstances have changed?

    The initial motivation for “no direct creation” was to:

    1. avoid leaking plaintext secrets into the “id” (serialized recipe). Because IDs are typically understood to be safe to copy paste, share etc.
    2. avoid sending plaintext secrets on the wire

    One major change is that we plan on making the engine remote; and our understanding of IDs and their role has evolved. So maybe it’s OK to create secrets directly after all?

    TLDR my first guess would be either option 2 (hidden api call) or “option 2+” (regular api call). Skeptical about option 1 because it means rushing a pretty big design discussion for a short term problem.

  5. aluzzardi commented on Nov 2, 2022

    @aluzzardi
    Contributor
    1. Bunch of edge cases we run into. The model of API-based creation broke down in a few places. For instance, when added in an extension it only works on un-cached runs (cached runs return a SecretID without the extension having run AddSecret, resulting in a broken secret)
    2. To add to the above -- edge cases are caused by "AddSecret" being very special. It's the only call that alters the internal API state (whereas all other calls are idempotent, multi tenant, etc -- AddSecret is not)

    I'd prefer we don't rush into neither Option 1 (needs design) nor Option 2+ (ripple effects, not easily reversible). @vito is going to post a summary of Option 3 (do nothing in core, work around with the existing API)

  6. vito commented on Nov 2, 2022

    @vito
    Contributor

    You can actually create in-memory secrets already in a kind of round-about way:

    client.Directory().WithNewFile("bruce", dagger.DirectoryWithNewFileOpts{
      Contents: "im always angry",
    }).File("bruce").Secret()

    Caveat: this will result in a SecretID that embeds the secret value in plaintext (albeit obfuscated below multiple layers of base64 ✨ security ✨). But maybe that's enough for now? As long as the SecretID itself isn't leaked this should still accomplish the goal of keeping its value out of the container layers.

    This feels roughly the same level of risk as option 2 or introducing a dedicated "create secret" API, but the benefit is it already exists.

    If you want to avoid leaking values into the SecretID you could instead leak them to the host filesystem and clean them up after:

    os.WriteFile("/sekrits/bruce", []byte("im always angry"), 0600)
    defer os.Remove("/sekrits/bruce")
    
    client.Host().Directory("/sekrits").File("bruce").Secret()
    
    // ... do API calls ...

    For extra pizazz you could try using a fifo/pipe instead but we're skeptical that'll actually work. :P

    Alternatively you could use env vars, but that's pretty sketchy too.

    Between these two my preference would be the first one, since it's essentially what this issue proposes but feels like a workaround (which really it is).

  7. shykes commented on Nov 2, 2022

    @shykes
    Contributor

    If I understand correctly, Option 3 would be basically "all userland", no new API call either private or public? If so, that would be an ideal short term solution :)

    Thanks for the additional details.

  8. jlongtine commented on Nov 2, 2022

    @jlongtine
    ContributorAuthor

    @jlongtine how absurd and awful of an idea is this: you currently can load secrets from env vars, so whenever you need to load a secret in dagger-cue you export an env var and then use that to load the secret in? Would need to be careful if dagger-cue ever forks off child processes of course. But could be a path to avoiding any changes to the API if it's plausible.

    @sipsma I've heard worse ideas in my time. :)

    I think I'm going to start with this... see if works out, then move onto one of @vito's suggestions if it doesn't for whatever reason.

    I appreciate you all digging into this with/for me!

  9. jlongtine commented on Nov 4, 2022

    @jlongtine
    ContributorAuthor

    An update: Using environment variables works! I've got that all up and running, and it unblocked me on a bunch of stuff. Thanks for all the help, everyone!

  10. added
    area/engine
    and removed on Dec 5, 2022
  11. jlongtine commented on Dec 7, 2022

    @jlongtine
    ContributorAuthor

    I had a good discussion today with @sipsma and @vito about this.

    Our proposal as a first pass (with subsequent designs for extensions, etc) is to add a GraphQL API to allow the creation of new Secrets. This will add those secrets to the secret.Store, and allow for retrieval by Buildkit.

    Everything feels pretty solid on implementation, but we still have some open questions around the API.

    API Design

    I had previously suggested one or the other of these:

    extend type Query {
      "Load a secret from its ID"
      secret(id: SecretID): Secret!
    }
    
    type Secret {
      "The identifier for this secret"
      id: SecretID!
    
      "The value of this secret"
      plaintext: String!
    
      "Create a new secret"
      new(plaintext: String!) Secret!
    }
    extend type Query {
      "Load a secret from its ID"
      secret(id: SecretID, plaintext: String): Secret!
    }

    With the following behavior:

    if id == nil && plaintext == nil {
      return err
    } else if id != nil && plaintext == nil {
      return the secret with id = id
    } else if id != nil && plaintext != nil {
      create a new secret with this id and plaintext, unless a secret with this id already exists (and return an error in this case)
    } else if id == nil && plaintext != nil {
      create a new secret with this plaintext, setting the id to sha256(plaintext)
    }
    

    @sipsma Also suggested this as a possibility:

    extend type Query {
      withSecret(id: String!, plaintext: String!): Query!
    }

    which would allow queries like:

    query {
      withSecret(id: "mysecret", plaintext: "supersecretsecret") {
        container {
          from(address: "alpine") {
            withSecretVariable(name: "MYSECRET", secret: "mysecret") {
              withExec(args: ["printenv", "MYSECRET"]) {
                stdout
              }
            }
          }
        }
      }
    }

    @shykes, @aluzzardi Do you like either of these, or have any other ideas for this API?

  12. 7 remaining items

  13. vito commented on Dec 8, 2022

    @vito
    Contributor

    For example, Bass constructs the Dagger client once on boot - it'll be awkward to use dynamic secrets here since the secrets don't exist until you use the runtime. I could move the Dagger client construction to each runtime call, but that might worsen performance (assuming dagger.Connect does engine session setup/etc.)

    Also just realized this is actually an argument in favor of withSecret (#3442 (comment)) since I wouldn't need to carefully ensure secret names don't collide in shared session state with a long-lived client.

  14. self-assigned this
    on Dec 8, 2022
  15. shykes commented on Dec 14, 2022

    @shykes
    Contributor

    Here's my proposal: #4193

  16. dolanor commented on Dec 14, 2022

    @dolanor
    Contributor

    I don't have a lot of knowledge on BuildKit secret management. @jlongtine helped understand better but I feel I'm still not completely sure how it works.
    I spent some time today in creating some diagram to share and be able to have feedback on my comprehension.

    I had a first diagram that I presented to @jlongtine again.
    (somehow, the mermaid interpreter from GitHub can't parse the code, so I put it in the annex)

    Capture d’écran 2022-12-14 à 18 57 04

    So I thought the dagger-session would completely run in the Docker container, with the graphql server as well. But @jlongtine told me that the graphql server runs in the SDK (from the provisionning logic).

    So the connection between the SDK and the Dagger Engine is not GraphQL based, but more gRPC for BuildKit Gateway?

    Then, the graph would look like that:
    Capture d’écran 2022-12-14 à 20 06 37

    So my questions are:

    • What is right? What is wrong in these diagrams?
    • Is the Dagger Session in the SDK client or in a container? (there was a dagger-engine-session container back in the old days, like, last week)
    • Related: Is the session attachable client-side or server-side? GraphQL Client? Or BuildKit Client?
    • Are the Secrets fetched from the host or from buildkit?
    • How does the secret provider communicate with the host?
      • through the session attachable, via dual-direction gRPC connection? With some sort of callback?
      • it doesn't communicate with the host and we serialize everything on the client and send to BuildKit in 1 direction/request?

    Annex: mermaid code

    1st Graph: GraphQL Server in the Dagger Engine

    Details
    graph LR
        subgraph Host
        subgraph UserCode[User Code]
            
            code --> nsfe(["core.NewSecretFromHostEnv('MY_SECRET')"])
        end
    
        
    
        UserCode --> SDK
    
        subgraph SDK
            gqlbuilder[GraphQL query builder]
    
            gqlbuilder -- build query --> hvs
    
            hvs[ HostVariable  secret ] ~~~ spc
            spc[Secrets Provider Connection] --> pt
            pt(["secret.PlainText()"]) --> ge(["os.GetEnv('MY_SECRET')"])
        end
    
        end
    
        SDK -.- provision -.-> DaggerEngine
        SDK -- GraphQL --> DaggerEngine
        subgraph Docker
            subgraph DaggerEngine
                direction LR
    
                subgraph GraphQLServer
    
                    gs[GraphQL Server] -- GraphQL to LLB --> LLB[LLB DAG]
                    LLB --- as(["llb.AddSecret( 'MY_SECRET' )"])
                    
                    as ~~~ sa
    
                    sa[Session Attachable] ---> sp
                    sp[Secrets Provider] --> getSecret(["GetSecret('MY_SECRET')"])
                end
    
                GraphQLServer -- query gRPC --> BuildKit
    
                subgraph BuildKit
    
                    bk[BuildKit]
    
                end
    
                GraphQLServer -- session connection --- BuildKit
                BuildKit -- inject secrets from\n provider as file mounts --> Containers
    
                subgraph Containers
    
                    c1[Container 1] --> c2[Container 2]
                end
    
                
            end
    
            
        end
        SDK == secrets provider session attachable connection === DaggerEngine
    

    2nd Graph: GraphQL server in the SDK/Client

    Details
    graph TD
        subgraph Host
            direction LR
    
            subgraph UserCode[User Code]
                direction TB
    
                code --> wev(["WithEnvironmentVariable('MY_SECRET')"])
            end
    
            
    
            wev --> gqlbuilder
    
            subgraph SDK
                direction TB
    
                gqlbuilder[GraphQL query builder]
    
                gqlbuilder -- build query --> gs
    
                
                subgraph provisioner
    
                end
    
                subgraph GraphQLServer
    
                    gs[GraphQL Request] -- GraphQL to LLB --> LLBDAG{{LLB DAG}}
                    
                    
    
                end
    
                subgraph DaggerSession
                    bkgwClient[BuildKit Gateway client]
                    sa[Session Attachable] --- sp
                    sp[Secrets Provider] --> getSecret(["GetSecret('MY_SECRET')"])
                    getSecret --> pt
                pt(["secretIDPayload{FromHostEnv: 'MY_SECRET'}.PlainText()"]) --> ge(["os.GetEnv('MY_SECRET')"])
    
                end
    
                
            end
        end
        
    
        bkSession -- "session connection (gRPC)" --- sa
        
        provisioner -- provision --> DaggerEngine
    
        
        subgraph Docker
            subgraph DaggerEngine
                direction LR
    
    
    
                
    
                subgraph BuildKit
                    bkSession[Session]
                    solver
    
                    bkgwClient -- "query (gRPC)" --> bkSession
                    bkSession --> llbDAG{{"LLB DAG"}}
                    LLBDAG --> bkgwClient
                    llbDAG --> as(["llb.AddSecret( 'MY_SECRET' )"])
                    as -- GetSecret --> sp
                end
    
                ge ---> solver
                solver -- inject secrets from\n provider as file mounts --> Containers
    
                subgraph Containers
    
                    c1[Container 1] --> c2[Container 2]
                end
    
                
            end
    
            
        end
    
  17. sipsma commented on Dec 19, 2022

    @sipsma
    Contributor

    @dolanor

    What is right? What is wrong in these diagrams?

    The second diagram is pretty much spot-on 👍

    Is the Dagger Session in the SDK client or in a container

    dagger session is a (hidden) subcommand of our CLI. So the way it works is the SDK either finds a pre-existing CLI and uses that or it defaults to downloading one to ~/.cache/dagger and using that. It then invokes dagger session as a child process and communicates w/ it over localhost.

    Working on documenting all this as part of: #4215

    Related: Is the session attachable client-side or server-side? GraphQL Client? Or BuildKit Client?

    Session attachables are implemented on the client-side. They are basically a way of allowing buildkit to reach back to the client and ask for something (secrets, a local dir sync, unix socket forward, etc.)

    Are the Secrets fetched from the host or from buildkit?

    They're fetched from the client, so the host that the client is on

    How does the secret provider communicate with the host?
    through the session attachable, via dual-direction gRPC connection? With some sort of callback?

    Yes, session attachables work vi some grpc magic in the buildkit client code that creates a channel where buildkit can make requests to the client.

  18. changed the title [-]Support the creation of new Secrets via the API[/-] [+]In-memory secret store[/+] on Jan 20, 2023
  19. shykes commented on Jan 20, 2023

    @shykes
    Contributor

    Note, I gave this design proposal a nickname ("in-memory secret store") to make it easier to distinguish from other design proposals for handling dynamic secrets. See #4426 (this issue is listed as "option B").

  20. samalba commented on Jan 24, 2023

    @samalba
    Contributor

    Screenshot 2023-01-24 at 2 41 55 PM

    My 2c, the original name made more sense to me. This one implies a specific secret provider implementation, which wasn't the intent when @jlongtine opened it.
  21. shykes commented on Jan 24, 2023

    @shykes
    Contributor

    My 2c, the original name made more sense to me. This one implies a specific secret provider implementation, which wasn't the intent when @jlongtine opened it.

    The problem is that we have several possible ways of "creating a new secret via the API", and we need a way to distinguish them. Right now we have "in-memory store" (store the value of the secrets in memory) + "helpers" (retrieve the value of secrets from helpers). What names did you have in mind?

  22. changed the title [-]In-memory secret store[/-] [+]Proposal: in-memory secret store for dynamic secrets[/+] on Jan 27, 2023
  23. shykes commented on Feb 27, 2023

    @shykes
    Contributor

    We have reached consensus on another design for implementing dynamic secrets (this is "option A"; we are going for "option D"). I am closing this to reflect the decision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/engine

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions