Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡️ improve microsoft.groups.length #5153

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions providers/ms365/resources/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ package resources

import (
"context"
"errors"

abstractions "github.com/microsoft/kiota-abstractions-go"
"github.com/microsoftgraph/msgraph-sdk-go/groups"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"go.mondoo.com/cnquery/v11/llx"
"go.mondoo.com/cnquery/v11/providers/ms365/connection"
"go.mondoo.com/cnquery/v11/types"
)

func (m *mqlMicrosoftGroup) id() (string, error) {
return m.Id.Data, nil
func (a *mqlMicrosoftGroup) id() (string, error) {
return a.Id.Data, nil
}

func (a *mqlMicrosoftGroup) members() ([]interface{}, error) {
Expand All @@ -35,9 +38,12 @@ func (a *mqlMicrosoftGroup) members() ([]interface{}, error) {
Top: &top,
}
ctx := context.Background()
resp, err := graphClient.Groups().ByGroupId(groupId).Members().Get(ctx, &groups.ItemMembersRequestBuilderGetRequestConfiguration{
QueryParameters: queryParams,
})
resp, err := graphClient.Groups().
ByGroupId(groupId).
Members().
Get(ctx, &groups.ItemMembersRequestBuilderGetRequestConfiguration{
QueryParameters: queryParams,
})
if err != nil {
return nil, transformError(err)
}
Expand All @@ -60,9 +66,10 @@ func (a *mqlMicrosoftGroup) members() ([]interface{}, error) {
continue
}

newUserResource, err := a.MqlRuntime.NewResource(a.MqlRuntime, "microsoft.user", map[string]*llx.RawData{
"id": llx.StringDataPtr(memberId),
})
newUserResource, err := a.MqlRuntime.
NewResource(a.MqlRuntime, "microsoft.user", map[string]*llx.RawData{
"id": llx.StringDataPtr(memberId),
})
if err != nil {
return nil, err
}
Expand All @@ -72,7 +79,12 @@ func (a *mqlMicrosoftGroup) members() ([]interface{}, error) {
return res, nil
}

func (a *mqlMicrosoft) groups() ([]interface{}, error) {
func (a *mqlMicrosoft) groups() (*mqlMicrosoftGroups, error) {
mqlResource, err := CreateResource(a.MqlRuntime, "microsoft.groups", map[string]*llx.RawData{})
return mqlResource.(*mqlMicrosoftGroups), err
}

func (a *mqlMicrosoftGroups) list() ([]interface{}, error) {
conn := a.MqlRuntime.Connection.(*connection.Ms365Connection)
graphClient, err := conn.GraphClient()
if err != nil {
Expand Down Expand Up @@ -117,3 +129,24 @@ func (a *mqlMicrosoft) groups() ([]interface{}, error) {

return res, nil
}

func (a *mqlMicrosoftGroups) length() (int64, error) {
conn := a.MqlRuntime.Connection.(*connection.Ms365Connection)
graphClient, err := conn.GraphClient()
if err != nil {
return 0, err
}

opts := &groups.CountRequestBuilderGetRequestConfiguration{Headers: abstractions.NewRequestHeaders()}
opts.Headers.Add("ConsistencyLevel", "eventual")
length, err := graphClient.Groups().Count().Get(context.Background(), opts)
if err != nil {
return 0, err
}
if length == nil {
// This should never happen, but we better check
return 0, errors.New("unable to count groups, counter parameter API returned nil")
}

return int64(*length), nil
}
11 changes: 9 additions & 2 deletions providers/ms365/resources/ms365.lr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ microsoft {
// List of users
users() []microsoft.user
// List of groups
groups() []microsoft.group
groups() microsoft.groups
// List of domains
domains() []microsoft.domain
// List of applications
Expand All @@ -30,6 +30,13 @@ microsoft {
tenantDomainName() string
}

// Microsoft groups
microsoft.groups {
[]microsoft.group
// Total number of Microsoft groups
length() int
}

// Microsoft Entra tenant
microsoft.tenant @defaults("name") {
// Organization ID
Expand Down Expand Up @@ -830,4 +837,4 @@ private ms365.teams.teamsMeetingPolicyConfig {
private ms365.teams.teamsMessagingPolicyConfig {
// Whether users can report security concerns
allowSecurityEndUserReporting bool
}
}
97 changes: 91 additions & 6 deletions providers/ms365/resources/ms365.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions providers/ms365/resources/ms365.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ resources:
min_mondoo_version: 9.0.0
is_private: true
min_mondoo_version: 5.15.0
microsoft.groups:
fields:
length: {}
list: {}
min_mondoo_version: 9.0.0
microsoft.groups.length:
fields: {}
min_mondoo_version: 9.0.0
microsoft.keyCredential:
fields:
description: {}
Expand Down
Loading