Memcached 的 Istio 授权策略
Istio Authorization polices for memcached
我是 Istio 授权策略的新手,我需要一些设置授权策略的帮助:
场景如下:
我有一个名为 namespace1 的命名空间,其中包含 4 个微服务 运行。对于上下文,我们称它们为 A、B、C、D。并且所有 4 个微服务都启用了 istio-sidecar 注入。
有一个名为 namespace2 的命名空间,其中包含 2 个微服务 运行。对于上下文,我们称它们为 E,F。并且两个微服务都启用了 istio-sidecar 注入。
现在我已经按照 Memcached using mcrouter 到命名空间 memcached 部署了 Memcached 服务。并且所有 pods 的 Memcached 也都启用了 istio-sidecar 注入。
现在我有一个场景,我必须只允许来自命名空间 1 中的 B 和 C 微服务的调用对 memcached 服务进行调用,并拒绝来自命名空间 1 中的 A 和 D 的调用以及来自任何其他命名空间的调用。是否可以使用 istio 授权策略实现此目的?
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: authorization-policy-deny-all
namespace: memcached
spec:
selector:
matchLabels:
app: activator
action: DENY
rules:
- from:
- source:
notNamespaces: ["namespace1"]
这是我能想到的最好的方法,我只允许来自 namepsace1 的调用并拒绝来自所有其他名称空间的调用。我不知道如何拒绝来自命名空间 1 中的 A 和 D 微服务的调用。
您还可以使用委托人来允许访问。至于来自 Istio documentation on Authorization Policy 的示例:
所以类似的事情应该是可能的:
- from:
- source:
principals: ["cluster.local/ns/namespace1/sa/B","cluster.local/ns/namespace1/sa/C"]
根据文档:
1. If there are any DENY policies that match the request, deny the request.
2. If there are no ALLOW policies for the workload, allow the request.
3. If any of the ALLOW policies match the request, allow the request.
4. Deny the request.
因此,如果您对 memcached 有一个 ALLOW 策略,并允许从 B 和 C 访问(规则 3),那么应该拒绝从其他来源对 memcached 的其他请求(规则 2 不允许访问,因为您有一个允许政策)。
(未经测试)
这是一种可能有效的设置。
---
spec:
selector:
matchLabels:
app: activator
action: ALLOW
rules:
- from:
- source:
principals:
- cluster.local/ns/namespace1/sa/memcached--user
- B & C 使用与其他服务不同的服务帐户。假设服务帐户名称是 memcached--user(取决于 B 和 C 所需的角色,您甚至可能希望每个服务都有单独的服务帐户)
- 定义 AuthorizationPolicy 以允许主体访问,主体是 B 和 C 使用的服务帐户。
- 确保启用了 mTLS。如 docs 中所述,此字段(主体)需要启用 mTLS。
- 确保选择器配置正确。
希望这能解决您的问题。
我是 Istio 授权策略的新手,我需要一些设置授权策略的帮助:
场景如下:
我有一个名为 namespace1 的命名空间,其中包含 4 个微服务 运行。对于上下文,我们称它们为 A、B、C、D。并且所有 4 个微服务都启用了 istio-sidecar 注入。
有一个名为 namespace2 的命名空间,其中包含 2 个微服务 运行。对于上下文,我们称它们为 E,F。并且两个微服务都启用了 istio-sidecar 注入。
现在我已经按照 Memcached using mcrouter 到命名空间 memcached 部署了 Memcached 服务。并且所有 pods 的 Memcached 也都启用了 istio-sidecar 注入。
现在我有一个场景,我必须只允许来自命名空间 1 中的 B 和 C 微服务的调用对 memcached 服务进行调用,并拒绝来自命名空间 1 中的 A 和 D 的调用以及来自任何其他命名空间的调用。是否可以使用 istio 授权策略实现此目的?
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
name: authorization-policy-deny-all
namespace: memcached
spec:
selector:
matchLabels:
app: activator
action: DENY
rules:
- from:
- source:
notNamespaces: ["namespace1"]
这是我能想到的最好的方法,我只允许来自 namepsace1 的调用并拒绝来自所有其他名称空间的调用。我不知道如何拒绝来自命名空间 1 中的 A 和 D 微服务的调用。
您还可以使用委托人来允许访问。至于来自 Istio documentation on Authorization Policy 的示例:
所以类似的事情应该是可能的:
- from:
- source:
principals: ["cluster.local/ns/namespace1/sa/B","cluster.local/ns/namespace1/sa/C"]
根据文档:
1. If there are any DENY policies that match the request, deny the request.
2. If there are no ALLOW policies for the workload, allow the request.
3. If any of the ALLOW policies match the request, allow the request.
4. Deny the request.
因此,如果您对 memcached 有一个 ALLOW 策略,并允许从 B 和 C 访问(规则 3),那么应该拒绝从其他来源对 memcached 的其他请求(规则 2 不允许访问,因为您有一个允许政策)。
(未经测试)
这是一种可能有效的设置。
---
spec:
selector:
matchLabels:
app: activator
action: ALLOW
rules:
- from:
- source:
principals:
- cluster.local/ns/namespace1/sa/memcached--user
- B & C 使用与其他服务不同的服务帐户。假设服务帐户名称是 memcached--user(取决于 B 和 C 所需的角色,您甚至可能希望每个服务都有单独的服务帐户)
- 定义 AuthorizationPolicy 以允许主体访问,主体是 B 和 C 使用的服务帐户。
- 确保启用了 mTLS。如 docs 中所述,此字段(主体)需要启用 mTLS。
- 确保选择器配置正确。
希望这能解决您的问题。