Skip to content

Scrub secrets - #4518

Merged
dolanor merged 12 commits into
dagger:mainfrom
dolanor:secret-scrub
Feb 23, 2023
Merged

dolanor merged 12 commits into
dagger:mainfrom
dolanor:secret-scrub

Conversation

@dolanor

@dolanor dolanor commented Feb 2, 2023 •

Copy link
Copy Markdown
Contributor

This PR provides a way to get the 0.2 feature that would scrub secrets from logs in case if it leaks.

For now, I only handle env var, and the scrub writer is heavily mocked. Also, the files need to be moved around, there will be a refactor later in the PR advancement.

I'll update this description along the implementation.

@dolanor
dolanor marked this pull request as draft February 2, 2023 16:07
@dolanor dolanor linked an issue Feb 2, 2023 that may be closed by this pull request
@dolanor

dolanor commented Feb 3, 2023

Copy link
Copy Markdown
Contributor Author

I don't really know how to get access to the secrets values from the shim.
Since the shim is quite simple and only deals with files and exec, it doesn't have access to the buildkit secretStore.
By creating and using the _DAGGER_SCRUB_SECRET, I have access to the secrets' names, but to get their values, I need to actually connect to buildkit session and get access to the secret store. I don't know if it's what we want, and I don't know how to do it from the shim without creating a Frankenstein.

Can you enlighten me @aluzzardi, @sipsma ?

@aluzzardi

Copy link
Copy Markdown
Contributor

Just read the secrets like a user would: from ENV or from the File

The proposal I wrote mentioned passing env names and file paths, not secret IDs

@samalba

samalba commented Feb 8, 2023

Copy link
Copy Markdown
Contributor

Closes #4413 #3173

Once the PR is ready to review, we should double check that it solves the two issues above before merging.

@dolanor
dolanor force-pushed the secret-scrub branch 2 times, most recently from 0e8bfb7 to 299af3f Compare February 11, 2023 01:29
@dolanor

dolanor commented Feb 11, 2023 •

Copy link
Copy Markdown
Contributor Author

@samalba : Are you sure about #3173?

@aluzzardi @sipsma @vito : I was hoping to set the _DAGGER_SCRUB_SECRETS env var in core/secret.go, but somehow, from the shim, it seems it's not set. Am I missing some layer where I need to pass an env var along?
My attempt at setting it is in the last WIP commit . If you can enlighten me, as the reading + scrubbing env var seems functional on the shim side.

@vito

vito commented Feb 12, 2023

Copy link
Copy Markdown
Contributor

@dolanor

I was hoping to set the _DAGGER_SCRUB_SECRETS env var in core/secret.go, but somehow, from the shim, it seems it's not set. Am I missing some layer where I need to pass an env var along?

Yep you guessed it. To pass an env var to the shim you should set it via LLB in (core.Container).Exec, just like this:

dagger/core/container.go

Lines 906 to 908 in b9518e4

if opts.RedirectStdout != "" {
runOpts = append(runOpts, llb.AddEnv("_DAGGER_REDIRECT_STDOUT", opts.RedirectStdout))
}

You can use the Secrets field to know what secret var names + paths to set:

dagger/core/container.go

Lines 86 to 99 in b9518e4

// Secrets to expose to the container.
Secrets []ContainerSecret `json:"secret_env,omitempty"`
// Sockets to expose to the container.
Sockets []ContainerSocket `json:"sockets,omitempty"`
}
// ContainerSecret configures a secret to expose, either as an environment
// variable or mounted to a file path.
type ContainerSecret struct {
Secret SecretID `json:"secret"`
EnvName string `json:"env,omitempty"`
MountPath string `json:"path,omitempty"`
}

Comment thread cmd/shim/main.go Resolved
@dolanor

dolanor commented Feb 15, 2023

Copy link
Copy Markdown
Contributor Author

leaving #1892 for future reference.

@dolanor

dolanor commented Feb 15, 2023

Copy link
Copy Markdown
Contributor Author

Testing the correct integration with the shim etc, I found 2 bugs. With a big help from @grouville .

#4607
#4608

The current PR won't fix them. I propose we review this PR as is as it is at feature parity with the 0.2 implementation.
I'll do follow up PR to fix those different issues.

