Skip to content

Commit c91dcbd

Browse files
authored
Types removal security index template (#39705) (#39728)
As we are moving to single type indices, we need to address this change in security-related indexes. To address this, we are - updating index templates to use preferred type name `_doc` - updating the API calls to use preferred type name `_doc` Upgrade impact:- In case of an upgrade from 6.x, the security index has type `doc` and this will keep working as there is a single type and `_doc` works as an alias to an existing type. The change is handled in the `SecurityIndexManager` when we load mappings and settings from the template. Previously, we used to do a `PutIndexTemplateRequest` with the mapping source JSON with the type name. This has been modified to remove the type name from the source. So in the case of an upgrade, the `doc` type is updated whereas for fresh installs `_doc` is updated. This happens as backend handles `_doc` as an alias to the existing type name. An optional step is to `reindex` security index and update the type to `_doc`. Since we do not support the security audit log index, that template has been deleted. Relates: #38637
1 parent aaecaf5 commit c91dcbd

File tree

17 files changed

+118
-178
lines changed

17 files changed

+118
-178
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/authc/support/mapper/NativeRoleMappingStoreField.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public final class NativeRoleMappingStoreField {
1010
public static final String DOC_TYPE_FIELD = "doc_type";
1111
public static final String DOC_TYPE_ROLE_MAPPING = "role-mapping";
1212
public static final String ID_PREFIX = DOC_TYPE_ROLE_MAPPING + "_";
13-
public static final String SECURITY_GENERIC_TYPE = "doc";
1413

1514
private NativeRoleMappingStoreField() {}
1615
}

x-pack/plugin/core/src/main/resources/security-index-template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
}
3434
},
3535
"mappings" : {
36-
"doc" : {
36+
"_doc" : {
3737
"_meta": {
3838
"security-version": "${security.template.version}"
3939
},

x-pack/plugin/core/src/main/resources/security_audit_log.json

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

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,16 @@
9393

9494
import javax.crypto.SecretKeyFactory;
9595

96+
import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME;
9697
import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING;
9798
import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN;
9899
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;
100+
import static org.elasticsearch.xpack.security.support.SecurityIndexManager.SECURITY_INDEX_NAME;
99101

100102
public class ApiKeyService {
101103

102104
private static final Logger logger = LogManager.getLogger(ApiKeyService.class);
103105
private static final DeprecationLogger deprecationLogger = new DeprecationLogger(logger);
104-
private static final String TYPE = "doc";
105106
static final String API_KEY_ID_KEY = "_security_api_key_id";
106107
static final String API_KEY_ROLE_DESCRIPTORS_KEY = "_security_api_key_role_descriptors";
107108
static final String API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY = "_security_api_key_limited_by_role_descriptors";
@@ -248,7 +249,7 @@ public void createApiKey(Authentication authentication, CreateApiKeyRequest requ
248249
.endObject()
249250
.endObject();
250251
final IndexRequest indexRequest =
251-
client.prepareIndex(SecurityIndexManager.SECURITY_INDEX_NAME, TYPE)
252+
client.prepareIndex(SECURITY_INDEX_NAME, SINGLE_MAPPING_NAME)
252253
.setSource(builder)
253254
.setRefreshPolicy(request.getRefreshPolicy())
254255
.request();
@@ -286,8 +287,10 @@ void authenticateWithApiKeyIfPresent(ThreadContext ctx, ActionListener<Authentic
286287
}
287288

288289
if (credentials != null) {
289-
final GetRequest getRequest = client.prepareGet(SecurityIndexManager.SECURITY_INDEX_NAME, TYPE, credentials.getId())
290-
.setFetchSource(true).request();
290+
final GetRequest getRequest = client
291+
.prepareGet(SECURITY_INDEX_NAME, SINGLE_MAPPING_NAME, credentials.getId())
292+
.setFetchSource(true)
293+
.request();
291294
executeAsyncWithOrigin(ctx, SECURITY_ORIGIN, getRequest, ActionListener.<GetResponse>wrap(response -> {
292295
if (response.isExists()) {
293296
try (ApiKeyCredentials ignore = credentials) {
@@ -693,7 +696,7 @@ private void findApiKeys(final BoolQueryBuilder boolQuery, boolean filterOutInva
693696
expiredQuery.should(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("expiration_time")));
694697
boolQuery.filter(expiredQuery);
695698
}
696-
final SearchRequest request = client.prepareSearch(SecurityIndexManager.SECURITY_INDEX_NAME)
699+
final SearchRequest request = client.prepareSearch(SECURITY_INDEX_NAME)
697700
.setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings))
698701
.setQuery(boolQuery)
699702
.setVersion(false)
@@ -766,9 +769,10 @@ private void indexInvalidation(Collection<String> apiKeyIds, ActionListener<Inva
766769
} else {
767770
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
768771
for (String apiKeyId : apiKeyIds) {
769-
UpdateRequest request = client.prepareUpdate(SecurityIndexManager.SECURITY_INDEX_NAME, TYPE, apiKeyId)
770-
.setDoc(Collections.singletonMap("api_key_invalidated", true))
771-
.request();
772+
UpdateRequest request = client
773+
.prepareUpdate(SECURITY_INDEX_NAME, SINGLE_MAPPING_NAME, apiKeyId)
774+
.setDoc(Collections.singletonMap("api_key_invalidated", true))
775+
.request();
772776
bulkRequestBuilder.add(request);
773777
}
774778
bulkRequestBuilder.setRefreshPolicy(RefreshPolicy.WAIT_UNTIL);

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@
129129

130130
import static org.elasticsearch.action.support.TransportActions.isShardNotAvailableException;
131131
import static org.elasticsearch.gateway.GatewayService.STATE_NOT_RECOVERED_BLOCK;
132+
import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME;
132133
import static org.elasticsearch.search.SearchService.DEFAULT_KEEPALIVE_SETTING;
133134
import static org.elasticsearch.xpack.core.ClientHelper.SECURITY_ORIGIN;
134135
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;
136+
import static org.elasticsearch.xpack.security.support.SecurityIndexManager.SECURITY_INDEX_NAME;
135137
import static org.elasticsearch.threadpool.ThreadPool.Names.GENERIC;
136138

137139
/**
@@ -158,7 +160,6 @@ public final class TokenService {
158160
"\", error=\"invalid_token\", error_description=\"The access token expired\"";
159161
private static final String MALFORMED_TOKEN_WWW_AUTH_VALUE = "Bearer realm=\"" + XPackField.SECURITY +
160162
"\", error=\"invalid_token\", error_description=\"The access token is malformed\"";
161-
private static final String TYPE = "doc";
162163
private static final BackoffPolicy DEFAULT_BACKOFF = BackoffPolicy.exponentialBackoff();
163164

164165
public static final String THREAD_POOL_NAME = XPackField.SECURITY + "-token-key";
@@ -280,7 +281,7 @@ private void createUserToken(String userTokenId, Authentication authentication,
280281
builder.endObject();
281282
final String documentId = getTokenDocumentId(userToken);
282283
IndexRequest request =
283-
client.prepareIndex(SecurityIndexManager.SECURITY_INDEX_NAME, TYPE, documentId)
284+
client.prepareIndex(SECURITY_INDEX_NAME, SINGLE_MAPPING_NAME, documentId)
284285
.setOpType(OpType.CREATE)
285286
.setSource(builder)
286287
.setRefreshPolicy(RefreshPolicy.WAIT_UNTIL)
@@ -377,7 +378,7 @@ void getUserTokenFromId(String userTokenId, ActionListener<UserToken> listener)
377378
securityIndex.checkIndexVersionThenExecute(
378379
ex -> listener.onFailure(traceLog("prepare security index", userTokenId, ex)),
379380
() -> {
380-
final GetRequest getRequest = client.prepareGet(SecurityIndexManager.SECURITY_INDEX_NAME, TYPE,
381+
final GetRequest getRequest = client.prepareGet(SecurityIndexManager.SECURITY_INDEX_NAME, SINGLE_MAPPING_NAME,
381382
getTokenDocumentId(userTokenId)).request();
382383
Consumer<Exception> onFailure = ex -> listener.onFailure(traceLog("decode token", userTokenId, ex));
383384
executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, getRequest,
@@ -638,10 +639,11 @@ private void indexInvalidation(Collection<String> tokenIds, ActionListener<Token
638639
} else {
639640
BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
640641
for (String tokenId : tokenIds) {
641-
UpdateRequest request = client.prepareUpdate(SecurityIndexManager.SECURITY_INDEX_NAME, TYPE, getTokenDocumentId(tokenId))
642-
.setDoc(srcPrefix, Collections.singletonMap("invalidated", true))
643-
.setFetchSource(srcPrefix, null)
644-
.request();
642+
UpdateRequest request = client
643+
.prepareUpdate(SECURITY_INDEX_NAME, SINGLE_MAPPING_NAME, getTokenDocumentId(tokenId))
644+
.setDoc(srcPrefix, Collections.singletonMap("invalidated", true))
645+
.setFetchSource(srcPrefix, null)
646+
.request();
645647
bulkRequestBuilder.add(request);
646648
}
647649
bulkRequestBuilder.setRefreshPolicy(RefreshPolicy.WAIT_UNTIL);
@@ -734,7 +736,7 @@ public void refreshToken(String refreshToken, ActionListener<Tuple<UserToken, St
734736
*/
735737
private void findTokenFromRefreshToken(String refreshToken, ActionListener<SearchResponse> listener,
736738
Iterator<TimeValue> backoff) {
737-
SearchRequest request = client.prepareSearch(SecurityIndexManager.SECURITY_INDEX_NAME)
739+
SearchRequest request = client.prepareSearch(SECURITY_INDEX_NAME)
738740
.setQuery(QueryBuilders.boolQuery()
739741
.filter(QueryBuilders.termQuery("doc_type", TOKEN_DOC_TYPE))
740742
.filter(QueryBuilders.termQuery("refresh_token.token", refreshToken)))
@@ -880,7 +882,7 @@ public void onFailure(Exception e) {
880882
updateMap.put("refresh_time", refreshTime.toEpochMilli());
881883
updateMap.put("superseded_by", getTokenDocumentId(newUserTokenId));
882884
UpdateRequestBuilder updateRequest =
883-
client.prepareUpdate(SecurityIndexManager.SECURITY_INDEX_NAME, TYPE, tokenDocId)
885+
client.prepareUpdate(SecurityIndexManager.SECURITY_INDEX_NAME, SINGLE_MAPPING_NAME, tokenDocId)
884886
.setDoc("refresh_token", updateMap)
885887
.setFetchSource(true)
886888
.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
@@ -970,7 +972,7 @@ public void onFailure(Exception e) {
970972

971973
private void getTokenDocAsync(String tokenDocId, ActionListener<GetResponse> listener) {
972974
GetRequest getRequest =
973-
client.prepareGet(SecurityIndexManager.SECURITY_INDEX_NAME, TYPE, tokenDocId).request();
975+
client.prepareGet(SECURITY_INDEX_NAME, SINGLE_MAPPING_NAME, tokenDocId).request();
974976
executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, getRequest, listener, client::get);
975977
}
976978

@@ -1127,7 +1129,7 @@ public void findActiveTokensForRealm(String realmName, ActionListener<Collection
11271129
)
11281130
);
11291131

1130-
final SearchRequest request = client.prepareSearch(SecurityIndexManager.SECURITY_INDEX_NAME)
1132+
final SearchRequest request = client.prepareSearch(SECURITY_INDEX_NAME)
11311133
.setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings))
11321134
.setQuery(boolQuery)
11331135
.setVersion(false)
@@ -1170,7 +1172,7 @@ public void findActiveTokensForUser(String username, ActionListener<Collection<T
11701172
)
11711173
);
11721174

1173-
final SearchRequest request = client.prepareSearch(SecurityIndexManager.SECURITY_INDEX_NAME)
1175+
final SearchRequest request = client.prepareSearch(SECURITY_INDEX_NAME)
11741176
.setScroll(DEFAULT_KEEPALIVE_SETTING.get(settings))
11751177
.setQuery(boolQuery)
11761178
.setVersion(false)
@@ -1277,8 +1279,8 @@ private void checkIfTokenIsValid(UserToken userToken, ActionListener<UserToken>
12771279
listener.onResponse(null);
12781280
} else {
12791281
securityIndex.checkIndexVersionThenExecute(listener::onFailure, () -> {
1280-
final GetRequest getRequest = client.prepareGet(SecurityIndexManager.SECURITY_INDEX_NAME, TYPE,
1281-
getTokenDocumentId(userToken)).request();
1282+
final GetRequest getRequest = client.prepareGet(SECURITY_INDEX_NAME, SINGLE_MAPPING_NAME, getTokenDocumentId(userToken))
1283+
.request();
12821284
Consumer<Exception> onFailure = ex -> listener.onFailure(traceLog("check token state", userToken.getId(), ex));
12831285
executeAsyncWithOrigin(client.threadPool().getThreadContext(), SECURITY_ORIGIN, getRequest,
12841286
ActionListener.<GetResponse>wrap(response -> {

0 commit comments

Comments
 (0)