ALLOW
策略还是 DENY
策略。ALLOW
和 DENY
策略应用于同一范围时,DNEY
策略的优先级高于 ALLOW
策略,生效的规则如下:DENY
策略,则拒绝该请求的访问。ALLOW
策略,则允许该请求的访问。ALLOW
策略,且请求匹配到了任何一条 ALLOW
策略,则允许该请求的访问。apiVersion: security.istio.io/v1beta1kind: AuthorizationPolicymetadata:name: allow-allnamespace: defaultspec:action: ALLOWrules:- {}# 规则可以匹配任何请求
apiVersion: security.istio.io/v1beta1kind: AuthorizationPolicymetadata:name: deny-allnamespace: defaultspec:{}# 当 action 字段没有填写时,默认是 ALLOW,此时请求无法匹配任何规则
字段名称 | 字段类型 | 字段说明 |
metadata.name | string | AuthorizationPolicy 名称 |
metadata.namespace | string | AuthorizationPolicy 命名空间 |
spec.selector | map<string, string> | AuthorizationPolicy 使用填写的标签键值对,配合填写的 namespace,匹配配置下发的 Workload 范围: namespace 填写 istio-system,且 selector 字段不填写时,该策略生效范围为整个网格 namespace 填写非 istio-system 的 namespace,且 selector 字段不填写时,策略生效范围为填写的 namespace namespace 填写非 istio-system 的 namespace,且 selector 字段填写了有效键值对时,策略的生效范围为在所填 namespace 下根据 selector 匹配到的 Workload |
spec.action | - | 指定该策略是 ALLOW 策略还是 DENY 策略 |
spec.rules.from.source.principals | string[] | 源对等身份列表(即 service account),匹配 source.principal 字段 ,要求启用 mTLS,未填写时则允许任何 principal |
spec.rules.from.source.requestPrincipals | string[] | 请求身份列表(即 iss/sub claim),匹配 request.auth.principal 字段,未填写时则允许任何 requestPrincipals |
spec.rules.from.source.namespaces | string[] | 请求源的 namespace 列表,匹配 source.namespace 字段,要求启用 mTLS,未填写时允许来自任何 namespace 的请求 |
spec.rules.from.source.ipBlocks | string[] | IP block 列表,匹配 source.ip 字段,支持单 IP 写法(如 1.2.3.4 )或 CIDR 写法(如 1.2.3.4/24 ),未填写时允许任何源 IP 的访问 |
spec.rules.to.operation.hosts | string[] | 请求的域名列表,匹配 request.host 字段,未填写时允许任何域名,仅支持在 HTTP 协议请求中使用 |
spec.rules.to.operation.ports | string[] | 请求的端口列表,匹配 destination.port 字段,未填写时允许任何端口 |
spec.rules.to.operation.methods | string[] | 请求的方法列表,匹配 request.method 字段,使用 gRPC 协议时该值始终应为 POST 。未填写时允许任何方法 ,仅支持在 HTTP 协议请求中使用 |
spec.rules.to.operation.paths | string[] | 请求的路径,匹配 request.url_path 字段,未填写时允许任何路径,仅支持在 HTTP 协议请求中使用 |
spec.rules.when.condition.key | string | Istio 支持的条件字段名称,详见 Authorization Policy Conditions |
spec.rules.when.condition.values | string[] | 填写对应条件的值列表 |
apiVersion: v1kind: Namespacemetadata:name: testlabels:istio.io/rev: 1-6-9 # sidecar 自动注入(istio 1.6.9)spec:finalizers:- kubernetes---apiVersion: apps/v1kind: Deploymentmetadata:name: clientnamespace: testlabels:app: clientspec:replicas: 10selector:matchLabels:app: clienttemplate:metadata:labels:app: clientspec:containers:- name: clientimage: ccr.ccs.tencentyun.com/zhulei/testclient:v1imagePullPolicy: Alwaysenv:- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: REGIONvalue: "guangzhou-zoneA"ports:- containerPort: 7000protocol: TCP---apiVersion: v1kind: Servicemetadata:name: clientnamespace: testlabels:app: clientspec:ports:- name: httpport: 7000protocol: TCPselector:app: clienttype: ClusterIP---apiVersion: v1kind: Namespacemetadata:name: baselabels:istio.io/rev: 1-6-9spec:finalizers:- kubernetes---apiVersion: apps/v1kind: Deploymentmetadata:name: usernamespace: baselabels:app: userspec:replicas: 1selector:matchLabels:app: usertemplate:metadata:labels:app: userspec:containers:- name: userimage: ccr.ccs.tencentyun.com/zhulei/testuser:v1imagePullPolicy: Alwaysenv:- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: REGIONvalue: "guangzhou-zoneB"ports:- containerPort: 7000---apiVersion: v1kind: Servicemetadata:name: usernamespace: baselabels:app: userspec:ports:- port: 7000name: httpselector:app: user
apiVersion: security.istio.io/v1beta1kind: AuthorizationPolicymetadata:name: base-authznamespace: basespec:action: DENYrules:- from:- source:namespaces:- test
httpbin.foo
,并配置通过 Ingress Gateway 暴露此服务到公网:apiVersion: v1kind: Namespacemetadata:name: foolabels:istio.io/rev: 1-6-9 # 开启 namespace 的 sidecar 自动注入(istio 版本 1.6.9)spec:finalizers:- kubernetes---apiVersion: v1kind: ServiceAccountmetadata:name: httpbinnamespace: foo---apiVersion: v1kind: Servicemetadata:name: httpbinnamespace: foolabels:app: httpbinservice: httpbinspec:ports:- name: httpport: 8000targetPort: 80selector:app: httpbin---apiVersion: apps/v1kind: Deploymentmetadata:name: httpbinnamespace: foospec:replicas: 1selector:matchLabels:app: httpbinversion: v1template:metadata:labels:app: httpbinversion: v1spec:serviceAccountName: httpbincontainers:- image: docker.io/kennethreitz/httpbinimagePullPolicy: IfNotPresentname: httpbinports:- containerPort: 80
apiVersion: networking.istio.io/v1alpha3kind: Gatewaymetadata:name: httpbin-gatewaynamespace: foospec:selector:app: istio-ingressgatewayistio: ingressgatewayservers:- port:number: 80name: httpprotocol: HTTPhosts:- "*"---apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:name: httpbinnamespace: foospec:hosts:- "*"gateways:- httpbin-gatewayhttp:- route:- destination:port:number: 8000host: httpbin.foo.svc.cluster.local
curl "$INGRESS_IP:80/headers" -s -o /dev/null -w "%{http_code}\\n"
测试服务的连通性,注意您需要将代码中的 $INGRESS_IP
替换为您的边缘代理网关 IP 地址,正常情况下会返回 200
返回码。apiVersion: security.istio.io/v1beta1kind: AuthorizationPolicymetadata:name: black-listnamespace: istio-systemspec:selector:matchLabels:app: istio-ingressgatewayistio: ingressgatewayrules:- from:- source:ipBlocks:- $您的本机 IP 地址action: DENY
curl "$INGRESS_IP:80/headers" -s -o /dev/null -w "%{http_code}\\n"
测试服务的连通性,注意您需要将代码中的 $INGRESS_IP
替换为您的边缘代理网关 IP 地址,此时访问失败,返回 403
返回码,黑名单策略生效。
本页内容是否解决了您的问题?