PS: after thinking a little bit, I guess @vito 's approach in concourse might work (but I fear it might not work for other chars than \n)

@dolanor
dolanor force-pushed the secret-scrub branch 2 times, most recently from ad1a7b9 to 9f484d6 Compare February 16, 2023 03:53
@dolanor
dolanor marked this pull request as ready for review February 16, 2023 03:53
@dolanor
dolanor requested a review from aluzzardi February 16, 2023 03:53
@dolanor
dolanor force-pushed the secret-scrub branch 5 times, most recently from 9763e9d to 5b38d64 Compare February 16, 2023 15:22

@vito vito 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.

Looking good! Only found one real issue, the rest are nitpicks

Comment thread cmd/shim/main.go Resolved
Comment thread core/secret_scrub.go Resolved
Comment thread cmd/shim/main.go Resolved
Comment thread cmd/shim/main_test.go Resolved
Comment thread core/integration/secret_test.go Resolved

@aluzzardi aluzzardi 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.

Agree with @vito's comments -- added a couple of very minor nits but otherwise looks good

Also please squash commits before merging

Comment thread core/secret_scrub.go Resolved
Comment thread cmd/shim/main.go Resolved
@dolanor
dolanor force-pushed the secret-scrub branch 2 times, most recently from 41f7c08 to 684d372 Compare February 17, 2023 14:59
@dolanor

dolanor commented Feb 17, 2023

Copy link
Copy Markdown
Contributor Author

The commits are always squashed now, we can't merge without squashing in the GitHub interface.

@gerhard
gerhard requested a review from vito February 20, 2023 12:13
@dolanor

dolanor commented Feb 22, 2023

Copy link
Copy Markdown
Contributor Author

I fixed everything that was found. Good for another pass
cc @vito @aluzzardi @sipsma

@vito vito 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.

Sorry, just a couple more nitpicks, one of which is nitpicking my own suggestion 😅

Comment thread cmd/shim/secret_scrub.go Resolved
Comment thread core/container.go Resolved
feat(cmd/shim): preliminary support for secret scrubbing

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

use previous scrubbed value "***"

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

inject secret id list in the scrubwriter

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

feat(shim): reads secret to scrub from _DAGGER_SCRUB_SECRETS

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

refactor(shim): pass context in the ScrubWriter constructor

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

fix(shim): use os.Environ() to get secret values

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

WIP

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

core: pass _DAGGER_SCRUB_SECRETS to the shim

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

chore: clean up debug

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

feat: register files to scrub

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

feat(shim): scrub secrets from file

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

feat: scrub os.Stderr as well

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

refactor(shim): use fs.FS for opening secret files.

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

refactor(shim): move scrub writer at the end of the file

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

refactor: create a SecretToScrubInfo shared type

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

test: update test now that we scrub the secrets

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

Update core/secret_scrub.go

Co-authored-by: Alex Suraci <[email protected]>
Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

test: actually verify secret values (in containers)

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

test: fix bad check

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

doc: fix comment to match the new name

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>

refactor: move secret scrub into its dedicated files

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>
it allows for better testability, although, the input should be good.

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>
Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>
@dolanor

dolanor commented Feb 23, 2023 •

Copy link
Copy Markdown
Contributor Author

Just re-based. It was quite atrocious because my first commits were in conflict in the ./cmd/shim/main.go before I had a dedicated file for it.
So after a few unsuccessful rebases, I decided to squash my first commits until I split the logic in another file.
Then a simple rebase on upstream/main worked automatically.
The service code was the tricky one to merge automatically. But still, thanks @vito for it, I want to play with it :).

Good for review again.

@dolanor
dolanor force-pushed the secret-scrub branch 2 times, most recently from 0cea89c to 2eb2a88 Compare February 23, 2023 15:56
Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>
@dolanor
dolanor requested a review from vito February 23, 2023 17:46

@vito vito 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.

Sorry for the absolute paths distraction!

re: rebasing, yeah it gets pretty painful with long-lived PRs. I've found it easier to just continuously merge instead of rebasing each commit.

Comment thread cmd/shim/secret_scrub.go Resolved
Comment thread cmd/shim/secret_scrub.go Resolved
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.

Scrub secret values to avoid leaks

6 participants