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

[Fleet] support force flag to add/remove package_policies #96713

Merged
merged 4 commits into from
Apr 12, 2021
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
12 changes: 7 additions & 5 deletions x-pack/plugins/fleet/server/routes/package_policy/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,20 @@ export const createPackagePolicyHandler: RequestHandler<
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const esClient = context.core.elasticsearch.client.asCurrentUser;
const user = (await appContextService.getSecurity()?.authc.getCurrentUser(request)) || undefined;
const user = appContextService.getSecurity()?.authc.getCurrentUser(request) || undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw this in VS code:
Screen Shot 2021-04-11 at 3 50 19 PM

Everything I found also indicates getCurrentUser is sync

getCurrentUser: (request: KibanaRequest) => AuthenticatedUser | null;
&
const getCurrentUser = (request: KibanaRequest) =>
http.auth.get<AuthenticatedUser>(request).state ?? null;

const { force, ...newPolicy } = request.body;
try {
const newData = await packagePolicyService.runExternalCallbacks(
'packagePolicyCreate',
{ ...request.body },
newPolicy,
context,
request
);

// Create package policy
const packagePolicy = await packagePolicyService.create(soClient, esClient, newData, {
user,
force,
});
const body: CreatePackagePolicyResponse = { item: packagePolicy };
return response.ok({
Expand All @@ -114,7 +116,7 @@ export const updatePackagePolicyHandler: RequestHandler<
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const esClient = context.core.elasticsearch.client.asCurrentUser;
const user = (await appContextService.getSecurity()?.authc.getCurrentUser(request)) || undefined;
const user = appContextService.getSecurity()?.authc.getCurrentUser(request) || undefined;
const packagePolicy = await packagePolicyService.get(soClient, request.params.packagePolicyId);

if (!packagePolicy) {
Expand Down Expand Up @@ -155,13 +157,13 @@ export const deletePackagePolicyHandler: RequestHandler<
> = async (context, request, response) => {
const soClient = context.core.savedObjects.client;
const esClient = context.core.elasticsearch.client.asCurrentUser;
const user = (await appContextService.getSecurity()?.authc.getCurrentUser(request)) || undefined;
const user = appContextService.getSecurity()?.authc.getCurrentUser(request) || undefined;
try {
const body: DeletePackagePoliciesResponse = await packagePolicyService.delete(
soClient,
esClient,
request.body.packagePolicyIds,
{ user }
{ user, force: request.body.force }
);
return response.ok({
body,
Expand Down
10 changes: 6 additions & 4 deletions x-pack/plugins/fleet/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,17 @@ class AgentPolicyService {
esClient: ElasticsearchClient,
id: string,
packagePolicyIds: string[],
options: { user?: AuthenticatedUser; bumpRevision: boolean } = { bumpRevision: true }
options: { user?: AuthenticatedUser; bumpRevision: boolean; force?: boolean } = {
bumpRevision: true,
}
): Promise<AgentPolicy> {
const oldAgentPolicy = await this.get(soClient, id, false);

if (!oldAgentPolicy) {
throw new Error('Agent policy not found');
}

if (oldAgentPolicy.is_managed) {
if (oldAgentPolicy.is_managed && !options?.force) {
throw new IngestManagerError(`Cannot update integrations of managed policy ${id}`);
}

Expand All @@ -497,15 +499,15 @@ class AgentPolicyService {
esClient: ElasticsearchClient,
id: string,
packagePolicyIds: string[],
options?: { user?: AuthenticatedUser }
options?: { user?: AuthenticatedUser; force?: boolean }
): Promise<AgentPolicy> {
const oldAgentPolicy = await this.get(soClient, id, false);

if (!oldAgentPolicy) {
throw new Error('Agent policy not found');
}

if (oldAgentPolicy.is_managed) {
if (oldAgentPolicy.is_managed && !options?.force) {
throw new IngestManagerError(`Cannot remove integrations of managed policy ${id}`);
}

Expand Down
14 changes: 9 additions & 5 deletions x-pack/plugins/fleet/server/services/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ class PackagePolicyService {
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
packagePolicy: NewPackagePolicy,
options?: { id?: string; user?: AuthenticatedUser; bumpRevision?: boolean }
options?: { id?: string; user?: AuthenticatedUser; bumpRevision?: boolean; force?: boolean }
): Promise<PackagePolicy> {
// Check that its agent policy does not have a package policy with the same name
const parentAgentPolicy = await agentPolicyService.get(soClient, packagePolicy.policy_id);
if (!parentAgentPolicy) {
throw new Error('Agent policy not found');
}
if (parentAgentPolicy.is_managed) {
if (parentAgentPolicy.is_managed && !options?.force) {
throw new IngestManagerError(
`Cannot add integrations to managed policy ${parentAgentPolicy.id}`
);
Expand All @@ -77,7 +77,9 @@ class PackagePolicyService {
(siblingPackagePolicy) => siblingPackagePolicy.name === packagePolicy.name
)
) {
throw new Error('There is already a package with the same name on this agent policy');
throw new IngestManagerError(
'There is already a package with the same name on this agent policy'
);
}

// Add ids to stream
Expand Down Expand Up @@ -106,7 +108,7 @@ class PackagePolicyService {
if (isPackageLimited(pkgInfo)) {
const agentPolicy = await agentPolicyService.get(soClient, packagePolicy.policy_id, true);
if (agentPolicy && doesAgentPolicyAlreadyIncludePackage(agentPolicy, pkgInfo.name)) {
throw new Error(
throw new IngestManagerError(
`Unable to create package policy. Package '${pkgInfo.name}' already exists on this agent policy.`
);
}
Expand Down Expand Up @@ -140,6 +142,7 @@ class PackagePolicyService {
{
user: options?.user,
bumpRevision: options?.bumpRevision ?? true,
force: options?.force,
}
);

Expand Down Expand Up @@ -367,7 +370,7 @@ class PackagePolicyService {
soClient: SavedObjectsClientContract,
esClient: ElasticsearchClient,
ids: string[],
options?: { user?: AuthenticatedUser; skipUnassignFromAgentPolicies?: boolean }
options?: { user?: AuthenticatedUser; skipUnassignFromAgentPolicies?: boolean; force?: boolean }
): Promise<DeletePackagePoliciesResponse> {
const result: DeletePackagePoliciesResponse = [];

Expand All @@ -385,6 +388,7 @@ class PackagePolicyService {
[packagePolicy.id],
{
user: options?.user,
force: options?.force,
}
);
}
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/fleet/server/types/models/package_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const PackagePolicyBaseSchema = {

export const NewPackagePolicySchema = schema.object({
...PackagePolicyBaseSchema,
force: schema.maybe(schema.boolean()),
});

export const UpdatePackagePolicySchema = schema.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@ export const UpdatePackagePolicyRequestSchema = {
export const DeletePackagePoliciesRequestSchema = {
body: schema.object({
packagePolicyIds: schema.arrayOf(schema.string()),
force: schema.maybe(schema.boolean()),
}),
};
Loading