Skip to content

Commit 09b4b06

Browse files
author
prachi
committed
Bug 9447 - ModifySshKeyCommand contents are logged...
Since private and public keys are logged, this is a Security concern Changes: Added capability to 'Command' instances to support excluding certain fields from getting logged using GSON @expose annotation.
1 parent e69774e commit 09b4b06

4 files changed

Lines changed: 47 additions & 13 deletions

File tree

‎api/src/com/cloud/agent/api/Command.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,9 @@ public void setContextParam(String name, String value) {
5656
public String getContextParam(String name) {
5757
return contextMap.get(name);
5858
}
59+
60+
public boolean doesCommandUseExposeAnnotation(){
61+
return false;
62+
}
63+
5964
}

‎api/src/com/cloud/agent/api/ModifySshKeysCommand.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
package com.cloud.agent.api;
2020

21+
import com.google.gson.annotations.Expose;
22+
2123
public class ModifySshKeysCommand extends Command {
22-
private String _pubKey;
23-
private String _prvKey;
24+
@Expose(serialize = false, deserialize = false) private String _pubKey;
25+
@Expose(serialize = false, deserialize = false) private String _prvKey;
2426
public ModifySshKeysCommand() {
2527

2628
}
@@ -39,5 +41,11 @@ public boolean executeInSequence() {
3941
// TODO Auto-generated method stub
4042
return false;
4143
}
44+
45+
@Override
46+
public boolean doesCommandUseExposeAnnotation(){
47+
return true;
48+
}
49+
4250

4351
}

‎core/src/com/cloud/agent/transport/ArrayTypeAdaptor.java‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@
4040
public class ArrayTypeAdaptor<T> implements JsonDeserializer<T[]>, JsonSerializer<T[]> {
4141

4242
static final GsonBuilder s_gBuilder;
43+
static final GsonBuilder s_exposeAnnotationBuilder;
4344
static {
4445
s_gBuilder = Request.initBuilder();
45-
// final Type listType = new TypeToken<List<VolumeVO>>() {}.getType();
46-
// s_gBuilder.registerTypeAdapter(listType, new VolListTypeAdaptor());
46+
s_exposeAnnotationBuilder = Request.initBuilderWithExposeAnnotation();
4747
}
4848

4949

@@ -56,6 +56,12 @@ public JsonElement serialize(T[] src, Type typeOfSrc, JsonSerializationContext c
5656
Gson gson = s_gBuilder.create();
5757
JsonArray array = new JsonArray();
5858
for (T cmd : src) {
59+
if(typeOfSrc.getClass().isInstance(Command.class)){
60+
Command innercmd = (Command)cmd;
61+
if(innercmd.doesCommandUseExposeAnnotation()){
62+
gson = s_exposeAnnotationBuilder.create();
63+
}
64+
}
5965
JsonObject obj = new JsonObject();
6066
obj.add(cmd.getClass().getName().substring(s_pkg.length()), gson.toJsonTree(cmd));
6167
array.add(obj);

‎core/src/com/cloud/agent/transport/Request.java‎

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import com.cloud.utils.NumbersUtil;
3333
import com.cloud.utils.Pair;
3434
import com.cloud.utils.exception.CloudRuntimeException;
35+
import com.google.gson.ExclusionStrategy;
36+
import com.google.gson.FieldAttributes;
3537
import com.google.gson.Gson;
3638
import com.google.gson.GsonBuilder;
3739
import com.google.gson.JsonArray;
@@ -88,20 +90,33 @@ public static Version get(final byte ver) throws UnsupportedVersionException {
8890
protected static final short FLAG_CONTROL = 0x40;
8991

9092
protected static final GsonBuilder s_gBuilder;
93+
protected static final GsonBuilder s_exposeAnnotationBuilder;
94+
9195
static {
9296
s_gBuilder = new GsonBuilder();
93-
s_gBuilder.registerTypeAdapter(Command[].class, new ArrayTypeAdaptor<Command>());
94-
s_gBuilder.registerTypeAdapter(Answer[].class, new ArrayTypeAdaptor<Answer>());
95-
// final Type listType = new TypeToken<List<VolumeVO>>() {}.getType();
96-
// s_gBuilder.registerTypeAdapter(listType, new VolListTypeAdaptor());
97-
s_gBuilder.registerTypeAdapter(new TypeToken<List<PortConfig>>() {}.getType(), new PortConfigListTypeAdaptor());
98-
s_gBuilder.registerTypeAdapter(new TypeToken<Pair<Long, Long>>() {}.getType(), new NwGroupsCommandTypeAdaptor());
99-
s_logger.info("Builder inited.");
97+
setDefaultGsonConfig(s_gBuilder);
98+
s_logger.info("Default Builder inited.");
99+
100+
s_exposeAnnotationBuilder = new GsonBuilder();
101+
setDefaultGsonConfig(s_exposeAnnotationBuilder);
102+
s_exposeAnnotationBuilder.excludeFieldsWithoutExposeAnnotation();
103+
s_logger.info("Expose annotation Builder inited.");
104+
}
105+
106+
public static void setDefaultGsonConfig(GsonBuilder builder){
107+
builder.registerTypeAdapter(Command[].class, new ArrayTypeAdaptor<Command>());
108+
builder.registerTypeAdapter(Answer[].class, new ArrayTypeAdaptor<Answer>());
109+
builder.registerTypeAdapter(new TypeToken<List<PortConfig>>() {}.getType(), new PortConfigListTypeAdaptor());
110+
builder.registerTypeAdapter(new TypeToken<Pair<Long, Long>>() {}.getType(), new NwGroupsCommandTypeAdaptor());
100111
}
101112

102113
public static GsonBuilder initBuilder() {
103114
return s_gBuilder;
104115
}
116+
117+
public static GsonBuilder initBuilderWithExposeAnnotation() {
118+
return s_exposeAnnotationBuilder;
119+
}
105120

106121
protected Version _ver;
107122
protected long _seq;
@@ -240,7 +255,7 @@ public Command[] getCommands() {
240255
public String toString() {
241256
String content = _content;
242257
if (content == null) {
243-
final Gson gson = s_gBuilder.create();
258+
final Gson gson = s_gBuilder.create();
244259
try {
245260
content = gson.toJson(_cmds);
246261
} catch(Throwable e) {
@@ -330,7 +345,7 @@ public void log(long agentId, String msg) {
330345
s_logger.trace(buf.toString());
331346
}
332347
}
333-
348+
334349
/**
335350
* Factory method for Request and Response. It expects the bytes to be
336351
* correctly formed so it's possible that it throws underflow exceptions

0 commit comments

Comments
 (0)