Skip to content

Commit

Permalink
AST-10757: transform teams, users and report
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinoantunes1 committed Mar 14, 2022
1 parent b476b5d commit 51508ed
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 60 deletions.
2 changes: 1 addition & 1 deletion internal/app/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func getEncryptedKey(key []byte) (encKey []byte, err error) {
return
}

func NewMarshalDataSource(obj interface{}) func() ([]byte, error) {
func NewJSONDataSource(obj interface{}) func() ([]byte, error) {
return func() ([]byte, error) {
return json.Marshal(obj)
}
Expand Down
8 changes: 4 additions & 4 deletions internal/app/export/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// TransformTeams flattens teams.
func TransformTeams(teams []*rest.Team) []*rest.Team {
var out []*rest.Team
out := make([]*rest.Team, 0)
for _, e := range teams {
e.ParendID = 0
e.Name = strings.ReplaceAll(strings.TrimLeft(e.FullName, "/"), "/", "_")
Expand All @@ -23,7 +23,7 @@ func TransformTeams(teams []*rest.Team) []*rest.Team {
// TransformUsers reassigns users in the context of flatten teams.
// Note "teams" list passed must be the original, non-flattened, list
func TransformUsers(users []*rest.User, teams []*rest.Team) []*rest.User {
var out []*rest.User
out := make([]*rest.User, 0)
for _, e := range users {
for _, teamID := range e.TeamIDs {
e.TeamIDs = append(e.TeamIDs, getAllChildTeamIDs(teamID, teams)...)
Expand All @@ -35,7 +35,7 @@ func TransformUsers(users []*rest.User, teams []*rest.Team) []*rest.User {

// TransformSamlTeamMappings updates team mapping in the context of flatten teams.
func TransformSamlTeamMappings(samlTeamMappings []*rest.SamlTeamMapping) []*rest.SamlTeamMapping {
var out []*rest.SamlTeamMapping
out := make([]*rest.SamlTeamMapping, 0)
for _, e := range samlTeamMappings {
e.TeamFullPath = "/" + strings.ReplaceAll(strings.TrimLeft(e.TeamFullPath, "/"), "/", "_")
out = append(out, e)
Expand All @@ -58,7 +58,7 @@ func TransformScanReport(xml []byte) ([]byte, error) {

// getAllChildTeamIDs returns all child team ids relative to a root team id.
func getAllChildTeamIDs(root int, teams []*rest.Team) []int {
var out []int
out := make([]int, 0)
for _, e := range teams {
if e.ParendID == root {
out = append(out, e.ID)
Expand Down
27 changes: 15 additions & 12 deletions internal/integration/rest/apiclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ const (
type Client interface {
Authenticate(username, password string) error
PostResponseBody(endpoint string, body io.Reader) ([]byte, error)
GetUsers() ([]User, error)
GetUsers() ([]*User, error)
GetRoles() ([]byte, error)
GetTeams() ([]Team, error)
GetTeams() ([]*Team, error)
GetLdapServers() ([]byte, error)
GetLdapRoleMappings() ([]byte, error)
GetLdapTeamMappings() ([]byte, error)
GetSamlIdentityProviders() ([]byte, error)
GetSamlRoleMappings() ([]byte, error)
GetSamlTeamMappings() ([]SamlTeamMapping, error)
GetSamlTeamMappings() ([]*SamlTeamMapping, error)
GetProjectsWithLastScanID(fromDate string, offset, limit int) (*[]ProjectWithLastScanID, error)
GetTriagedResultsByScanID(scanID int) (*[]TriagedScanResult, error)
CreateScanReport(scanID int, reportType string, retry Retry) ([]byte, error)
Expand Down Expand Up @@ -219,18 +219,20 @@ func (c *APIClient) getReportStatusResponse(report ReportResponse) (*StatusRespo
return &status, nil
}

func (c *APIClient) GetUsers() ([]User, error) {
var users []User
return users, c.unmarshalResponseBody(usersEndpoint, &users)
func (c *APIClient) GetUsers() ([]*User, error) {
var users []*User
err := c.unmarshalResponseBody(usersEndpoint, &users)
return users, err
}

func (c *APIClient) GetRoles() ([]byte, error) {
return c.getResponseBody(rolesEndpoint)
}

func (c *APIClient) GetTeams() ([]Team, error) {
var teams []Team
return teams, c.unmarshalResponseBody(teamsEndpoint, &teams)
func (c *APIClient) GetTeams() ([]*Team, error) {
var teams []*Team
err := c.unmarshalResponseBody(teamsEndpoint, &teams)
return teams, err
}

func (c *APIClient) GetLdapServers() ([]byte, error) {
Expand All @@ -253,9 +255,10 @@ func (c *APIClient) GetSamlRoleMappings() ([]byte, error) {
return c.getResponseBody(samlRoleMappingsEndpoint)
}

func (c *APIClient) GetSamlTeamMappings() ([]SamlTeamMapping, error) {
var samlTeamMappings []SamlTeamMapping
return samlTeamMappings, c.unmarshalResponseBody(teamMappingsEndpoint, &samlTeamMappings)
func (c *APIClient) GetSamlTeamMappings() ([]*SamlTeamMapping, error) {
var samlTeamMappings []*SamlTeamMapping
err := c.unmarshalResponseBody(teamMappingsEndpoint, &samlTeamMappings)
return samlTeamMappings, err
}

func (c *APIClient) getReportIDStatus(reportID int) ([]byte, error) {
Expand Down
6 changes: 3 additions & 3 deletions internal/integration/rest/apiclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func TestAPIClient_GetUsers(t *testing.T) {
result, err := client.GetUsers()

assert.NoError(t, err)
expected := []User{
expected := []*User{
{ID: 1, UserName: "test1", LastLoginDate: "2021-08-17T12:22:28.2331383Z", Active: true},
{ID: 2, UserName: "test2", LastLoginDate: "2021-08-17T12:22:28.2331383Z", Active: true},
{ID: 3, UserName: "test3", LastLoginDate: "2021-08-17T12:22:28.2331383Z", Active: true},
Expand Down Expand Up @@ -213,7 +213,7 @@ func TestAPIClient_GetTeams(t *testing.T) {
result, err := client.GetTeams()

assert.NoError(t, err)
expected := []Team{
expected := []*Team{
{ID: 1, Name: "test1", FullName: "/CxServer/test1", ParendID: 0},
{ID: 2, Name: "test2", FullName: "/CxServer/test2", ParendID: 1},
{ID: 3, Name: "test3", FullName: "/CxServer/test3", ParendID: 1},
Expand All @@ -240,7 +240,7 @@ func TestAPIClient_GetSamlTeamMappings(t *testing.T) {
result, err := client.GetSamlTeamMappings()

assert.NoError(t, err)
expected := []SamlTeamMapping{
expected := []*SamlTeamMapping{
{ID: 3, SamlIdentityProviderID: 2, TeamID: 4, TeamFullPath: "/CxServer/Mapped SAML", SamlAttributeValue: "TeamA"},
}
assert.Equal(t, expected, result)
Expand Down
13 changes: 10 additions & 3 deletions internal/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,12 @@ func fetchUsersData(client rest.Client, exporter export2.Exporter) error {
if usersErr != nil {
return errors.Wrap(usersErr, "failed getting users")
}
if err := exporter.AddFileWithDataSource(export2.UsersFileName, export2.NewMarshalDataSource(users)); err != nil {
teams, teamsErr := client.GetTeams()
if teamsErr != nil {
return errors.Wrap(teamsErr, "failed getting teams")
}
usersDataSource := export2.NewJSONDataSource(export2.TransformUsers(users, teams))
if err := exporter.AddFileWithDataSource(export2.UsersFileName, usersDataSource); err != nil {
return err
}
if err := exporter.AddFileWithDataSource(export2.RolesFileName, client.GetRoles); err != nil {
Expand Down Expand Up @@ -267,7 +272,8 @@ func fetchTeamsData(client rest.Client, exporter export2.Exporter) error {
if teamsErr != nil {
return errors.Wrap(teamsErr, "failed getting teams")
}
if err := exporter.AddFileWithDataSource(export2.TeamsFileName, export2.NewMarshalDataSource(teams)); err != nil {
teamsDataSource := export2.NewJSONDataSource(export2.TransformTeams(teams))
if err := exporter.AddFileWithDataSource(export2.TeamsFileName, teamsDataSource); err != nil {
return err
}
if err := exporter.AddFileWithDataSource(export2.LdapTeamMappingsFileName, client.GetLdapTeamMappings); err != nil {
Expand All @@ -277,7 +283,8 @@ func fetchTeamsData(client rest.Client, exporter export2.Exporter) error {
if samlTeamMappingsErr != nil {
return errors.Wrap(samlTeamMappingsErr, "failed getting saml team mappings")
}
if err := exporter.AddFileWithDataSource(export2.SamlTeamMappingsFileName, export2.NewMarshalDataSource(samlTeamMappings)); err != nil {
samlTeamMappingsDataSource := export2.NewJSONDataSource(export2.TransformSamlTeamMappings(samlTeamMappings))
if err := exporter.AddFileWithDataSource(export2.SamlTeamMappingsFileName, samlTeamMappingsDataSource); err != nil {
return err
}
if _, fileErr := os.Stat(export2.LdapServersFileName); os.IsNotExist(fileErr) {
Expand Down
52 changes: 36 additions & 16 deletions internal/process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type mockExpectProps struct {

type usersExpect struct {
Users mockExpectProps
Teams mockExpectProps
Roles mockExpectProps
LdapRoleMappings mockExpectProps
SamlRoleMappings mockExpectProps
Expand All @@ -49,9 +50,14 @@ type teamsExpect struct {
func fetchUsersSetupExpects(client *mock_integration_rest.MockClient, expect *usersExpect) {
client.EXPECT().
GetUsers().
Return([]rest.User{}, expect.Users.ReturnError).
Return([]*rest.User{}, expect.Users.ReturnError).
MinTimes(expect.Users.RunCount).
MaxTimes(expect.Users.RunCount)
client.EXPECT().
GetTeams().
Return([]*rest.Team{}, expect.Teams.ReturnError).
MinTimes(expect.Teams.RunCount).
MaxTimes(expect.Teams.RunCount)
client.EXPECT().
GetRoles().
Return([]byte{}, expect.Roles.ReturnError).
Expand Down Expand Up @@ -82,7 +88,7 @@ func fetchUsersSetupExpects(client *mock_integration_rest.MockClient, expect *us
func fetchTeamsSetupExpects(client *mock_integration_rest.MockClient, expect *teamsExpect) {
client.EXPECT().
GetTeams().
Return([]rest.Team{}, expect.Teams.ReturnError).
Return([]*rest.Team{}, expect.Teams.ReturnError).
MinTimes(expect.Teams.RunCount).
MaxTimes(expect.Teams.RunCount)
client.EXPECT().
Expand All @@ -92,7 +98,7 @@ func fetchTeamsSetupExpects(client *mock_integration_rest.MockClient, expect *te
MaxTimes(expect.LdapTeamMappings.RunCount)
client.EXPECT().
GetSamlTeamMappings().
Return([]rest.SamlTeamMapping{}, expect.SamlTeamMappings.ReturnError).
Return([]*rest.SamlTeamMapping{}, expect.SamlTeamMappings.ReturnError).
MinTimes(expect.SamlTeamMappings.RunCount).
MaxTimes(expect.SamlTeamMappings.RunCount)
client.EXPECT().
Expand Down Expand Up @@ -283,13 +289,15 @@ func TestFetchUsersData(t *testing.T) {
{
usersExpect{
Users: mockExpectProps{nil, 1},
Teams: mockExpectProps{nil, 1},
Roles: mockExpectProps{rolesErr, 1},
},
rolesErr,
},
{
usersExpect{
Users: mockExpectProps{nil, 1},
Teams: mockExpectProps{nil, 1},
Roles: mockExpectProps{nil, 1},
LdapRoleMappings: mockExpectProps{ldapMappingsErr, 1},
},
Expand All @@ -298,6 +306,7 @@ func TestFetchUsersData(t *testing.T) {
{
usersExpect{
Users: mockExpectProps{nil, 1},
Teams: mockExpectProps{nil, 1},
Roles: mockExpectProps{nil, 1},
LdapRoleMappings: mockExpectProps{nil, 1},
SamlRoleMappings: mockExpectProps{samlMappingsErr, 1},
Expand All @@ -307,6 +316,7 @@ func TestFetchUsersData(t *testing.T) {
{
usersExpect{
Users: mockExpectProps{nil, 1},
Teams: mockExpectProps{nil, 1},
Roles: mockExpectProps{nil, 1},
LdapRoleMappings: mockExpectProps{nil, 1},
SamlRoleMappings: mockExpectProps{nil, 1},
Expand All @@ -317,6 +327,7 @@ func TestFetchUsersData(t *testing.T) {
{
usersExpect{
Users: mockExpectProps{nil, 1},
Teams: mockExpectProps{nil, 1},
Roles: mockExpectProps{nil, 1},
LdapRoleMappings: mockExpectProps{nil, 1},
SamlRoleMappings: mockExpectProps{nil, 1},
Expand Down Expand Up @@ -360,6 +371,7 @@ func TestFetchUsersData(t *testing.T) {
{
fetchMockExpects: usersExpect{
Users: mockExpectProps{nil, 1},
Teams: mockExpectProps{nil, 1},
},
writeMockExpects: usersExpect{
Users: mockExpectProps{usersErr, 1},
Expand All @@ -369,6 +381,7 @@ func TestFetchUsersData(t *testing.T) {
{
fetchMockExpects: usersExpect{
Users: mockExpectProps{nil, 1},
Teams: mockExpectProps{nil, 1},
Roles: mockExpectProps{nil, 1},
},
writeMockExpects: usersExpect{
Expand All @@ -380,6 +393,7 @@ func TestFetchUsersData(t *testing.T) {
{
fetchMockExpects: usersExpect{
Users: mockExpectProps{nil, 1},
Teams: mockExpectProps{nil, 1},
Roles: mockExpectProps{nil, 1},
LdapRoleMappings: mockExpectProps{nil, 1},
},
Expand All @@ -393,6 +407,7 @@ func TestFetchUsersData(t *testing.T) {
{
fetchMockExpects: usersExpect{
Users: mockExpectProps{nil, 1},
Teams: mockExpectProps{nil, 1},
Roles: mockExpectProps{nil, 1},
LdapRoleMappings: mockExpectProps{nil, 1},
SamlRoleMappings: mockExpectProps{nil, 1},
Expand All @@ -408,6 +423,7 @@ func TestFetchUsersData(t *testing.T) {
{
fetchMockExpects: usersExpect{
Users: mockExpectProps{nil, 1},
Teams: mockExpectProps{nil, 1},
Roles: mockExpectProps{nil, 1},
LdapRoleMappings: mockExpectProps{nil, 1},
SamlRoleMappings: mockExpectProps{nil, 1},
Expand All @@ -425,6 +441,7 @@ func TestFetchUsersData(t *testing.T) {
{
fetchMockExpects: usersExpect{
Users: mockExpectProps{nil, 1},
Teams: mockExpectProps{nil, 1},
Roles: mockExpectProps{nil, 1},
LdapRoleMappings: mockExpectProps{nil, 1},
SamlRoleMappings: mockExpectProps{nil, 1},
Expand Down Expand Up @@ -459,6 +476,7 @@ func TestFetchUsersData(t *testing.T) {
client := mock_integration_rest.NewMockClient(gomock.NewController(t))
fetchUsersSetupExpects(client, &usersExpect{
Users: mockExpectProps{nil, 1},
Teams: mockExpectProps{nil, 1},
Roles: mockExpectProps{nil, 1},
LdapRoleMappings: mockExpectProps{nil, 1},
SamlRoleMappings: mockExpectProps{nil, 1},
Expand Down Expand Up @@ -1008,7 +1026,8 @@ func TestFetchSelectedData(t *testing.T) {
t.Run("export users success case", func(t *testing.T) {
ctrl := gomock.NewController(t)
client := mock_integration_rest.NewMockClient(ctrl)
client.EXPECT().GetUsers().Return([]rest.User{}, nil)
client.EXPECT().GetUsers().Return([]*rest.User{}, nil)
client.EXPECT().GetTeams().Return([]*rest.Team{}, nil)
exporter := mock_app_export.NewMockExporter(ctrl)
exporter.EXPECT().AddFileWithDataSource(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
args := Args{
Expand All @@ -1024,7 +1043,8 @@ func TestFetchSelectedData(t *testing.T) {
t.Run("export users fails if fetch or write fails", func(t *testing.T) {
ctrl := gomock.NewController(t)
client := mock_integration_rest.NewMockClient(ctrl)
client.EXPECT().GetUsers().Return([]rest.User{}, nil)
client.EXPECT().GetUsers().Return([]*rest.User{}, nil)
client.EXPECT().GetTeams().Return([]*rest.Team{}, nil)
exporter := mock_app_export.NewMockExporter(ctrl)
exporter.EXPECT().AddFileWithDataSource(gomock.Eq(export.UsersFileName), gomock.Any()).
Return(nil)
Expand All @@ -1043,9 +1063,9 @@ func TestFetchSelectedData(t *testing.T) {
t.Run("export users and teams success case", func(t *testing.T) {
ctrl := gomock.NewController(t)
client := mock_integration_rest.NewMockClient(ctrl)
client.EXPECT().GetUsers().Return([]rest.User{}, nil)
client.EXPECT().GetTeams().Return([]rest.Team{}, nil)
client.EXPECT().GetSamlTeamMappings().Return([]rest.SamlTeamMapping{}, nil)
client.EXPECT().GetUsers().Return([]*rest.User{}, nil)
client.EXPECT().GetTeams().Return([]*rest.Team{}, nil).Times(2)
client.EXPECT().GetSamlTeamMappings().Return([]*rest.SamlTeamMapping{}, nil)
exporter := mock_app_export.NewMockExporter(ctrl)
exporter.EXPECT().AddFileWithDataSource(gomock.Any(), gomock.Any()).Return(nil).AnyTimes()
args := Args{
Expand All @@ -1061,8 +1081,8 @@ func TestFetchSelectedData(t *testing.T) {
t.Run("export users and teams fail if fetch or write fails", func(t *testing.T) {
ctrl := gomock.NewController(t)
client := mock_integration_rest.NewMockClient(ctrl)
client.EXPECT().GetUsers().Return([]rest.User{}, nil)
client.EXPECT().GetTeams().Return([]rest.Team{}, nil)
client.EXPECT().GetUsers().Return([]*rest.User{}, nil)
client.EXPECT().GetTeams().Return([]*rest.Team{}, nil).Times(2)
exporter := mock_app_export.NewMockExporter(ctrl)
exporter.EXPECT().AddFileWithDataSource(gomock.Eq(export.UsersFileName), gomock.Any()).
Return(nil)
Expand Down Expand Up @@ -1097,9 +1117,9 @@ func TestFetchSelectedData(t *testing.T) {
{ID: 1, LastScanID: 1},
{ID: 2, LastScanID: 2},
}
client.EXPECT().GetUsers().Return([]rest.User{}, nil)
client.EXPECT().GetTeams().Return([]rest.Team{}, nil)
client.EXPECT().GetSamlTeamMappings().Return([]rest.SamlTeamMapping{}, nil)
client.EXPECT().GetUsers().Return([]*rest.User{}, nil)
client.EXPECT().GetTeams().Return([]*rest.Team{}, nil).Times(2)
client.EXPECT().GetSamlTeamMappings().Return([]*rest.SamlTeamMapping{}, nil)
client.EXPECT().
GetProjectsWithLastScanID(gomock.Any(), gomock.Eq(0), gomock.Any()).
Return(&projectPage, nil)
Expand Down Expand Up @@ -1132,9 +1152,9 @@ func TestFetchSelectedData(t *testing.T) {
t.Run("export users, teams and results fails if result processing fails", func(t *testing.T) {
ctrl := gomock.NewController(t)
client := mock_integration_rest.NewMockClient(ctrl)
client.EXPECT().GetUsers().Return([]rest.User{}, nil)
client.EXPECT().GetTeams().Return([]rest.Team{}, nil)
client.EXPECT().GetSamlTeamMappings().Return([]rest.SamlTeamMapping{}, nil)
client.EXPECT().GetUsers().Return([]*rest.User{}, nil)
client.EXPECT().GetTeams().Return([]*rest.Team{}, nil).Times(2)
client.EXPECT().GetSamlTeamMappings().Return([]*rest.SamlTeamMapping{}, nil)
client.EXPECT().
GetProjectsWithLastScanID(gomock.Any(), gomock.Eq(0), gomock.Any()).
Return(&[]rest.ProjectWithLastScanID{}, fmt.Errorf("failed fetching projects"))
Expand Down
Loading

0 comments on commit 51508ed

Please sign in to comment.