001package io.ebean.config;
002
003/**
004 * Define the encryption options for a bean property.
005 * <p>
006 * You can define the encryption options for a Bean property via the Encrypt
007 * annotation and programmatically via {@link EncryptDeployManager}.
008 * </p>
009 *
010 * @see EncryptDeployManager#getEncryptDeploy(TableName, String)
011 */
012public class EncryptDeploy {
013
014  /**
015   * Use to define that no encryption should be used.
016   */
017  public static final EncryptDeploy NO_ENCRYPT = new EncryptDeploy(Mode.MODE_NO_ENCRYPT, true, 0);
018
019  /**
020   * Use to define that the Encrypt annotation should be used to control
021   * encryption.
022   */
023  public static final EncryptDeploy ANNOTATION = new EncryptDeploy(Mode.MODE_ANNOTATION, true, 0);
024
025  /**
026   * Use to define that Encryption should be used and String types should use DB
027   * encryption.
028   */
029  public static final EncryptDeploy ENCRYPT_DB = new EncryptDeploy(Mode.MODE_ENCRYPT, true, 0);
030
031  /**
032   * Use to define that Java client Encryption should be used (rather than DB
033   * encryption).
034   */
035  public static final EncryptDeploy ENCRYPT_CLIENT = new EncryptDeploy(Mode.MODE_ENCRYPT, false, 0);
036
037  /**
038   * The Encryption mode.
039   */
040  public enum Mode {
041    /**
042     * Encrypt the property using DB encryption or Java client encryption
043     * depending on the type and dbEncryption flag.
044     */
045    MODE_ENCRYPT,
046
047    /**
048     * No encryption is used, even if there is an Encryption annotation on the
049     * property.
050     */
051    MODE_NO_ENCRYPT,
052
053    /**
054     * Use encryption options defined by the Encryption annotation on the
055     * property. If no annotation is on the property it is not encrypted.
056     */
057    MODE_ANNOTATION
058  }
059
060  private final Mode mode;
061
062  private final boolean dbEncrypt;
063
064  private final int dbLength;
065
066  /**
067   * Construct with all options for Encryption including the dbLength.
068   *
069   * @param mode      the Encryption mode
070   * @param dbEncrypt set to false if you want to use Java client side encryption rather
071   *                  than DB encryption.
072   * @param dbLength  set the DB length to use.
073   */
074  public EncryptDeploy(Mode mode, boolean dbEncrypt, int dbLength) {
075    this.mode = mode;
076    this.dbEncrypt = dbEncrypt;
077    this.dbLength = dbLength;
078  }
079
080  /**
081   * Return the encryption mode.
082   */
083  public Mode getMode() {
084    return mode;
085  }
086
087  /**
088   * Return true if String type should use DB encryption.
089   * <p>
090   * Return false if String type should use java client encryption instead.
091   * </p>
092   */
093  public boolean isDbEncrypt() {
094    return dbEncrypt;
095  }
096
097  /**
098   * Return a hint to specify the DB length.
099   * <p>
100   * Returning 0 means just use the normal DB length determination.
101   * </p>
102   */
103  public int getDbLength() {
104    return dbLength;
105  }
106}