Skip to content

Configuring your client

Tomas Celaya edited this page Dec 29, 2017 · 1 revision

The java-manta SDK supports an abundance of methods for client configuration in order to accomodate extremely flexible configuration methods, simple environment-based configuration, plain setters, and everything in between. In this guide, we'll explore some of these strategies for providing configuration to the client.

The ConfigContext interfaces

Central to the java-manta SDK is the ConfigContext interface, providing getters for each option listed in the configuration parameters table. No setters are included on this interface since some styles of configuration are immutable. A secondary interface named SettableConfigContext extends ConfigContext and includes setters which can be used to modify configuration parameters. Additionally, the SettableConfigContext includes a type parameter to allow for chaining setter calls.

Let's create a variable to hold our configuration, we'll name it config and start with just the defaults:

ConfigContext config = new DefaultsConfigContext();

Setting your own configuration values

Interfaces are great, but eventually you'll need to actually include your own settings. There are a variety of options for this task depending on where the values are going to be specified. The following table relates configuration sources to the relevant ConfigContext implementation:

Class Source
StandardConfigContext Plain Java code using setters
EnvVarConfigContext Environment variables
SystemSettingsConfigContext System properties (-D command-line flags or .properties files)
MapConfigContext java.util.Map where keys are property names
DefaultsConfigContext Default configuration

We'll see examples of each of these below, focusing specifically on StandardConfigContext, SystemSettingsConfigContext, and EnvVarConfigContext in addition to briefly discussing the behavior of DefaultsConfigContext and MapConfigContext.

Minimal Configuration

At the bare minimum, a username and key ID must be set. Additionally, a private key should be present at ~/.ssh/id_rsa and it's corresponding public key has been uploaded to the Triton Portal. Specifying the path to your private key is recommended to avoid confusion, even if it resides at the default path.

Note that the key ID can be specified in both SHA256 and MD5 formats, with or without the leading prefix. The key ID (or fingerprint) can be found using ssh-keygen -l -f ./path/to/manta/key and is prefixed with either SHA256: or MD5: in the ssh-keygen output. In the following example we'll use the MD5 fingerprint provided in the portal.

ConfigContext config = new StandardConfigContext();
config.setMantaUser("you");
config.setMantaKeyId("5b:7e:fd:27:2e:8c:4c:3a:0e:6e:07:24:f8:62:8c:b9");

/*
  The following two parameters set the same values DefaultsConfigContext would provide
  and are needed when only a StandardConfigContext is being used.
*/

config.setMantaURL("https://us-east.manta.joyent.com:443")
config.setMantaKeyPath(System.getProperty("user.home") + "/.ssh/id_rsa")

This is all the configuration most users will need to access Triton Public Cloud Object Storage. Unfortunately, certain components within the client depend on configuration of the HTTP layer, so usage of a DefaultsConfigContext is still encouraged.

Chaining configuration

Most users are not interested in tweaking every single setting available and should rely on a DefaultsConfigContext to some degree. On the other hand, the DefaultsConfigContext is not mutable, so how do we include our own credentials? The answer is ChainedConfigContext, which accepts a variable number of other ConfigContext objects and copies non-null values into itself. Let's assume the username, key

Note that DefaultsConfigContext is specifically detected when combining configuration parameters and will not overwrite any configuration parameters that are already set.

StandardConfigContext custom = new StandardConfigContext()
  .setMantaUser("you")
  .setMantaKeyId("5b:7e:fd:27:2e:8c:4c:3a:0e:6e:07:24:f8:62:8c:b9");

ConfigContext config = new ChainedConfigContext(new DefaultsConfigContext(), custom);

/*
  Changing the argument order would not change the resulting configuration since
  DefaultsConfigContext does not override other configuration, so the following
  invocation is equivalent:

    new ChainedConfigContext(custom, new DefaultsConfigContext());
*/

Multiple configuration sources

Since ChainedConfigContext can accept an arbitrary number of ConfigContexts it's possible to mix any number of configuration settings. The following configuration setup mimics a user's expectation that values can be specified on the command line when invoking their program with java -Dmanta.user=you ..., followed by environment variables, and finally using default values for anything not specified by the aforementioned sources:

ConfigContext config = new ChainedConfigContext(
  new DefaultsConfigContext(),
  new EnvVarConfigContext(),
  new SystemSettingsConfigContext());

The above snippet is recommended for users that want to get started quickly and don't want to compile credentials or the location of credentials into their code.

Appendix

Source: