Skip to content

Commit 7b9e4ca

Browse files
Removing secure string wrapper (#128698) (#128894)
1 parent 74ea779 commit 7b9e4ca

File tree

12 files changed

+43
-105
lines changed

12 files changed

+43
-105
lines changed

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/common/JsonUtils.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.xpack.inference.common;
99

1010
import org.elasticsearch.common.Strings;
11+
import org.elasticsearch.common.settings.SecureString;
1112
import org.elasticsearch.xcontent.XContentBuilder;
1213
import org.elasticsearch.xcontent.json.JsonXContent;
1314

@@ -23,7 +24,11 @@ public class JsonUtils {
2324
public static <T> String toJson(T value, String field) {
2425
try {
2526
XContentBuilder builder = JsonXContent.contentBuilder();
26-
builder.value(value);
27+
if (value instanceof SecureString secureString) {
28+
builder.value(secureString.toString());
29+
} else {
30+
builder.value(value);
31+
}
2732
return Strings.toString(builder);
2833
} catch (Exception e) {
2934
throw new IllegalStateException(

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/ServiceUtils.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.rest.RestStatus;
2323
import org.elasticsearch.xpack.core.ml.inference.assignment.AdaptiveAllocationsSettings;
2424
import org.elasticsearch.xpack.inference.services.settings.ApiKeySecrets;
25-
import org.elasticsearch.xpack.inference.services.settings.SerializableSecureString;
2625

2726
import java.net.URI;
2827
import java.net.URISyntaxException;
@@ -652,7 +651,7 @@ public static void validateMapValues(
652651
}
653652
}
654653

655-
public static Map<String, SerializableSecureString> convertMapStringsToSecureString(
654+
public static Map<String, SecureString> convertMapStringsToSecureString(
656655
Map<String, ?> map,
657656
String settingName,
658657
ValidationException validationException
@@ -661,11 +660,11 @@ public static Map<String, SerializableSecureString> convertMapStringsToSecureStr
661660
return Map.of();
662661
}
663662

664-
validateMapStringValues(map, settingName, validationException, true);
663+
var validatedMap = validateMapStringValues(map, settingName, validationException, true);
665664

666-
return map.entrySet()
665+
return validatedMap.entrySet()
667666
.stream()
668-
.collect(Collectors.toMap(Map.Entry::getKey, e -> new SerializableSecureString((String) e.getValue())));
667+
.collect(Collectors.toMap(Map.Entry::getKey, e -> new SecureString(e.getValue().toCharArray())));
669668
}
670669

671670
/**

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/custom/CustomSecretSettings.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
import org.elasticsearch.common.ValidationException;
1313
import org.elasticsearch.common.io.stream.StreamInput;
1414
import org.elasticsearch.common.io.stream.StreamOutput;
15+
import org.elasticsearch.common.settings.SecureString;
1516
import org.elasticsearch.core.Nullable;
1617
import org.elasticsearch.inference.SecretSettings;
1718
import org.elasticsearch.xcontent.XContentBuilder;
18-
import org.elasticsearch.xpack.inference.services.settings.SerializableSecureString;
1919

2020
import java.io.IOException;
2121
import java.util.HashMap;
@@ -48,22 +48,22 @@ public static CustomSecretSettings fromMap(@Nullable Map<String, Object> map) {
4848
return new CustomSecretSettings(secureStringMap);
4949
}
5050

51-
private final Map<String, SerializableSecureString> secretParameters;
51+
private final Map<String, SecureString> secretParameters;
5252

5353
@Override
5454
public SecretSettings newSecretSettings(Map<String, Object> newSecrets) {
5555
return fromMap(new HashMap<>(newSecrets));
5656
}
5757

58-
public CustomSecretSettings(@Nullable Map<String, SerializableSecureString> secretParameters) {
58+
public CustomSecretSettings(@Nullable Map<String, SecureString> secretParameters) {
5959
this.secretParameters = Objects.requireNonNullElse(secretParameters, Map.of());
6060
}
6161

6262
public CustomSecretSettings(StreamInput in) throws IOException {
63-
secretParameters = in.readImmutableMap(SerializableSecureString::new);
63+
secretParameters = in.readImmutableMap(StreamInput::readSecureString);
6464
}
6565

66-
public Map<String, SerializableSecureString> getSecretParameters() {
66+
public Map<String, SecureString> getSecretParameters() {
6767
return secretParameters;
6868
}
6969

@@ -74,7 +74,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
7474
builder.startObject(SECRET_PARAMETERS);
7575
{
7676
for (var entry : secretParameters.entrySet()) {
77-
builder.field(entry.getKey(), entry.getValue());
77+
builder.field(entry.getKey(), entry.getValue().toString());
7878
}
7979
}
8080
builder.endObject();
@@ -95,7 +95,7 @@ public TransportVersion getMinimalSupportedVersion() {
9595

9696
@Override
9797
public void writeTo(StreamOutput out) throws IOException {
98-
out.writeMap(secretParameters, (streamOutput, v) -> { v.writeTo(streamOutput); });
98+
out.writeMap(secretParameters, StreamOutput::writeSecureString);
9999
}
100100

101101
@Override

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/custom/request/CustomRequest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.elasticsearch.xpack.inference.external.request.Request;
2222
import org.elasticsearch.xpack.inference.services.custom.CustomModel;
2323
import org.elasticsearch.xpack.inference.services.custom.CustomServiceSettings;
24-
import org.elasticsearch.xpack.inference.services.settings.SerializableSecureString;
2524

2625
import java.net.URI;
2726
import java.net.URISyntaxException;
@@ -70,8 +69,6 @@ private static void addStringParams(Map<String, String> stringParams, Map<String
7069
for (var entry : paramsToAdd.entrySet()) {
7170
if (entry.getValue() instanceof String str) {
7271
stringParams.put(entry.getKey(), str);
73-
} else if (entry.getValue() instanceof SerializableSecureString serializableSecureString) {
74-
stringParams.put(entry.getKey(), serializableSecureString.getSecureString().toString());
7572
} else if (entry.getValue() instanceof SecureString secureString) {
7673
stringParams.put(entry.getKey(), secureString.toString());
7774
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/settings/SerializableSecureString.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/common/JsonUtilsTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.elasticsearch.xcontent.ToXContent;
1414
import org.elasticsearch.xcontent.ToXContentFragment;
1515
import org.elasticsearch.xcontent.XContentBuilder;
16-
import org.elasticsearch.xpack.inference.services.settings.SerializableSecureString;
1716

1817
import java.io.IOException;
1918
import java.util.List;
@@ -53,16 +52,16 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
5352
assertThat(toJson(1.1f, "field"), is("1.1"));
5453
assertThat(toJson(true, "field"), is("true"));
5554
assertThat(toJson(false, "field"), is("false"));
56-
assertThat(toJson(new SerializableSecureString("api_key"), "field"), is("\"api_key\""));
55+
assertThat(toJson(new SecureString("api_key".toCharArray()), "field"), is("\"api_key\""));
5756
}
5857

5958
public void testToJson_ThrowsException_WhenUnableToSerialize() {
60-
var exception = expectThrows(IllegalStateException.class, () -> toJson(new SecureString("string".toCharArray()), "field"));
59+
var exception = expectThrows(IllegalStateException.class, () -> toJson(new Object(), "field"));
6160
assertThat(
6261
exception.getMessage(),
6362
is(
6463
"Failed to serialize value as JSON, field: field, error: "
65-
+ "cannot write xcontent for unknown value of type class org.elasticsearch.common.settings.SecureString"
64+
+ "cannot write xcontent for unknown value of type class java.lang.Object"
6665
)
6766
);
6867
}

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/ServiceUtilsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99

1010
import org.elasticsearch.ElasticsearchStatusException;
1111
import org.elasticsearch.common.ValidationException;
12+
import org.elasticsearch.common.settings.SecureString;
1213
import org.elasticsearch.core.Booleans;
1314
import org.elasticsearch.core.TimeValue;
1415
import org.elasticsearch.core.Tuple;
1516
import org.elasticsearch.inference.InputType;
1617
import org.elasticsearch.test.ESTestCase;
1718
import org.elasticsearch.xpack.core.ml.inference.assignment.AdaptiveAllocationsSettings;
18-
import org.elasticsearch.xpack.inference.services.settings.SerializableSecureString;
1919

2020
import java.util.EnumSet;
2121
import java.util.HashMap;
@@ -1116,7 +1116,7 @@ public void testConvertMapStringsToSecureString() {
11161116
var validation = new ValidationException();
11171117
assertThat(
11181118
convertMapStringsToSecureString(Map.of("key", "value", "key2", "abc"), "setting", validation),
1119-
is(Map.of("key", new SerializableSecureString("value"), "key2", new SerializableSecureString("abc")))
1119+
is(Map.of("key", new SecureString("value".toCharArray()), "key2", new SecureString("abc".toCharArray())))
11201120
);
11211121
}
11221122

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/custom/CustomModelTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.xpack.inference.services.custom;
99

1010
import org.apache.http.HttpHeaders;
11+
import org.elasticsearch.common.settings.SecureString;
1112
import org.elasticsearch.core.Nullable;
1213
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper;
1314
import org.elasticsearch.inference.SimilarityMeasure;
@@ -17,7 +18,6 @@
1718
import org.elasticsearch.xpack.inference.services.custom.response.ErrorResponseParser;
1819
import org.elasticsearch.xpack.inference.services.custom.response.TextEmbeddingResponseParser;
1920
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings;
20-
import org.elasticsearch.xpack.inference.services.settings.SerializableSecureString;
2121
import org.hamcrest.MatcherAssert;
2222

2323
import java.util.HashMap;
@@ -30,7 +30,7 @@ public class CustomModelTests extends ESTestCase {
3030
private static final String taskSettingsValue = "test_taskSettings_value";
3131

3232
private static final String secretSettingsKey = "test_secret_key";
33-
private static final SerializableSecureString secretSettingsValue = new SerializableSecureString("test_secret_value");
33+
private static final SecureString secretSettingsValue = new SecureString("test_secret_value".toCharArray());
3434
private static final String url = "http://www.abc.com";
3535

3636
public void testOverride_DoesNotModifiedFields_TaskSettingsIsEmpty() {

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/custom/CustomRequestManagerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.elasticsearch.ElasticsearchStatusException;
1111
import org.elasticsearch.action.support.PlainActionFuture;
12+
import org.elasticsearch.common.settings.SecureString;
1213
import org.elasticsearch.core.TimeValue;
1314
import org.elasticsearch.inference.InferenceServiceResults;
1415
import org.elasticsearch.inference.TaskType;
@@ -19,7 +20,6 @@
1920
import org.elasticsearch.xpack.inference.services.custom.response.ErrorResponseParser;
2021
import org.elasticsearch.xpack.inference.services.custom.response.RerankResponseParser;
2122
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings;
22-
import org.elasticsearch.xpack.inference.services.settings.SerializableSecureString;
2323
import org.junit.After;
2424
import org.junit.Before;
2525

@@ -73,7 +73,7 @@ public void testCreateRequest_ThrowsException_ForInvalidUrl() {
7373
TaskType.RERANK,
7474
serviceSettings,
7575
new CustomTaskSettings(Map.of("url", "^")),
76-
new CustomSecretSettings(Map.of("api_key", new SerializableSecureString("my-secret-key")))
76+
new CustomSecretSettings(Map.of("api_key", new SecureString("my-secret-key".toCharArray())))
7777
);
7878

7979
var listener = new PlainActionFuture<InferenceServiceResults>();

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/custom/CustomSecretSettingsTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
import org.elasticsearch.common.Strings;
1212
import org.elasticsearch.common.ValidationException;
1313
import org.elasticsearch.common.io.stream.Writeable;
14+
import org.elasticsearch.common.settings.SecureString;
1415
import org.elasticsearch.common.xcontent.XContentHelper;
1516
import org.elasticsearch.xcontent.XContentBuilder;
1617
import org.elasticsearch.xcontent.XContentFactory;
1718
import org.elasticsearch.xcontent.XContentType;
1819
import org.elasticsearch.xpack.core.ml.AbstractBWCWireSerializationTestCase;
19-
import org.elasticsearch.xpack.inference.services.settings.SerializableSecureString;
2020

2121
import java.io.IOException;
2222
import java.util.HashMap;
@@ -28,10 +28,10 @@
2828

2929
public class CustomSecretSettingsTests extends AbstractBWCWireSerializationTestCase<CustomSecretSettings> {
3030
public static CustomSecretSettings createRandom() {
31-
Map<String, SerializableSecureString> secretParameters = randomMap(
31+
Map<String, SecureString> secretParameters = randomMap(
3232
0,
3333
5,
34-
() -> tuple(randomAlphaOfLength(5), new SerializableSecureString(randomAlphaOfLength(5)))
34+
() -> tuple(randomAlphaOfLength(5), new SecureString(randomAlphaOfLength(5).toCharArray()))
3535
);
3636

3737
return new CustomSecretSettings(secretParameters);
@@ -44,7 +44,7 @@ public void testFromMap() {
4444

4545
assertThat(
4646
CustomSecretSettings.fromMap(secretParameters),
47-
is(new CustomSecretSettings(Map.of("test_key", new SerializableSecureString("test_value"))))
47+
is(new CustomSecretSettings(Map.of("test_key", new SecureString("test_value".toCharArray()))))
4848
);
4949
}
5050

@@ -59,7 +59,7 @@ public void testFromMap_RemovesNullValues() {
5959

6060
assertThat(
6161
CustomSecretSettings.fromMap(modifiableMap(Map.of(CustomSecretSettings.SECRET_PARAMETERS, mapWithNulls))),
62-
is(new CustomSecretSettings(Map.of("value", new SerializableSecureString("abc"))))
62+
is(new CustomSecretSettings(Map.of("value", new SecureString("abc".toCharArray()))))
6363
);
6464
}
6565

@@ -87,7 +87,7 @@ public void testFromMap_DefaultsToEmptyMap_WhenSecretParametersField_DoesNotExis
8787
}
8888

8989
public void testXContent() throws IOException {
90-
var entity = new CustomSecretSettings(Map.of("test_key", new SerializableSecureString("test_value")));
90+
var entity = new CustomSecretSettings(Map.of("test_key", new SecureString("test_value".toCharArray())));
9191

9292
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
9393
entity.toXContent(builder, null);

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/services/custom/CustomServiceTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.xpack.inference.services.custom;
99

1010
import org.elasticsearch.action.support.PlainActionFuture;
11+
import org.elasticsearch.common.settings.SecureString;
1112
import org.elasticsearch.core.Nullable;
1213
import org.elasticsearch.index.mapper.vectors.DenseVectorFieldMapper;
1314
import org.elasticsearch.inference.InferenceServiceResults;
@@ -35,7 +36,6 @@
3536
import org.elasticsearch.xpack.inference.services.custom.response.SparseEmbeddingResponseParser;
3637
import org.elasticsearch.xpack.inference.services.custom.response.TextEmbeddingResponseParser;
3738
import org.elasticsearch.xpack.inference.services.settings.RateLimitSettings;
38-
import org.elasticsearch.xpack.inference.services.settings.SerializableSecureString;
3939

4040
import java.io.IOException;
4141
import java.util.EnumSet;
@@ -123,7 +123,7 @@ private static CustomModel assertCommonModelFields(Model model) {
123123
assertThat(customModel.getTaskSettings().getParameters(), is(Map.of("test_key", "test_value")));
124124
assertThat(
125125
customModel.getSecretSettings().getSecretParameters(),
126-
is(Map.of("test_key", new SerializableSecureString("test_value")))
126+
is(Map.of("test_key", new SecureString("test_value".toCharArray())))
127127
);
128128

129129
return customModel;
@@ -249,7 +249,7 @@ private static CustomModel createInternalEmbeddingModel(
249249
new ErrorResponseParser("$.error.message", inferenceId)
250250
),
251251
new CustomTaskSettings(Map.of("key", "test_value")),
252-
new CustomSecretSettings(Map.of("test_key", new SerializableSecureString("test_value")))
252+
new CustomSecretSettings(Map.of("test_key", new SecureString("test_value".toCharArray())))
253253
);
254254
}
255255

@@ -271,7 +271,7 @@ private static CustomModel createCustomModel(TaskType taskType, CustomResponsePa
271271
new ErrorResponseParser("$.error.message", inferenceId)
272272
),
273273
new CustomTaskSettings(Map.of("key", "test_value")),
274-
new CustomSecretSettings(Map.of("test_key", new SerializableSecureString("test_value")))
274+
new CustomSecretSettings(Map.of("test_key", new SecureString("test_value".toCharArray())))
275275
);
276276
}
277277

0 commit comments

Comments
 (0)