forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use updated onPreAuth from Platform (elastic#71552)
* Use updated onPreAuth from Platform * Add config flag. Increase default value. * Set max connections flag default to 0 (disabled) * Don't use limiting logic on checkin route * Confirm preAuth handler only added when max > 0 Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
c89f3f8
commit 5af7d21
Showing
9 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
x-pack/plugins/ingest_manager/server/routes/limited_concurrency.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { coreMock } from 'src/core/server/mocks'; | ||
import { registerLimitedConcurrencyRoutes } from './limited_concurrency'; | ||
import { IngestManagerConfigType } from '../index'; | ||
|
||
describe('registerLimitedConcurrencyRoutes', () => { | ||
test(`doesn't call registerOnPreAuth if maxConcurrentConnections is 0`, async () => { | ||
const mockSetup = coreMock.createSetup(); | ||
const mockConfig = { fleet: { maxConcurrentConnections: 0 } } as IngestManagerConfigType; | ||
registerLimitedConcurrencyRoutes(mockSetup, mockConfig); | ||
|
||
expect(mockSetup.http.registerOnPreAuth).not.toHaveBeenCalled(); | ||
}); | ||
|
||
test(`calls registerOnPreAuth once if maxConcurrentConnections is 1`, async () => { | ||
const mockSetup = coreMock.createSetup(); | ||
const mockConfig = { fleet: { maxConcurrentConnections: 1 } } as IngestManagerConfigType; | ||
registerLimitedConcurrencyRoutes(mockSetup, mockConfig); | ||
|
||
expect(mockSetup.http.registerOnPreAuth).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
test(`calls registerOnPreAuth once if maxConcurrentConnections is 1000`, async () => { | ||
const mockSetup = coreMock.createSetup(); | ||
const mockConfig = { fleet: { maxConcurrentConnections: 1000 } } as IngestManagerConfigType; | ||
registerLimitedConcurrencyRoutes(mockSetup, mockConfig); | ||
|
||
expect(mockSetup.http.registerOnPreAuth).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
72 changes: 72 additions & 0 deletions
72
x-pack/plugins/ingest_manager/server/routes/limited_concurrency.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { | ||
CoreSetup, | ||
KibanaRequest, | ||
LifecycleResponseFactory, | ||
OnPreAuthToolkit, | ||
} from 'kibana/server'; | ||
import { LIMITED_CONCURRENCY_ROUTE_TAG } from '../../common'; | ||
import { IngestManagerConfigType } from '../index'; | ||
class MaxCounter { | ||
constructor(private readonly max: number = 1) {} | ||
private counter = 0; | ||
valueOf() { | ||
return this.counter; | ||
} | ||
increase() { | ||
if (this.counter < this.max) { | ||
this.counter += 1; | ||
} | ||
} | ||
decrease() { | ||
if (this.counter > 0) { | ||
this.counter -= 1; | ||
} | ||
} | ||
lessThanMax() { | ||
return this.counter < this.max; | ||
} | ||
} | ||
|
||
function shouldHandleRequest(request: KibanaRequest) { | ||
const tags = request.route.options.tags; | ||
return tags.includes(LIMITED_CONCURRENCY_ROUTE_TAG); | ||
} | ||
|
||
export function registerLimitedConcurrencyRoutes(core: CoreSetup, config: IngestManagerConfigType) { | ||
const max = config.fleet.maxConcurrentConnections; | ||
if (!max) return; | ||
|
||
const counter = new MaxCounter(max); | ||
core.http.registerOnPreAuth(function preAuthHandler( | ||
request: KibanaRequest, | ||
response: LifecycleResponseFactory, | ||
toolkit: OnPreAuthToolkit | ||
) { | ||
if (!shouldHandleRequest(request)) { | ||
return toolkit.next(); | ||
} | ||
|
||
if (!counter.lessThanMax()) { | ||
return response.customError({ | ||
body: 'Too Many Requests', | ||
statusCode: 429, | ||
}); | ||
} | ||
|
||
counter.increase(); | ||
|
||
// requests.events.aborted$ has a bug (but has test which explicitly verifies) where it's fired even when the request completes | ||
// https://github.com/elastic/kibana/pull/70495#issuecomment-656288766 | ||
request.events.aborted$.toPromise().then(() => { | ||
counter.decrease(); | ||
}); | ||
|
||
return toolkit.next(); | ||
}); | ||
} |