Skip to content

Commit

Permalink
🐛 Add explicit select fields for fetching microsoft graph users. Fix … (
Browse files Browse the repository at this point in the history
#898)

Cherry-picks #897 into v7.

Signed-off-by: Preslav <[email protected]>
  • Loading branch information
preslavgerchev authored Feb 7, 2023
1 parent 2f5ff7a commit ba44834
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
7 changes: 3 additions & 4 deletions motor/providers/microsoft/msgraph/msgraphclient/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"github.com/microsoftgraph/msgraph-sdk-go/models/odataerrors"
)

func TransformODataError(err error) error {
oDataErr := err.(*odataerrors.ODataError)
if oDataErr != nil {
func TransformError(err error) error {
oDataErr, ok := err.(*odataerrors.ODataError)
if ok && oDataErr != nil {
if err := oDataErr.GetError(); err != nil {
return errors.Newf("error while performing request. Code: %s, Message: %s", *err.GetCode(), *err.GetMessage())
}
}

return err
}
46 changes: 26 additions & 20 deletions resources/packs/ms365/msgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (m *mqlMicrosoft) GetSettings() ([]interface{}, error) {
ctx := context.Background()
settings, err := graphClient.GroupSettings().Get(ctx, &groupsettings.GroupSettingsRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}
return core.JsonToDictSlice(msgraphconv.NewSettings(settings.GetValue()))
}
Expand All @@ -72,7 +72,7 @@ func (m *mqlMicrosoft) GetOrganizations() ([]interface{}, error) {
ctx := context.Background()
resp, err := graphClient.Organization().Get(ctx, &organization.OrganizationRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

res := []interface{}{}
Expand Down Expand Up @@ -125,7 +125,7 @@ func (m *mqlMicrosoft) GetGroups() ([]interface{}, error) {
ctx := context.Background()
resp, err := graphClient.Groups().Get(ctx, &groups.GroupsRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

res := []interface{}{}
Expand Down Expand Up @@ -168,10 +168,16 @@ func (m *mqlMicrosoft) GetUsers() ([]interface{}, error) {
return nil, err
}

selectFields := []string{
"id", "accountEnabled", "city", "companyName", "country", "createdDateTime", "department", "displayName", "employeeId", "givenName",
"jobTitle", "mail", "mobilePhone", "otherMails", "officeLocation", "postalCode", "state", "streetAddress", "surname", "userPrincipalName", "userType",
}
ctx := context.Background()
resp, err := graphClient.Users().Get(ctx, &users.UsersRequestBuilderGetRequestConfiguration{})
resp, err := graphClient.Users().Get(ctx, &users.UsersRequestBuilderGetRequestConfiguration{QueryParameters: &users.UsersRequestBuilderGetQueryParameters{
Select: selectFields,
}})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

res := []interface{}{}
Expand Down Expand Up @@ -235,7 +241,7 @@ func (m *mqlMicrosoft) GetServiceprincipals() ([]interface{}, error) {
ctx := context.Background()
resp, err := graphClient.ServicePrincipals().Get(ctx, &serviceprincipals.ServicePrincipalsRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

res := []interface{}{}
Expand Down Expand Up @@ -276,7 +282,7 @@ func (m *mqlMicrosoft) GetDomains() ([]interface{}, error) {
ctx := context.Background()
resp, err := graphClient.Domains().Get(ctx, &domains.DomainsRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

res := []interface{}{}
Expand Down Expand Up @@ -334,7 +340,7 @@ func (m *mqlMicrosoftDomain) GetServiceConfigurationRecords() ([]interface{}, er
ctx := context.Background()
resp, err := graphClient.DomainsById(id).ServiceConfigurationRecords().Get(ctx, &domains.DomainsItemServiceConfigurationRecordsRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

res := []interface{}{}
Expand Down Expand Up @@ -434,7 +440,7 @@ func (m *mqlMicrosoft) GetApplications() ([]interface{}, error) {
ctx := context.Background()
resp, err := graphClient.Applications().Get(ctx, &applications.ApplicationsRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

res := []interface{}{}
Expand Down Expand Up @@ -484,7 +490,7 @@ func (m *mqlMicrosoftUser) GetSettings() (interface{}, error) {
ctx := context.Background()
userSettings, err := graphClient.UsersById(id).Settings().Get(ctx, &users.UsersItemSettingsRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

return core.JsonToDict(msgraphconv.NewUserSettings(userSettings))
Expand Down Expand Up @@ -561,7 +567,7 @@ func (m *mqlMicrosoftSecurity) GetLatestSecureScores() (interface{}, error) {
ctx := context.Background()
resp, err := graphClient.Security().SecureScores().Get(ctx, &security.SecuritySecureScoresRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

scores := resp.GetValue()
Expand Down Expand Up @@ -599,7 +605,7 @@ func (m *mqlMicrosoftSecurity) GetSecureScores() ([]interface{}, error) {
ctx := context.Background()
resp, err := graphClient.Security().SecureScores().Get(ctx, &security.SecuritySecureScoresRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

res := []interface{}{}
Expand Down Expand Up @@ -642,7 +648,7 @@ func (m *mqlMicrosoftPolicies) GetAuthorizationPolicy() (interface{}, error) {
ctx := context.Background()
resp, err := graphClient.Policies().AuthorizationPolicy().Get(ctx, &policies.PoliciesAuthorizationPolicyRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

return core.JsonToDict(msgraphconv.NewAuthorizationPolicy(resp))
Expand All @@ -667,7 +673,7 @@ func (m *mqlMicrosoftPolicies) GetIdentitySecurityDefaultsEnforcementPolicy() (i
ctx := context.Background()
policy, err := graphClient.Policies().IdentitySecurityDefaultsEnforcementPolicy().Get(ctx, &policies.PoliciesIdentitySecurityDefaultsEnforcementPolicyRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

return core.JsonToDict(msgraphconv.NewIdentitySecurityDefaultsEnforcementPolicy(policy))
Expand All @@ -693,7 +699,7 @@ func (m *mqlMicrosoftPolicies) GetAdminConsentRequestPolicy() (interface{}, erro
ctx := context.Background()
policy, err := graphClient.Policies().AdminConsentRequestPolicy().Get(ctx, &policies.PoliciesAdminConsentRequestPolicyRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}
return core.JsonToDict(msgraphconv.NewAdminConsentRequestPolicy(policy))
}
Expand All @@ -718,7 +724,7 @@ func (m *mqlMicrosoftPolicies) GetPermissionGrantPolicies() (interface{}, error)
ctx := context.Background()
resp, err := graphClient.Policies().PermissionGrantPolicies().Get(ctx, &policies.PoliciesPermissionGrantPoliciesRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}
return core.JsonToDictSlice(msgraphconv.NewPermissionGrantPolicies(resp.GetValue()))
}
Expand Down Expand Up @@ -746,7 +752,7 @@ func (m *mqlMicrosoftRolemanagement) GetRoleDefinitions() (interface{}, error) {
ctx := context.Background()
resp, err := graphClient.RoleManagement().Directory().RoleDefinitions().Get(ctx, &rolemanagement.RoleManagementDirectoryRoleDefinitionsRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

res := []interface{}{}
Expand Down Expand Up @@ -810,7 +816,7 @@ func (m *mqlMicrosoftRolemanagementRoledefinition) GetAssignments() ([]interface
}
resp, err := graphClient.RoleManagement().Directory().RoleAssignments().Get(ctx, requestConfig)
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

roleAssignments := resp.GetValue()
Expand Down Expand Up @@ -861,7 +867,7 @@ func (m *mqlMicrosoftDevicemanagement) GetDeviceConfigurations() ([]interface{},
ctx := context.Background()
resp, err := graphClient.DeviceManagement().DeviceConfigurations().Get(ctx, &devicemanagement.DeviceManagementDeviceConfigurationsRequestBuilderGetRequestConfiguration{})
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

res := []interface{}{}
Expand Down Expand Up @@ -1048,7 +1054,7 @@ func (m *mqlMicrosoftDevicemanagement) GetDeviceCompliancePolicies() ([]interfac
}
resp, err := graphClient.DeviceManagement().DeviceCompliancePolicies().Get(ctx, requestConfig)
if err != nil {
return nil, msgraphclient.TransformODataError(err)
return nil, msgraphclient.TransformError(err)
}

compliancePolicies := resp.GetValue()
Expand Down

0 comments on commit ba44834

Please sign in to comment.