Enable security on functions

If you want to enable security on functions, complete the following steps.

Prerequisites

Step 1: Configure function workers

To use the secret APIs from the context, you need to set the following two parameters for function workers.

  • secretsProviderConfiguratorClassName
  • secretsProviderConfiguratorConfig

Pulsar Functions provided two types of SecretsProviderConfigurator implementation and both can be used as the value of secretsProviderConfiguratorClassName directly:

  • org.apache.pulsar.functions.secretsproviderconfigurator.DefaultSecretsProviderConfigurator: This is a barebones version of a secrets provider which wires in ClearTextSecretsProvider to the function instances.
  • org.apache.pulsar.functions.secretsproviderconfigurator.KubernetesSecretsProviderConfigurator: This is used by default for running in Kubernetes and it uses Kubernetes built-in secrets and binds them as environment variables (via EnvironmentBasedSecretsProvider) within the function container to ensure that the secrets are available to the function at runtime.

Function workers use the org.apache.pulsar.functions.secretsproviderconfigurator.SecretsProviderConfigurator interface to choose the SecretsProvider class name and its associated configurations at the time of starting the function instances.

Function instances use the org.apache.pulsar.functions.secretsprovider.SecretsProvider interface to fetch the secrets. The implementation that SecretsProvider uses is determined by SecretsProviderConfigurator.

You can also implement your own SecretsProviderConfigurator if you want to use different SecretsProvider for function instances.

Enable security on functions - 图1note

Currently, only Java and Python runtime support SecretsProvider. The Java and Python Runtime have the following two providers:

  • ClearTextSecretsProvider (default for DefaultSecretsProviderConfigurator)
  • EnvironmentBasedSecretsProvider (default for KubernetesSecretsProviderConfigurator)

Step 2: Get the secret

Once SecretsProviderConfigurator is set, you can get the secret using the Context object as follows.

  • Java
  • Python
  1. import org.apache.pulsar.functions.api.Context;
  2. import org.apache.pulsar.functions.api.Function;
  3. import org.slf4j.Logger;
  4. public class GetSecretValueFunction implements Function<String, Void> {
  5. @Override
  6. public Void process(String input, Context context) throws Exception {
  7. Logger LOG = context.getLogger();
  8. String secretValue = context.getSecret(input);
  9. if (!secretValue.isEmpty()) {
  10. LOG.info("The secret {} has value {}", input, secretValue);
  11. } else {
  12. LOG.warn("No secret with key {}", input);
  13. }
  14. return null;
  15. }
  16. }
  1. from pulsar import Function
  2. class GetSecretValueFunction(Function):
  3. def process(self, input, context):
  4. logger = context.get_logger()
  5. secret_value = context.get_secret(input)
  6. if secret_provider is None:
  7. logger.warn('No secret with key {0} '.format(input))
  8. else:
  9. logger.info("The secret {0} has value {1}".format(input, secret_value))