Skip to content

Commit

Permalink
⚡️ improve microsoft.groups.length
Browse files Browse the repository at this point in the history
Closes #5109

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Feb 21, 2025
1 parent 03ab221 commit a6b2e83
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 25 deletions.
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
}
9 changes: 8 additions & 1 deletion 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.users
// 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
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.

20 changes: 11 additions & 9 deletions providers/ms365/resources/ms365.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,19 @@ resources:
mailEnabled: {}
mailNickname: {}
members: {}
membershipRule: {}
membershipRuleProcessingState: {}
membershipRule:
min_mondoo_version: 9.0.0
membershipRuleProcessingState:
min_mondoo_version: 9.0.0
securityEnabled: {}
visibility: {}
visibility:
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.keyCredential:
fields:
Expand Down Expand Up @@ -219,11 +227,6 @@ resources:
version: {}
is_private: true
min_mondoo_version: 9.0.0
microsoft.rolemanagement.roledefinitions:
fields:
filter: {}
list: {}
min_mondoo_version: 9.0.0
microsoft.roles:
fields:
filter: {}
Expand Down Expand Up @@ -392,7 +395,6 @@ resources:
filter: {}
list: {}
search: {}
userPrincipalName: {}
min_mondoo_version: 9.0.0
ms365.exchangeonline:
fields:
Expand Down

0 comments on commit a6b2e83

Please sign in to comment.