Skip to content

Isolate secrets to each module - #6601

Merged
jedevc merged 12 commits into
dagger:mainfrom
jedevc:module-secret-isolation
Feb 26, 2024
Merged

jedevc merged 12 commits into
dagger:mainfrom
jedevc:module-secret-isolation

Conversation

@jedevc

@jedevc jedevc commented Feb 6, 2024 •

Copy link
Copy Markdown
Contributor

Original feature request:

Right now the in-memory secret store is shared across a whole dagger session, which includes the original client caller and all the modules it invokes (including transitively).

Instead secrets should be passable as inputs/outputs of Functions but only accessible that explicit way, not accessible via the "ambient secret store".

To achieve this isolation, we keep the same global secret store, but ensure that each secret has a "scope" - this scope split the global secret store into lots of smaller stores.

Note, this is really quite a tricky puzzle, and the requirements become more fiddly as you lay them out:

  • Secrets across different sessions/modules should be allowed the same name
  • Secrets shouldn't affect caching (any more than they do today)
  • Direct access to the secret store using the "feature" where they can be pulled directly using the DockerBuild API without explicit passing
  • What should the "lifetime" of a "module-scoped ambient secret store" be? If I create a secret in a Foo function call, but then try and access it in Bar, is it still there or is it only valid per-call? If it's still there, then does it matter which module called me - and does that make for weird dependency state?

We can tick all these boxes by doing the prefixing trick - we also need to explicitly handle the DockerBuild frontend case, so we protect against reading directly from there. The only question really, is what prefix to use. A couple ideas I had:

  • Client ID - but this is very bad for caching, so probably not
  • Hash of the secret ID - this doesn't work, since there is no real parent (beyond the root query object)
  • Unique ID of the current module (git hash + subpath for git modules, contenthash for a directory module) - quite good, maybe work to calculate, but also not an option for generic privileged nesting
    • Could avoid some weird lifetime issues if we extended this and also included the unique IDs of all the parents as well (so A->X and B->X calls would have two separately scoped X secret stores)
  • Generate a unique buildkit secret for every use of a secret and base the prefix off a hash of everything prior to the use - also potentially possible, but now we need to make sure we don't accidentally cause a buildkit edge merge here!
  • ...something else I'm not thinking of?

TODO:

  • Prefix secrets with the client ID that created them (which isolates them). Update all secret access logic with this.
  • Intercept dockerfile builds, and rewrite secrets with the client prefix.
    • This is needed, since frontends have the ability to directly access secrets from the secret store (via ExecOp/etc). We can use a custom gateway/frontend to protect against this.
  • Use a "stable" ID for prefixing (instead of client IDs). Every time we change a secret ID we invalidate cache, so we have to avoid doing this.
    • Ensure this is tested as well

@jedevc jedevc added this to the v0.10.0 milestone Feb 6, 2024
@jedevc
jedevc force-pushed the module-secret-isolation branch 2 times, most recently from 9593df3 to e0d0a35 Compare February 8, 2024 12:46
@jedevc
jedevc force-pushed the module-secret-isolation branch from e0d0a35 to d8b1908 Compare February 12, 2024 12:30
@jedevc
jedevc marked this pull request as draft February 12, 2024 13:25
@jedevc
jedevc force-pushed the module-secret-isolation branch 3 times, most recently from ef78f23 to 2d91181 Compare February 16, 2024 15:56
@sipsma

sipsma commented Feb 18, 2024

Copy link
Copy Markdown
Contributor

I haven't looked through this in depth yet, but I just did a quick test and found that the grpc client metadata ends up plumbed all the way through to where ExecOp asks for secrets (here). So if you call engine.ClientMetadataFromContext in our GetSecret implementation (here) you can actually access the ClientID (without any changes that would break caching).

I think this offers a simpler route to implementing isolation based on the client ID since each module gets its own even within the context of a session. Just wondering if you tried out that route too.

Just leaving this as a quick drive-by thought since I hit a timebox, will resume taking a look again later.

@jedevc

jedevc commented Feb 18, 2024 •

Copy link
Copy Markdown
Contributor Author

I haven't looked through this in depth yet, but I just did a quick test and found that the grpc client metadata ends up plumbed all the way through to where ExecOp asks for secrets (here). So if you call engine.ClientMetadataFromContext in our GetSecret implementation (here) you can actually access the ClientID (without any changes that would break caching).

I think this offers a simpler route to implementing isolation based on the client ID since each module gets its own even within the context of a session. Just wondering if you tried out that route too.

