From 94379a67015a453fee806e2c2e2f55fa1d515c1b Mon Sep 17 00:00:00 2001
From: Joe Portner <5295965+jportner@users.noreply.github.com>
Date: Tue, 31 Aug 2021 12:46:59 -0400
Subject: [PATCH] Add session cleanup usage data

---
 .../usage_collector/security_usage_collector.test.ts  |  9 ++++++---
 .../usage_collector/security_usage_collector.ts       | 11 +++++++++++
 .../schema/xpack_plugins.json                         |  6 ++++++
 3 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/x-pack/plugins/security/server/usage_collector/security_usage_collector.test.ts b/x-pack/plugins/security/server/usage_collector/security_usage_collector.test.ts
index c9e1cf6915bba..0515a1e1969bf 100644
--- a/x-pack/plugins/security/server/usage_collector/security_usage_collector.test.ts
+++ b/x-pack/plugins/security/server/usage_collector/security_usage_collector.test.ts
@@ -49,6 +49,7 @@ describe('Security UsageCollector', () => {
     httpAuthSchemes: ['apikey'],
     sessionIdleTimeoutInMinutes: 60,
     sessionLifespanInMinutes: 43200,
+    sessionCleanupInMinutes: 60,
   };
 
   describe('initialization', () => {
@@ -108,6 +109,7 @@ describe('Security UsageCollector', () => {
       httpAuthSchemes: [],
       sessionIdleTimeoutInMinutes: 0,
       sessionLifespanInMinutes: 0,
+      sessionCleanupInMinutes: 0,
     });
   });
 
@@ -471,12 +473,12 @@ describe('Security UsageCollector', () => {
     });
   });
 
-  describe('session expirations', () => {
+  describe('session', () => {
     // Note: can't easily test deprecated 'sessionTimeout' value here because of the way that config deprecation renaming works
-    it('reports customized session idleTimeout and lifespan', async () => {
+    it('reports customized session idleTimeout, lifespan, and cleanupInterval', async () => {
       const config = createSecurityConfig(
         ConfigSchema.validate({
-          session: { idleTimeout: '123m', lifespan: '456m' },
+          session: { idleTimeout: '123m', lifespan: '456m', cleanupInterval: '789m' },
         })
       );
       const usageCollection = usageCollectionPluginMock.createSetupContract();
@@ -491,6 +493,7 @@ describe('Security UsageCollector', () => {
         ...DEFAULT_USAGE,
         sessionIdleTimeoutInMinutes: 123,
         sessionLifespanInMinutes: 456,
+        sessionCleanupInMinutes: 789,
       });
     });
   });
diff --git a/x-pack/plugins/security/server/usage_collector/security_usage_collector.ts b/x-pack/plugins/security/server/usage_collector/security_usage_collector.ts
index 5703969c4df79..b09866ee47d2e 100644
--- a/x-pack/plugins/security/server/usage_collector/security_usage_collector.ts
+++ b/x-pack/plugins/security/server/usage_collector/security_usage_collector.ts
@@ -20,6 +20,7 @@ interface Usage {
   httpAuthSchemes: string[];
   sessionIdleTimeoutInMinutes: number;
   sessionLifespanInMinutes: number;
+  sessionCleanupInMinutes: number;
 }
 
 interface Deps {
@@ -122,6 +123,13 @@ export function registerSecurityUsageCollector({ usageCollection, config, licens
             'The global session lifespan expiration that is configured, in minutes (0 if disabled).',
         },
       },
+      sessionCleanupInMinutes: {
+        type: 'long',
+        _meta: {
+          description:
+            'The session cleanup interval that is configured, in minutes (0 if disabled).',
+        },
+      },
     },
     fetch: () => {
       const {
@@ -140,6 +148,7 @@ export function registerSecurityUsageCollector({ usageCollection, config, licens
           httpAuthSchemes: [],
           sessionIdleTimeoutInMinutes: 0,
           sessionLifespanInMinutes: 0,
+          sessionCleanupInMinutes: 0,
         };
       }
 
@@ -175,6 +184,7 @@ export function registerSecurityUsageCollector({ usageCollection, config, licens
       const sessionExpirations = config.session.getExpirationTimeouts(); // get global expiration values
       const sessionIdleTimeoutInMinutes = sessionExpirations.idleTimeout?.asMinutes() ?? 0;
       const sessionLifespanInMinutes = sessionExpirations.lifespan?.asMinutes() ?? 0;
+      const sessionCleanupInMinutes = config.session.cleanupInterval?.asMinutes() ?? 0;
 
       return {
         auditLoggingEnabled: legacyAuditLoggingEnabled || ecsAuditLoggingEnabled,
@@ -186,6 +196,7 @@ export function registerSecurityUsageCollector({ usageCollection, config, licens
         httpAuthSchemes,
         sessionIdleTimeoutInMinutes,
         sessionLifespanInMinutes,
+        sessionCleanupInMinutes,
       };
     },
   });
diff --git a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json
index 10c84ecc06d5a..38e74e15f7ae7 100644
--- a/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json
+++ b/x-pack/plugins/telemetry_collection_xpack/schema/xpack_plugins.json
@@ -5508,6 +5508,12 @@
           "_meta": {
             "description": "The global session lifespan expiration that is configured, in minutes (0 if disabled)."
           }
+        },
+        "sessionCleanupInMinutes": {
+          "type": "long",
+          "_meta": {
+            "description": "The session cleanup interval that is configured, in minutes (0 if disabled)."
+          }
         }
       }
     },