Skip to content

Commit

Permalink
[Search Connectors] - Remove duplicate agent policy when connector is…
Browse files Browse the repository at this point in the history
… created as an integration (elastic#208123)

## Summary

With Agentless Connectors, users can create a connector without a
`connector_id` or a `connector_name`, so when a policy is created
without them, they won't be skipped anymore.

The connectors do end up getting their `connector_id `updated to be the
policy's `package_policy_id`, so when the check is done whether or not
to deploy native connectors and create a policy, there's an an
additional check for whether `connector_id` is the `package_policy_id`.

- Closes
https://github.com/orgs/elastic/projects/740/views/58?pane=issue&itemId=94923696&issue=elastic%7Csearch-team%7C9164
- Closes
https://github.com/orgs/elastic/projects/740/views/58?pane=issue&itemId=94923696&issue=elastic%7Csearch-team%7C9164

### Checklist

Check the PR satisfies following conditions.

Reviewers should verify this PR satisfies this list as well.

- [ ] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/src/platform/packages/shared/kbn-i18n/README.md)
- [ ]
[Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html)
was added for features that require explanation or tutorials
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [ ] If a plugin configuration key changed, check if it needs to be
allowlisted in the cloud and added to the [docker
list](https://github.com/elastic/kibana/blob/main/src/dev/build/tasks/os_packages/docker_generator/resources/base/bin/kibana-docker)
- [ ] This was checked for breaking HTTP API changes, and any breaking
changes have been approved by the breaking-change committee. The
`release_note:breaking` label should be applied in these situations.
- [ ] [Flaky Test
Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was
used on any tests changed
- [ ] The PR description includes the appropriate Release Notes section,
and the correct `release_note:*` label is applied per the
[guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

### Identify risks

Does this PR introduce any risks? For example, consider risks like hard
to test bugs, performance regression, potential of data loss.

Describe the risk, its severity, and mitigation for each identified
risk. Invite stakeholders and evaluate how to proceed before merging.

- [ ] [See some risk
examples](https://github.com/elastic/kibana/blob/main/RISK_MATRIX.mdx)
- [ ] ...

---------

Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
(cherry picked from commit 7ee6c3f)
  • Loading branch information
meghanmurphy1 committed Feb 3, 2025
1 parent e30388c commit 626777e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ describe('AgentlessConnectorsInfraService', () => {
expect(policies[1].agent_policy_ids).toBe(thirdPackagePolicy.policy_ids);
});

test('Skips policies that have missing fields', async () => {
test('Returns policies that have missing connector_id and connector_name but not service_type', async () => {
const firstPackagePolicy = createPackagePolicyMock();
firstPackagePolicy.id = 'this-is-package-policy-id';
firstPackagePolicy.policy_ids = ['this-is-agent-policy-id'];
Expand All @@ -325,13 +325,28 @@ describe('AgentlessConnectorsInfraService', () => {
} as PackagePolicyInput,
];

const thirdPackagePolicy = createPackagePolicyMock();
thirdPackagePolicy.inputs = [
{
type: 'connectors-py',
compiled_input: {
connector_id: '000002',
service_type: 'github',
},
} as PackagePolicyInput,
];

packagePolicyService.fetchAllItems.mockResolvedValue(
getMockPolicyFetchAllItems([[firstPackagePolicy], [secondPackagePolicy]])
getMockPolicyFetchAllItems([
[firstPackagePolicy],
[secondPackagePolicy],
[thirdPackagePolicy],
])
);

const policies = await service.getConnectorPackagePolicies();

expect(policies.length).toBe(0);
expect(policies.length).toBe(2);
});
});
describe('deployConnector', () => {
Expand Down Expand Up @@ -579,6 +594,20 @@ describe('module', () => {
is_deleted: false,
};

const confluenceConnector: ConnectorMetadata = {
id: '000004',
name: 'Confluence Connector',
service_type: 'confluence',
is_deleted: false,
};

const confluenceConnectorEmptySettings: ConnectorMetadata = {
id: '',
name: '',
service_type: 'confluence',
is_deleted: false,
};

const deleted = (connector: ConnectorMetadata): ConnectorMetadata => {
return {
id: connector.id,
Expand Down Expand Up @@ -606,6 +635,12 @@ describe('module', () => {
connector_settings: mysqlConnector,
};

const confluencePackagePolicy: PackagePolicyMetadata = {
package_policy_id: '000004',
agent_policy_ids: [],
connector_settings: confluenceConnectorEmptySettings,
};

describe('getPoliciesToDelete', () => {
test('Returns one policy if connector has been soft-deleted', async () => {
const policiesToDelete = getPoliciesToDelete(
Expand Down Expand Up @@ -688,5 +723,14 @@ describe('module', () => {
expect(missingConnectors).toContain(sharepointConnector);
expect(missingConnectors).toContain(mysqlConnector);
});

test('Returns none if Policy is created without a connector_id or connector_name', async () => {
const missingConnectors = getConnectorsToDeploy(
[githubPackagePolicy, sharepointPackagePolicy, mysqlPackagePolicy, confluencePackagePolicy],
[githubConnector, sharepointConnector, mysqlConnector, confluenceConnector]
);

expect(missingConnectors.length).toBe(0);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export class AgentlessConnectorsInfraService {
}

if (input.compiled_input.connector_id == null) {
this.logger.debug(`Policy ${policy.id} is missing connector_id, skipping`);
continue;
this.logger.debug(`Policy ${policy.id} is missing connector_id`);
// No need to skip, that's fine
}

if (input.compiled_input.connector_name == null) {
Expand Down Expand Up @@ -265,7 +265,11 @@ export const getConnectorsToDeploy = (

// If no package policies reference this connector by id then it should be deployed
if (
packagePolicies.every((packagePolicy) => packagePolicy.connector_settings.id !== connector.id)
packagePolicies.every(
(packagePolicy) =>
connector.id !== packagePolicy.connector_settings.id &&
connector.id !== packagePolicy.package_policy_id
)
) {
results.push(connector);
}
Expand Down

0 comments on commit 626777e

Please sign in to comment.