Hm just one concern with this - if two modules call this at the same time, we'd edge merge, and only get one call to the ExecOp. So theoretically, a module without access to the secret, could get access to the result of a computation that required the secret (though it wouldn't actually leak the secret).

The only way to avoid this would be to make sure we actually have different cache keys (e.g. by actually changing the secret access IDs).

That's why I went this slightly more complex route, but maybe there's a clever way we could still make it work? Potentially we could inject an env var/similar to every ExecOp in a module, to force a different cache key cross modules, but that feels a bit overly extreme.

@sipsma

sipsma commented Feb 19, 2024 •

Copy link
Copy Markdown
Contributor

Hm just one concern with this - if two modules call this at the same time, we'd edge merge, and only get one call to the ExecOp. So theoretically, a module without access to the secret, could get access to the result of a computation that required the secret (though it wouldn't actually leak the secret).

I'm presuming we were going to update to use a stable id as prefix rather than client id, and at that point we'd have the same edge merge problem, right?

I was thinking through the cases where this would be undesirable and realized that practically speaking, the "edge-merge" case is actually the same as re-use of a cached exec op. E.g. all the cases where it would be undesirable for an edge merge to happen can also happen if exec-ops from completely different sessions get de-duped due to local/remote cache hits.

So I think this actually gets at a larger long-standing and pre-existing issue. Probably still be worth addressing as part of our changes here though.

The only thing I can think of to completely squash this problem in all cases (edge merge, cache hits) would be to change the underlying secret ID (i.e. the one that is provided to LLB and used in GetSecret calls to the attachable) to be a hash of the secret value.

  • This would work conceptually because the main problem with edge merges/cache hits is that a malicious client could set a secret to an invalid value but still get the result of the exec-op that used the correct value. But if the underlying ID is actually a product of the value, then the only way to get an edge-merge/cache-hit would be if the malicious client actually knew that value (at which point its game over anyways)
  • We'd ideally want to do it in such a way that we never actually pass that hash around to module code and instead just rewrite it internally right before submitting LLB to be solved (i.e. with OpDAG).

So trying to put everything together, we could:

  1. Store secrets in the secret store with this metadata attached to each entry:
    • name - the user-specified name
    • client ID - the client ID that has access to this secret
    • plaintext
    • hash - the hash of the plaintext
  2. When an exec op is submitted, update secret IDs to be the hash rather than name (using the secret store to know the mapping, we can append an extra method to our implementation to support this in our own code pretty easily)
  3. GetSecret is updated to:
    • check the client ID from the context
    • find the secret with the provided hash (the secret ID) that is available to that client ID

WDYT @jedevc? I'm not opposed to any solution that works and handles all the cases; the above just feels simplest and most robust to me (if the theory works out in practice), but that's obviously subjective.

@jedevc

jedevc commented Feb 20, 2024

Copy link
Copy Markdown
Contributor Author

I'm presuming we were going to update to use a stable id as prefix rather than client id, and at that point we'd have the same edge merge problem, right?

Yes, we need a stable id. I was actually thinking that we could pick a "module id" as a stable id:

  • For a git module, this would be hash(commit SHA + subpath)
  • For a local module, this would be hash(blob ID + subpath)
    • For the top-level module, we could try and keep this more constant (by using hash(path), instead), so that as you develop your module, you don't keep getting different secret IDs.

This is stable, should be pretty easy to propagate everywhere, but doesn't really provide isolation between generic nested sessions (though I'm not necessarily sure this is an issue, this is more obscure, and also expectations of security are much lower).

Also, what's really cool about this is that we allow caches/edge-merges for secrets only inside the context of that module - other modules get different buildkit secret ids, so they can't collide.

We'd ideally want to do it in such a way that we never actually pass that hash around to module code

This is potentially a pain 😢 Without this, we don't have a way to distinguish secrets from each other - and we can't even rely on session id here. If I return a Secret from my module (the result of looking it up using secret), and then it gets into another module, then secret gets called in the context of the consumer module. So we need some identifying info in the secret itself (see accessor in the current PR).

to be a hash of the secret value.

Ah, nit-pick, but this then changes secret value changes to always invalidate cache - it's a tiny issue, but if possible I'd like to preserve the current behavior.

I'm not opposed to any solution that works and handles all the cases

I think I'm going to try the module "ID" as the secret prefix - it ticks all the required cases, and also addresses the things I mentioned above. Even though it is a bit more work, it shouldn't be that much trickier.

I don't think I'm far away from getting this PR into a ready state, should be done this week, just need one more proper focus session.

@jedevc
jedevc force-pushed the module-secret-isolation branch from 0d94a8c to 6445b56 Compare February 22, 2024 15:25
Comment thread core/schema/secret.go Outdated
Comment on lines 115 to 118

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.

