From 04c428a9d86089d5ccb710f6167f6c64cc9e8585 Mon Sep 17 00:00:00 2001 From: Dave Augustus <95467821+dave-augustus@users.noreply.github.com> Date: Tue, 22 Feb 2022 22:33:07 +0530 Subject: [PATCH] Dave/stalwart 27 (#6719) * Validate Zip File Signed-off-by: Pappu Kumar * Done ZIp file Validation Signed-off-by: Pappu Kumar * Test ZIp file Validation Signed-off-by: Pappu Kumar * Key dump check added Signed-off-by: Pappu Kumar * Changes based on PR comments Signed-off-by: Pappu Kumar * Done testing Signed-off-by: Pappu Kumar * Merge conflict Signed-off-by: Pappu Kumar --- .../migrations/pipeline/utility.go | 35 ++++++++++++ .../migrations/pipeline/utility_test.go | 53 +++++++++++++++++++ .../infra-proxy-service/pipeline/models.go | 3 ++ .../testdata/backup/key_dump.json | 0 .../organizations/key_dump.json | 0 5 files changed, 91 insertions(+) create mode 100644 components/infra-proxy-service/testdata/backup/key_dump.json create mode 100644 components/infra-proxy-service/testdata/keydumpnotfound/organizations/key_dump.json diff --git a/components/infra-proxy-service/migrations/pipeline/utility.go b/components/infra-proxy-service/migrations/pipeline/utility.go index 622907d9901..d3c08cc9de8 100644 --- a/components/infra-proxy-service/migrations/pipeline/utility.go +++ b/components/infra-proxy-service/migrations/pipeline/utility.go @@ -4,6 +4,9 @@ import ( "archive/zip" "context" "encoding/json" + + "errors" + "io" "os" "path" @@ -342,6 +345,37 @@ func Unzip(ctx context.Context, mst storage.MigrationStorage, result pipeline.Re return result, nil } +func ValidateZip(ctx context.Context, st storage.Storage, mst storage.MigrationStorage, result pipeline.Result) (pipeline.Result, error) { + var err error + foundOrg := false + // Find under unzip folder where org folder exists + if err := filepath.Walk(result.Meta.UnzipFolder, func(path string, f os.FileInfo, err error) error { + + // check if base path is `organizations` + if filepath.Base(path) == "organizations" { + // reassign unzipFolder + foundOrg = true + result.Meta.UnzipFolder = filepath.Dir(path) + } + return nil + }); err != nil { + log.Errorf("Failed to find organizations folder for migration id %s : %s", result.Meta.MigrationID, err.Error()) + return result, err + } + if !foundOrg { + return result, errors.New("cannot find organizations folder") + } + // Check if the reassigned unzip folder contains key_dump.json + _, err = os.Stat(result.Meta.UnzipFolder + "/key_dump.json") + if err != nil { + log.Errorf("Failed to validate unzip folder for migration id %s : %s", result.Meta.MigrationID, err.Error()) + return result, err + } + + result.Meta.IsValid = true + return result, nil +} + // ParseOrgUserAssociation sync the automate org users with chef server org users func ParseOrgUserAssociation(ctx context.Context, st storage.Storage, result pipeline.Result) (pipeline.Result, error) { log.Info("Starting with the parsing org user association for migration id :", result.Meta.MigrationID) @@ -534,4 +568,5 @@ func createMapForOrgAdminsInJson(adminsJson pipeline.AdminsJson) map[string]stri orgAdminsMap[username] = "" } return orgAdminsMap + } diff --git a/components/infra-proxy-service/migrations/pipeline/utility_test.go b/components/infra-proxy-service/migrations/pipeline/utility_test.go index 1f59500df74..1532f691fd9 100644 --- a/components/infra-proxy-service/migrations/pipeline/utility_test.go +++ b/components/infra-proxy-service/migrations/pipeline/utility_test.go @@ -11,6 +11,7 @@ import ( "github.com/chef/automate/components/infra-proxy-service/storage/testDB" "github.com/golang/mock/gomock" "github.com/pkg/errors" + "github.com/stretchr/testify/require" ) func TestStoreOrg(t *testing.T) { @@ -130,6 +131,57 @@ func TestCreatePreview(t *testing.T) { } } +func TestValidateZip(t *testing.T) { + type args struct { + name string + ctx context.Context + st storage.Storage + mst storage.MigrationStorage + result pipeline.Result + requiredErr error + } + + arg := []args{ + { + name: "Validated", + ctx: context.Background(), + st: &testDB.TestDB{}, + mst: &testDB.MigrationDB{}, + result: pipeline.Result{Meta: pipeline.Meta{UnzipFolder: "../../testdata/backup", ServerID: "server1", MigrationID: "mig1"}}, + requiredErr: nil, + }, + { + name: "Organization not found", + ctx: context.Background(), + st: &testDB.TestDB{}, + mst: &testDB.MigrationDB{}, + result: pipeline.Result{Meta: pipeline.Meta{UnzipFolder: "../../testdata/orgnotfound", ServerID: "server1", MigrationID: "mig1"}}, + requiredErr: errors.New("cannot find organizations folder"), + }, + { + name: "KeyDump not found", + ctx: context.Background(), + st: &testDB.TestDB{}, + mst: &testDB.MigrationDB{}, + result: pipeline.Result{Meta: pipeline.Meta{UnzipFolder: "../../testdata/keydumpnotfound", ServerID: "server1", MigrationID: "mig1"}}, + requiredErr: errors.New("stat ../../testdata/keydumpnotfound/key_dump.json: no such file or directory"), + }, + } + + for _, ar := range arg { + t.Run(ar.name, func(t *testing.T) { + res, err := ValidateZip(ar.ctx, ar.st, ar.mst, ar.result) + if err != nil { + require.Equal(t, err.Error(), ar.requiredErr.Error()) + } else { + require.NoError(t, err) + require.True(t, res.Meta.IsValid) + require.NotEmpty(t, res.Meta.UnzipFolder) + } + }) + } +} + func TestUserOrgAssociation(t *testing.T) { deleteBackUp := "../../testdata/deleteBackup/" skipBackup := "../../testdata/skipBackup/" @@ -160,6 +212,7 @@ func TestUserOrgAssociation(t *testing.T) { if !reflect.DeepEqual(got.ParsedResult.OrgsUsers, tt.want1.ParsedResult.OrgsUsers) { t.Errorf("ParseOrgUserAssociation() got = %v, want %v", got.ParsedResult.OrgsUsers, tt.want1.ParsedResult.OrgsUsers) } + }) } } diff --git a/components/infra-proxy-service/pipeline/models.go b/components/infra-proxy-service/pipeline/models.go index 74a04a095a8..5360c876b09 100644 --- a/components/infra-proxy-service/pipeline/models.go +++ b/components/infra-proxy-service/pipeline/models.go @@ -33,6 +33,9 @@ type Meta struct { // Migration ID MigrationID string `json:"migration_id"` + + // To check whether zip file extracted successfully + IsValid bool `json:"is_valid"` } type StageResult struct { diff --git a/components/infra-proxy-service/testdata/backup/key_dump.json b/components/infra-proxy-service/testdata/backup/key_dump.json new file mode 100644 index 00000000000..e69de29bb2d diff --git a/components/infra-proxy-service/testdata/keydumpnotfound/organizations/key_dump.json b/components/infra-proxy-service/testdata/keydumpnotfound/organizations/key_dump.json new file mode 100644 index 00000000000..e69de29bb2d