Related to: https://github.com/fleetdm/fleet/issues/49212 Changes: - Updated the custom hook to create `sails.androidProxyApiRequestCount`, and to log and reset the value every minute - Updated android proxy endpoints to increment `sails.androidProxyApiRequestCount` every time a request to the Android management API is sent <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Monitoring** * Added comprehensive tracking for Android Management API requests across enrollment, enterprise, device, application, policy, and command operations. * Added periodic request-count logging and automatic resets when Android Enterprise credentials are configured. * Improved reporting alignment with minute-based API limits while keeping logs quiet during periods without requests. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
77 lines
2.5 KiB
JavaScript
Vendored
77 lines
2.5 KiB
JavaScript
Vendored
module.exports = {
|
|
|
|
|
|
friendlyName: 'Get is enterprise managed by fleet',
|
|
|
|
|
|
description: 'Checks the list of Android Enterprises managed by Fleet\'s Enterprise Management Google project and returns true if the provided enterprise ID is present.',
|
|
|
|
|
|
inputs: {
|
|
androidEnterpriseId: {
|
|
type: 'string',
|
|
required: true,
|
|
description: 'The enterprise ID of the Android Enterprise '
|
|
}
|
|
},
|
|
|
|
|
|
exits: {
|
|
|
|
success: {
|
|
outputFriendlyName: 'Is enterprise managed by fleet',
|
|
outputType: 'boolean',
|
|
},
|
|
},
|
|
|
|
|
|
fn: async function ({androidEnterpriseId}) {
|
|
|
|
require('assert')(sails.config.custom.androidEnterpriseServiceAccountEmailAddress);
|
|
require('assert')(sails.config.custom.androidEnterpriseServiceAccountPrivateKey);
|
|
require('assert')(sails.config.custom.androidEnterpriseProjectId);
|
|
|
|
let isEnterpriseManagedByFleet = false;
|
|
|
|
|
|
// Get the shared Google API auth client with the getAndroidManagementAuthorizationClient helper
|
|
let androidManagementAuthClient = await sails.helpers.androidProxy.getAndroidManagementAuthorizationClient();
|
|
|
|
let { google } = require('googleapis');
|
|
let androidManagementConnection = google.androidmanagement({version: 'v1', auth: androidManagementAuthClient});
|
|
|
|
// Use Google's LIST call to check if enterprise exists.
|
|
let enterprises = [];
|
|
let tokenForNextPageOfEnterprises;
|
|
await sails.helpers.flow.until(async ()=>{
|
|
sails.androidProxyApiRequestCount++;// Count this Android Management API request toward the per-minute total logged in api/hooks/custom/index.js.
|
|
let listEnterprisesResponse = await androidManagementConnection.enterprises.list({
|
|
projectId: sails.config.custom.androidEnterpriseProjectId,
|
|
pageSize: 100,
|
|
pageToken: tokenForNextPageOfEnterprises,
|
|
});
|
|
tokenForNextPageOfEnterprises = listEnterprisesResponse.data.nextPageToken;
|
|
enterprises = enterprises.concat(listEnterprisesResponse.data.enterprises);
|
|
|
|
if(!listEnterprisesResponse.data.nextPageToken){
|
|
return true;
|
|
}
|
|
});
|
|
|
|
// Check the list of enterprises
|
|
let enterpriseExistsInTheListOfEnterprises = _.find(enterprises, (enterprise)=>{
|
|
return enterprise.name === `enterprises/${androidEnterpriseId}` || enterprise.name === androidEnterpriseId;
|
|
});
|
|
|
|
if(enterpriseExistsInTheListOfEnterprises){
|
|
isEnterpriseManagedByFleet = true;
|
|
}
|
|
|
|
// Send back the result through the success exit.
|
|
return isEnterpriseManagedByFleet;
|
|
}
|
|
|
|
|
|
};
|
|
|