|
| 1 | +package com.bettercloud.vault.api.mounts; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.stream.Collectors; |
| 7 | + |
| 8 | +import com.bettercloud.vault.json.Json; |
| 9 | +import com.bettercloud.vault.json.JsonObject; |
| 10 | + |
| 11 | +import lombok.Getter; |
| 12 | + |
| 13 | +/** |
| 14 | + * <p>A container for options sent to mounts endpoints on the Secret Engine backend as REST payload. This class is |
| 15 | + * meant for use with a builder pattern style. Example usage:</p> |
| 16 | + * |
| 17 | + * <blockquote> |
| 18 | + * <pre>{@code |
| 19 | + * final MountPayload payload = new MountPayload() |
| 20 | + * .defaultLeaseTtl(TimeToLive.of(30, TimeUnit.MINUTES)) |
| 21 | + * .maxLeaseTtl(TimeToLive.of(30, TimeUnit.MINUTES)) |
| 22 | + * .description("description of pki"); |
| 23 | + * }</pre> |
| 24 | + * </blockquote> |
| 25 | + */ |
| 26 | +public class MountPayload implements Serializable { |
| 27 | + private static final long serialVersionUID = 839595627039704093L; |
| 28 | + |
| 29 | + @Getter private TimeToLive defaultLeaseTtl; |
| 30 | + @Getter private TimeToLive maxLeaseTtl; |
| 31 | + @Getter private String description; |
| 32 | + @Getter private Boolean forceNoCache; |
| 33 | + @Getter private String pluginName; |
| 34 | + @Getter private Boolean local; |
| 35 | + @Getter private Boolean sealWrap; |
| 36 | + private List<String> auditNonHmacRequestKeys; |
| 37 | + private List<String> auditNonHmacResponseKeys; |
| 38 | + |
| 39 | + public MountPayload defaultLeaseTtl(final TimeToLive defaultLeaseTtl) { |
| 40 | + this.defaultLeaseTtl = defaultLeaseTtl; |
| 41 | + return this; |
| 42 | + } |
| 43 | + |
| 44 | + public MountPayload maxLeaseTtl(final TimeToLive maxLeaseTtl) { |
| 45 | + this.maxLeaseTtl = maxLeaseTtl; |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + public MountPayload description(final String description) { |
| 50 | + this.description = description; |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + public MountPayload forceNoCache(final Boolean forceNoCache) { |
| 55 | + this.forceNoCache = forceNoCache; |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + public MountPayload pluginName(final String pluginName) { |
| 60 | + this.pluginName = pluginName; |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + public MountPayload local(final Boolean local) { |
| 65 | + this.local = local; |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + public MountPayload sealWrap(final Boolean sealWrap) { |
| 70 | + this.sealWrap = sealWrap; |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + public MountPayload auditNonHmacRequestKeys(final List<String> auditNonHmacRequestKeys) { |
| 75 | + if (auditNonHmacRequestKeys != null) { |
| 76 | + this.auditNonHmacRequestKeys = new ArrayList<>(); |
| 77 | + this.auditNonHmacRequestKeys.addAll(auditNonHmacRequestKeys); |
| 78 | + } |
| 79 | + return this; |
| 80 | + } |
| 81 | + |
| 82 | + public MountPayload auditNonHmacResponseKeys(final List<String> auditNonHmacResponseKeys) { |
| 83 | + if (auditNonHmacResponseKeys != null) { |
| 84 | + this.auditNonHmacResponseKeys = new ArrayList<>(); |
| 85 | + this.auditNonHmacResponseKeys.addAll(auditNonHmacResponseKeys); |
| 86 | + } |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + public List<String> getAuditNonHmacRequestKeys() { |
| 91 | + if (auditNonHmacRequestKeys == null) { |
| 92 | + return null; |
| 93 | + } else { |
| 94 | + final List<String> clone = new ArrayList<>(); |
| 95 | + clone.addAll(auditNonHmacRequestKeys); |
| 96 | + return clone; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public List<String> getAuditNonHmacResponseKeys() { |
| 101 | + if (auditNonHmacResponseKeys == null) { |
| 102 | + return null; |
| 103 | + } else { |
| 104 | + final List<String> clone = new ArrayList<>(); |
| 105 | + clone.addAll(auditNonHmacResponseKeys); |
| 106 | + return clone; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public JsonObject toEnableJson(MountType type) { |
| 111 | + final JsonObject jsonObject = Json.object(); |
| 112 | + |
| 113 | + jsonObject.addIfNotNull("type", type.value()); |
| 114 | + jsonObject.addIfNotNull("description", this.description); |
| 115 | + jsonObject.addIfNotNull("config", this.toConfigJson()); |
| 116 | + jsonObject.addIfNotNull("plugin_name", this.pluginName); |
| 117 | + jsonObject.addIfNotNull("local", this.local); |
| 118 | + jsonObject.addIfNotNull("seal_wrap", this.sealWrap); |
| 119 | + |
| 120 | + return jsonObject; |
| 121 | + } |
| 122 | + |
| 123 | + public JsonObject toTuneJson() { |
| 124 | + final JsonObject jsonObject = Json.object(); |
| 125 | + |
| 126 | + if (this.defaultLeaseTtl != null) { |
| 127 | + jsonObject.addIfNotNull("default_lease_ttl", this.defaultLeaseTtl.toString()); |
| 128 | + } |
| 129 | + |
| 130 | + if (this.maxLeaseTtl != null) { |
| 131 | + jsonObject.addIfNotNull("max_lease_ttl", this.maxLeaseTtl.toString()); |
| 132 | + } |
| 133 | + |
| 134 | + jsonObject.addIfNotNull("description", this.description); |
| 135 | + |
| 136 | + if (this.auditNonHmacRequestKeys != null && this.auditNonHmacRequestKeys.size() > 0) { |
| 137 | + jsonObject.addIfNotNull("audit_non_hmac_request_keys", this.auditNonHmacRequestKeys.stream().collect(Collectors.joining(","))); |
| 138 | + } |
| 139 | + |
| 140 | + if (this.auditNonHmacResponseKeys != null && this.auditNonHmacResponseKeys.size() > 0) { |
| 141 | + jsonObject.addIfNotNull("audit_non_hmac_response_keys", this.auditNonHmacResponseKeys.stream().collect(Collectors.joining(","))); |
| 142 | + } |
| 143 | + |
| 144 | + return jsonObject; |
| 145 | + } |
| 146 | + |
| 147 | + private JsonObject toConfigJson() { |
| 148 | + final JsonObject jsonObject = toTuneJson(); |
| 149 | + |
| 150 | + jsonObject.addIfNotNull("force_no_cache", this.forceNoCache); |
| 151 | + jsonObject.addIfNotNull("plugin_name", this.pluginName); |
| 152 | + |
| 153 | + if (jsonObject.isEmpty()) { |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + return jsonObject; |
| 158 | + } |
| 159 | +} |
0 commit comments