Skip to content

Secret Scrub Multi Write - #5149

Merged
sipsma merged 26 commits into
dagger:mainfrom
matiasinsaurralde:secret-scrub-multi-write-3
Jun 13, 2023
Merged

sipsma merged 26 commits into
dagger:mainfrom
matiasinsaurralde:secret-scrub-multi-write-3

Conversation

@matiasinsaurralde

@matiasinsaurralde matiasinsaurralde commented May 17, 2023 •

Copy link
Copy Markdown
Contributor

Picking up #4944 with some refactoring:

First iteration:

  • Simplify SecretScrubWriter write logic and only use []byte for matching and replacements. I also explored strings.Replacer and bytes.Replacer but didn't find a good overall performance in this context.
  • Refactored SecretScrubWriter to store secrets as []byte during initialization, also moved empty secret value check to initialization step (it was previously happening on every Write) as part of splitSecretsByLine.
  • Refactored and parallelized all tests in secret_scrub_test.go.

Second/current iteration:

  • Removed writer implementation and implemented a reader based on an existing replace package.
  • Simplified integration tests and refactored tests to use the new reader.
  • Uses exec.Cmd pipes to capture output and io.Copy for writing to corresponding files and actual stdout and stderr.

@matiasinsaurralde
matiasinsaurralde force-pushed the secret-scrub-multi-write-3 branch from fc9a852 to fffa7d8 Compare May 17, 2023 16:06

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

Just did a quick high-level pass since it's still in draft, but overall looks like it's all heading in a good direction!

Comment thread cmd/shim/secret_scrub_test.go Resolved
@matiasinsaurralde
matiasinsaurralde force-pushed the secret-scrub-multi-write-3 branch from fffa7d8 to b9f782f Compare May 23, 2023 18:42
@matiasinsaurralde
matiasinsaurralde marked this pull request as ready for review May 23, 2023 19:41
@matiasinsaurralde
matiasinsaurralde force-pushed the secret-scrub-multi-write-3 branch from 19d3111 to 9fada4e Compare May 23, 2023 19:42

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

Was just digging into the weeds of the implementation and got a better feeling for how intricate it is.

That got me wondering if there is any library that we can use instead to do this. I found this one, which at a glance looks like it might do what we need? But not totally sure.

I'm not opposed to doing all this ourselves if it's the best option, just want to double check it makes sense to be written/maintained by us.

Comment thread cmd/shim/secret_scrub.go Resolved
Comment thread cmd/shim/main.go Resolved
@gerhard

gerhard commented May 25, 2023

Copy link
Copy Markdown
Contributor

This seems best left for a future release, will not be in this week.

@gerhard

gerhard commented Jun 1, 2023 •

Copy link
Copy Markdown
Contributor

I don't see this one making it into this release @matiasinsaurralde. Would be nice to get it merged next week maybe. cc @samalba @mircubed

@matiasinsaurralde
matiasinsaurralde force-pushed the secret-scrub-multi-write-3 branch 2 times, most recently from 3974e80 to 6e9a051 Compare June 5, 2023 14:13

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

one tiny comment about more test coverage, otherwise looks great! Glad the replace package was able to simplify so much for us.

cc @vito in case you want to take a look, I saw you had to deal with similar issues in concourse

Comment thread cmd/shim/secret_scrub_test.go Resolved

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

Really cool that we found a package for this! Happy to see the code shrink and the tests grow. :)

Minor feedback, and +1 to @sipsma's test suggestion.

Comment thread cmd/shim/secret_scrub.go Resolved
@matiasinsaurralde
matiasinsaurralde force-pushed the secret-scrub-multi-write-3 branch from 6e9a051 to 3242008 Compare June 6, 2023 13:06
Comment thread cmd/shim/main.go Outdated

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.

I just noticed the test failure, looks like it may be significant: https://github.com/dagger/dagger/actions/runs/5189016555/jobs/9353423519?pr=5149#step:5:14608

