Skip to content

Commit 5e17bc5

Browse files
Consistent Secure Settings #40416
Introduces a new `ConsistentSecureSettingsValidatorService` service that exposes a single public method, namely `allSecureSettingsConsistent`. The method returns `true` if the local node's secure settings (inside the keystore) are equal to the master's, and `false` otherwise. Technically, the local node has to have exactly the same secure settings - setting names should not be missing or in surplus - for all `SecureSetting` instances that are flagged with the newly introduced `Property.Consistent`. It is worth highlighting that the `allSecureSettingsConsistent` is not a consensus view across the cluster, but rather the local node's perspective in relation to the master.
1 parent b599c68 commit 5e17bc5

File tree

20 files changed

+886
-39
lines changed

20 files changed

+886
-39
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/DiffableStringMap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
*/
4040
public class DiffableStringMap extends AbstractMap<String, String> implements Diffable<DiffableStringMap> {
4141

42+
public static final DiffableStringMap EMPTY = new DiffableStringMap(Collections.emptyMap());
43+
4244
private final Map<String, String> innerMap;
4345

4446
DiffableStringMap(final Map<String, String> map) {
@@ -75,6 +77,8 @@ public static Diff<DiffableStringMap> readDiffFrom(StreamInput in) throws IOExce
7577
*/
7678
public static class DiffableStringMapDiff implements Diff<DiffableStringMap> {
7779

80+
public static final DiffableStringMapDiff EMPTY = new DiffableStringMapDiff(DiffableStringMap.EMPTY, DiffableStringMap.EMPTY);
81+
7882
private final List<String> deletes;
7983
private final Map<String, String> upserts; // diffs also become upserts
8084

server/src/main/java/org/elasticsearch/cluster/metadata/MetaData.java

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ public interface Custom extends NamedDiffable<Custom>, ToXContentFragment, Clust
170170
private final Settings transientSettings;
171171
private final Settings persistentSettings;
172172
private final Settings settings;
173+
private final DiffableStringMap hashesOfConsistentSettings;
173174
private final ImmutableOpenMap<String, IndexMetaData> indices;
174175
private final ImmutableOpenMap<String, IndexTemplateMetaData> templates;
175176
private final ImmutableOpenMap<String, Custom> customs;
@@ -184,7 +185,7 @@ public interface Custom extends NamedDiffable<Custom>, ToXContentFragment, Clust
184185
private final SortedMap<String, AliasOrIndex> aliasAndIndexLookup;
185186

186187
MetaData(String clusterUUID, boolean clusterUUIDCommitted, long version, CoordinationMetaData coordinationMetaData,
187-
Settings transientSettings, Settings persistentSettings,
188+
Settings transientSettings, Settings persistentSettings, DiffableStringMap hashesOfConsistentSettings,
188189
ImmutableOpenMap<String, IndexMetaData> indices, ImmutableOpenMap<String, IndexTemplateMetaData> templates,
189190
ImmutableOpenMap<String, Custom> customs, String[] allIndices, String[] allOpenIndices, String[] allClosedIndices,
190191
SortedMap<String, AliasOrIndex> aliasAndIndexLookup) {
@@ -195,6 +196,7 @@ public interface Custom extends NamedDiffable<Custom>, ToXContentFragment, Clust
195196
this.transientSettings = transientSettings;
196197
this.persistentSettings = persistentSettings;
197198
this.settings = Settings.builder().put(persistentSettings).put(transientSettings).build();
199+
this.hashesOfConsistentSettings = hashesOfConsistentSettings;
198200
this.indices = indices;
199201
this.customs = customs;
200202
this.templates = templates;
@@ -246,6 +248,10 @@ public Settings persistentSettings() {
246248
return this.persistentSettings;
247249
}
248250

251+
public Map<String, String> hashesOfConsistentSettings() {
252+
return this.hashesOfConsistentSettings;
253+
}
254+
249255
public CoordinationMetaData coordinationMetaData() {
250256
return this.coordinationMetaData;
251257
}
@@ -767,6 +773,9 @@ public static boolean isGlobalStateEquals(MetaData metaData1, MetaData metaData2
767773
if (!metaData1.persistentSettings.equals(metaData2.persistentSettings)) {
768774
return false;
769775
}
776+
if (!metaData1.hashesOfConsistentSettings.equals(metaData2.hashesOfConsistentSettings)) {
777+
return false;
778+
}
770779
if (!metaData1.templates.equals(metaData2.templates())) {
771780
return false;
772781
}
@@ -821,6 +830,7 @@ private static class MetaDataDiff implements Diff<MetaData> {
821830
private CoordinationMetaData coordinationMetaData;
822831
private Settings transientSettings;
823832
private Settings persistentSettings;
833+
private Diff<DiffableStringMap> hashesOfConsistentSettings;
824834
private Diff<ImmutableOpenMap<String, IndexMetaData>> indices;
825835
private Diff<ImmutableOpenMap<String, IndexTemplateMetaData>> templates;
826836
private Diff<ImmutableOpenMap<String, Custom>> customs;
@@ -832,6 +842,7 @@ private static class MetaDataDiff implements Diff<MetaData> {
832842
coordinationMetaData = after.coordinationMetaData;
833843
transientSettings = after.transientSettings;
834844
persistentSettings = after.persistentSettings;
845+
hashesOfConsistentSettings = after.hashesOfConsistentSettings.diff(before.hashesOfConsistentSettings);
835846
indices = DiffableUtils.diff(before.indices, after.indices, DiffableUtils.getStringKeySerializer());
836847
templates = DiffableUtils.diff(before.templates, after.templates, DiffableUtils.getStringKeySerializer());
837848
customs = DiffableUtils.diff(before.customs, after.customs, DiffableUtils.getStringKeySerializer(), CUSTOM_VALUE_SERIALIZER);
@@ -850,6 +861,11 @@ private static class MetaDataDiff implements Diff<MetaData> {
850861
}
851862
transientSettings = Settings.readSettingsFromStream(in);
852863
persistentSettings = Settings.readSettingsFromStream(in);
864+
if (in.getVersion().onOrAfter(Version.V_7_3_0)) {
865+
hashesOfConsistentSettings = DiffableStringMap.readDiffFrom(in);
866+
} else {
867+
hashesOfConsistentSettings = DiffableStringMap.DiffableStringMapDiff.EMPTY;
868+
}
853869
indices = DiffableUtils.readImmutableOpenMapDiff(in, DiffableUtils.getStringKeySerializer(), IndexMetaData::readFrom,
854870
IndexMetaData::readDiffFrom);
855871
templates = DiffableUtils.readImmutableOpenMapDiff(in, DiffableUtils.getStringKeySerializer(), IndexTemplateMetaData::readFrom,
@@ -869,6 +885,9 @@ public void writeTo(StreamOutput out) throws IOException {
869885
}
870886
Settings.writeSettingsToStream(transientSettings, out);
871887
Settings.writeSettingsToStream(persistentSettings, out);
888+
if (out.getVersion().onOrAfter(Version.V_7_3_0)) {
889+
hashesOfConsistentSettings.writeTo(out);
890+
}
872891
indices.writeTo(out);
873892
templates.writeTo(out);
874893
customs.writeTo(out);
@@ -883,6 +902,7 @@ public MetaData apply(MetaData part) {
883902
builder.coordinationMetaData(coordinationMetaData);
884903
builder.transientSettings(transientSettings);
885904
builder.persistentSettings(persistentSettings);
905+
builder.hashesOfConsistentSettings(hashesOfConsistentSettings.apply(part.hashesOfConsistentSettings));
886906
builder.indices(indices.apply(part.indices));
887907
builder.templates(templates.apply(part.templates));
888908
builder.customs(customs.apply(part.customs));
@@ -902,6 +922,9 @@ public static MetaData readFrom(StreamInput in) throws IOException {
902922
}
903923
builder.transientSettings(readSettingsFromStream(in));
904924
builder.persistentSettings(readSettingsFromStream(in));
925+
if (in.getVersion().onOrAfter(Version.V_7_3_0)) {
926+
builder.hashesOfConsistentSettings(new DiffableStringMap(in));
927+
}
905928
int size = in.readVInt();
906929
for (int i = 0; i < size; i++) {
907930
builder.put(IndexMetaData.readFrom(in), false);
@@ -930,6 +953,9 @@ public void writeTo(StreamOutput out) throws IOException {
930953
}
931954
writeSettingsToStream(transientSettings, out);
932955
writeSettingsToStream(persistentSettings, out);
956+
if (out.getVersion().onOrAfter(Version.V_7_3_0)) {
957+
hashesOfConsistentSettings.writeTo(out);
958+
}
933959
out.writeVInt(indices.size());
934960
for (IndexMetaData indexMetaData : this) {
935961
indexMetaData.writeTo(out);
@@ -970,6 +996,7 @@ public static class Builder {
970996
private CoordinationMetaData coordinationMetaData = CoordinationMetaData.EMPTY_META_DATA;
971997
private Settings transientSettings = Settings.Builder.EMPTY_SETTINGS;
972998
private Settings persistentSettings = Settings.Builder.EMPTY_SETTINGS;
999+
private DiffableStringMap hashesOfConsistentSettings = new DiffableStringMap(Collections.emptyMap());
9731000

9741001
private final ImmutableOpenMap.Builder<String, IndexMetaData> indices;
9751002
private final ImmutableOpenMap.Builder<String, IndexTemplateMetaData> templates;
@@ -989,6 +1016,7 @@ public Builder(MetaData metaData) {
9891016
this.coordinationMetaData = metaData.coordinationMetaData;
9901017
this.transientSettings = metaData.transientSettings;
9911018
this.persistentSettings = metaData.persistentSettings;
1019+
this.hashesOfConsistentSettings = metaData.hashesOfConsistentSettings;
9921020
this.version = metaData.version;
9931021
this.indices = ImmutableOpenMap.builder(metaData.indices);
9941022
this.templates = ImmutableOpenMap.builder(metaData.templates);
@@ -1152,6 +1180,20 @@ public Builder persistentSettings(Settings settings) {
11521180
return this;
11531181
}
11541182

1183+
public DiffableStringMap hashesOfConsistentSettings() {
1184+
return this.hashesOfConsistentSettings;
1185+
}
1186+
1187+
public Builder hashesOfConsistentSettings(DiffableStringMap hashesOfConsistentSettings) {
1188+
this.hashesOfConsistentSettings = hashesOfConsistentSettings;
1189+
return this;
1190+
}
1191+
1192+
public Builder hashesOfConsistentSettings(Map<String, String> hashesOfConsistentSettings) {
1193+
this.hashesOfConsistentSettings = new DiffableStringMap(hashesOfConsistentSettings);
1194+
return this;
1195+
}
1196+
11551197
public Builder version(long version) {
11561198
this.version = version;
11571199
return this;
@@ -1225,8 +1267,8 @@ public MetaData build() {
12251267
String[] allClosedIndicesArray = allClosedIndices.toArray(new String[allClosedIndices.size()]);
12261268

12271269
return new MetaData(clusterUUID, clusterUUIDCommitted, version, coordinationMetaData, transientSettings, persistentSettings,
1228-
indices.build(), templates.build(), customs.build(), allIndicesArray, allOpenIndicesArray, allClosedIndicesArray,
1229-
aliasAndIndexLookup);
1270+
hashesOfConsistentSettings, indices.build(), templates.build(), customs.build(), allIndicesArray, allOpenIndicesArray,
1271+
allClosedIndicesArray, aliasAndIndexLookup);
12301272
}
12311273

12321274
private SortedMap<String, AliasOrIndex> buildAliasAndIndexLookup() {
@@ -1350,6 +1392,8 @@ public static MetaData fromXContent(XContentParser parser) throws IOException {
13501392
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
13511393
builder.put(IndexMetaData.Builder.fromXContent(parser), false);
13521394
}
1395+
} else if ("hashes_of_consistent_settings".equals(currentFieldName)) {
1396+
builder.hashesOfConsistentSettings(parser.mapStrings());
13531397
} else if ("templates".equals(currentFieldName)) {
13541398
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
13551399
builder.put(IndexTemplateMetaData.Builder.fromXContent(parser, parser.currentName()));

server/src/main/java/org/elasticsearch/cluster/service/ClusterService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public ClusterService(Settings settings, ClusterSettings clusterSettings, Thread
7373
}
7474

7575
public ClusterService(Settings settings, ClusterSettings clusterSettings, MasterService masterService,
76-
ClusterApplierService clusterApplierService) {
76+
ClusterApplierService clusterApplierService) {
7777
this.settings = settings;
7878
this.nodeName = Node.NODE_NAME_SETTING.get(settings);
7979
this.masterService = masterService;

server/src/main/java/org/elasticsearch/common/hash/MessageDigests.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,24 @@ private static MessageDigest get(ThreadLocal<MessageDigest> messageDigest) {
9595
* @return a hex representation of the input as a String.
9696
*/
9797
public static String toHexString(byte[] bytes) {
98-
Objects.requireNonNull(bytes);
99-
StringBuilder sb = new StringBuilder(2 * bytes.length);
98+
return new String(toHexCharArray(bytes));
99+
}
100100

101+
/**
102+
* Encodes the byte array into a newly created hex char array, without allocating any other temporary variables.
103+
*
104+
* @param bytes the input to be encoded as hex.
105+
* @return the hex encoding of the input as a char array.
106+
*/
107+
public static char[] toHexCharArray(byte[] bytes) {
108+
Objects.requireNonNull(bytes);
109+
final char[] result = new char[2 * bytes.length];
101110
for (int i = 0; i < bytes.length; i++) {
102111
byte b = bytes[i];
103-
sb.append(HEX_DIGITS[b >> 4 & 0xf]).append(HEX_DIGITS[b & 0xf]);
112+
result[2 * i] = HEX_DIGITS[b >> 4 & 0xf];
113+
result[2 * i + 1] = HEX_DIGITS[b & 0xf];
104114
}
105-
106-
return sb.toString();
115+
return result;
107116
}
108117

109118
}

server/src/main/java/org/elasticsearch/common/settings/ClusterSettings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@
120120
* Encapsulates all valid cluster level settings.
121121
*/
122122
public final class ClusterSettings extends AbstractScopedSettings {
123+
123124
public ClusterSettings(final Settings nodeSettings, final Set<Setting<?>> settingsSet) {
124125
this(nodeSettings, settingsSet, Collections.emptySet());
125126
}
126127

127-
public ClusterSettings(
128-
final Settings nodeSettings, final Set<Setting<?>> settingsSet, final Set<SettingUpgrader<?>> settingUpgraders) {
128+
public ClusterSettings(final Settings nodeSettings, final Set<Setting<?>> settingsSet, final Set<SettingUpgrader<?>> settingUpgraders) {
129129
super(nodeSettings, settingsSet, settingUpgraders, Property.NodeScope);
130130
addSettingsUpdater(new LoggingSettingUpdater(nodeSettings));
131131
}

0 commit comments

Comments
 (0)