/docker-secrets

A small library for loading Docker Secrets

Primary LanguageJavaOtherNOASSERTION

docker-secrets

A small library for loading Docker Secrets

Build Status Apache 2.0

Importing and Using

Importing

Currently the only way to import this library is via jitpack. This allows you grab and import the project as an artifact from Github.

You'll need to add jitpack to your repository list in gradle.

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

and you'll need to add the dependency:

dependencies {
    compile 'com.github.Hazz223:docker-secrets:X.X.X'
}

Using the Project

Once the project has been imported, call the DockerSecretsLoaderBuilder.builder(), which will return an instance of the builder. For the defaults, use this line: DockerSecretsLoaderBuilder.builder().build(). This will expect secrets to be kept in the default location - /run/secrets/

The Loader can also be customised:

  • Calling .withSecretFolder("my customer secret folder"") will allow a defined secrets location
  • Calling withFileLoader(mySpecialFileLoader) will enable the functionality to pass a custom implementation of DockerSecretsFileLoader, if the default one does not suffice.

After any/all of the above, .build() needs to be called to return an instance of DockerSecretsLoader.

Methods

Two methods are available:

  • loadAsMap()
  • loadAsProperties() Where the key is the secret file name, and the value is the secret files content.

If the secrets folder directory is empty, a DockerSecretsException is thrown. If any of the secret files can't be read, a DockerSecretsException is thrown.

Use with Spring Boot

I've had a few requests on how to use the project with Spring Boot. Here's a current working example:

@Configuration
public class SecretsConfiguration {

    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @PostConstruct // run this block as soon as the app starts
    public void loadSecrets(){

        try{
            Map<String, String> dockerSecrets = DockerSecretsLoaderBuilder.build().loadAsMap(); // load all the secrets
            dockerSecrets.forEach(System::setProperty); // put them all into the system properties
        } catch (DockerSecretsException ex){
            log.warn("Failed to load secrets", ex); // log failure. Though you can also fail the project start instead.
        }
    }
}

Requirements

There are no external requirements for the project, however it is currently Java 8 only.

Contributing

If you'd like to add more to this project, please fork the project and submit a pull request. You can also contact me on twitter.

Licence

This is under the Apache 2.0 license. More info can be found here

Deploy

mvn clean package deploy clean