|
| 1 | +/* |
| 2 | + * Copyright (C) 2019 OnGres, Inc. |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +package io.stackgres.common.crd.storages; |
| 7 | + |
| 8 | +import java.util.Objects; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.annotation.JsonIgnore; |
| 11 | +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; |
| 12 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 13 | +import io.quarkus.runtime.annotations.RegisterForReflection; |
| 14 | +import io.stackgres.common.StackGresUtil; |
| 15 | +import io.stackgres.common.validation.FieldReference; |
| 16 | +import io.stackgres.common.validation.FieldReference.ReferencedField; |
| 17 | +import io.stackgres.common.validation.ValidEnum; |
| 18 | +import io.sundr.builder.annotations.Buildable; |
| 19 | +import jakarta.validation.Valid; |
| 20 | +import jakarta.validation.constraints.AssertTrue; |
| 21 | + |
| 22 | +@RegisterForReflection |
| 23 | +@JsonInclude(JsonInclude.Include.NON_DEFAULT) |
| 24 | +@JsonIgnoreProperties(ignoreUnknown = true) |
| 25 | +@Buildable(editableEnabled = false, validationEnabled = false, generateBuilderPackage = false, |
| 26 | + lazyCollectionInitEnabled = false, lazyMapInitEnabled = false, |
| 27 | + builderPackage = "io.fabric8.kubernetes.api.builder") |
| 28 | +public class StorageEncryption { |
| 29 | + |
| 30 | + @ValidEnum(enumClass = StorageEncryptionMethod.class, allowNulls = true, |
| 31 | + message = "mode can be sodium or openpgp") |
| 32 | + private String method; |
| 33 | + |
| 34 | + @Valid |
| 35 | + private SodiumStorageEncryption sodium; |
| 36 | + |
| 37 | + @Valid |
| 38 | + private OpenPgpStorageEncryption openpgp; |
| 39 | + |
| 40 | + @ReferencedField("sodium") |
| 41 | + interface Sodium extends FieldReference { } |
| 42 | + |
| 43 | + @ReferencedField("openpgp") |
| 44 | + interface Openpgp extends FieldReference { } |
| 45 | + |
| 46 | + @JsonIgnore |
| 47 | + @AssertTrue(message = "sodium section is not specified.", |
| 48 | + payload = { Sodium.class }) |
| 49 | + public boolean isSodiumRequired() { |
| 50 | + return method == null || !method.equals(StorageEncryptionMethod.SODIUM.toString()) || sodium != null; |
| 51 | + } |
| 52 | + |
| 53 | + @JsonIgnore |
| 54 | + @AssertTrue(message = "openpgp section is not specified.", |
| 55 | + payload = { Openpgp.class }) |
| 56 | + public boolean isOpenpgpRequired() { |
| 57 | + return method == null || !method.equals(StorageEncryptionMethod.OPENPGP.toString()) || openpgp != null; |
| 58 | + } |
| 59 | + |
| 60 | + @JsonIgnore |
| 61 | + @AssertTrue(message = "sodium and openpgp sections are mutually exclusive.", |
| 62 | + payload = { Openpgp.class, Sodium.class }) |
| 63 | + public boolean isEncryptionMethodSectionsMutuallyExclusive() { |
| 64 | + return method == null |
| 65 | + || (sodium == null && openpgp == null) |
| 66 | + || (sodium == null && openpgp != null) |
| 67 | + || (sodium != null && openpgp == null); |
| 68 | + } |
| 69 | + |
| 70 | + public String getMethod() { |
| 71 | + return method; |
| 72 | + } |
| 73 | + |
| 74 | + public void setMethod(String method) { |
| 75 | + this.method = method; |
| 76 | + } |
| 77 | + |
| 78 | + public SodiumStorageEncryption getSodium() { |
| 79 | + return sodium; |
| 80 | + } |
| 81 | + |
| 82 | + public void setSodium(SodiumStorageEncryption sodium) { |
| 83 | + this.sodium = sodium; |
| 84 | + } |
| 85 | + |
| 86 | + public OpenPgpStorageEncryption getOpenpgp() { |
| 87 | + return openpgp; |
| 88 | + } |
| 89 | + |
| 90 | + public void setOpenpgp(OpenPgpStorageEncryption openpgp) { |
| 91 | + this.openpgp = openpgp; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public int hashCode() { |
| 96 | + return Objects.hash(method, openpgp, sodium); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean equals(Object obj) { |
| 101 | + if (this == obj) { |
| 102 | + return true; |
| 103 | + } |
| 104 | + if (!(obj instanceof StorageEncryption)) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + StorageEncryption other = (StorageEncryption) obj; |
| 108 | + return Objects.equals(method, other.method) && Objects.equals(openpgp, other.openpgp) |
| 109 | + && Objects.equals(sodium, other.sodium); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String toString() { |
| 114 | + return StackGresUtil.toPrettyYaml(this); |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments