Skip to content

Commit

Permalink
✨ Add page iterator for fetching users and service principals in MS365.
Browse files Browse the repository at this point in the history
Signed-off-by: Preslav <[email protected]>
  • Loading branch information
preslavgerchev committed Feb 29, 2024
1 parent 3adc50a commit 966105c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
28 changes: 28 additions & 0 deletions providers/ms365/resources/iterator.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 6 additions & 3 deletions providers/ms365/resources/serviceprincipals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -50,17 +51,19 @@ 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,
})
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{}{}
Expand Down
9 changes: 7 additions & 2 deletions providers/ms365/resources/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down

0 comments on commit 966105c

Please sign in to comment.