问题描述

使用Azure Policy,对订阅下的全部Activity Log配置Diagnostic Setting,要求:

  1. 在Subscription或Management Group级别,针对未启用Activity Log功能的订阅,启用Activity Log功能;
  2. 对已经启用了Activity log功能的订阅,使用该Policy纠正并统一其参数配置;
  3. 所收集到的Azure Activity Log存储在特定的Storage Account,保留周期为6个月;
  4. Activity logs将收集一下log:
    • Administrative
    • Security
    • Alert
    • Recommendation
    • ResourceHealth

问题解答

针对需求,一条一条的匹配

1. 在Subscription或Management Group级别,针对未启用Activity Log功能的订阅,启用Activity Log功能

因为需要Policy Scan的资源为 Subscription,所以第一步是需要扫描所有的订阅资源。然后在检查订阅下的Microsoft.Insights/diagnosticSettings配置。

    "policyRule": {
"if": {
"field": "type",
"equals": "Microsoft.Resources/subscriptions"
},

2. 对已经启用了Activity log功能的订阅,使用该Policy纠正并统一其参数配置

3. 所收集到的Azure Activity Log存储在特定的Storage Account,保留周期为6个月

第三点中:需要特定的Storage Account,所以把它作为Policy参数进行设置,然后判断storageAccountId 值是否一样。6个月的保留周期设置因为新的UI上没有这个设定值,所以需要创建Storage Account中去设置,不在Policy中实现。

第二点中:要求使用同一个Storage Acocunt,所以这里并不是判断是否配置了Storage Account,而是必须要使用ID相等。

    {
"field": "Microsoft.Insights/diagnosticSettings/storageAccountId",
"equals": "[parameters('storageAccount')]"
},

4. Activity logs将收集一下log: a). Administrative b). Security c). Alert d). Recommendation e). ResourceHealth

因为DiagnosticSettings 在ARM资源中是数组对象,所以使用logs[*] , 并且通过count  where equals 运算符。

当Policy的条件满足后,接下来就是需要考虑DeployIfNotExists的配置了

  • ExistenceScope : 允许的值为 Subscription 和 ResourceGroup, 但是默认值为Resource Group。所以此处必须修改为Subscription
  • ExistenceCondition :如果任何匹配的相关资源评估结果为 true,该效果就会得到满足并且不会触发部署。
  • DeploymentScope:允许的值为 Subscription 和 ResourceGroup, 默认值是 ResourceGroup。因为修改的资源为订阅的诊断配置。所以需要设置该值,并且也必须在Deployment中指定location属性。否则会遇见the location is missing 报错。

