Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cases with flag do not fail on forbidden enabled. #2154

Merged
merged 5 commits into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public class SearchOperationTest {

@BeforeClass
public static void createTestData() {
try(Client client = cluster.getInternalNodeClient()){
try(Client client = cluster.getInternalNodeClient()) {
client.prepareIndex(SONG_INDEX_NAME).setId(ID_S1).setRefreshPolicy(IMMEDIATE).setSource(SONGS[0]).get();
client.prepareIndex(UPDATE_DELETE_OPERATION_INDEX_NAME).setId(DOCUMENT_TO_UPDATE_ID).setRefreshPolicy(IMMEDIATE).setSource("field", "value").get();
client.admin().indices().aliases(new IndicesAliasesRequest().addAliasAction(new AliasActions(ADD).indices(SONG_INDEX_NAME).alias(SONG_LYRICS_ALIAS))).actionGet();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ public TestSecurityConfig anonymousAuth(boolean anonymousAuthEnabled) {
config.anonymousAuth(anonymousAuthEnabled);
return this;
}

public TestSecurityConfig doNotFailOnForbidden(boolean doNotFailOnForbidden) {
config.doNotFailOnForbidden(doNotFailOnForbidden);
return this;
}

public TestSecurityConfig authc(AuthcDomain authcDomain) {
config.authc(authcDomain);
Expand Down Expand Up @@ -128,13 +133,21 @@ public TestSecurityConfig audit(AuditConfiguration auditConfiguration) {

public static class Config implements ToXContentObject {
private boolean anonymousAuth;

private Boolean doNotFailOnForbidden;

private Map<String, AuthcDomain> authcDomainMap = new LinkedHashMap<>();

public Config anonymousAuth(boolean anonymousAuth) {
this.anonymousAuth = anonymousAuth;
return this;
}

public Config doNotFailOnForbidden(Boolean doNotFailOnForbidden) {
this.doNotFailOnForbidden = doNotFailOnForbidden;
return this;
}

public Config authc(AuthcDomain authcDomain) {
authcDomainMap.put(authcDomain.id, authcDomain);
return this;
Expand All @@ -150,6 +163,9 @@ public XContentBuilder toXContent(XContentBuilder xContentBuilder, Params params
xContentBuilder.field("anonymous_auth_enabled", true);
xContentBuilder.endObject();
}
if(doNotFailOnForbidden != null) {
xContentBuilder.field("do_not_fail_on_forbidden", doNotFailOnForbidden);
}

xContentBuilder.field("authc", authcDomainMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,11 @@ public Builder certificates(TestCertificates certificates) {
return this;
}

public Builder doNotFailOnForbidden(boolean doNotFailOnForbidden) {
testSecurityConfig.doNotFailOnForbidden(doNotFailOnForbidden);
return this;
}

public LocalCluster build() {
try {
if(testCertificates == null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ public static SearchRequest queryStringQueryRequest(String indexName, String que
return searchRequest;
}

public static SearchRequest queryStringQueryRequest(String[] indicesNames, String queryString) {
SearchRequest searchRequest = new SearchRequest(indicesNames);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.queryStringQuery(queryString));
searchRequest.source(searchSourceBuilder);
return searchRequest;
}

public static SearchRequest queryStringQueryRequest(String queryString) {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
*/
package org.opensearch.test.framework.matcher;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeDiagnosingMatcher;

import org.opensearch.action.get.GetResponse;

import static java.util.Objects.requireNonNull;

class GetResponseContainOnlyDocumentIdMatcher extends TypeSafeDiagnosingMatcher<GetResponse> {

private final String indexName;
private final String documentId;

public GetResponseContainOnlyDocumentIdMatcher(String indexName, String documentId) {
this.indexName = requireNonNull(indexName, "Index name is required");
this.documentId = requireNonNull(documentId, "Document id is required");
}

@Override
protected boolean matchesSafely(GetResponse response, Description mismatchDescription) {
if(indexName.equals(response.getIndex()) == false ) {
mismatchDescription.appendText(" index name ").appendValue(response.getIndex()).appendText(" is incorrect ");
return false;
}
if(documentId.equals(response.getId()) == false) {
mismatchDescription.appendText(" id ").appendValue(response.getId()).appendText(" is incorrect ");
return false;
}
if(response.isExists()) {
mismatchDescription.appendText(" document exist what is not desired ");
return false;
}
return true;
}

@Override
public void describeTo(Description description) {
description.appendText("Response should contain document id from index ").appendValue(indexName).appendText(" with id ")
.appendValue(documentId).appendText(" but document should not be present ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public static Matcher<GetResponse> containDocument(String indexName, String docu
return new GetResponseDocumentIdMatcher(indexName, documentId);
}

public static Matcher<GetResponse> containOnlyDocumentId(String indexName, String documentId) {
return new GetResponseContainOnlyDocumentIdMatcher(indexName, documentId);
}

public static Matcher<GetResponse> documentContainField(String fieldName, Object fieldValue) {
return new GetResponseDocumentFieldValueMatcher(fieldName, fieldValue);
}
Expand Down