-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Copy collector enhanced #237
Changes from 12 commits
f77f09f
07bc145
829f311
dba0209
5bfdffd
d5ab4e3
03c3f07
85976fc
425344a
9750a79
6cdc808
eec86f5
3a8126e
2c10570
ffc8154
d59e311
257e246
340e0d4
ca6e3e4
af004be
0225123
880f435
58c6229
f73aff0
b4a6a86
6c222eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,101 @@ | ||
package collect | ||
|
||
import ( | ||
"archive/tar" | ||
"bytes" | ||
"encoding/binary" | ||
"io" | ||
"path/filepath" | ||
|
||
troubleshootv1beta1 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta1" | ||
"github.com/replicatedhq/troubleshoot/pkg/redact" | ||
) | ||
|
||
func redactMap(input map[string][]byte, additionalRedactors []*troubleshootv1beta1.Redact) (map[string][]byte, error) { | ||
result := make(map[string][]byte) | ||
for k, v := range input { | ||
if v != nil { | ||
redacted, err := redact.Redact(v, k, additionalRedactors) | ||
if v == nil { | ||
continue | ||
} | ||
//If the file is a .tar file, it must not be redacted. Instead it is decompressed and each file inside the | ||
//tar is decompressed, redacted and compressed back into the tar. | ||
if filepath.Ext(k) == ".tar" { | ||
tarFile := bytes.NewBuffer(v) | ||
unRedacted, tarHeaders, err := untarFile(tarFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
redacted, err := redactMap(unRedacted, additionalRedactors) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result[k], err = tarFiles(redacted, tarHeaders) | ||
if err != nil { | ||
return nil, err | ||
} | ||
result[k] = redacted | ||
//Content of the tar file was redacted. Continue to next file. | ||
continue | ||
} | ||
redacted, err := redact.Redact(v, k, additionalRedactors) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can corrupt There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes! Only when the file is inside the folder. I suggested a possible upgrade to the code to decompress .tgz and .tar.gz files too. If you think it would be useful I can work in this functionality! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should address this in a separate pr if we choose to |
||
if err != nil { | ||
return nil, err | ||
} | ||
result[k] = redacted | ||
} | ||
return result, nil | ||
} | ||
|
||
func tarFiles(tarContent map[string][]byte, tarHeaders map[string]*tar.Header) ([]byte, error) { | ||
buff := new(bytes.Buffer) | ||
tw := tar.NewWriter(buff) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, definitely. It is way more robust in the way you suggest |
||
defer tw.Close() | ||
for p, f := range tarContent { | ||
if tarHeaders[p].FileInfo().IsDir() { | ||
err := tw.WriteHeader(tarHeaders[p]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
continue | ||
} | ||
//File size must be recalculated in case the redactor added some bytes when redacting. | ||
tarHeaders[p].Size = int64(binary.Size(f)) | ||
err := tw.WriteHeader(tarHeaders[p]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
_, err = tw.Write(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
err := tw.Close() | ||
emosbaugh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return buff.Bytes(), nil | ||
|
||
} | ||
|
||
func untarFile(tarFile *bytes.Buffer) (map[string][]byte, map[string]*tar.Header, error) { | ||
tarReader := tar.NewReader(tarFile) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defer close There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you refering to the tar reader? I think it has not a Close() function. I've used defer for the tarWriter |
||
tarHeaders := make(map[string]*tar.Header) | ||
tarContent := make(map[string][]byte) | ||
for { | ||
header, err := tarReader.Next() | ||
if err != nil { | ||
if err != io.EOF { | ||
return nil, nil, err | ||
} | ||
break | ||
} | ||
file := new(bytes.Buffer) | ||
_, err = io.Copy(file, tarReader) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
tarContent[header.Name] = file.Bytes() | ||
tarHeaders[header.Name] = header | ||
|
||
} | ||
return tarContent, tarHeaders, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does it need to pipe into
cat
? Can the pipe be removed and thensh
is not needed, so it's just thetar
command by itself.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Dimitriy! In some cases, depending on the version of tar installed in the container, we may get the error:
tar: Refusing to write archive contents to terminal (missing -f option?)
tar: Error is not recoverable: exiting now
Talking about this matter with Andrew and Ethan we came to the conclusion that piping it would be the best way to avoid the error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@manavellamnimble can you add a comment in the code explaining this. I can see someone changing this and breaking things inadvertently.