Custom Authentication with AMPS spark CLI
The AMPS spark CLI uses the AMPS Java client to authenticate to AMPS. If your site uses a custom authentication strategy, AMPS spark CLI can use the same authenticator as your Java applications.
Provide a Username and Password
If the default AMPS authenticator supports your site's authentication strategy, provide the username and password in the -server option. A custom authenticator factory is not required in this case.
%> spark sow -server user:pass@localhost:9007 -type json -topic myTopic
For more information on authentication options, see AMPS spark CLI Authentication.
Implement an Authenticator Factory
To use a custom authenticator, implement the AuthenticatorFactory interface provided in amps_spark.jar. In the standalone AMPS spark CLI distribution, this JAR is in the dist/lib directory.
The interface defines a method that receives the connection URI and returns an AMPS Java client Authenticator:
package com.crankuptheamps.spark;
import com.crankuptheamps.client.Authenticator;
import com.crankuptheamps.client.exception.AuthenticationException;
public interface AuthenticatorFactory {
public Authenticator createAuthenticator(String connectionURI)
throws AuthenticationException;
}
The following example logs when the factory is called and returns the default authenticator. Replace the returned authenticator with your site's implementation to use your custom authentication strategy.
package com.example;
import com.crankuptheamps.spark.AuthenticatorFactory;
import com.crankuptheamps.client.Authenticator;
import com.crankuptheamps.client.DefaultAuthenticator;
import com.crankuptheamps.client.exception.AuthenticationException;
public class MyAuthenticatorFactory implements AuthenticatorFactory {
public MyAuthenticatorFactory() {}
@Override
public Authenticator createAuthenticator(String connectionURI)
throws AuthenticationException {
System.err.println("Creating authenticator");
return new DefaultAuthenticator();
}
}
The factory must have a public constructor that takes no arguments. For details on implementing an Authenticator, see the AMPS Java Client API documentation.
Load the Custom Authenticator
Provide the fully qualified name of your factory class with the -authenticator option:
%> spark sow -server localhost:9007 -type json -topic myTopic \
-authenticator com.example.MyAuthenticatorFactory
The factory and its dependencies must be available on the Java classpath used by AMPS spark CLI. If the launcher cannot find your factory, invoke Java directly with an explicit classpath. For example, on Linux or macOS:
%> java -cp "${SPARKDIR}/dist/lib/*:${AUTHENTICATOR_DIR}/myAuth.jar" \
com.crankuptheamps.spark.Spark \
sow -server localhost:9007 -type json -topic myTopic \
-authenticator com.example.MyAuthenticatorFactory
In this example, SPARKDIR is the standalone AMPS spark CLI installation directory, and AUTHENTICATOR_DIR contains myAuth.jar, which provides the factory and your custom authenticator. Include any additional dependencies in the classpath. On Windows, use a semicolon (;) to separate classpath entries instead of a colon (:), and use the environment variable syntax for your shell.
For repeated use, you can create a wrapper script that sets the classpath and supplies the -authenticator option for your site.