Don't know, but I'm wondering if we are just ending up not always flushing all the output sometimes?

@matiasinsaurralde matiasinsaurralde Jun 9, 2023 •

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 pushed the same changes and didn't see the issue again however I'm facing the following scenarios when testing latest main:

  • On M1 with Docker Desktop all TestContainerWithMountedSecretOwner tests work fine.
  • On my Linux box, if I use a regular user I see this issue:
=== RUN   TestContainerWithMountedSecretOwner/userid
#1 resolve image config for docker.io/library/alpine:3.16.2
    container_test.go:3671: 
        	Error Trace:	/home/matias/go/src/github.com/dagger/dagger/core/integration/container_test.go:3671
        	Error:      	Not equal: 
        	            	expected: "auser auser"
        	            	actual  : "auser "
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-auser auser
        	            	+auser 
        	Test:       	TestContainerWithMountedSecretOwner/userid
  • If I ran everything as root (sudo ./hack/dev, etc.) tests pass.

I tried the same with this branch, when running with sudo test pass, regular usar fails.

So I'm wandering the following:

  • I see there's some logic around inheriting permissions in 48fb31d. Could this break depending on which user is being used in the host?
  • Based on the above, do we run tests in some privileged mode? And/or did we change anything related to it since the last failing test report you did?

Could be good to involve @vito here too, thanks! Besides that I don't see that the previously failing test is related to my changes but would be good to understand why the issue happened.

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.

I am running as a normal user on:

Server:
 Server Version: 20.10.21
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Native Overlay Diff: true
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
 containerd version: v1.6.18
 Kernel Version: 5.15.81
 Operating System: NixOS 22.11 (Raccoon)
 Architecture: x86_64

First 5 local runs of _EXPERIMENTAL_DAGGER_INTERACTIVE_TUI=1 dagger run ./hack/make engine:test passed:
image

Next 5 local runs of _EXPERIMENTAL_DAGGER_INTERACTIVE_TUI=1 dagger run ./hack/make engine:testrace also passed. I have delete the Engine container as well as the volume after every run. If this is a race condition - which I suspect it is - it's not an easy one to trigger. I will keep running it until I do.

FTR, this is the last run in CI which hit it: https://github.com/dagger/dagger/actions/runs/5189016555/jobs/9353423519?pr=5149#step:5:14607

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.

@gerhard Interesting, for me it works properly on Mac using a regular user.
The issue seems to occur on Linux only.

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.

I would also guess this is some kind of race condition or issue with the secret scrubbing. These tests should have no relation to what user runs them on the host. 🤔 I can't think of a way to convince stat -c '%U %G' to print an empty %G, so it seems more like the last chunk of output is bring dropped. Happy to be wrong, but that's my guess.

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.

@vito makes sense however I also see it in latest main -without my changes-. Will debug a bit more on Monday.

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've verified this again, tried the following:

  • Ran TestContainerWithMountedSecretOwner multiple times in both branches (main and this one), with and without race. Didn't see any issues so far.
  • Added a panic() to the block where secret scrub logic is initialized here so that the execution breaks If this part of the code is reached: regular secret scrub integration tests like TestBigSecretScrubbed got the panic but TestContainerWithMountedSecretOwner didn't, this indicates that the logic that's part of this PR isn't involved in TestContainerWithMountedSecretOwner at all.

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.

Also the reason why test TestContainerWithMountedSecretOwner/userid has been failing to me seems to be a caching issue. Even with rebuilds it would still print the wrong output (a user ).
I renamed the test to TestContainerWithMountedSecretOwner/userid-111, ran go test -v -count=1 -timeout 30s -run ^TestContainerWithMountedSecretOwner/userid-111$ github.com/dagger/dagger/core/integration and the results are just fine!

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.

Are you saying that if TestContainerWithMountedSecretOwner/userid test changes, the previous run remains cached? That would be surprising behaviour which we should address.

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.

I continued running this as mentioned, and even though I have hit other issues - still to create them - I was not able to reproduce the original failure. I have seen it fail once locally, before I started looking for it, but not since.

