OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Creating a NewPageIterator for Run Hunting Query Using the Microsoft Graph SDK for Go

  • Thread starter Thread starter Rob Buhler
  • Start date Start date
R

Rob Buhler

Guest
I'm getting an 'Error creating page iterator: value property missing in response object' using the Microsoft Graph SDK in Go and trying to create a NewPageIterator. How can I resolve this issue?

The code works if I take the pagination part out. I'm trying to get pagination to work. I'm also using the Echo web framework, but my question is specifically to how to create a correct Page Iterator that will work with run hunting query

Code:
package handlers

import (
    "context"
    "encoding/json"
    "fmt"
    "io"
    "log"
    "net/http"
    "os"

    "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
    "github.com/labstack/echo/v4"
    msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
    msgraphcore "github.com/microsoftgraph/msgraph-sdk-go-core"
    "github.com/microsoftgraph/msgraph-sdk-go/models/security"
    graphsecurity "github.com/microsoftgraph/msgraph-sdk-go/security"
)

func getClientWithCredentials() (*msgraphsdk.GraphServiceClient, error) {

    tenantID := os.Getenv("AZURE_TENANT_ID")
    clientID := os.Getenv("AZURE_CLIENT_ID")
    clientSecret := os.Getenv("AZURE_CLIENT_SECRET")

    cred, err := azidentity.NewClientSecretCredential(tenantID, clientID, clientSecret, nil)

    if err != nil {
        fmt.Printf("Error creating credentials: %v\n", err)
    }

    client, err := msgraphsdk.NewGraphServiceClientWithCredentials(cred, nil)
    if err != nil {
        fmt.Printf("Error creating client: %v\n", err)
        return client, err
    }
    return client, nil
}

func RunKQL(c echo.Context) error {

    body, err := io.ReadAll(c.Request().Body)
    if err != nil {
        return err
    }
    query := string(body)

    timespan := c.QueryParam("timespan")

    client, err := getClientWithCredentials()
    if err != nil {
        c.Logger().Fatalf("Could not get access token: %v", err)
    }

    requestBody := graphsecurity.NewMicrosoftgraphsecurityrunhuntingqueryRunHuntingQueryPostRequestBody()
    requestBody.SetQuery(&query)
    if timespan != "" {
        requestBody.SetTimespan(&timespan)
    }
    result, err := client.Security().MicrosoftGraphSecurityRunHuntingQuery().Post(context.Background(), requestBody, nil)
    if err != nil {
        c.Logger().Fatalf("Error running hunting query: %v", err)
    }

    // Initialize iterator - this is not working to create a NewPageIterator
    pageIterator, err := msgraphcore.NewPageIterator[*security.HuntingQueryResultsable](
        result,
        client.GetAdapter(),
        security.CreateHuntingQueryResultsFromDiscriminatorValue)
    if err != nil {
        log.Fatalf("Error creating page iterator: %v\n", err)
    }

    //Iterate over all pages
    jsonObjects := make([]map[string]interface{}, 0)
    err2 := pageIterator.Iterate(
        context.Background(),
        func(pageItem *security.HuntingQueryResultsable) bool {
            // Process each page item here
            if results := (*pageItem).GetResults(); results != nil {
                for _, result := range results {
                    jsonData := result.GetAdditionalData()
                    jsonObjects = append(jsonObjects, jsonData)
                }
            }
            // Return true to continue iterating to the next page
            return true
        })
    if err2 != nil {
        log.Fatalf("Error iterating over security results: %v\n", err2)
    }

    //Working without using Page Iterator
    // jsonObjects := make([]map[string]interface{}, 0)
    // for _, result := range result.GetResults() {
    //  jsonData := result.GetAdditionalData()
    //  jsonObjects = append(jsonObjects, jsonData)
    // }

    jsonRes, err := json.MarshalIndent(jsonObjects, "", "  ")
    if err != nil {
        fmt.Println("Error marshalling JSON array:", err)
        return c.String(http.StatusInternalServerError, "Error generating JSON response")
    }

    return c.String(http.StatusOK, string(jsonRes))
}
<p>I'm getting an '<strong>Error creating page iterator: value property missing in response object</strong>' using the Microsoft Graph SDK in Go and trying to create a NewPageIterator. How can I resolve this issue?</p>
<p>The code works if I take the pagination part out. I'm trying to get pagination to work.
I'm also using the Echo web framework, but my question is specifically to how to create a correct Page Iterator that will work with run hunting query</p>
<pre><code>package handlers

import (
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/labstack/echo/v4"
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
msgraphcore "github.com/microsoftgraph/msgraph-sdk-go-core"
"github.com/microsoftgraph/msgraph-sdk-go/models/security"
graphsecurity "github.com/microsoftgraph/msgraph-sdk-go/security"
)

func getClientWithCredentials() (*msgraphsdk.GraphServiceClient, error) {

tenantID := os.Getenv("AZURE_TENANT_ID")
clientID := os.Getenv("AZURE_CLIENT_ID")
clientSecret := os.Getenv("AZURE_CLIENT_SECRET")

cred, err := azidentity.NewClientSecretCredential(tenantID, clientID, clientSecret, nil)

if err != nil {
fmt.Printf("Error creating credentials: %v\n", err)
}

client, err := msgraphsdk.NewGraphServiceClientWithCredentials(cred, nil)
if err != nil {
fmt.Printf("Error creating client: %v\n", err)
return client, err
}
return client, nil
}

func RunKQL(c echo.Context) error {

body, err := io.ReadAll(c.Request().Body)
if err != nil {
return err
}
query := string(body)

timespan := c.QueryParam("timespan")

client, err := getClientWithCredentials()
if err != nil {
c.Logger().Fatalf("Could not get access token: %v", err)
}

requestBody := graphsecurity.NewMicrosoftgraphsecurityrunhuntingqueryRunHuntingQueryPostRequestBody()
requestBody.SetQuery(&query)
if timespan != "" {
requestBody.SetTimespan(&timespan)
}
result, err := client.Security().MicrosoftGraphSecurityRunHuntingQuery().Post(context.Background(), requestBody, nil)
if err != nil {
c.Logger().Fatalf("Error running hunting query: %v", err)
}

// Initialize iterator - this is not working to create a NewPageIterator
pageIterator, err := msgraphcore.NewPageIterator[*security.HuntingQueryResultsable](
result,
client.GetAdapter(),
security.CreateHuntingQueryResultsFromDiscriminatorValue)
if err != nil {
log.Fatalf("Error creating page iterator: %v\n", err)
}

//Iterate over all pages
jsonObjects := make([]map[string]interface{}, 0)
err2 := pageIterator.Iterate(
context.Background(),
func(pageItem *security.HuntingQueryResultsable) bool {
// Process each page item here
if results := (*pageItem).GetResults(); results != nil {
for _, result := range results {
jsonData := result.GetAdditionalData()
jsonObjects = append(jsonObjects, jsonData)
}
}
// Return true to continue iterating to the next page
return true
})
if err2 != nil {
log.Fatalf("Error iterating over security results: %v\n", err2)
}

//Working without using Page Iterator
// jsonObjects := make([]map[string]interface{}, 0)
// for _, result := range result.GetResults() {
// jsonData := result.GetAdditionalData()
// jsonObjects = append(jsonObjects, jsonData)
// }

jsonRes, err := json.MarshalIndent(jsonObjects, "", " ")
if err != nil {
fmt.Println("Error marshalling JSON array:", err)
return c.String(http.StatusInternalServerError, "Error generating JSON response")
}

return c.String(http.StatusOK, string(jsonRes))
}
</code></pre>
Continue reading...
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top