forked from blackstork-io/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add microsoft graph data source (blackstork-io#242)
- Loading branch information
Showing
16 changed files
with
792 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
--- | ||
title: "`microsoft_graph` data source" | ||
plugin: | ||
name: blackstork/microsoft | ||
description: "The `microsoft_graph` data source queries Microsoft Graph" | ||
tags: [] | ||
version: "v0.4.2" | ||
source_github: "https://github.com/blackstork-io/fabric/tree/main/internal/microsoft/" | ||
resource: | ||
type: data-source | ||
type: docs | ||
--- | ||
|
||
{{< breadcrumbs 2 >}} | ||
|
||
{{< plugin-resource-header "blackstork/microsoft" "microsoft" "v0.4.2" "microsoft_graph" "data source" >}} | ||
|
||
## Description | ||
The `microsoft_graph` data source queries Microsoft Graph. | ||
|
||
## Installation | ||
|
||
To use `microsoft_graph` data source, you must install the plugin `blackstork/microsoft`. | ||
|
||
To install the plugin, add the full plugin name to the `plugin_versions` map in the Fabric global configuration block (see [Global configuration]({{< ref "configs.md#global-configuration" >}}) for more details), as shown below: | ||
|
||
```hcl | ||
fabric { | ||
plugin_versions = { | ||
"blackstork/microsoft" = ">= v0.4.2" | ||
} | ||
} | ||
``` | ||
|
||
Note the version constraint set for the plugin. | ||
|
||
## Configuration | ||
|
||
The data source supports the following configuration arguments: | ||
|
||
```hcl | ||
config data microsoft_graph { | ||
# The Azure client ID | ||
# | ||
# Required string. | ||
# For example: | ||
client_id = "some string" | ||
# The Azure client secret. Required if private_key_file/privat_key/cert_thumbprint is not provided. | ||
# | ||
# Optional string. | ||
# Default value: | ||
client_secret = null | ||
# The Azure tenant ID | ||
# | ||
# Required string. | ||
# For example: | ||
tenant_id = "some string" | ||
# The path to the private key file. Ignored if private_key/client_secret is provided. | ||
# | ||
# Optional string. | ||
# Default value: | ||
private_key_file = null | ||
# The private key contents. Ignored if client_secret is provided. | ||
# | ||
# Optional string. | ||
# Default value: | ||
private_key = null | ||
# The key passphrase. Ignored if client_secret is provided. | ||
# | ||
# Optional string. | ||
# Default value: | ||
key_passphrase = null | ||
} | ||
``` | ||
|
||
## Usage | ||
|
||
The data source supports the following execution arguments: | ||
|
||
```hcl | ||
data microsoft_graph { | ||
# The API version | ||
# | ||
# Optional string. | ||
# Default value: | ||
api_version = "beta" | ||
# The endpoint to query | ||
# | ||
# Required string. | ||
# For example: | ||
endpoint = "/security/incidents" | ||
# The query parameters | ||
# | ||
# Optional map of string. | ||
# Default value: | ||
query_params = null | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
fabric { | ||
plugin_versions = { | ||
"blackstork/microsoft" = ">= 0.4 < 1.0 || 0.4.0-rev0" | ||
} | ||
} | ||
|
||
document "example" { | ||
meta { | ||
name = "example_document" | ||
} | ||
|
||
data microsoft_graph "mygraph" { | ||
config { | ||
client_id = "" | ||
client_secret = "" | ||
tenant_id = "" | ||
# private_key_file = "<path-to-key-file>" | ||
} | ||
api_version = "v1.0" | ||
endpoint = "/security/incidents" | ||
query_params = { | ||
"$top" = "10" | ||
} | ||
} | ||
|
||
title = "List of Security Incidents" | ||
|
||
content table { | ||
rows = query_jq(".data.microsoft_graph.mygraph.value") | ||
columns = [ | ||
{ | ||
"header" = "Severity" | ||
"value" = "{{.row.value.severity}}" | ||
}, | ||
{ | ||
"header" = "Display Name" | ||
"value" = "{{.row.value.displayName}}" | ||
} | ||
] | ||
} | ||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
) | ||
|
||
const graphUrl = "https://graph.microsoft.com" | ||
|
||
type graphClient struct { | ||
accessToken string | ||
apiVersion string | ||
client *http.Client | ||
} | ||
|
||
func NewGraphClient(accessToken string, apiVersion string) *graphClient { | ||
return &graphClient{ | ||
accessToken: accessToken, | ||
apiVersion: apiVersion, | ||
client: &http.Client{}, | ||
} | ||
} | ||
|
||
func (cli *graphClient) prepare(r *http.Request) { | ||
r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", cli.accessToken)) | ||
} | ||
|
||
func (cli *graphClient) QueryGraph(ctx context.Context, endpoint string, queryParams url.Values) (result interface{}, err error) { | ||
requestUrl, err := url.Parse(graphUrl + fmt.Sprintf("/%s%s", cli.apiVersion, endpoint)) | ||
if err != nil { | ||
return | ||
} | ||
if queryParams != nil { | ||
requestUrl.RawQuery = queryParams.Encode() | ||
} | ||
r, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil) | ||
if err != nil { | ||
return | ||
} | ||
cli.prepare(r) | ||
res, err := cli.client.Do(r) | ||
if err != nil { | ||
return | ||
} | ||
if res.StatusCode != http.StatusOK { | ||
err = fmt.Errorf("microsoft graph client returned status code: %d", res.StatusCode) | ||
return | ||
} | ||
defer res.Body.Close() | ||
if err := json.NewDecoder(res.Body).Decode(&result); err != nil { | ||
return nil, err | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.