# authorizationpolicies

Istio授权策略可对网格中的工作负载进行访问控制。

授权策略同时支持允许和拒绝策略。当允许和拒绝策略同时用于工作负载时,将首先评估拒绝策略。评估由以下规则确定:

* 如果有任何与请求匹配的DENY策略,请拒绝该请求。
* 如果没有针对工作负载的ALLOW策略,则允许该请求。
* 如果任何ALLOW策略与请求匹配,则允许该请求。
* 拒绝请求。

Istio授权策略还支持AUDIT操作,以决定是否记录请求。审核策略不影响对工作负载是允许还是拒绝请求。仅基于ALLOW和DENY政策将允许或拒绝请求。

如果在工作负载上有匹配请求的AUDIT策略,则该请求将在内部标记为应审核。必须配置并启用单独的插件才能实际执行审核决定并完成审核行为。如果没有启用此类支持插件,则将不审核该请求。当前,唯一受支持的插件是Telemetry v2 Stackdriver插件。

这是Istio授权策略的示例:

它将操作设置为"ALLOW"以创建允许策略。默认操作为"ALLOW",但在策略中明确表示很有用。

它允许来自以下方面的请求:

* 服务帐户"cluster.local/ns/default/sa/sleep"或
* 命名空间"test"

通过以下方式访问工作负载:

* 前缀"/info"的路径处的"GET"方法,或者
* 路径"/data"处的"POST"方法。

当请求具有由"<https://accounts.google.com"发出的有效JWT令牌时。>

其他任何请求都将被拒绝。

```yaml
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
 name: httpbin
 namespace: foo
spec:
 action: ALLOW
 rules:
 - from:
   - source:
       principals: ["cluster.local/ns/default/sa/sleep"]
   - source:
       namespaces: ["test"]
   to:
   - operation:
       methods: ["GET"]
       paths: ["/info*"]
   - operation:
       methods: ["POST"]
       paths: ["/data"]
   when:
   - key: request.auth.claims[iss]
     values: ["https://accounts.google.com"]
```

以下是将操作设置为`DENY`以创建拒绝策略的另一个示例。对于`foo`名称空间中的所有工作负载,它拒绝从`dev`名称空间到"POST"方法的请求。

```yaml
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
 name: httpbin
 namespace: foo
spec:
 action: DENY
 rules:
 - from:
   - source:
       namespaces: ["dev"]
   to:
   - operation:
       methods: ["POST"]
```

以下授权策略将操作设置为`audit`。它将审核所有带`/user/profile`前缀的GET请求。

```yaml
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  namespace: ns1
  name: anyname
spec:
  selector:
    matchLabels:
      app: myapi
  action: audit
  rules:
  - to:
    - operation:
        methods: ["GET"]
        paths: ["/user/profile/*"]
```

授权策略范围(目标)由"元数据/命名空间"和可选的"选择器"确定。

metadata/namespace:告诉该策略应用哪个名称空间.如果设置为根名称空间,则该策略将应用于网格中的所有名称空间。 工作负载"selector"可用于进一步限制策略的应用位置。

以下授权策略适用于名称空间栏中包含标签" app:httpbin"的工作负载。

```
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
 name: policy
 namespace: bar
spec:
 selector:
   matchLabels:
     app: httpbin
```

以下授权策略适用于名称空间foo中的所有工作负载

```
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
 name: policy
 namespace: foo
spec:
  {}
```

以下授权策略适用于网格中所有名称空间中包含标签"version:v1"的工作负载.(假设根名称空间配置为"istio-config")。

```
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
 name: policy
 namespace: istio-config
spec:
 selector:
   matchLabels:
     version: v1
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://rocdu.gitbook.io/deep-understanding-of-istio/3/4.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