完整的Policy

  1 {
2 "mode": "All",
3 "policyRule": {
4 "if": {
5 "field": "type",
6 "equals": "Microsoft.Resources/subscriptions"
7 },
8 "then": {
9 "effect": "[parameters('effect')]",
10 "details": {
11 "type": "Microsoft.Insights/diagnosticSettings",
12 "ExistenceScope": "Subscription",
13 "existenceCondition": {
14 "allOf": [
15 {
16 "field": "Microsoft.Insights/diagnosticSettings/storageAccountId",
17 "equals": "[parameters('storageAccount')]"
18 },
19 {
20 "count": {
21 "field": "Microsoft.Insights/diagnosticSettings/logs[*]",
22 "where": {
23 "allOf": [
24 {
25 "anyof": [
26 {
27 "field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
28 "equals": "Administrative"
29 },
30 {
31 "field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
32 "equals": "Security"
33 },
34 {
35 "field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
36 "equals": "Alert"
37 },
38 {
39 "field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
40 "equals": "Recommendation"
41 },
42 {
43 "field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
44 "equals": "ResourceHealth"
45 }
46 ]
47 },
48 {
49 "field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
50 "equals": "true"
51 }
52 ]
53 }
54 },
55 "equals": 5
56 }
57 ]
58 },
59 "deploymentScope": "subscription",
60 "deployment": {
61 "location": "chinaeast2",
62 "properties": {
63 "mode": "incremental",
64 "template": {
65 "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
66 "contentVersion": "1.0.0.0",
67 "parameters": {
68 "storageAccount": {
69 "type": "string"
70 },
71 "logsEnabled": {
72 "type": "string"
73 },
74 "profileName": {
75 "type": "string"
76 }
77 },
78 "variables": {},
79 "resources": [
80 {
81 "type": "Microsoft.Insights/diagnosticSettings",
82 "apiVersion": "2017-05-01-preview",
83 "name": "[parameters('profileName')]",
84 "location": "global",
85 "dependsOn": [],
86 "properties": {
87 "storageAccountId": "[parameters('storageAccount')]",
88 "logs": [
89 {
90 "category": "Administrative",
91 "enabled": "[parameters('logsEnabled')]"
92 },
93 {
94 "category": "Security",
95 "enabled": "[parameters('logsEnabled')]"
96 },
97 {
98 "category": "Alert",
99 "enabled": "[parameters('logsEnabled')]"
100 },
101 {
102 "category": "Recommendation",
103 "enabled": "[parameters('logsEnabled')]"
104 },
105 {
106 "category": "ResourceHealth",
107 "enabled": "[parameters('logsEnabled')]"
108 }
109 ]
110 }
111 }
112 ],
113 "outputs": {}
114 },
115 "parameters": {
116 "storageAccount": {
117 "value": "[parameters('storageAccount')]"
118 },
119 "logsEnabled": {
120 "value": "[parameters('logsEnabled')]"
121 },
122 "profileName": {
123 "value": "[parameters('profileName')]"
124 }
125 }
126 }
127 },
128 "roleDefinitionIds": [
129 "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c",
130 "/providers/Microsoft.Authorization/roleDefinitions/17d1049b-9a84-46fb-8f53-869881c3d3ab"
131 ]
132 }
133 }
134 },
135 "parameters": {
136 "effect": {
137 "type": "String",
138 "metadata": {
139 "displayName": "Effect",
140 "description": "Enable or disable the execution of the policy"
141 },
142 "allowedValues": [
143 "DeployIfNotExists",
144 "Disabled"
145 ],
146 "defaultValue": "DeployIfNotExists"
147 },
148 "profileName": {
149 "type": "String",
150 "metadata": {
151 "displayName": "Profile name",
152 "description": "The diagnostic settings profile name"
153 },
154 "defaultValue": "setbypolicy_storageaccount"
155 },
156 "storageAccount": {
157 "type": "String",
158 "metadata": {
159 "displayName": "Storage Account Name",
160 "description": "Select storage account from dropdown list. If this workspace is outside of the scope of the assignment you must manually grant 'Log Analytics Contributor' permissions (or similar) to the policy assignment's principal ID.",
161 "strongType": "Microsoft.Storage/storageAccounts",
162 "assignPermissions": true
163 },
164 "defaultValue": "/subscriptions/<subscription id>/resourcegroups/<resource group name>/providers/microsoft.storage/storageaccounts/<storage account name>"
165 },
166 "logsEnabled": {
167 "type": "String",
168 "metadata": {
169 "displayName": "Enable logs",
170 "description": "Whether to enable logs stream to the Log Analytics workspace - True or False"
171 },
172 "allowedValues": [
173 "True",
174 "False"
175 ],
176 "defaultValue": "True"
177 }
178 }
179 }

可能遇见的错误

1: location 错误

"deploymentScope": "subscription",