@matiasinsaurralde
matiasinsaurralde force-pushed the secret-scrub-multi-write-3 branch 2 times, most recently from 9fbd078 to 16e58c4 Compare June 8, 2023 22:23
@matiasinsaurralde
matiasinsaurralde force-pushed the secret-scrub-multi-write-3 branch 3 times, most recently from 16e58c4 to f7b37c6 Compare June 10, 2023 21:34
Comment thread cmd/shim/main.go Outdated

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.

@matiasinsaurralde I think the test failures are due to use of cmd.StdoutPipe and cmd.StderrPipe. The docs for those say:

It is thus incorrect to call Wait before all reads from the pipe have completed. For the same reason, it is incorrect to use Run when using StderrPipe. See the StdoutPipe example for idiomatic usage.

I was able to repro the failures consistently when pulling this PR locally, I was no longer able to repro w/ this (very rough patch):

diff --git a/cmd/shim/main.go b/cmd/shim/main.go
index ecdc8567..09972d94 100644
--- a/cmd/shim/main.go
+++ b/cmd/shim/main.go
@@ -291,21 +291,17 @@ func shim() int {
                cmd.Stderr = errWriter
        } else {
                envToScrub := os.Environ()
-               stdoutPipe, err := cmd.StdoutPipe()
-               if err != nil {
-                       panic(err)
-               }
-               scrubOutReader, err := NewSecretScrubReader(stdoutPipe, currentDirPath, shimFS, envToScrub, secretsToScrub)
+               outR, outW := io.Pipe()
+               cmd.Stdout = outW
+               scrubOutReader, err := NewSecretScrubReader(outR, currentDirPath, shimFS, envToScrub, secretsToScrub)
                if err != nil {
                        panic(err)
                }
                go io.Copy(outWriter, scrubOutReader)
 
-               stderrPipe, err := cmd.StderrPipe()
-               if err != nil {
-                       panic(err)
-               }
-               scrubErrReader, err := NewSecretScrubReader(stderrPipe, currentDirPath, shimFS, envToScrub, secretsToScrub)
+               errR, errW := io.Pipe()
+               cmd.Stderr = errW
+               scrubErrReader, err := NewSecretScrubReader(errR, currentDirPath, shimFS, envToScrub, secretsToScrub)
                if err != nil {
                        panic(err)
                }

dolanor and others added 21 commits June 13, 2023 12:33
Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>
when the scrubbing happens, it will try to scrub the
biggest first if a secret is a substring of a bigger one,
it will avoid partial leaks of the bigger secret

Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>
Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>
Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>
Signed-off-by: Tanguy ⧓ Herrmann <[email protected]>
Signed-off-by: Matias Insaurralde <[email protected]>
Signed-off-by: Matias Insaurralde <[email protected]>
Signed-off-by: Matias Insaurralde <[email protected]>
Signed-off-by: Matias Insaurralde <[email protected]>
Signed-off-by: Matias Insaurralde <[email protected]>
Signed-off-by: Matias Insaurralde <[email protected]>
Signed-off-by: Matias Insaurralde <[email protected]>
@matiasinsaurralde
matiasinsaurralde force-pushed the secret-scrub-multi-write-3 branch from fd20bad to 190a826 Compare June 13, 2023 16:33
Comment thread cmd/shim/main.go
if _, found := internalEnv("_DAGGER_ENABLE_NESTING"); !found {
// no nesting; run as normal
return cmd.Run()
if err := cmd.Start(); err != nil {

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 approach looks good, we just also need to update the use of cmd.Run on line 611 with the same thing

Comment thread cmd/shim/main.go
cmdErr = cmd.Start()
if cmdErr != nil {
return cmdErr
}

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.

Don't we need a pipeWg.Wait call before the cmd.Wait here too?

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.

@sipsma My bad, just added it!

@sipsma
sipsma merged commit 7ab8a69 into dagger:main Jun 13, 2023
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.

5 participants