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

keywhiz.api.automation.v2.SecretDetailResponseV2 Maven / Gradle / Ivy

There is a newer version: 0.10.1
Show newest version
package keywhiz.api.automation.v2;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.UnsignedLong;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;

import keywhiz.api.model.Secret;
import keywhiz.api.model.SecretSeries;

import static com.google.common.base.Strings.nullToEmpty;
import static keywhiz.api.model.Secret.decodedLength;

@AutoValue public abstract class SecretDetailResponseV2 {
  SecretDetailResponseV2() {} // prevent sub-classing

  public static Builder builder() {
    return new AutoValue_SecretDetailResponseV2.Builder()
        .content("")
        .versions(null)
        .description("")
        .type(null)
        .metadata(ImmutableMap.of());
  }

  @AutoValue.Builder public abstract static class Builder {
    // intended to be package-private
    abstract String content();
    abstract Builder size(UnsignedLong size);
    abstract Builder versions(@Nullable ImmutableList versions);
    public abstract Builder metadata(ImmutableMap metadata);
    abstract SecretDetailResponseV2 autoBuild();

    public abstract Builder name(String name);
    public abstract Builder content(String secret);
    public abstract Builder description(String description);
    public abstract Builder createdAtSeconds(long createdAt);
    public abstract Builder createdBy(String person);
    public abstract Builder type(@Nullable String type);

    public Builder versions(@Nullable Iterable versions) {
      return versions(versions == null ? null : ImmutableList.copyOf(versions));
    }

    public Builder metadata(Map metadata) {
      return metadata(ImmutableMap.copyOf(metadata));
    }

    public Builder series(SecretSeries series) {
      return this
          .name(series.name())
          .description(series.description())
          .createdAtSeconds(series.createdAt().toEpochSecond())
          .createdBy(series.createdBy())
          .type(series.type().orElse(null));
    }

    public Builder secret(Secret secret) {
      return this
          .name(secret.getName())
          .versions(ImmutableList.of(secret.getVersion()))
          .description(secret.getDescription())
          .content(secret.getSecret())
          .createdAtSeconds(secret.getCreatedAt().toEpochSecond())
          .createdBy(secret.getCreatedBy())
          .type(secret.getType().orElse(null))
          .metadata(secret.getMetadata());
    }

    public SecretDetailResponseV2 build() {
      // throws IllegalArgumentException if content not base64
      Base64.getDecoder().decode(content());
      return this
          .size(UnsignedLong.valueOf(decodedLength(content())))
          .autoBuild();
    }
  }

  /**
   * Static factory method used by Jackson for deserialization
   */
  @SuppressWarnings("unused")
  @JsonCreator public static SecretDetailResponseV2 fromParts(
      @JsonProperty("name") String name,
      @JsonProperty("version") @Nullable List versions,
      @JsonProperty("description") @Nullable String description,
      @JsonProperty("content") String content,
      @JsonProperty("size") UnsignedLong size,
      @JsonProperty("createdAtSeconds") long createdAtSeconds,
      @JsonProperty("createdBy") String createdBy,
      @JsonProperty("type") @Nullable String type,
      @JsonProperty("metadata") @Nullable Map metadata) {
    return builder()
        .name(name)
        .versions(versions == null ? null : ImmutableList.copyOf(versions))
        .description(nullToEmpty(description))
        .content(content)
        .size(size)
        .createdAtSeconds(createdAtSeconds)
        .createdBy(createdBy)
        .type(type)
        .metadata(metadata == null ? ImmutableMap.of() : ImmutableMap.copyOf(metadata))
        .build();
  }

  // TODO: Consider Optional values in place of Nullable.
  @JsonProperty("name") public abstract String name();
  @JsonProperty("version") @Nullable public abstract ImmutableList versions();
  @JsonProperty("description") public abstract String description();
  @JsonProperty("content") public abstract String content();
  @JsonProperty("size") public abstract UnsignedLong size();
  @JsonProperty("createdAtSeconds") public abstract long createdAtSeconds();
  @JsonProperty("createdBy") public abstract String createdBy();
  @JsonProperty("type") @Nullable public abstract String type();
  @JsonProperty("metadata") public abstract ImmutableMap metadata();

  @Override public final String toString() {
    return MoreObjects.toStringHelper(this)
        .add("name", name())
        .add("versions", versions())
        .add("description", description())
        .add("content", "[REDACTED]")
        .add("size", size())
        .add("createdAtSeconds", createdAtSeconds())
        .add("createdBy", createdBy())
        .add("type", type())
        .add("metadata", metadata())
        .omitNullValues()
        .toString();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy