You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "michaeljmarshall (via GitHub)" <gi...@apache.org> on 2023/03/22 05:23:20 UTC
[GitHub] [pulsar] michaeljmarshall opened a new pull request, #19888: [feat][fn] PIP-257: Support mounting k8s ServiceAccount for OIDC auth
michaeljmarshall opened a new pull request, #19888:
URL: https://github.com/apache/pulsar/pull/19888
PIP: #19771
### Motivation
In order to make OIDC work with functions, we must give them a way to authenticate with the broker using tokens that are able to be validated by an using an Authorization Server. This PR introduces the `KubernetesServiceAccountAuthProvider`.
### Modifications
* Create an `KubernetesServiceAccountAuthProvider` implementation. It adds a service account token volume projection as defined in the k8s docs [here](https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/#service-account-token-volume-projection). The implementation provides a way to specify the expiration time that the token will receive.
* Instead of creating a secret with the broker's trusted `ca.crt` in it, this new `KubernetesServiceAccountAuthProvider` expects a secret to already exist with the `ca.crt`. The major advantage for this implementation is that when the `ca.crt` is rotated, we can refresh it (assuming the client is configured to observe the updated file).
* One point of divergence from the `KubernetesSecretsTokenAuthProvider` implementation is that I did not provide a way for functions to authenticate as the anonymous role. It seems like a stretch that functions would use such authentication because it will not be multi-tenant. However, if that is a concern, we can add the support.
### Verifying this change
I verified the correctness of the code with unit tests. I'll verify the integration with k8s once we've determined this PR's design is correct.
### Does this pull request potentially affect one of the following parts:
This adds new configuration options to the function worker.
### Documentation
- [x] `doc-required`
### Matching PR in forked repository
PR in forked repository: https://github.com/michaeljmarshall/pulsar/pull/36
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] codecov-commenter commented on pull request #19888: [feat][fn] PIP-257: Support mounting k8s ServiceAccount for OIDC auth
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #19888:
URL: https://github.com/apache/pulsar/pull/19888#issuecomment-1498579691
## [Codecov](https://codecov.io/gh/apache/pulsar/pull/19888?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) Report
> Merging [#19888](https://codecov.io/gh/apache/pulsar/pull/19888?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (f585cb3) into [master](https://codecov.io/gh/apache/pulsar/commit/32e677d5264d9145a5765735ae1e6ba0e88a5ef9?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) (32e677d) will **increase** coverage by `1.15%`.
> The diff coverage is `73.60%`.
[](https://codecov.io/gh/apache/pulsar/pull/19888?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
```diff
@@ Coverage Diff @@
## master #19888 +/- ##
============================================
+ Coverage 71.70% 72.85% +1.15%
- Complexity 31368 31611 +243
============================================
Files 1848 1862 +14
Lines 137151 137453 +302
Branches 15111 15133 +22
============================================
+ Hits 98338 100138 +1800
+ Misses 30904 29357 -1547
- Partials 7909 7958 +49
```
| Flag | Coverage Δ | |
|---|---|---|
| inttests | `24.31% <4.80%> (?)` | |
| systests | `24.85% <4.80%> (?)` | |
| unittests | `72.13% <73.60%> (+0.43%)` | :arrow_up: |
Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#carryforward-flags-in-the-pull-request-comment) to find out more.
| [Impacted Files](https://codecov.io/gh/apache/pulsar/pull/19888?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation) | Coverage Δ | |
|---|---|---|
| [...ker/loadbalance/extensions/BrokerRegistryImpl.java](https://codecov.io/gh/apache/pulsar/pull/19888?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci9sb2FkYmFsYW5jZS9leHRlbnNpb25zL0Jyb2tlclJlZ2lzdHJ5SW1wbC5qYXZh) | `83.33% <ø> (ø)` | |
| [...a/org/apache/pulsar/proxy/server/ProxyService.java](https://codecov.io/gh/apache/pulsar/pull/19888?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLXByb3h5L3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9wdWxzYXIvcHJveHkvc2VydmVyL1Byb3h5U2VydmljZS5qYXZh) | `79.36% <60.86%> (-0.82%)` | :arrow_down: |
| [...uth/KubernetesServiceAccountTokenAuthProvider.java](https://codecov.io/gh/apache/pulsar/pull/19888?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWZ1bmN0aW9ucy9ydW50aW1lL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9wdWxzYXIvZnVuY3Rpb25zL2F1dGgvS3ViZXJuZXRlc1NlcnZpY2VBY2NvdW50VG9rZW5BdXRoUHJvdmlkZXIuamF2YQ==) | `77.27% <77.27%> (ø)` | |
| [...rg/apache/pulsar/broker/web/PulsarWebResource.java](https://codecov.io/gh/apache/pulsar/pull/19888?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWJyb2tlci9zcmMvbWFpbi9qYXZhL29yZy9hcGFjaGUvcHVsc2FyL2Jyb2tlci93ZWIvUHVsc2FyV2ViUmVzb3VyY2UuamF2YQ==) | `64.03% <100.00%> (+0.08%)` | :arrow_up: |
| [...functions/auth/KubernetesFunctionAuthProvider.java](https://codecov.io/gh/apache/pulsar/pull/19888?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWZ1bmN0aW9ucy9ydW50aW1lL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9wdWxzYXIvZnVuY3Rpb25zL2F1dGgvS3ViZXJuZXRlc0Z1bmN0aW9uQXV0aFByb3ZpZGVyLmphdmE=) | `80.00% <100.00%> (+5.00%)` | :arrow_up: |
| [...s/runtime/kubernetes/KubernetesRuntimeFactory.java](https://codecov.io/gh/apache/pulsar/pull/19888?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWZ1bmN0aW9ucy9ydW50aW1lL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9wdWxzYXIvZnVuY3Rpb25zL3J1bnRpbWUva3ViZXJuZXRlcy9LdWJlcm5ldGVzUnVudGltZUZhY3RvcnkuamF2YQ==) | `80.42% <100.00%> (+0.35%)` | :arrow_up: |
| [...ime/kubernetes/KubernetesRuntimeFactoryConfig.java](https://codecov.io/gh/apache/pulsar/pull/19888?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation#diff-cHVsc2FyLWZ1bmN0aW9ucy9ydW50aW1lL3NyYy9tYWluL2phdmEvb3JnL2FwYWNoZS9wdWxzYXIvZnVuY3Rpb25zL3J1bnRpbWUva3ViZXJuZXRlcy9LdWJlcm5ldGVzUnVudGltZUZhY3RvcnlDb25maWcuamF2YQ==) | `97.36% <100.00%> (+0.14%)` | :arrow_up: |
... and [239 files with indirect coverage changes](https://codecov.io/gh/apache/pulsar/pull/19888/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=The+Apache+Software+Foundation)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] Anonymitaet commented on pull request #19888: [feat][fn] PIP-257: Support mounting k8s ServiceAccount for OIDC auth
Posted by "Anonymitaet (via GitHub)" <gi...@apache.org>.
Anonymitaet commented on PR #19888:
URL: https://github.com/apache/pulsar/pull/19888#issuecomment-1501545677
Hi @michaeljmarshall thanks for bringing this feature!
I see this PR is labeled with doc-required, for these new configurations, do we need to add some instructions and usage examples to [function docs](https://pulsar.apache.org/docs/next/functions-runtime-kubernetes/)?
If so, feel free to add docs and ping me review. Thank you!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on pull request #19888: [feat][fn] PIP-257: Support mounting k8s ServiceAccount for OIDC auth
Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall commented on PR #19888:
URL: https://github.com/apache/pulsar/pull/19888#issuecomment-1498232627
/pulsarbot rerun-failure-checks
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on pull request #19888: [feat][fn] PIP-257: Support mounting k8s ServiceAccount for OIDC auth
Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall commented on PR #19888:
URL: https://github.com/apache/pulsar/pull/19888#issuecomment-1540753298
Docs added here: https://github.com/apache/pulsar-site/pull/570
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on a diff in pull request #19888: [feat][fn] PIP-257: Support mounting k8s ServiceAccount for OIDC auth
Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall commented on code in PR #19888:
URL: https://github.com/apache/pulsar/pull/19888#discussion_r1146779611
##########
pulsar-functions/runtime/src/main/java/org/apache/pulsar/functions/auth/KubernetesServiceAccountAuthProvider.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.functions.auth;
+
+import io.kubernetes.client.openapi.apis.CoreV1Api;
+import io.kubernetes.client.openapi.models.V1Container;
+import io.kubernetes.client.openapi.models.V1KeyToPath;
+import io.kubernetes.client.openapi.models.V1PodSpec;
+import io.kubernetes.client.openapi.models.V1ProjectedVolumeSource;
+import io.kubernetes.client.openapi.models.V1SecretVolumeSource;
+import io.kubernetes.client.openapi.models.V1ServiceAccountTokenProjection;
+import io.kubernetes.client.openapi.models.V1StatefulSet;
+import io.kubernetes.client.openapi.models.V1Volume;
+import io.kubernetes.client.openapi.models.V1VolumeMount;
+import io.kubernetes.client.openapi.models.V1VolumeProjection;
+import java.nio.file.Paths;
+import java.util.Optional;
+import org.apache.pulsar.broker.authentication.AuthenticationDataSource;
+import org.apache.pulsar.client.impl.auth.AuthenticationToken;
+import org.apache.pulsar.functions.instance.AuthenticationConfig;
+import org.apache.pulsar.functions.proto.Function;
+import org.apache.pulsar.functions.runtime.kubernetes.KubernetesRuntimeFactoryConfig;
+import org.eclipse.jetty.util.StringUtil;
+
+/**
+ * Kubernetes Function Authentication Provider that uses adds a service account to a function pod's container
+ * definition. This is used to authenticate the function instance via OpenId Connect.
+ *
+ * Note: the pod inherits the namespaces default service account.
+ */
+public class KubernetesServiceAccountAuthProvider implements KubernetesFunctionAuthProvider {
+
+ private static final String SERVICE_ACCOUNT_VOLUME_NAME = "service-account-token";
+ private static final String TRUST_CERT_VOLUME_NAME = "ca-cert";
+ private static final String DEFAULT_MOUNT_DIR = "/etc/auth";
+ private static final String FUNCTION_AUTH_TOKEN = "token";
+ private static final String FUNCTION_CA_CERT = "ca.crt";
+ private String brokerTrustCertsSecretName;
+ private long serviceAccountTokenExpirationSeconds;
+
+ @Override
+ public void initialize(CoreV1Api coreClient, byte[] caBytes,
+ java.util.function.Function<Function.FunctionDetails, String> namespaceCustomizerFunc,
+ KubernetesRuntimeFactoryConfig config) {
+ setNamespaceProviderFunc(namespaceCustomizerFunc);
+ brokerTrustCertsSecretName = config.getBrokerClientTrustCertsSecretName();
+ serviceAccountTokenExpirationSeconds = config.getServiceAccountTokenExpirationSeconds();
+ }
+
+ @Override
+ public void configureAuthenticationConfig(AuthenticationConfig authConfig,
+ Optional<FunctionAuthData> functionAuthData) {
+ authConfig.setClientAuthenticationPlugin(AuthenticationToken.class.getName());
+ authConfig.setClientAuthenticationParameters(Paths.get(DEFAULT_MOUNT_DIR, FUNCTION_AUTH_TOKEN)
+ .toUri().toString());
+ // if we have ca bytes, update the new path for the CA
+ if (StringUtil.isNotBlank(brokerTrustCertsSecretName)) {
+ authConfig.setTlsTrustCertsFilePath(String.format("%s/%s", DEFAULT_MOUNT_DIR, FUNCTION_CA_CERT));
+ }
+ }
+
+ /**
+ * No need to cache anything because we rely on Kubernetes to generate the token used for authentication.
+ */
+ @Override
+ public Optional<FunctionAuthData> cacheAuthData(Function.FunctionDetails funcDetails,
+ AuthenticationDataSource authenticationDataSource)
+ throws Exception {
+ return Optional.empty();
+ }
+
+ /**
+ * No need to update anything because we rely on Kubernetes to generate the token used for authentication.
+ */
+ @Override
+ public Optional<FunctionAuthData> updateAuthData(Function.FunctionDetails funcDetails,
+ Optional<FunctionAuthData> existingFunctionAuthData,
+ AuthenticationDataSource authenticationDataSource)
+ throws Exception {
+ return Optional.empty();
+ }
+
+ /**
+ * No need to clean up anything because we rely on Kubernetes to clean up the secret when the pod is deleted.
+ */
+ @Override
+ public void cleanUpAuthData(Function.FunctionDetails funcDetails, Optional<FunctionAuthData> functionAuthData)
+ throws Exception {
+
+ }
+
+ @Override
+ public void initialize(CoreV1Api coreClient) {
Review Comment:
The Pulsar Function Worker does not call this method. We could probably deprecate now and remove it later, if we want. It is used in another implementation here:
https://github.com/apache/pulsar/blob/01a49929951f70f3858a59a15f31cf15c16c1c8e/pulsar-functions/runtime/src/main/java/org/apache/pulsar/functions/auth/KubernetesFunctionAuthProvider.java#L40-L45
From an API design perspective, it seems like we only need the `initialize` method and could deprecate the `setCaBytes` and `setNamespaceProviderFunc` methods as well since they are only called in the `initialize` method and never outside of the class.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall commented on a diff in pull request #19888: [feat][fn] PIP-257: Support mounting k8s ServiceAccount for OIDC auth
Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall commented on code in PR #19888:
URL: https://github.com/apache/pulsar/pull/19888#discussion_r1146786693
##########
pulsar-functions/runtime/src/main/java/org/apache/pulsar/functions/runtime/kubernetes/KubernetesRuntimeFactoryConfig.java:
##########
@@ -169,4 +169,17 @@ public class KubernetesRuntimeFactoryConfig {
)
protected int gracePeriodSeconds = 5;
+ @FieldContext(
+ doc = "The Kubernetes secret containing the broker's trust certs. If it is not set, the function will not"
+ + " use a custom trust store. The secret must already exist in each function's target namespace."
+ + " The secret must contain a key named `ca.crt` with the trust certs."
Review Comment:
Great question. First, I think I might have needed to use `ca.pem`. Would that clear things up?
Second, the current design reads the bytes from the file at this path:
https://github.com/apache/pulsar/blob/9bb3a213e8d7fca474c8e32882f9a76e5bc649ff/pulsar-functions/runtime/src/main/java/org/apache/pulsar/functions/worker/WorkerConfig.java#L788-L799
and that relies on:
https://github.com/apache/pulsar/blob/9bb3a213e8d7fca474c8e32882f9a76e5bc649ff/pulsar-functions/runtime/src/main/java/org/apache/pulsar/functions/worker/WorkerConfig.java#L615-L629
So we do not allow for configuration at the moment, and we expect users to provide the right format already. We can, of course, improve on that design.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] michaeljmarshall merged pull request #19888: [feat][fn] PIP-257: Support mounting k8s ServiceAccount for OIDC auth
Posted by "michaeljmarshall (via GitHub)" <gi...@apache.org>.
michaeljmarshall merged PR #19888:
URL: https://github.com/apache/pulsar/pull/19888
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar] eolivelli commented on a diff in pull request #19888: [feat][fn] PIP-257: Support mounting k8s ServiceAccount for OIDC auth
Posted by "eolivelli (via GitHub)" <gi...@apache.org>.
eolivelli commented on code in PR #19888:
URL: https://github.com/apache/pulsar/pull/19888#discussion_r1144922484
##########
pulsar-functions/runtime/src/main/java/org/apache/pulsar/functions/runtime/kubernetes/KubernetesRuntimeFactoryConfig.java:
##########
@@ -169,4 +169,17 @@ public class KubernetesRuntimeFactoryConfig {
)
protected int gracePeriodSeconds = 5;
+ @FieldContext(
+ doc = "The Kubernetes secret containing the broker's trust certs. If it is not set, the function will not"
+ + " use a custom trust store. The secret must already exist in each function's target namespace."
+ + " The secret must contain a key named `ca.crt` with the trust certs."
Review Comment:
is there any expectations about the "format" of the file ?
should we have an entry which describes the format used to create the file ?
##########
pulsar-functions/runtime/src/main/java/org/apache/pulsar/functions/auth/KubernetesServiceAccountAuthProvider.java:
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.functions.auth;
+
+import io.kubernetes.client.openapi.apis.CoreV1Api;
+import io.kubernetes.client.openapi.models.V1Container;
+import io.kubernetes.client.openapi.models.V1KeyToPath;
+import io.kubernetes.client.openapi.models.V1PodSpec;
+import io.kubernetes.client.openapi.models.V1ProjectedVolumeSource;
+import io.kubernetes.client.openapi.models.V1SecretVolumeSource;
+import io.kubernetes.client.openapi.models.V1ServiceAccountTokenProjection;
+import io.kubernetes.client.openapi.models.V1StatefulSet;
+import io.kubernetes.client.openapi.models.V1Volume;
+import io.kubernetes.client.openapi.models.V1VolumeMount;
+import io.kubernetes.client.openapi.models.V1VolumeProjection;
+import java.nio.file.Paths;
+import java.util.Optional;
+import org.apache.pulsar.broker.authentication.AuthenticationDataSource;
+import org.apache.pulsar.client.impl.auth.AuthenticationToken;
+import org.apache.pulsar.functions.instance.AuthenticationConfig;
+import org.apache.pulsar.functions.proto.Function;
+import org.apache.pulsar.functions.runtime.kubernetes.KubernetesRuntimeFactoryConfig;
+import org.eclipse.jetty.util.StringUtil;
+
+/**
+ * Kubernetes Function Authentication Provider that uses adds a service account to a function pod's container
+ * definition. This is used to authenticate the function instance via OpenId Connect.
+ *
+ * Note: the pod inherits the namespaces default service account.
+ */
+public class KubernetesServiceAccountAuthProvider implements KubernetesFunctionAuthProvider {
+
+ private static final String SERVICE_ACCOUNT_VOLUME_NAME = "service-account-token";
+ private static final String TRUST_CERT_VOLUME_NAME = "ca-cert";
+ private static final String DEFAULT_MOUNT_DIR = "/etc/auth";
+ private static final String FUNCTION_AUTH_TOKEN = "token";
+ private static final String FUNCTION_CA_CERT = "ca.crt";
+ private String brokerTrustCertsSecretName;
+ private long serviceAccountTokenExpirationSeconds;
+
+ @Override
+ public void initialize(CoreV1Api coreClient, byte[] caBytes,
+ java.util.function.Function<Function.FunctionDetails, String> namespaceCustomizerFunc,
+ KubernetesRuntimeFactoryConfig config) {
+ setNamespaceProviderFunc(namespaceCustomizerFunc);
+ brokerTrustCertsSecretName = config.getBrokerClientTrustCertsSecretName();
+ serviceAccountTokenExpirationSeconds = config.getServiceAccountTokenExpirationSeconds();
+ }
+
+ @Override
+ public void configureAuthenticationConfig(AuthenticationConfig authConfig,
+ Optional<FunctionAuthData> functionAuthData) {
+ authConfig.setClientAuthenticationPlugin(AuthenticationToken.class.getName());
+ authConfig.setClientAuthenticationParameters(Paths.get(DEFAULT_MOUNT_DIR, FUNCTION_AUTH_TOKEN)
+ .toUri().toString());
+ // if we have ca bytes, update the new path for the CA
+ if (StringUtil.isNotBlank(brokerTrustCertsSecretName)) {
+ authConfig.setTlsTrustCertsFilePath(String.format("%s/%s", DEFAULT_MOUNT_DIR, FUNCTION_CA_CERT));
+ }
+ }
+
+ /**
+ * No need to cache anything because we rely on Kubernetes to generate the token used for authentication.
+ */
+ @Override
+ public Optional<FunctionAuthData> cacheAuthData(Function.FunctionDetails funcDetails,
+ AuthenticationDataSource authenticationDataSource)
+ throws Exception {
+ return Optional.empty();
+ }
+
+ /**
+ * No need to update anything because we rely on Kubernetes to generate the token used for authentication.
+ */
+ @Override
+ public Optional<FunctionAuthData> updateAuthData(Function.FunctionDetails funcDetails,
+ Optional<FunctionAuthData> existingFunctionAuthData,
+ AuthenticationDataSource authenticationDataSource)
+ throws Exception {
+ return Optional.empty();
+ }
+
+ /**
+ * No need to clean up anything because we rely on Kubernetes to clean up the secret when the pod is deleted.
+ */
+ @Override
+ public void cleanUpAuthData(Function.FunctionDetails funcDetails, Optional<FunctionAuthData> functionAuthData)
+ throws Exception {
+
+ }
+
+ @Override
+ public void initialize(CoreV1Api coreClient) {
Review Comment:
this method is not meant to be called, I suggest to throw an exception here
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org