diff --git a/providers/ms365/resources/iterator.go b/providers/ms365/resources/iterator.go new file mode 100644 index 0000000000..a2fba1ef2b --- /dev/null +++ b/providers/ms365/resources/iterator.go @@ -0,0 +1,28 @@ +// Copyright (c) Mondoo, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package resources + +import ( + "context" + + abstractions "github.com/microsoft/kiota-abstractions-go" + "github.com/microsoft/kiota-abstractions-go/serialization" + msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core" +) + +func iterate[T interface{}](ctx context.Context, res interface{}, adapter abstractions.RequestAdapter, constructorFunc serialization.ParsableFactory) ([]T, error) { + resp := []T{} + iterator, err := msgraphgocore.NewPageIterator[T](res, adapter, constructorFunc) + if err != nil { + return nil, transformError(err) + } + err = iterator.Iterate(ctx, func(u T) bool { + resp = append(resp, u) + return true + }) + if err != nil { + return nil, transformError(err) + } + return resp, nil +} diff --git a/providers/ms365/resources/serviceprincipals.go b/providers/ms365/resources/serviceprincipals.go index 314af3b278..5956b6b75a 100644 --- a/providers/ms365/resources/serviceprincipals.go +++ b/providers/ms365/resources/serviceprincipals.go @@ -6,6 +6,7 @@ package resources import ( "context" + "github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/microsoftgraph/msgraph-sdk-go/serviceprincipals" "go.mondoo.com/cnquery/v10/llx" "go.mondoo.com/cnquery/v10/providers-sdk/v1/plugin" @@ -50,7 +51,7 @@ func fetchServicePrincipals(runtime *plugin.Runtime, conn *connection.Ms365Conne graphClient, err := conn.GraphClient() if err != nil { return nil, err - } // TODO: what if we have more than 1k SPs? + } ctx := context.Background() resp, err := graphClient.ServicePrincipals().Get(ctx, &serviceprincipals.ServicePrincipalsRequestBuilderGetRequestConfiguration{ QueryParameters: params, @@ -58,9 +59,11 @@ func fetchServicePrincipals(runtime *plugin.Runtime, conn *connection.Ms365Conne if err != nil { return nil, transformError(err) } - + sps, err := iterate[*models.ServicePrincipal](ctx, resp, graphClient.GetAdapter(), serviceprincipals.CreateDeltaGetResponseFromDiscriminatorValue) + if err != nil { + return nil, transformError(err) + } res := []interface{}{} - sps := resp.GetValue() for _, sp := range sps { hideApp := stringx.Contains(sp.GetTags(), "HideApp") assignments := []interface{}{} diff --git a/providers/ms365/resources/users.go b/providers/ms365/resources/users.go index ba7d2be96c..4ce2af2f84 100644 --- a/providers/ms365/resources/users.go +++ b/providers/ms365/resources/users.go @@ -6,6 +6,7 @@ package resources import ( "context" + "github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/microsoftgraph/msgraph-sdk-go/users" "go.mondoo.com/cnquery/v10/llx" "go.mondoo.com/cnquery/v10/providers-sdk/v1/util/convert" @@ -29,15 +30,19 @@ func (a *mqlMicrosoft) users() ([]interface{}, error) { "jobTitle", "mail", "mobilePhone", "otherMails", "officeLocation", "postalCode", "state", "streetAddress", "surname", "userPrincipalName", "userType", } ctx := context.Background() + top := int32(999) resp, err := graphClient.Users().Get(ctx, &users.UsersRequestBuilderGetRequestConfiguration{QueryParameters: &users.UsersRequestBuilderGetQueryParameters{ Select: selectFields, + Top: &top, }}) if err != nil { return nil, transformError(err) } - + users, err := iterate[*models.User](ctx, resp, graphClient.GetAdapter(), users.CreateDeltaGetResponseFromDiscriminatorValue) + if err != nil { + return nil, transformError(err) + } res := []interface{}{} - users := resp.GetValue() for _, u := range users { graphUser, err := CreateResource(a.MqlRuntime, "microsoft.user", map[string]*llx.RawData{