From 1d8759b5e0d60541c611e051ae2fc015cad1f9fd Mon Sep 17 00:00:00 2001 From: vj Date: Sat, 21 Sep 2024 12:37:34 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20add=20resource=20for=20azure=20func?= =?UTF-8?q?tions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- providers/azure/resources/azure.lr | 16 +++ providers/azure/resources/azure.lr.go | 124 ++++++++++++++++++ .../azure/resources/azure.lr.manifest.yaml | 27 ++++ providers/azure/resources/web.go | 54 ++++++++ 4 files changed, 221 insertions(+) diff --git a/providers/azure/resources/azure.lr b/providers/azure/resources/azure.lr index ea20e2722f..297ebd1cfb 100644 --- a/providers/azure/resources/azure.lr +++ b/providers/azure/resources/azure.lr @@ -70,6 +70,20 @@ azure.subscription @defaults ("name") { iot() azure.subscription.iotService } +// Azure function +private azure.subscription.webService.function @defaults("name") { + // ID of the function + id string + // Name of the function + name string + // Type of function + type string + // Kind of function + kind string + // Properties for the function + properties dict +} + // Azure resource group private azure.subscription.resourcegroup @defaults("name location") { // Resource group ID @@ -1031,6 +1045,8 @@ private azure.subscription.webService.appsite @defaults("id name location") { stack() dict // Diagnostic settings for the web app site diagnosticSettings() []azure.subscription.monitorService.diagnosticsetting + // List of functions for the web app site + functions []azure.subscription.webService.function } // Azure AppSite authentication settings diff --git a/providers/azure/resources/azure.lr.go b/providers/azure/resources/azure.lr.go index 6d85568819..9a4198c5a6 100644 --- a/providers/azure/resources/azure.lr.go +++ b/providers/azure/resources/azure.lr.go @@ -26,6 +26,10 @@ func init() { Init: initAzureSubscription, Create: createAzureSubscription, }, + "azure.subscription.webService.function": { + // to override args, implement: initAzureSubscriptionWebServiceFunction(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) + Create: createAzureSubscriptionWebServiceFunction, + }, "azure.subscription.resourcegroup": { // to override args, implement: initAzureSubscriptionResourcegroup(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) Create: createAzureSubscriptionResourcegroup, @@ -589,6 +593,21 @@ var getDataFields = map[string]func(r plugin.Resource) *plugin.DataRes{ "azure.subscription.iot": func(r plugin.Resource) *plugin.DataRes { return (r.(*mqlAzureSubscription).GetIot()).ToDataRes(types.Resource("azure.subscription.iotService")) }, + "azure.subscription.webService.function.id": func(r plugin.Resource) *plugin.DataRes { + return (r.(*mqlAzureSubscriptionWebServiceFunction).GetId()).ToDataRes(types.String) + }, + "azure.subscription.webService.function.name": func(r plugin.Resource) *plugin.DataRes { + return (r.(*mqlAzureSubscriptionWebServiceFunction).GetName()).ToDataRes(types.String) + }, + "azure.subscription.webService.function.type": func(r plugin.Resource) *plugin.DataRes { + return (r.(*mqlAzureSubscriptionWebServiceFunction).GetType()).ToDataRes(types.String) + }, + "azure.subscription.webService.function.kind": func(r plugin.Resource) *plugin.DataRes { + return (r.(*mqlAzureSubscriptionWebServiceFunction).GetKind()).ToDataRes(types.String) + }, + "azure.subscription.webService.function.properties": func(r plugin.Resource) *plugin.DataRes { + return (r.(*mqlAzureSubscriptionWebServiceFunction).GetProperties()).ToDataRes(types.Dict) + }, "azure.subscription.resourcegroup.id": func(r plugin.Resource) *plugin.DataRes { return (r.(*mqlAzureSubscriptionResourcegroup).GetId()).ToDataRes(types.String) }, @@ -1738,6 +1757,9 @@ var getDataFields = map[string]func(r plugin.Resource) *plugin.DataRes{ "azure.subscription.webService.appsite.diagnosticSettings": func(r plugin.Resource) *plugin.DataRes { return (r.(*mqlAzureSubscriptionWebServiceAppsite).GetDiagnosticSettings()).ToDataRes(types.Array(types.Resource("azure.subscription.monitorService.diagnosticsetting"))) }, + "azure.subscription.webService.appsite.functions": func(r plugin.Resource) *plugin.DataRes { + return (r.(*mqlAzureSubscriptionWebServiceAppsite).GetFunctions()).ToDataRes(types.Array(types.Resource("azure.subscription.webService.function"))) + }, "azure.subscription.webService.appsiteauthsettings.id": func(r plugin.Resource) *plugin.DataRes { return (r.(*mqlAzureSubscriptionWebServiceAppsiteauthsettings).GetId()).ToDataRes(types.String) }, @@ -3005,6 +3027,30 @@ var setDataFields = map[string]func(r plugin.Resource, v *llx.RawData) bool { r.(*mqlAzureSubscription).Iot, ok = plugin.RawToTValue[*mqlAzureSubscriptionIotService](v.Value, v.Error) return }, + "azure.subscription.webService.function.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlAzureSubscriptionWebServiceFunction).__id, ok = v.Value.(string) + return + }, + "azure.subscription.webService.function.id": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlAzureSubscriptionWebServiceFunction).Id, ok = plugin.RawToTValue[string](v.Value, v.Error) + return + }, + "azure.subscription.webService.function.name": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlAzureSubscriptionWebServiceFunction).Name, ok = plugin.RawToTValue[string](v.Value, v.Error) + return + }, + "azure.subscription.webService.function.type": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlAzureSubscriptionWebServiceFunction).Type, ok = plugin.RawToTValue[string](v.Value, v.Error) + return + }, + "azure.subscription.webService.function.kind": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlAzureSubscriptionWebServiceFunction).Kind, ok = plugin.RawToTValue[string](v.Value, v.Error) + return + }, + "azure.subscription.webService.function.properties": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlAzureSubscriptionWebServiceFunction).Properties, ok = plugin.RawToTValue[interface{}](v.Value, v.Error) + return + }, "azure.subscription.resourcegroup.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) { r.(*mqlAzureSubscriptionResourcegroup).__id, ok = v.Value.(string) return @@ -4733,6 +4779,10 @@ var setDataFields = map[string]func(r plugin.Resource, v *llx.RawData) bool { r.(*mqlAzureSubscriptionWebServiceAppsite).DiagnosticSettings, ok = plugin.RawToTValue[[]interface{}](v.Value, v.Error) return }, + "azure.subscription.webService.appsite.functions": func(r plugin.Resource, v *llx.RawData) (ok bool) { + r.(*mqlAzureSubscriptionWebServiceAppsite).Functions, ok = plugin.RawToTValue[[]interface{}](v.Value, v.Error) + return + }, "azure.subscription.webService.appsiteauthsettings.__id": func(r plugin.Resource, v *llx.RawData) (ok bool) { r.(*mqlAzureSubscriptionWebServiceAppsiteauthsettings).__id, ok = v.Value.(string) return @@ -6933,6 +6983,75 @@ func (c *mqlAzureSubscription) GetIot() *plugin.TValue[*mqlAzureSubscriptionIotS }) } +// mqlAzureSubscriptionWebServiceFunction for the azure.subscription.webService.function resource +type mqlAzureSubscriptionWebServiceFunction struct { + MqlRuntime *plugin.Runtime + __id string + // optional: if you define mqlAzureSubscriptionWebServiceFunctionInternal it will be used here + Id plugin.TValue[string] + Name plugin.TValue[string] + Type plugin.TValue[string] + Kind plugin.TValue[string] + Properties plugin.TValue[interface{}] +} + +// createAzureSubscriptionWebServiceFunction creates a new instance of this resource +func createAzureSubscriptionWebServiceFunction(runtime *plugin.Runtime, args map[string]*llx.RawData) (plugin.Resource, error) { + res := &mqlAzureSubscriptionWebServiceFunction{ + MqlRuntime: runtime, + } + + err := SetAllData(res, args) + if err != nil { + return res, err + } + + if res.__id == "" { + res.__id, err = res.id() + if err != nil { + return nil, err + } + } + + if runtime.HasRecording { + args, err = runtime.ResourceFromRecording("azure.subscription.webService.function", res.__id) + if err != nil || args == nil { + return res, err + } + return res, SetAllData(res, args) + } + + return res, nil +} + +func (c *mqlAzureSubscriptionWebServiceFunction) MqlName() string { + return "azure.subscription.webService.function" +} + +func (c *mqlAzureSubscriptionWebServiceFunction) MqlID() string { + return c.__id +} + +func (c *mqlAzureSubscriptionWebServiceFunction) GetId() *plugin.TValue[string] { + return &c.Id +} + +func (c *mqlAzureSubscriptionWebServiceFunction) GetName() *plugin.TValue[string] { + return &c.Name +} + +func (c *mqlAzureSubscriptionWebServiceFunction) GetType() *plugin.TValue[string] { + return &c.Type +} + +func (c *mqlAzureSubscriptionWebServiceFunction) GetKind() *plugin.TValue[string] { + return &c.Kind +} + +func (c *mqlAzureSubscriptionWebServiceFunction) GetProperties() *plugin.TValue[interface{}] { + return &c.Properties +} + // mqlAzureSubscriptionResourcegroup for the azure.subscription.resourcegroup resource type mqlAzureSubscriptionResourcegroup struct { MqlRuntime *plugin.Runtime @@ -11407,6 +11526,7 @@ type mqlAzureSubscriptionWebServiceAppsite struct { ConnectionSettings plugin.TValue[interface{}] Stack plugin.TValue[interface{}] DiagnosticSettings plugin.TValue[[]interface{}] + Functions plugin.TValue[[]interface{}] } // createAzureSubscriptionWebServiceAppsite creates a new instance of this resource @@ -11550,6 +11670,10 @@ func (c *mqlAzureSubscriptionWebServiceAppsite) GetDiagnosticSettings() *plugin. }) } +func (c *mqlAzureSubscriptionWebServiceAppsite) GetFunctions() *plugin.TValue[[]interface{}] { + return &c.Functions +} + // mqlAzureSubscriptionWebServiceAppsiteauthsettings for the azure.subscription.webService.appsiteauthsettings resource type mqlAzureSubscriptionWebServiceAppsiteauthsettings struct { MqlRuntime *plugin.Runtime diff --git a/providers/azure/resources/azure.lr.manifest.yaml b/providers/azure/resources/azure.lr.manifest.yaml index f69e532982..83ed688843 100644 --- a/providers/azure/resources/azure.lr.manifest.yaml +++ b/providers/azure/resources/azure.lr.manifest.yaml @@ -20,6 +20,8 @@ resources: cloudDefender: {} compute: {} cosmosDb: {} + functions: + min_mondoo_version: 9.0.0 iam: min_mondoo_version: 9.0.0 id: {} @@ -604,6 +606,17 @@ resources: refs: - title: Azure Cosmos DB documentation url: https://learn.microsoft.com/en-us/azure/cosmos-db/ + azure.subscription.function: + fields: + id: {} + kind: {} + name: {} + properties: {} + type: {} + min_mondoo_version: 9.0.0 + platform: + name: + - azure azure.subscription.iotService: fields: hubs: {} @@ -2853,6 +2866,8 @@ resources: connectionSettings: {} diagnosticSettings: min_mondoo_version: 9.0.0 + functions: + min_mondoo_version: 9.0.0 id: {} identity: {} kind: {} @@ -2901,3 +2916,15 @@ resources: refs: - title: Azure Web documentation url: https://learn.microsoft.com/en-us/azure/?product=web + azure.subscription.webService.function: + fields: + id: {} + kind: {} + name: {} + properties: {} + type: {} + is_private: true + min_mondoo_version: 9.0.0 + platform: + name: + - azure diff --git a/providers/azure/resources/web.go b/providers/azure/resources/web.go index baeca21503..addb601f73 100644 --- a/providers/azure/resources/web.go +++ b/providers/azure/resources/web.go @@ -394,6 +394,60 @@ func (a *mqlAzureSubscriptionWebServiceAppsite) metadata() (interface{}, error) return res, nil } +func (a *mqlAzureSubscriptionWebServiceAppsite) functions() ([]interface{}, error) { + conn := a.MqlRuntime.Connection.(*connection.AzureConnection) + ctx := context.Background() + token := conn.Token() + id := a.Id.Data + resourceID, err := ParseResourceID(id) + if err != nil { + return nil, err + } + client, err := web.NewWebAppsClient(resourceID.SubscriptionID, token, &arm.ClientOptions{ + ClientOptions: conn.ClientOptions(), + }) + if err != nil { + return nil, err + } + + site, err := resourceID.Component("sites") + if err != nil { + return nil, err + } + pager := client.NewListFunctionsPager(resourceID.ResourceGroup, site, &web.WebAppsClientListFunctionsOptions{}) + res := []interface{}{} + + for pager.More() { + page, err := pager.NextPage(ctx) + if err != nil { + return nil, err + } + for _, entry := range page.Value { + props, err := convert.JsonToDict(entry.Properties) + if err != nil { + return nil, err + } + mqlAzure, err := CreateResource(a.MqlRuntime, "azure.subscription.webService.function", + map[string]*llx.RawData{ + "id": llx.StringDataPtr(entry.ID), + "name": llx.StringDataPtr(entry.Name), + "type": llx.StringDataPtr(entry.Type), + "kind": llx.StringDataPtr(entry.Kind), + "properties": llx.AnyData(props), + }) + if err != nil { + return nil, err + } + res = append(res, mqlAzure) + } + } + return res, nil +} + +func (a *mqlAzureSubscriptionWebServiceFunction) id() (string, error) { + return a.id() +} + func (a *mqlAzureSubscriptionWebServiceAppsite) connectionSettings() (interface{}, error) { conn := a.MqlRuntime.Connection.(*connection.AzureConnection) ctx := context.Background()