Skip to content

Commit

Permalink
Dave/stalwart 27 (#6719)
Browse files Browse the repository at this point in the history
* Validate Zip File

Signed-off-by: Pappu Kumar <pappu.kumar@progress.com>

* Done ZIp file Validation

Signed-off-by: Pappu Kumar <pappu.kumar@progress.com>

* Test ZIp file Validation

Signed-off-by: Pappu Kumar <pappu.kumar@progress.com>

* Key dump check added

Signed-off-by: Pappu Kumar <pappu.kumar@progress.com>

* Changes based on PR comments

Signed-off-by: Pappu Kumar <pappu.kumar@progress.com>

* Done testing

Signed-off-by: Pappu Kumar <pappu.kumar@progress.com>

* Merge conflict

Signed-off-by: Pappu Kumar <pappu.kumar@progress.com>
  • Loading branch information
GorillaGigabytes authored and vinay033 committed May 16, 2022
1 parent 3845a1e commit cc41d57
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
35 changes: 35 additions & 0 deletions components/infra-proxy-service/migrations/pipeline/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"archive/zip"
"context"
"encoding/json"

"errors"

"io"
"os"
"path"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -534,4 +568,5 @@ func createMapForOrgAdminsInJson(adminsJson pipeline.AdminsJson) map[string]stri
orgAdminsMap[username] = ""
}
return orgAdminsMap

}
53 changes: 53 additions & 0 deletions components/infra-proxy-service/migrations/pipeline/utility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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/"
Expand Down Expand Up @@ -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)
}

})
}
}
3 changes: 3 additions & 0 deletions components/infra-proxy-service/pipeline/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Empty file.
Empty file.

0 comments on commit cc41d57

Please sign in to comment.