Class EnvCheck

java.lang.Object
io.aiven.commons.system.EnvCheck

public final class EnvCheck extends Object
Checks URLs against the URLs allowed as per the org.apache.kafka.sasl.oauthbearer.allowed.urls environment variable. If the variable is not set all URLs are allowed.
  • Method Details

    • formatError

      public static String formatError(EnvCheck.Type type, String value)
      Format the error message for the specified URL.
      Parameters:
      type - the type check that failed
      value - the value to create an error message for.
      Returns:
      the error message.
    • allowed

      public static boolean allowed(EnvCheck.Type type, String value)
      Checks if the URL is allowed as per the org.apache.kafka.sasl.oauthbearer.allowed.urls environment variable.
      Parameters:
      type - the type to check.
      value - the value to check.
      Returns:
      true if the value is listed in the environment variable for the type, false if the environment variable is not set or the value is not listed.
    • throwIfNotAllowed

      public static void throwIfNotAllowed(EnvCheck.Type type, String value) throws IllegalArgumentException
      Thorw an exception if the specified URL is not listed in the org.apache.kafka.sasl.oauthbearer.allowed.urls environment variable.
      Parameters:
      type - the type to check.
      value - the value to check
      Throws:
      IllegalArgumentException - if the allowed(Type, String) returns false.
    • getEnvVars

      public static Map<String,String> getEnvVars() throws NoSuchFieldException, IllegalAccessException
      Return an updatable version of the environment variables. Changes to the map will be reflected in the system environment variables.

      This method will only work if the command line options --add-opens java.base/java.util=io.aiven.commons.system and --add-opens java.base/java.lang=io.aiven.commons.system are used.

      Changes made to the returned map change the environment variables. This is handy for testing but should be done in a try-catch-finally block with the values being reset in the finally block.

      To remove an environment variable use the Map.remove(Object) method. Setting the map value to null will throw an exception.

      Returns:
      a modifiable map backed by the system environment variables.
      Throws:
      NoSuchFieldException - Should not be thrown. Indicates a change to the Collections.UnmodifiableMap implementation.
      IllegalAccessException - if the command line options were not specified when the JVM was loaded.