GitHub user mattcasters added a comment to the discussion: Dynamic variables

I tried to build a quick secret lookup plugin for GCP Secret Manager and after 
some library shenanigans...

![image](https://github.com/user-attachments/assets/ad8288b8-3825-4d8c-8a73-d105d3fff0cb)

I'll try to spend some time over the weekend to give a number of Google 
libraries and Beam an update.
But anyway:

```java
@Getter
@Setter
@GuiPlugin
@VariableResolverPlugin(
    id = "Variable-Resolver-GoogleSecretManager",
    name = "Google Secret Manager Variable Resolver",
    description = "Automatically look up values of secrets in Google Secret 
Manager",
    documentationUrl =
        "/variables/resolvers/google-secret-manager.html" // TODO: write this 
documentation
    )
public class GooleSecretManagerVariableResolver implements IVariableResolver {

  /** The name of the variable that will contain the expression in the 
pipeline. */
  @GuiWidgetElement(
      id = "projectId",
      order = "0!",
      label =
          
"i18n:org.apache.hop.core.variables.resolver:GooleSecretManagerVariableResolver.label.ProjectId",
      type = GuiElementType.TEXT,
      parentId = VariableResolver.GUI_PLUGIN_ELEMENT_PARENT_ID)
  @HopMetadataProperty
  private String projectId;

  /** The name of the variable that will contain the expression in the 
pipeline. */
  @GuiWidgetElement(
      id = "locationId",
      order = "02",
      label =
          
"i18n:org.apache.hop.core.variables.resolver:GooleSecretManagerVariableResolver.label.LocationId",
      type = GuiElementType.TEXT,
      parentId = VariableResolver.GUI_PLUGIN_ELEMENT_PARENT_ID)
  @HopMetadataProperty
  private String locationId;

  @Override
  public void init() {
    // Not used at this time.  If performance is too bad for the client 
construction we can still do
    // it.
    // For now we assume that it's all just using web services anyway.
  }

  @Override
  public String resolve(String secretId, IVariables variables) throws 
HopException {

    if (StringUtils.isEmpty(secretId)) {
      return secretId;
    }

    try {
      SecretManagerServiceSettings.Builder settingsBuilder =
          SecretManagerServiceSettings.newBuilder();
      if (StringUtils.isNotEmpty(locationId)) {
        String apiEndpoint = 
String.format("secretmanager.%s.rep.googleapis.com:443", locationId);
        settingsBuilder.setEndpoint(apiEndpoint);
      }
      SecretManagerServiceSettings settings = settingsBuilder.build();

      try (SecretManagerServiceClient client = 
SecretManagerServiceClient.create(settings)) {

        String actualLocationId = variables.resolve(locationId);
        String actualProjectId = variables.resolve(projectId);

        // Get the secret version name.
        //
        SecretVersionName secretName;
        if (StringUtils.isEmpty(actualLocationId)) {
          secretName =
              SecretVersionName.ofProjectSecretSecretVersionName(
                  actualProjectId, secretId, "latest");
        } else {
          secretName =
              SecretVersionName.ofProjectLocationSecretSecretVersionName(
                  actualProjectId, actualLocationId, secretId, "latest");
        }

        // Get the payload for this version
        //
        AccessSecretVersionResponse response = 
client.accessSecretVersion(secretName);

        // Create the secret.
        return response.getPayload().getData().toStringUtf8();
      }
    } catch (Exception e) {
      throw new HopException(
          "Error looking up secret key '" + secretId + "' in Google Secret 
Manager", e);
    }
  }

  @Override
  public void setPluginId() {
    // Nothing to set
  }

  @Override
  public String getPluginId() {
    return "Variable-Resolver-GoogleSecretManager";
  }

  @Override
  public void setPluginName(String pluginName) {
    // Nothing to set
  }

  @Override
  public String getPluginName() {
    return "Google Secret Manager Variable Resolver";
  }
}
``` 

GitHub link: 
https://github.com/apache/hop/discussions/4763#discussioncomment-11773748

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]

Reply via email to