Kubernetes Java API 不使用提供的用户名密码

问题描述 投票:0回答:3

这是关于 Kubernetes Java 客户端 0.2 版本的。我猜想在 Java API 中使用基本身份验证的方法就是这样做

ApiClient client = Config.fromUserPassword( "https://....:6443", "user", "password", false );
Configuration.setDefaultApiClient( client );
CoreV1Api api = new CoreV1Api();
// Make api call like
api.listNode(...)

但是上面的代码总是返回403 Forbidden。从响应消息来看,请求中似乎没有使用用户/密码。

{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"nodes is forbidden: User \"system:anonymous\" cannot list nodes at the cluster scope","reason":"Forbidden","details":{"kind":"nodes"},"code":403}

我还对代码进行了一些调试,我可能会回答我自己的问题,但看起来在 CoreV1Api 的方法中,它从不添加基本身份验证作为身份验证方法,而仅使用 BearerToken。是否支持基本身份验证或者我应该使用其他 API 类?

java kubernetes
3个回答
0
投票

许多 kubernetes 集群不设置基本身份验证,仅设置不记名令牌身份验证。您确定您的集群配置了基本身份验证吗?

https://kubernetes.io/docs/admin/authentication/#static-password-file


0
投票

回答我自己的问题,但看起来当前版本的客户端实际上并未执行用户/通行证身份验证。不过 BearerToken 正在工作。


0
投票

java 客户端会忽略

HttpBasicAuth
对象,但如果您使用
ApiKeyAuth
对象并将密钥前缀设置为“Basic”,并将 API 密钥设置为 base64 编码的凭据,它将起作用。

例如:

String credentials = new String(Base64.getEncoder().encode("user:password".getBytes()));
ApiClient defaultClient = Configuration.getDefaultApiClient();
defaultClient.setBasePath("https://256.256.256.256");
ApiKeyAuth fakeBearerToken = (ApiKeyAuth) defaultClient.getAuthentication("BearerToken");
fakeBearerToken.setApiKey(credentials);
fakeBearerToken.setApiKeyPrefix("Basic");

这是可行的,因为 kubernetes 客户端只需将 API 密钥前缀与前缀连接起来,并将结果放入“Authorization”标头中。

© www.soinside.com 2019 - 2024. All rights reserved.