👀 so this seems to work like a stable ID per module...

@vito does it seem reasonable that this would stay somewhat stable between runs? If not, it doesn't have sufficient uniqueness guarantees, I can go and add a module ID somewhere, but I'd like to avoid doing all the extra special casing for that if possible.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You should use m.Source.ID().Digest(), I setup source to be usable as a stable ID with the exact properties you described here. Was previously needed to detecting dependency loops, but that got refactored away recently, but I guess it will come in handy again here.

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.

perfect, awesome! Thanks, have updated to use that as well.

@jedevc
jedevc marked this pull request as ready for review February 22, 2024 17:54
@jedevc
jedevc requested review from a team as code owners February 22, 2024 17:54
@jedevc

jedevc commented Feb 22, 2024 •

Copy link
Copy Markdown
Contributor Author

Think I've got most of the tests passing now (:crossed_fingers:). Should be ready for a more proper review :tada:

@vito

vito commented Feb 22, 2024 •

Copy link
Copy Markdown
Contributor

Ah, nit-pick, but this then changes secret value changes to always invalidate cache - it's a tiny issue, but if possible I'd like to preserve the current behavior.

Yeah I think this is important. Just keeping in mind that people might be using shortlived secrets (e.g. AWS access keys that expire after an hour), I think we'll definitely want to avoid cache busting on them. Which unfortunately makes things more difficult.

@sipsma

sipsma commented Feb 22, 2024

Copy link
Copy Markdown
Contributor

Ah, nit-pick, but this then changes secret value changes to always invalidate cache - it's a tiny issue, but if possible I'd like to preserve the current behavior.

Yeah I think this is important. Just keeping in mind that people might be using shortlived secrets (e.g. AWS access keys that expire after an hour), I think we'll definitely want to avoid cache busting on them. Which unfortunately makes things more difficult.

I know this is what we've landed on in the past, but I'm not sure I agree with it anymore. It means that you can provide invalid credentials that happen to have the same secret name and the operation will "succeed" due to caching. Something about that feels deeply wrong.

But:

  1. It's not worth bogging this PR down in philosophical issues that are ultimately tangential
  2. I highly suspect our long term plans around secret providers will alleviate these concerns (but also even more way out of scope of this PR)

@vito

vito commented Feb 22, 2024

Copy link
Copy Markdown
Contributor

I know this is what we've landed on in the past, but I'm not sure I agree with it anymore. It means that you can provide invalid credentials that happen to have the same secret name and the operation will "succeed" due to caching. Something about that feels deeply wrong.

Yeah the issue is we need to be able to distinguish between "I manually changed my secret and want that action to affect external state" and "my credentials are super secure and rotate all the time, but I would still like to enjoy caching." To me it's pretty bad if we end up in a state that doesn't allow frequent cred rotation and caching to coexist. But yeah this probably gets into higher level discussions re: state, deployments, and secret management in general.

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please double check my reasoning here, but I think there may still be a backdoor available via loadSecretFromID. Namely, even though the ID format and accessor are "opaque", it's not that hard for something malicious to derive those (i.e. figure out the accessor for another unrelated module in the same session) and construct an ID manually that's provided to loadSecretFromID and thus grant access to calling plaintext and/or using the secret in other API calls.

This is part of what I was getting at in this comment about more explicitly tracking what module should have access to what secret.

