-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rehydrate last dumps by cluster (#226)
* adding rehydrate command * adding list method in blob/puller * grpc rehydratelatest method * rehydrate new proto * generated grpc * generated puller mock * update protobuff * adding DumpResult to handle all path * typo logs * fixing listFiles function * cleaning * adding unit test for the DumpResult * adding unit test to IsTarGz * adding unit tests for blob storage (local only) * adding files for unit tests * PR comments * fix unit tests * fix linter * fixing unit test * PR comment * linter fix * merging rehydrate to ingest command
- Loading branch information
Showing
34 changed files
with
1,609 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package dump | ||
|
||
import ( | ||
"fmt" | ||
"path" | ||
"regexp" | ||
) | ||
|
||
type DumpResult struct { | ||
clusterName string | ||
RunID string | ||
isDir bool | ||
extension string | ||
} | ||
|
||
const ( | ||
DumpResultClusterNameRegex = `([A-Za-z0-9\.\-_]+)` | ||
DumpResultRunIDRegex = `([a-z0-9]{26})` | ||
DumpResultExtensionRegex = `\.?([a-z0-9\.]+)?` | ||
DumpResultPrefix = "kubehound_" | ||
DumpResultFilenameRegex = DumpResultPrefix + DumpResultClusterNameRegex + "_" + DumpResultRunIDRegex + DumpResultExtensionRegex | ||
DumpResultPathRegex = DumpResultClusterNameRegex + "/" + DumpResultFilenameRegex | ||
|
||
DumpResultTarWriterExtension = "tar.gz" | ||
) | ||
|
||
func NewDumpResult(clusterName, runID string, isCompressed bool) (*DumpResult, error) { | ||
dumpResult := &DumpResult{ | ||
clusterName: clusterName, | ||
RunID: runID, | ||
isDir: true, | ||
} | ||
if isCompressed { | ||
dumpResult.Compressed() | ||
} | ||
|
||
err := dumpResult.Validate() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return dumpResult, nil | ||
} | ||
|
||
func (i *DumpResult) Validate() error { | ||
re := regexp.MustCompile(DumpResultClusterNameRegex) | ||
if !re.MatchString(i.clusterName) { | ||
return fmt.Errorf("Invalid clustername: %q", i.clusterName) | ||
} | ||
|
||
matches := re.FindStringSubmatch(i.clusterName) | ||
if len(matches) == 2 && matches[1] != i.clusterName { | ||
return fmt.Errorf("Invalid clustername: %q", i.clusterName) | ||
} | ||
|
||
re = regexp.MustCompile(DumpResultRunIDRegex) | ||
if !re.MatchString(i.RunID) { | ||
return fmt.Errorf("Invalid runID: %q", i.RunID) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *DumpResult) Compressed() { | ||
i.isDir = false | ||
i.extension = DumpResultTarWriterExtension | ||
} | ||
|
||
// ./<clusterName>/kubehound_<clusterName>_<run_id> | ||
func (i *DumpResult) GetFullPath() string { | ||
filename := i.GetFilename() | ||
|
||
return path.Join(i.clusterName, filename) | ||
} | ||
|
||
func (i *DumpResult) GetFilename() string { | ||
filename := fmt.Sprintf("%s%s_%s", DumpResultPrefix, i.clusterName, i.RunID) | ||
if i.isDir { | ||
return filename | ||
} | ||
|
||
return fmt.Sprintf("%s.%s", filename, i.extension) | ||
} | ||
|
||
func ParsePath(path string) (*DumpResult, error) { | ||
// ./<clusterName>/kubehound_<clusterName>_<run_id>[.tar.gz] | ||
// re := regexp.MustCompile(`([a-z0-9\.\-_]+)/kubehound_([a-z0-9\.-_]+)_([a-z0-9]{26})\.?([a-z0-9\.]+)?`) | ||
re := regexp.MustCompile(DumpResultPathRegex) | ||
if !re.MatchString(path) { | ||
return nil, fmt.Errorf("Invalid path provided: %q", path) | ||
} | ||
|
||
matches := re.FindStringSubmatch(path) | ||
// The cluster name should match (parent dir and in the filename) | ||
if matches[1] != matches[2] { | ||
return nil, fmt.Errorf("Cluster name does not match in the path provided: %q", path) | ||
} | ||
|
||
clusterName := matches[1] | ||
runID := matches[3] | ||
extension := matches[4] | ||
|
||
isCompressed := false | ||
if extension != "" { | ||
isCompressed = true | ||
} | ||
result, err := NewDumpResult(clusterName, runID, isCompressed) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} |
Oops, something went wrong.