Related to: https://github.com/fleetdm/fleet/issues/41908 Changes: - Updated Android proxy endpoints (`create-android-enrollment-token`, `create-enterprise-webapp`, `delete-android-device`, `get-android-device`, `get-android-devices`, `get-enterprise-applications`, `issue-command-on-android-device`, `modify-android-device`, `modify-android-policies`, and `modify-enterprise-app-policy`) to send requests to the Android Management API without verifying that an enterprise is still managed by Fleet with the getIsEnterpriseManagedByFleet helper, and to return a `enterpriseNotAccessible` (notFound) response to Fleet servers if the Android management API returns a 403 response. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved Android management flows to handle authorization failures more consistently. * Requests now return a clearer “not accessible” response when access to an Android enterprise is denied. * Several Android device, app, policy, and enterprise actions now surface this response instead of falling back to generic errors. * Streamlined Android enterprise operations by removing an extra pre-check, letting the API response determine the final outcome. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
95 lines
4.1 KiB
JavaScript
Vendored
95 lines
4.1 KiB
JavaScript
Vendored
module.exports = {
|
|
|
|
|
|
friendlyName: 'Create android enrollment token',
|
|
|
|
|
|
description: 'Creates and returns an enrollment token for an Android enterprise',
|
|
|
|
|
|
inputs: {
|
|
androidEnterpriseId: {
|
|
type: 'string',
|
|
required: true,
|
|
},
|
|
},
|
|
|
|
|
|
exits: {
|
|
success: { description: 'An Android Enterprise enrollment token was returned to the Fleet instance.'},
|
|
missingAuthHeader: { description: 'This request was missing an authorization header.', responseType: 'unauthorized'},
|
|
unauthorized: { description: 'Invalid authentication token', responseType: 'unauthorized'},
|
|
notFound: { description: 'No Android enterprise found for this Fleet server.', responseType: 'notFound'},
|
|
enterpriseNotAccessible: { description: 'Fleet is not authorized to manage this Android enterprise.', responseType: 'notFound' },
|
|
managementApiError: { statusCode: 503, description: 'The Android management API returned a transient 5xx error.' },
|
|
},
|
|
|
|
|
|
fn: async function ({androidEnterpriseId}) {
|
|
// Extract fleetServerSecret from the Authorization header
|
|
let authHeader = this.req.get('authorization');
|
|
let fleetServerSecret;
|
|
|
|
if (authHeader && authHeader.startsWith('Bearer')) {
|
|
fleetServerSecret = authHeader.replace('Bearer', '').trim();
|
|
} else {
|
|
throw 'missingAuthHeader';
|
|
}
|
|
|
|
// Authenticate this request
|
|
let thisAndroidEnterprise = await AndroidEnterprise.findOne({
|
|
androidEnterpriseId: androidEnterpriseId,
|
|
});
|
|
|
|
// Return a 404 response if no records are found.
|
|
if(!thisAndroidEnterprise) {
|
|
throw 'notFound';
|
|
}
|
|
|
|
// Return an unauthorized response if the provided secret does not match.
|
|
if(thisAndroidEnterprise.fleetServerSecret !== fleetServerSecret) {
|
|
throw 'unauthorized';
|
|
}
|
|
|
|
let newEnrollmentToken = await sails.helpers.flow.build(async ()=>{
|
|
let { google } = require('googleapis');
|
|
let androidmanagement = google.androidmanagement('v1');
|
|
let googleAuth = new google.auth.GoogleAuth({
|
|
scopes: ['https://www.googleapis.com/auth/androidmanagement'],
|
|
credentials: {
|
|
client_email: sails.config.custom.androidEnterpriseServiceAccountEmailAddress,// eslint-disable-line camelcase
|
|
private_key: sails.config.custom.androidEnterpriseServiceAccountPrivateKey,// eslint-disable-line camelcase
|
|
},
|
|
});
|
|
// Acquire the google auth client, and bind it to all future calls
|
|
let authClient = await googleAuth.getClient();
|
|
google.options({auth: authClient});
|
|
// [?]: https://googleapis.dev/nodejs/googleapis/latest/androidmanagement/classes/Resource$Enterprises$Enrollmenttokens.html#create
|
|
let enrollmentTokenCreateResponse = await androidmanagement.enterprises.enrollmentTokens.create({
|
|
parent: `enterprises/${androidEnterpriseId}`,
|
|
// Note: Typically, we use defined inputs instead of accessing req.body directly. This behavior should not be repeated in future Android proxy endpoints.
|
|
requestBody: this.req.body,
|
|
});
|
|
return enrollmentTokenCreateResponse.data;
|
|
}).intercept({status: 429}, (err)=>{
|
|
// If the Android management API returns a 429 response, log an additional warning that will trigger a help-p1 alert.
|
|
sails.log.warn(`p1: Android management API rate limit exceeded!`);
|
|
return new Error(`When attempting to create an enrollment token for an Android enterprise (${androidEnterpriseId}), an error occurred. Error: ${err}`);
|
|
}).intercept({status: 403}, ()=>{
|
|
// If the Android management API returns a 403 response, return a enterpriseNotAccessible (notFound) response to the Fleet server.
|
|
return {'enterpriseNotAccessible': 'Fleet is not authorized to manage this Android enterprise.'};
|
|
}).intercept((err)=>{
|
|
if([502, 503, 504].includes(err.status)) {
|
|
return {'managementApiError': `The Android management API returned a transient 5xx error: ${err}`};
|
|
}
|
|
return new Error(`When attempting to create an enrollment token for an Android enterprise (${androidEnterpriseId}), an error occurred. Error: ${require('util').inspect(err)}`);
|
|
});
|
|
|
|
|
|
return newEnrollmentToken;
|
|
|
|
}
|
|
|
|
|
|
};
|