That's an option, but trying to think through less dramatic changes on top of the current PR (which like I said before, I'm not opposed to at all since I think it's workable):

  1. Somehow write a custom loadSecretFromID resolver rather than use the automatic one from dagql? cc @vito seems like something that should be possible but maybe not quite out-of-the-box yet?
  2. (I had more ideas but as I wrote them out they didn't make sense and I hit a timebox, so just pressing submit on my comment for now 😅)

Comment thread engine/buildkit/client.go Outdated
Comment on lines 126 to 130

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: isn't it available already via client.Opt.Worker.Executor()?

@jedevc

jedevc commented Feb 23, 2024 •

Copy link
Copy Markdown
Contributor Author

Please double check my reasoning here, but I think there may still be a backdoor available via loadSecretFromID. Namely, even though the ID format and accessor are "opaque", it's not that hard for something malicious to derive those (i.e. figure out the accessor for another unrelated module in the same session) and construct an ID manually that's provided to loadSecretFromID and thus grant access to calling plaintext and/or using the secret in other API calls.

Ok, so - I don't think there's an issue directly with loadSecretFromID - we could construct an ID manually that creates a secret, but still the secret will always be constructed from a call to secret. There is an issue with calling secret though, you can just guess the accessor and then grab a secret.

I had a rather nice shower-thought about how to resolve this (have pushed it in the last commit). Essentially, we use the plaintext-digest and put that into the ID - if you use an accessor, you must specify this plaintextDigest as well - when loading a secret we check that the plaintext matches. Providing this value essentially ensures that you are accessing an "officially" minted secret and that you have a real one.

This has a few fun properties:

  • Secrets minted using setSecret/setSecretFile set plaintextDigest + accessor, so when they're passed between modules, these are set, and so can be accessed easily.
  • If a malicious module attempts to mint a secret manually, it can't just guess the accessor, it also needs to work out what the digest of the secret is - at that point, it needs to know the full secret, which defeats the whole purpose of the attempt.
  • This is only a schema-level thing - the buildkit secret IDs are still entirely based on the accessor, so caching is unaffected.
  • This essentially splits out the two properties from this patch, instead of trying to merge them together:
    • Buildkit ID for uniqueness (based on module source ID)
    • Proof of secret ownership

However, one note - is it ok that the plaintext secret digest is part of the dagql graph? We take a lot of pains to keep the plaintext out of the graph, so I wonder if we need to do some other stuff to keep the plaintext secret digest out (but we can't just mark it as sensitive, otherwise the client never actually gets access to it, which is kind of the whole point).

If we do have concerns about that, we could potentially do some other magic as well, and combine the plaintext digest with the client ID (which is fine to use here, since this doesn't affect the buildkit caching), and track the client ID that set each secret in the secret store (which lets us verify).

Comment thread docs/docs-graphql/schema.graphqls Outdated

@jedevc jedevc Feb 23, 2024 •

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.

I genuinely can't see a legitimate reason for users calling this function manually - it exists purely as an implementation detail IMO (the only place I want it really is in tests, and we can do manual querybuilding there).

Is there a way that we can hide this from the generate graphql docs + all the SDKs? Seems related to #6714 (comment)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This has actually come up before in the wild, not just an internal thing - it was added for the internal call, yes, but in my mind it was also adding an API feature that has been requested for a while:

Does it still work without these arguments?

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.

Ah I think we can keep the function itself, but I think it shouldn't be possible to call with accessor/etc manually. I'd be happy with just making them hidden, but...

If we could just make it impossible to do so, then I don't think we'd even need the plaintext digest thing I introduced.

@jedevc
jedevc force-pushed the module-secret-isolation branch from 894c734 to a5cac3e Compare February 23, 2024 12:22
@vito

vito commented Feb 23, 2024

Copy link
Copy Markdown
Contributor

However, one note - is it ok that the plaintext secret digest is part of the dagql graph? We take a lot of pains to keep the plaintext out of the graph, so I wonder if we need to do some other stuff to keep the plaintext secret digest out (but we can't just mark it as sensitive, otherwise the client never actually gets access to it, which is kind of the whole point).

Bit of a brain dump, sorry:

It's a bit risky. The plaintext digest will be sent over the progress stream, all the way to the TUI and Cloud, and visible in tools like https://dagviz.fly.dev. Theoretically someone could just browse to one of your runs and grab the digest from there if they have access to it. It's an obtuse angle, but it exists.

It's not a dead end; in principle add something lesser to the "sensitive" flag that means "include this in IDs but don't emit it in telemetry" rather than "forbid this value from ever entering into an ID." The only thing is then you'll have IDs that need to be mutated as they pass through the metrics pipeline, so their digests will differ from the ones that actually evaluate. Nothing will actually break atm, but it's kind of an uncomfortable feeling, and might close certain caching-related doors in the future - e.g. if we wanted to lean harder on IDs as a cache key, this would add an asymmetry between frontend/backend. But maybe we could burn that bridge when we get to it.

FWIW a similar concept already exists where IDs can be marked meta and you can call Canonicalize() on an ID to trim out all the meta components when computing a cache key, but I was hoping to retire that, since it complicates some things1 and is only theoretically used for pipeline and focus APIs (which don't actually enable it because it turns out they need state, so ignoring them for a query level cache key isn't even correct, it's more relevant if we were using IDs as a cache key for a persistent store).

Maybe we could change how 'sensitive' works and rather than forbidding it from going into the ID, we implement a mechanism for redacting values as they're sent through the progress stream?

Footnotes

  1. If we wanted a more efficient deduped/flattened structure for IDs that maps digests to "tree-shaken" IDs that refer to their dependencies by digest instead of including them wholesale, the canonicalization process would require recalculating digests each time a meta value is removed. ↩

@sipsma

sipsma commented Feb 24, 2024

Copy link
Copy Markdown
Contributor

Essentially, we use the plaintext-digest and put that into the ID - if you use an accessor, you must specify this plaintextDigest as well - when loading a secret we check that the plaintext matches. Providing this value essentially ensures that you are accessing an "officially" minted secret and that you have a real one.

This does affect caching though, specifically of module functions themselves that accept secrets as args. The values of all args are part of the cache key for the exec-op underlying the function call. If the ID changes, that invalidates the execution of the function call.

  • This is somewhat more obscure atm since we invalidate cache of functions once per session, but it can still matter today (i.e. if you call the same function multiple times from within a session) and will matter enormously once we add support for cache control of function execution.

To be clear, I'm not opposed to this since like I said before I now think it makes sense to always invalidate cache based on the value of the secret. It's also how the CLI currently sets the secret name anyways too (code). I think it's essentially a step closer to what I suggested here #6601 (comment)

  • Also echoing what I said previously about the fact that while I agree it's unfortunate that this means short-lived tokens invalidate cache, that A) feels closer to correct behavior even if not performance optimal and B) can be addressed in the future via "secrets providers" (which in general feels like the better way of using short lived creds anyways)

The thing I'd want to avoid is just using the ClientID as you mentioned towards then end of the comment since that would break our plans to make functions fully cacheable across sessions in the near-ish future.

@jedevc

jedevc commented Feb 26, 2024

Copy link
Copy Markdown
Contributor Author

This does affect caching though, specifically of module functions themselves that accept secrets as args.

Agh, sorry, I did understand this at some point, and then it slipped my mind.

I now think it makes sense to always invalidate cache based on the value of the secret.

I don't have super strong opinions on the idea of this, but. If the secret hash is sensitive, then we want to avoid it leaking anywhere outside of the current machine:

  • Into telemetry
  • Into logs
  • And into cache (and magicache)

Since magicache will cache the inputs to functions, I'd really rather not include any hashed version of the password in there - any attacker with access to the cache (just like @vito's point above about dagviz/telemetry), can then perform an offline bruteforce attack - we then rely on the entropy of the secret to provide all the security, which just doesn't feel right. I can't see a way to be comfortable with potentially uploading secret hashes to caching services (it feels like a security incident just waiting to happen).


I think the only approach left is to carefully track exactly how secrets are passed between modules, and then when accessing secrets, ensure that that module was passed that secret (either by return from a module that had access, or by argument called by a module that had access).

This feels tricky though, since it also requires accounting for secrets that are then stored in custom objects.


Given the time before the release, I'm tempted to simply go back to just the accessor - even though it can be leaked by knowing the ID (I wrote a test for this and pushed it, we can enable it later).

Even though it's possible for a malicious module to potentially extract secrets from other running modules, I'm not actually convinced this is the highest priority - if I can run code on a machine, I can definitely think of lots of other nasty things to do - especially since we have privileged nesting/execs.

As-is, this PR at least prevents secrets from accidentally colliding over each other, so I think is still an improvement to take for v0.10.0 - there's clearly a lot of future work here.

@jedevc
jedevc force-pushed the module-secret-isolation branch from a5cac3e to c50d558 Compare February 26, 2024 16:50
Looks like I accidentally broke this. I don't really understand dagql
super well, but what I think was happening was that we would
re-evaluated the secret in the context of the new session, so passing
secrets between sessions broke.

The solution here seems to be to... pass the scope as part of the call
to secret.

Signed-off-by: Justin Chadwell <[email protected]>
This keys secrets in the secret store using the scope and the name, in a
unique+reproducible way, such that the scope remains secret.

This is just an HMAC (but this isn't fully implemented yet).

Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Justin Chadwell <[email protected]>
Signed-off-by: Justin Chadwell <[email protected]>
@jedevc
jedevc force-pushed the module-secret-isolation branch from c50d558 to 48c99c6 Compare February 26, 2024 17:38

@sipsma sipsma left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Given the time before the release, I'm tempted to simply go back to just the accessor - even though it can be leaked by knowing the ID (I wrote a test for this and pushed it, we can enable it later).

Even though it's possible for a malicious module to potentially extract secrets from other running modules, I'm not actually convinced this is the highest priority - if I can run code on a machine, I can definitely think of lots of other nasty things to do - especially since we have privileged nesting/execs.

As-is, this PR at least prevents secrets from accidentally colliding over each other, so I think is still an improvement to take for v0.10.0 - there's clearly a lot of future work here.

Okay yeah I agree this is a big step in the right direction and makes sense given the time frames 👍 (preemptively approving so we can merge ASAP)

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.

3 participants