Skip to content

Commit

Permalink
⚡️ improve microsoft.applications.length (#5161)
Browse files Browse the repository at this point in the history
Depends on #5156
Contributes to #5109
Relates to #5153

Use Microsoft `$count` parameter to make `microsoft.applications.length` more performant.

https://learn.microsoft.com/en-us/graph/query-parameters?tabs=http#count-parameter

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune authored Feb 27, 2025
1 parent 1fcb88a commit 1b402b4
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 8 deletions.
29 changes: 28 additions & 1 deletion providers/ms365/resources/applications.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/url"
"time"

abstractions "github.com/microsoft/kiota-abstractions-go"
"github.com/rs/zerolog/log"

"github.com/microsoftgraph/msgraph-sdk-go/applications"
Expand All @@ -23,7 +24,12 @@ import (
"go.mondoo.com/cnquery/v11/types"
)

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

func (a *mqlMicrosoftApplications) list() ([]interface{}, error) {
conn := a.MqlRuntime.Connection.(*connection.Ms365Connection)
graphClient, err := conn.GraphClient()
if err != nil {
Expand Down Expand Up @@ -59,6 +65,27 @@ func (a *mqlMicrosoft) applications() ([]interface{}, error) {
return res, nil
}

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

opts := &applications.CountRequestBuilderGetRequestConfiguration{Headers: abstractions.NewRequestHeaders()}
opts.Headers.Add("ConsistencyLevel", "eventual")
length, err := graphClient.Applications().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 applications, counter parameter API returned nil")
}

return int64(*length), nil
}

// newMqlMicrosoftApplication creates a new mqlMicrosoftApplication resource
// see https://learn.microsoft.com/en-us/entra/identity-platform/reference-microsoft-graph-app-manifest for a
// better description of the fields
Expand Down
9 changes: 8 additions & 1 deletion providers/ms365/resources/ms365.lr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ microsoft {
// List of domains
domains() []microsoft.domain
// List of applications
applications() []microsoft.application
applications() microsoft.applications
// List of service principals
serviceprincipals() []microsoft.serviceprincipal
// List of enterprise applications
Expand All @@ -30,6 +30,13 @@ microsoft {
tenantDomainName() string
}

// List of Microsoft Entra ID application registrations
microsoft.applications {
[]microsoft.application
// Total number of application registrations
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.

5 changes: 5 additions & 0 deletions providers/ms365/resources/ms365.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ resources:
value: {}
is_private: true
min_mondoo_version: 9.0.0
microsoft.applications:
fields:
length: {}
list: {}
min_mondoo_version: 9.0.0
microsoft.conditionalAccess:
fields:
namedLocations: {}
Expand Down

0 comments on commit 1b402b4

Please sign in to comment.