-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIonicParameters.java
More file actions
102 lines (88 loc) · 3.46 KB
/
IonicParameters.java
File metadata and controls
102 lines (88 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.ionic.sdk.addon.jdbc.impl;
import com.ionic.sdk.agent.Agent;
import com.ionic.sdk.agent.cipher.chunk.ChunkCipherV2;
import com.ionic.sdk.agent.cipher.chunk.data.ChunkCryptoEncryptAttributes;
import com.ionic.sdk.error.IonicException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Map;
/**
* Cache of data associated with {@link PreparedStatement}, and Ionic protection business logic.
*/
public class IonicParameters {
/**
* The JDBC parameters associated with the {@link java.sql.PreparedStatement}.
*/
private final Object[] parameters;
/**
* The Ionic config associated with the {@link java.sql.PreparedStatement}.
*/
private final Map<Integer, IonicColumnConfig> columnConfigs;
/**
* Ionic agent, used to protect data on database insert, and unprotect data on database fetch.
*/
private final Agent agent;
/**
* @return Ionic agent, used to protect data on insert into database, and unprotect data on fetch from database
*/
public Agent getAgent() {
return agent;
}
public IonicParameters(final int count, final Map<Integer, IonicColumnConfig> columnConfigs, final Agent agent) {
this.parameters = new Object[count];
this.columnConfigs = columnConfigs;
this.agent = agent;
}
/**
* @return the number of parameters specified in the SQL of the associated {@link PreparedStatement}
*/
public int getCount() {
return parameters.length;
}
/**
* Get the {@link PreparedStatement} parameter associated with the specified database index.
*
* @param dbIndex the ordinal of the parameter to be returned
* @return the {@link PreparedStatement} parameter
*/
public Object getParameter(final int dbIndex) {
return parameters[dbIndex - 1];
}
/**
* Set the {@link PreparedStatement} parameter associated with the specified database index.
*
* @param dbIndex the ordinal of the parameter to be set
* @param value the {@link PreparedStatement} parameter value to be set
*/
public void setParameter(final int dbIndex, final Object value) {
parameters[dbIndex - 1] = value;
}
public void clearParameters() {
for (int index = 0; (index < parameters.length); ++index) {
parameters[index] = null;
}
}
/**
* Before database commit of associated {@link PreparedStatement}, this function should be called to Ionic protect
* the values specified in the Ionic configuration.
*
* @throws SQLException on Ionic cryptography errors
*/
public void encrypt() throws SQLException {
final ChunkCipherV2 chunkCipher = new ChunkCipherV2(agent);
for (int index = 0; (index < parameters.length); ++index) {
final int dbIndex = index + 1;
final IonicColumnConfig columnConfig = columnConfigs.get(dbIndex);
final Object parameter = getParameter(dbIndex);
if ((columnConfig != null) && (parameter instanceof String)) {
try {
final ChunkCryptoEncryptAttributes encryptAttributes =
new ChunkCryptoEncryptAttributes(columnConfig.getCattrs());
setParameter(dbIndex, chunkCipher.encrypt((String) parameter, encryptAttributes));
} catch (IonicException e) {
throw new SQLException(e);
}
}
}
}
}