Skip to content

Commit

Permalink
[Fleet] Fleet server invalidate api keys for agent < 7.13 (#95789) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet authored Apr 1, 2021
1 parent 7a2d311 commit c32d4b6
Showing 1 changed file with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { listEnrollmentApiKeys, getEnrollmentAPIKey } from '../api_keys/enrollme
import { appContextService } from '../app_context';
import { isAgentsSetup } from '../agents';
import { agentPolicyService } from '../agent_policy';
import { invalidateAPIKeys } from '../api_keys';

export async function runFleetServerMigration() {
// If Agents are not setup skip as there is nothing to migrate
Expand Down Expand Up @@ -56,6 +57,7 @@ function getInternalUserSOClient() {
async function migrateAgents() {
const esClient = appContextService.getInternalUserESClient();
const soClient = getInternalUserSOClient();
const logger = appContextService.getLogger();
let hasMore = true;
while (hasMore) {
const res = await soClient.find({
Expand All @@ -75,11 +77,20 @@ async function migrateAgents() {
.getEncryptedSavedObjects()
.getDecryptedAsInternalUser<AgentSOAttributes>(AGENT_SAVED_OBJECT_TYPE, so.id);

await invalidateAPIKeys(
soClient,
[attributes.access_api_key_id, attributes.default_api_key_id].filter(
(keyId): keyId is string => keyId !== undefined
)
).catch((error) => {
logger.error(`Invalidating API keys for agent ${so.id} failed: ${error.message}`);
});

const body: FleetServerAgent = {
type: attributes.type,
active: attributes.active,
active: false,
enrolled_at: attributes.enrolled_at,
unenrolled_at: attributes.unenrolled_at,
unenrolled_at: new Date().toISOString(),
unenrollment_started_at: attributes.unenrollment_started_at,
upgraded_at: attributes.upgraded_at,
upgrade_started_at: attributes.upgrade_started_at,
Expand Down

0 comments on commit c32d4b6

Please sign in to comment.