Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⭐️ Adding the IoT resources #4626

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions providers/azure/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ require (
github.com/99designs/keyring v1.2.2 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/iothub/armiothub v1.3.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/internal v1.0.1 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/BurntSushi/toml v1.4.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions providers/azure/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v2 v2.0.0 h1:PTFG
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v2 v2.0.0/go.mod h1:LRr2FzBTQlONPPa5HREE5+RjSCTXl7BwOvYOaWTqCaI=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3 v3.0.0 h1:Kb8eVvjdP6kZqYnER5w/PiGCFp91yVgaxve3d7kCEpY=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3 v3.0.0/go.mod h1:lYq15QkJyEsNegz5EhI/0SXQ6spvGfgwBH/Qyzkoc/s=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/iothub/armiothub v1.3.0 h1:NZP+oPbAVFy7PhQ4PTD3SuGWbEziNhp7lphGkkN707s=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/iothub/armiothub v1.3.0/go.mod h1:djbLk3ngutFfQ9fSOM29UzywAkcBI1YUsuUnxTQGsqU=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/keyvault/armkeyvault v1.4.0 h1:HlZMUZW8S4P9oob1nCHxCCKrytxyLc+24nUJGssoEto=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/keyvault/armkeyvault v1.4.0/go.mod h1:StGsLbuJh06Bd8IBfnAlIFV3fLb+gkczONWf15hpX2E=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/managementgroups/armmanagementgroups v1.0.0 h1:pPvTJ1dY0sA35JOeFq6TsY2xj6Z85Yo23Pj4wCCvu4o=
Expand Down
10 changes: 10 additions & 0 deletions providers/azure/resources/azure.lr
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ azure.subscription @defaults ("name") {
advisor() azure.subscription.advisorService
// Policy service in the subscription
policy azure.subscription.policy
// IoT resources in the subscription
iot() azure.subscription.iotService
}

// Azure resource group
Expand Down Expand Up @@ -1960,4 +1962,12 @@ private azure.subscription.policy.assignment @defaults("name enforcementMode") {
description string
// Policy enforcement Mode
enforcementMode string
}

// Azure IoT Hub Service
private azure.subscription.iotService {
// Subscription identifier
subscriptionId string
// List of IoT hubs in the subscription
hubs() []dict
}
11 changes: 11 additions & 0 deletions providers/azure/resources/azure.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ resources:
iam:
min_mondoo_version: 9.0.0
id: {}
iot:
min_mondoo_version: 9.0.0
keyVault: {}
managedByTenants: {}
mariaDb: {}
Expand Down Expand Up @@ -602,6 +604,15 @@ resources:
refs:
- title: Azure Cosmos DB documentation
url: https://learn.microsoft.com/en-us/azure/cosmos-db/
azure.subscription.iotService:
fields:
hubs: {}
subscriptionId: {}
is_private: true
min_mondoo_version: 9.0.0
platform:
name:
- azure
azure.subscription.keyVault:
fields:
subscriptionId: {}
Expand Down
62 changes: 62 additions & 0 deletions providers/azure/resources/iot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Mondoo, Inc.
// SPDX-License-Identifier: BUSL-1.1

package resources

import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/iothub/armiothub"
"go.mondoo.com/cnquery/v11/llx"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/util/convert"
"go.mondoo.com/cnquery/v11/providers/azure/connection"
)

func initAzureSubscriptionIotService(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
if len(args) > 0 {
return args, nil, nil
}

conn, ok := runtime.Connection.(*connection.AzureConnection)
if !ok {
return nil, nil, fmt.Errorf("invalid connection provided, it is not an Azure connection")
}
args["subscriptionId"] = llx.StringData(conn.SubId())

return args, nil, nil
}

func (a *mqlAzureSubscriptionIotService) hubs() ([]interface{}, error) {
conn := a.MqlRuntime.Connection.(*connection.AzureConnection)
ctx := context.Background()
token := conn.Token()

subscriptionID := a.SubscriptionId.Data

clientFactory, err := armiothub.NewClientFactory(subscriptionID, token, nil)
if err != nil {
return nil, err
}

client := clientFactory.NewResourceClient()
hubsPager := client.NewListBySubscriptionPager(nil)
var hubs []interface{}

for hubsPager.More() {
page, err := hubsPager.NextPage(ctx)
if err != nil {
return nil, err
}
for _, hub := range page.Value {
hubData, err := convert.JsonToDict(hub)
if err != nil {
return nil, err
}
hubs = append(hubs, hubData)
}
}

return hubs, nil
}
11 changes: 11 additions & 0 deletions providers/azure/resources/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,14 @@ func (a *mqlAzureSubscription) advisor() (*mqlAzureSubscriptionAdvisorService, e
advisorSvc := svc.(*mqlAzureSubscriptionAdvisorService)
return advisorSvc, nil
}

func (a *mqlAzureSubscription) iot() (*mqlAzureSubscriptionIotService, error) {
svc, err := NewResource(a.MqlRuntime, "azure.subscription.iotService", map[string]*llx.RawData{
"subscriptionId": llx.StringData(a.SubscriptionId.Data),
})
if err != nil {
return nil, err
}
iotSvc := svc.(*mqlAzureSubscriptionIotService)
return iotSvc, nil
}
Loading