All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.io7m.certusine.etcd.CSEtcdOutputProvider Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2022 Mark Raynsford  https://www.io7m.com
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */


package com.io7m.certusine.etcd;

import com.io7m.certusine.api.CSAbstractNamedProvider;
import com.io7m.certusine.api.CSCertificateOutputProviderType;
import com.io7m.certusine.api.CSCertificateOutputType;
import com.io7m.certusine.api.CSConfigurationException;
import com.io7m.certusine.api.CSConfigurationParameterDescription;
import com.io7m.certusine.api.CSConfigurationParameters;
import com.io7m.certusine.etcd.internal.CSEtcdCredentials;
import com.io7m.certusine.etcd.internal.CSEtcdOutput;
import com.io7m.certusine.etcd.internal.CSEtcdStrings;

import java.io.IOException;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static java.util.Map.entry;

/**
 * An etcd certificate output.
 */

public final class CSEtcdOutputProvider
  extends CSAbstractNamedProvider
  implements CSCertificateOutputProviderType
{
  private static final String ENDPOINT_PARAMETER = "endpoint";
  private static final String USERNAME_PARAMETER = "username";
  private static final String PASSWORD_PARAMETER = "password";

  private final CSEtcdStrings strings;

  /**
   * Access to etcd.
   *
   * @param locale    A locale for error messages
   * @param inStrings String resources
   *
   * @throws IOException On I/O errors
   */

  public CSEtcdOutputProvider(
    final Locale locale,
    final CSEtcdStrings inStrings)
    throws IOException
  {
    super(locale);
    this.strings = Objects.requireNonNull(inStrings, "strings");
  }

  /**
   * Access to etcd.
   *
   * @param locale A locale for error messages
   *
   * @throws IOException On I/O errors
   */

  public CSEtcdOutputProvider(
    final Locale locale)
    throws IOException
  {
    this(locale, new CSEtcdStrings(locale));
  }

  /**
   * Access to etcd.
   *
   * @throws IOException On I/O errors
   */

  public CSEtcdOutputProvider()
    throws IOException
  {
    this(Locale.getDefault());
  }

  @Override
  public CSCertificateOutputType create(
    final String name,
    final CSConfigurationParameters parameters)
    throws CSConfigurationException
  {
    Objects.requireNonNull(name, "name");
    Objects.requireNonNull(parameters, "parameters");

    this.checkParameters(parameters);

    final var parameterMap = parameters.parameters();
    Optional credentials = Optional.empty();
    if (parameterMap.containsKey(USERNAME_PARAMETER)) {
      if (parameterMap.containsKey(PASSWORD_PARAMETER)) {
        credentials = Optional.of(
          new CSEtcdCredentials(
            parameterMap.get(USERNAME_PARAMETER),
            parameterMap.get(PASSWORD_PARAMETER)
          )
        );
      }
    }

    return new CSEtcdOutput(
      this.strings,
      name,
      credentials,
      parameterMap.get(ENDPOINT_PARAMETER)
    );
  }

  @Override
  public String name()
  {
    return "etcd";
  }

  @Override
  public String description()
  {
    return "Write certificates to an etcd server.";
  }

  @Override
  public Map parameters()
  {
    return Map.ofEntries(
      entry(
        ENDPOINT_PARAMETER,
        new CSConfigurationParameterDescription(
          ENDPOINT_PARAMETER,
          this.strings.format("parameterEndpoint"),
          "URI",
          true
        )
      ),
      entry(
        USERNAME_PARAMETER,
        new CSConfigurationParameterDescription(
          USERNAME_PARAMETER,
          this.strings.format("parameterUserName"),
          "User name",
          false
        )
      ),
      entry(
        PASSWORD_PARAMETER,
        new CSConfigurationParameterDescription(
          PASSWORD_PARAMETER,
          this.strings.format("parameterPassword"),
          "Password",
          false
        )
      )
    );
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy