forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSensitive.java
More file actions
252 lines (227 loc) · 9.63 KB
/
Sensitive.java
File metadata and controls
252 lines (227 loc) · 9.63 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package act.data;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.app.App;
import act.asm.*;
import act.util.AppByteCodeEnhancer;
import act.util.SimpleBean;
import org.osgl.util.S;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Mark a **String** typed field is sensitive.
* DB plugin should sense any field with this annotation so that
* sensitive data be encrypted when stored into database and
* decrypted after retrieving from database.
*
* If the framework found fields marked with `@Sensitive` it
* will generate getter and setter for the field, if there are
* already getter and setter defined, the method will be
* overwritten.
*
* **Note** if a non String typed field marked with
* `@Sensitive` nothing will be done for that field,
* however framework will log a warn message.
*
* The logic of a sensitive getter/setter:
*
* ```java
* {@literal @}Sensitive
* private String sensitiveData;
* // generated or overwritten getter
* public String getSensitiveData(String data) {
* return Act.crypto().decrypt(data);
* }
* public void setSenstiveData(String data) {
* this.data = Act.crypto().encrypt(data);
* }
* ```
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Sensitive {
String ASM_DESC = Type.getType(Sensitive.class).getDescriptor();
class Enhancer extends AppByteCodeEnhancer<Enhancer> {
private static final String DESC_SENSITIVE = Sensitive.ASM_DESC;
private static final String DESC_STRING = Type.getType(String.class).getDescriptor();
private static final String GETTER_DESC = S.concat("()", DESC_STRING);
private static final String SETTER_DESC = S.concat("(", DESC_STRING, ")V");
private Set<String> sensitiveFieldsForGetter = new HashSet<>();
private Set<String> sensitiveFieldsForSetter = new HashSet<>();
private SimpleBean.MetaInfoManager metaInfoManager;
private SimpleBean.MetaInfo metaInfo;
private String classInternalName;
private String className;
public Enhancer() {
}
public Enhancer(ClassVisitor cv) {
super(S.F.startsWith("act.").negate(), cv);
}
@Override
public AppByteCodeEnhancer app(App app) {
metaInfoManager = app.classLoader().simpleBeanInfoManager();
return super.app(app);
}
@Override
protected Class<Enhancer> subClass() {
return Enhancer.class;
}
@Override
protected void reset() {
sensitiveFieldsForSetter.clear();
sensitiveFieldsForGetter.clear();
className = null;
classInternalName = null;
metaInfo = null;
super.reset();
}
@Override
public int priority() {
return SimpleBean.ByteCodeEnhancer.PRIORITY + 1;
}
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
classInternalName = name;
className = Type.getObjectType(name).getClassName();
metaInfo = metaInfoManager.get(className);
super.visit(version, access, name, signature, superName, interfaces);
}
@Override
public FieldVisitor visitField(int access, final String name, final String fieldDesc, String signature, Object value) {
FieldVisitor fv = super.visitField(access, name, fieldDesc, signature, value);
boolean isStatic = ((access & ACC_STATIC) != 0);
return isStatic || S.neq(DESC_STRING, fieldDesc) ? fv : new FieldVisitor(ASM5, fv) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
if (DESC_SENSITIVE.equals(desc)) {
sensitiveFieldsForGetter.add(name);
sensitiveFieldsForSetter.add(name);
if (null != metaInfo) {
metaInfo.addSensitiveField(name);
}
}
return super.visitAnnotation(desc, visible);
}
};
}
@Override
public MethodVisitor visitMethod(int access, final String methodName, final String methodDesc, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, methodName, methodDesc, signature, exceptions);
String fieldName = fieldNameFromGetterOrSetter(methodName);
final String mappedFieldName = null != metaInfo ? metaInfo.aliasOf(fieldName) : fieldName;
final boolean isSetter = null != fieldName && methodName.startsWith("s");
Set<String> backlog = isSetter ? sensitiveFieldsForSetter : sensitiveFieldsForGetter;
return null == fieldName || !(backlog.contains(mappedFieldName)) ? mv : new MethodVisitor(ASM5, mv) {
@Override
public void visitCode() {
boolean generating = generatingMethods.get();
if (!generating) {
if (isSetter) {
logger.warn(methodName + "(" + Type.getType(methodDesc).getArgumentTypes()[0].getClassName() + ") rewritten for @Sensitive field: " + mappedFieldName);
} else {
logger.warn(methodName + "() rewritten for @Sensitive field: " + mappedFieldName);
}
}
if (isSetter) {
visitSetterCode(mappedFieldName, this);
if (!generating) {
sensitiveFieldsForSetter.remove(mappedFieldName);
}
} else {
visitGetterCode(mappedFieldName, this);
if (!generating) {
sensitiveFieldsForGetter.remove(mappedFieldName);
}
}
}
};
}
private static final AtomicBoolean generatingMethods = new AtomicBoolean(false);
@Override
public void visitEnd() {
super.visitEnd();
for (final String field: sensitiveFieldsForGetter) {
generatingMethods.set(true);
try {
MethodVisitor mv = visitMethod(
ACC_PUBLIC,
getterName(field),
GETTER_DESC,
null,
null);
mv.visitCode();
} finally {
generatingMethods.set(false);
}
}
for (final String field : sensitiveFieldsForSetter) {
generatingMethods.set(true);
try {
MethodVisitor mv = visitMethod(
ACC_PUBLIC,
setterName(field),
SETTER_DESC,
null,
null);
mv.visitCode();
} finally {
generatingMethods.set(false);
}
}
}
private String fieldNameFromGetterOrSetter(String methodName) {
int len = methodName.length();
if (len > 3 && (methodName.startsWith("get") || methodName.startsWith("set"))) {
return S.lowerFirst(methodName.substring(3));
}
return null;
}
private void visitSetterCode(final String field, final MethodVisitor mv) {
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESTATIC, "act/Act", "crypto", "()Lact/crypto/AppCrypto;", false);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "act/crypto/AppCrypto", "encrypt", "(Ljava/lang/String;)Ljava/lang/String;", false);
mv.visitFieldInsn(PUTFIELD, classInternalName, field, DESC_STRING);
mv.visitInsn(RETURN);
mv.visitMaxs(3, 2);
mv.visitEnd();
}
private void visitGetterCode(final String field, final MethodVisitor mv) {
mv.visitMethodInsn(INVOKESTATIC, "act/Act", "crypto", "()Lact/crypto/AppCrypto;", false);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, classInternalName, field, "Ljava/lang/String;");
mv.visitMethodInsn(INVOKEVIRTUAL, "act/crypto/AppCrypto", "decrypt", "(Ljava/lang/String;)Ljava/lang/String;", false);
mv.visitInsn(ARETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
private static String getterName(String fieldName) {
return S.concat("get", S.capFirst(fieldName));
}
private static String setterName(String fieldName) {
return "set" + S.capFirst(fieldName);
}
}
}