Skip to content

Commit a16e649

Browse files
committed
system index mapping workaround
1 parent 976538c commit a16e649

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/action/CreateApiKeyRequest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,17 @@ public void writeTo(StreamOutput out) throws IOException {
151151
out.writeOptionalTimeValue(expiration);
152152
out.writeList(roleDescriptors);
153153
refreshPolicy.writeTo(out);
154-
if (out.getVersion().onOrAfter(Version.V_7_13_0)) {
155-
out.writeMap(metadata);
154+
if (metadata != null && false == metadata.isEmpty()) {
155+
if (out.getVersion().before(Version.V_7_13_0)) {
156+
throw new IllegalArgumentException(
157+
"api key metadata requires minimum node version to be [7.13.0], got: [" + out.getVersion() + "]");
158+
} else {
159+
out.writeMap(metadata);
160+
}
161+
} else {
162+
if (out.getVersion().onOrAfter(Version.V_7_13_0)) {
163+
out.writeMap(metadata);
164+
}
156165
}
157166
}
158167
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/Security.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,12 +1277,16 @@ private static Settings getIndexSettings() {
12771277
}
12781278

12791279
private static XContentBuilder getIndexMappings() {
1280+
return getIndexMappings(Version.CURRENT);
1281+
}
1282+
1283+
public static XContentBuilder getIndexMappings(Version compatibleVersion) {
12801284
try {
12811285
final XContentBuilder builder = jsonBuilder();
12821286
builder.startObject();
12831287
{
12841288
builder.startObject("_meta");
1285-
builder.field(SECURITY_VERSION_STRING, Version.CURRENT.toString());
1289+
builder.field(SECURITY_VERSION_STRING, compatibleVersion.toString());
12861290
builder.endObject();
12871291

12881292
builder.field("dynamic", "strict");
@@ -1332,9 +1336,11 @@ private static XContentBuilder getIndexMappings() {
13321336
builder.field("dynamic", false);
13331337
builder.endObject();
13341338

1335-
builder.startObject("metadata_flattened");
1336-
builder.field("type", "flattened");
1337-
builder.endObject();
1339+
if (compatibleVersion.onOrAfter(Version.V_7_13_0)) {
1340+
builder.startObject("metadata_flattened");
1341+
builder.field("type", "flattened");
1342+
builder.endObject();
1343+
}
13381344

13391345
builder.startObject("enabled");
13401346
builder.field("type", "boolean");

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/support/SecurityIndexManager.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.cluster.metadata.Metadata;
3636
import org.elasticsearch.cluster.routing.IndexRoutingTable;
3737
import org.elasticsearch.cluster.service.ClusterService;
38+
import org.elasticsearch.common.Strings;
3839
import org.elasticsearch.common.xcontent.XContentType;
3940
import org.elasticsearch.gateway.GatewayService;
4041
import org.elasticsearch.index.Index;
@@ -43,6 +44,7 @@
4344
import org.elasticsearch.indices.IndexClosedException;
4445
import org.elasticsearch.indices.SystemIndexDescriptor;
4546
import org.elasticsearch.rest.RestStatus;
47+
import org.elasticsearch.xpack.security.Security;
4648

4749
import java.time.Instant;
4850
import java.util.HashSet;
@@ -384,8 +386,16 @@ public void onFailure(Exception e) {
384386
indexState.concreteIndexName,
385387
systemIndexDescriptor.getAliasName()
386388
);
389+
final String mappings;
390+
// Flattened field is available since 7.3.0 and it is used by API key metadata in 7.13.0.
391+
// Therefore if the minimum node version is before 7.3.0, we fallback to use the mapping from 7.12.0.
392+
if (indexState.minimumNodeVersion.before(Version.V_7_3_0)) {
393+
mappings = Strings.toString(Security.getIndexMappings(Version.V_7_12_0));
394+
} else {
395+
mappings = systemIndexDescriptor.getMappings();
396+
}
387397
PutMappingRequest request = new PutMappingRequest(indexState.concreteIndexName).source(
388-
systemIndexDescriptor.getMappings(),
398+
mappings,
389399
XContentType.JSON
390400
).type(MapperService.SINGLE_MAPPING_NAME).origin(systemIndexDescriptor.getOrigin());
391401
executeAsyncWithOrigin(client.threadPool().getThreadContext(), systemIndexDescriptor.getOrigin(), request,

0 commit comments

Comments
 (0)