"deployment": {

"location": "chinaeast2",

"properties": {

Code

LocationNotAvailableForDeployment

Message

The provided location 'global' is not available for deployment. List of available regions is 'chinaeast2,chinaeast,chinanorth3,chinanorth,chinanorth2'.

Note:  If the location is missing or the value is incorrect, you will encounter the LocationNotAvailableForDeployment error, the Error Message will be "The provided location 'global' is not available for deployment. List of available regions is 'chinaeast2, chinaeast, chinanorth3, chinanorth, chinanorth2'."

2:设置: logs[*].enabled条件错误

{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].enabled",
"equals": "true"
}

结果:

3: 设置:logs[*].category 条件错误

{
"field": "Microsoft.Insights/diagnosticSettings/logs[*].category",
"equals": "Administrative"
}

结果:

参考资料

  1. Azure Policy 模式:count 运算符 : https://docs.azure.cn/zh-cn/governance/policy/samples/pattern-count-operator
  2. 了解 [*] 别名 : https://docs.azure.cn/zh-cn/governance/policy/concepts/definition-structure#understanding-the--alias
  3. DeployIfNotExists 评估 :https://docs.azure.cn/zh-cn/governance/policy/concepts/effects#deployifnotexists-evaluation

参考资料

【Azure Policy】使用deployIfNotExists 把 Azure Activity logs 导出保存在Storage Account的更多相关文章

  1. 使用Azure Policy(策略)强制实现资源Tag的坑

    Azure的Tag(标记)可以帮助运维人员对云资源分类从而方便地进行计费和资源管理.然而在具体实践中工程师部署云资源的时候常常会忘记给资源做标记打Tag. 针对这个问题,Azure的官方文档建议是可以 ...

  2. Azure ARM (22) Azure Policy入门

    <Windows Azure Platform 系列文章目录> 我们知道,在Azure服务层级中,分为以下几个层次: 1.企业合同 2.订阅 3.资源组 4.资源 我们使用的Azure资源 ...

  3. Azure ARM (23) Azure Policy使用

    <Windows Azure Platform 系列文章目录> 在之前的文档中,我们介绍了Azure Policy的使用场景. 本章我们介绍如何创建和使用Azure Policy. 模拟场 ...

  4. 为 Azure IoT Edge 设备部署 Azure Stream Analytics 服务

    在前面的两篇文章<Azure IoT Edge on Windows 10 IoT Core>和<Azure IoT Edge on Raspberry Pi 3 with Rasp ...

  5. [Windows Azure] What is a Storage Account?

    What is a Storage Account? A storage account gives your applications access to Windows Azure Blob, T ...

  6. [Windows Azure] Enabling Diagnostics in Windows Azure

    Enabling Diagnostics in Windows Azure Windows Azure Diagnostics enables you to collect diagnostic da ...

  7. Azure 信用卡扣款 1 美元 & Azure 中国客服

    Azure 信用卡扣款 1 美元 & azure 中国客服 Azure 免费帐户常见问题 https://azure.microsoft.com/zh-cn/free/free-account ...

  8. 【Azure 环境】由为存储账号(Storage Account)拒绝分配权限而引出的Azure 蓝图(Blueprint)使用问题

    问题描述 当打开Azure存储账号(Storage Account)门户页面时,从 "访问控制(标识和访问管理)" 页面中发现有"拒绝分配"的功能,所以就思考, ...

  9. 【Azure 应用服务】Azure Function集成虚拟网络,设置被同在虚拟网络中的Storage Account触发,遇见Function无法触发的问题

    一切为了安全,所有的云上资源如支持内网资源访问,则都可以加入虚拟网络 问题描述 使用Azure Function处理Storage Account中Blob 新增,更新,删除等情况.Storage A ...

  10. Azure Backup (3) 使用Azure备份服务,备份Azure虚拟机

    <Windows Azure Platform 系列文章目录> 本将介绍,如何使用Azure备份服务,备份Azure虚拟机. 我们先预先创建2台Windows VM (命名为LeiVM00 ...

随机推荐

  1. Win10 下安装使用easyocr图片识别工具

    [前言] 最近在做图像识别相关的工作,找到了一个名为EasyOCR的pythoh 库. 使用过程中出现了一些问题,现做简单记录. [正文] 1. 安装EasyOCR 我用了最简单的方法:pip3 in ...

  2. .NET 按格式导出txt

    效果图 后台代码 private void DownTxt() { try { StringBuilder sb = new StringBuilder(); for (int i = 0; i &l ...

  3. 全网最适合入门的面向对象编程教程:09 类和对象的Python实现-类之间的关系,你知道多少?

    全网最适合入门的面向对象编程教程:09 类和对象的 Python 实现-类之间的关系,你知道多少? 摘要: 本文主要对类之间的关系进行了基本介绍,包括继承.组合.依赖关系,并辅以现实中的例子加以讲解, ...

  4. 【原创软件】第7期:文件夹生成器V1.0-按照列表批量生成文件夹,简单小巧

    一.背景 因为工作需要,需要批量创建文件夹.为了省去人工创建时间,使用aardio制作了一个软件. 二.功能演示 三.下载地址  https://www.123pan.com/s/9Rn9-1xppH ...

  5. Apache Kyuubi 在B站大数据场景下的应用实践

    01 背景介绍 近几年随着B站业务高速发展,数据量不断增加,离线计算集群规模从最初的两百台发展到目前近万台,从单机房发展到多机房架构.在离线计算引擎上目前我们主要使用Spark.Presto.Hive ...

  6. 判断浏览器是否是 IE 及 IE8 以下版本

    作为一个前端,避免不了会遇见IE的坑,其他浏览器都好好的,测到IE就完蛋,各种不支持,服气了 有些属性和方法是所有版本IE都不支持,而有些则是部分支持,在项目中能够,主要分界岭为IE8,我相信目前大部 ...

  7. Django DRF @action 装饰器

    @action 装饰器在Django REST Framework (DRF) 中非常有用,它可以帮助你在ViewSet中创建自定义的动作,而不仅仅是依赖标准的CRUD操作(Create, Read, ...

  8. vue高频面试题

    来源:B站程序员来了 第一部分:vue基础 1,v-if和v-for的优先级谁更高?同时出现该如何优化性能? 在同级出现的时候,render函数会将v-for和v-if同时渲染在一个名为_l的函数,在 ...

  9. java+mysql+tomcat环境变量配置(windows版)

    jdk8(本人用的jdk8) 系统变量->新建:{JAVA_HOME=[JDK安装目录]} 系统变量->PATH:头部追加%JAVA_HOME%\bin;%JAVA_HOME%\jre\b ...

  10. 【AppStore】IOS应用上架Appstore的一些小坑

    前言 上一篇文章写到如何上架IOS应用到Appstore,其中漏掉了些许期间遇到的小坑,现在补上 审核不通过原因 5.1.1 Guideline 5.1.1 - Legal - Privacy - D ...