Skip to content

Commit

Permalink
[Connector API] Fix bug with with wrong target index for access contr…
Browse files Browse the repository at this point in the history
…ol sync (elastic#109097)

(cherry picked from commit d9e6f54)

# Conflicts:
#	x-pack/plugin/ent-search/src/main/java/org/elasticsearch/xpack/application/connector/syncjob/ConnectorSyncJobIndexService.java
  • Loading branch information
jedrazb committed May 29, 2024
1 parent 62aa676 commit 31ecb8f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/109097.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 109097
summary: "[Connector API] Fix bug with with wrong target index for access control\
\ sync"
area: Application
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,27 @@ setup:
- exists: created_at
- exists: last_seen

---
'Create access control sync job - expect prefixed connector index name':
- do:
connector.sync_job_post:
body:
id: test-connector
job_type: access_control

- set: { id: id }

- match: { id: $id }

- do:
connector.sync_job_get:
connector_sync_job_id: $id

- match: { connector.id: test-connector }
- match: { job_type: access_control }
- match: { connector.index_name: .search-acl-filter-search-test }


---
'Create connector sync job with non-existing connector id':
- do:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.util.Objects;

import static org.elasticsearch.xcontent.ConstructingObjectParser.optionalConstructorArg;
import static org.elasticsearch.xpack.application.connector.ConnectorTemplateRegistry.ACCESS_CONTROL_INDEX_PREFIX;

/**
* Represents a Connector in the Elasticsearch ecosystem. Connectors are used for integrating
Expand Down Expand Up @@ -269,6 +270,10 @@ public Connector(StreamInput in) throws IOException {
}
);

public String getAccessControlIndexName() {
return ACCESS_CONTROL_INDEX_PREFIX + this.indexName;
}

static {
PARSER.declareStringOrNull(optionalConstructorArg(), API_KEY_ID_FIELD);
PARSER.declareStringOrNull(optionalConstructorArg(), API_KEY_SECRET_ID_FIELD);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ConnectorTemplateRegistry extends IndexTemplateRegistry {
public static final String CONNECTOR_SYNC_JOBS_INDEX_NAME_PATTERN = ".elastic-connectors-sync-jobs-v1";
public static final String CONNECTOR_SYNC_JOBS_TEMPLATE_NAME = "elastic-connectors-sync-jobs";

public static final String ACCESS_CONTROL_INDEX_PREFIX = ".search-acl-filter-";
public static final String ACCESS_CONTROL_INDEX_NAME_PATTERN = ".search-acl-filter-*";
public static final String ACCESS_CONTROL_TEMPLATE_NAME = "search-acl-filter";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ public void createConnectorSyncJob(
ActionListener<PostConnectorSyncJobAction.Response> listener
) {
String connectorId = request.getId();
ConnectorSyncJobType jobType = Objects.requireNonNullElse(request.getJobType(), ConnectorSyncJob.DEFAULT_JOB_TYPE);
try {
getSyncJobConnectorInfo(connectorId, listener.delegateFailure((l, connector) -> {

getSyncJobConnectorInfo(connectorId, jobType, listener.delegateFailure((l, connector) -> {
if (Strings.isNullOrEmpty(connector.getIndexName())) {
l.onFailure(
new ElasticsearchStatusException(
Expand All @@ -113,7 +113,6 @@ public void createConnectorSyncJob(
}

Instant now = Instant.now();
ConnectorSyncJobType jobType = Objects.requireNonNullElse(request.getJobType(), ConnectorSyncJob.DEFAULT_JOB_TYPE);
ConnectorSyncJobTriggerMethod triggerMethod = Objects.requireNonNullElse(
request.getTriggerMethod(),
ConnectorSyncJob.DEFAULT_TRIGGER_METHOD
Expand Down Expand Up @@ -482,7 +481,7 @@ private ConnectorSyncStatus getConnectorSyncJobStatusFromSearchResult(ConnectorS
);
}

private void getSyncJobConnectorInfo(String connectorId, ActionListener<Connector> listener) {
private void getSyncJobConnectorInfo(String connectorId, ConnectorSyncJobType jobType, ActionListener<Connector> listener) {
try {

final GetRequest request = new GetRequest(ConnectorIndexService.CONNECTOR_INDEX_NAME, connectorId);
Expand All @@ -502,11 +501,15 @@ public void onResponse(GetResponse response) {
connectorId,
XContentType.JSON
);
// Access control syncs write data to a separate index
String targetIndexName = jobType == ConnectorSyncJobType.ACCESS_CONTROL
? connector.getAccessControlIndexName()
: connector.getIndexName();

// Build the connector representation for sync job
final Connector syncJobConnector = new Connector.Builder().setConnectorId(connector.getConnectorId())
.setSyncJobFiltering(transformConnectorFilteringToSyncJobRepresentation(connector.getFiltering()))
.setIndexName(connector.getIndexName())
.setIndexName(targetIndexName)
.setLanguage(connector.getLanguage())
.setPipeline(connector.getPipeline())
.setServiceType(connector.getServiceType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import java.util.stream.Collectors;

import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.xpack.application.connector.ConnectorTemplateRegistry.ACCESS_CONTROL_INDEX_PREFIX;
import static org.elasticsearch.xpack.application.connector.ConnectorTestUtils.registerSimplifiedConnectorIndexTemplates;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -129,6 +130,18 @@ public void testCreateConnectorSyncJob() throws Exception {
assertThat(connectorSyncJob.getDeletedDocumentCount(), equalTo(0L));
}

public void testCreateConnectorSyncJob_WithAccessControlJobType_IndexIsPrefixed() throws Exception {
PostConnectorSyncJobAction.Request createAccessControlJobRequest = ConnectorSyncJobTestUtils
.getRandomPostConnectorSyncJobActionRequest(connectorOneId, ConnectorSyncJobType.ACCESS_CONTROL);

PostConnectorSyncJobAction.Response createAccessControlJobResponse = awaitPutConnectorSyncJob(createAccessControlJobRequest);

ConnectorSyncJob connectorSyncJob = awaitGetConnectorSyncJob(createAccessControlJobResponse.getId());

assertThat(connectorSyncJob.getJobType(), equalTo(ConnectorSyncJobType.ACCESS_CONTROL));
assertTrue(connectorSyncJob.getConnector().getIndexName().startsWith(ACCESS_CONTROL_INDEX_PREFIX));
}

public void testCreateConnectorSyncJob_WithMissingJobType_ExpectDefaultJobTypeToBeSet() throws Exception {
PostConnectorSyncJobAction.Request syncJobRequest = new PostConnectorSyncJobAction.Request(
connectorOneId,
Expand Down

0 comments on commit 31ecb8f

Please sign in to comment.