fleet/ee/bulk-operations-dashboard/api/controllers/profiles/upload-profile.js
Eric debb2d1790
Add app to manage scripts and profiles. (#21450)
Related to: #20296 

Changes:
- Added `ee/bulk-operations-dashboard`, a Sails.js app that lets users
manage configuration profiles and scripts across multiple teams on a
Fleet instance.
- Added a Github workflow to deploy the app to Heroku
- Added a Github workflow to test changes to the bulk operations
dashboard.
2024-08-22 14:59:15 -06:00

120 lines
3.3 KiB
JavaScript

module.exports = {
friendlyName: 'Upload profile',
description: '',
files: ['newProfile'],
inputs: {
newProfile: {
type: 'ref',
description: 'An Upstream with an incoming file upload.',
required: true,
},
teams: {
type: ['string'],
description: 'An array of team IDs that this profile will be added to'
}
},
exits: {
success: {
outputDescription: 'The new profile has been uploaded',
outputType: {},
},
noFileAttached: {
description: 'No file was attached.',
responseType: 'badRequest'
},
tooBig: {
description: 'The file is too big.',
responseType: 'badRequest'
},
},
fn: async function ({newProfile, teams}) {
let util = require('util');
let profile = await sails.reservoir(newProfile)
.intercept('E_EXCEEDS_UPLOAD_LIMIT', 'tooBig')
.intercept((err)=>new Error('The configuration profile upload failed. '+util.inspect(err)));
if(!profile) {
throw 'noFileAttached';
}
let profileContents = profile[0].contentBytes;
let profileFileName = profile[0].name;
let datelessExtensionlessFilename = profileFileName.replace(/^\d{4}-\d{2}-\d{2}_/, '').replace(/\.[^/.]+$/, '');
let extension = '.'+profileFileName.split('.').pop();
let profilePlatform = 'darwin';
if(_.endsWith(profileFileName, '.xml')) {
profilePlatform = 'windows';
}
let profileToReturn;
let newProfileInfo = {
name: datelessExtensionlessFilename,
platform: profilePlatform,
profileType: extension,
createdAt: Date.now(),
};
if(!teams) {
newProfileInfo.profileContents = profileContents;
profileToReturn = await UndeployedProfile.create(newProfileInfo).fetch();
} else {
let newTeams = [];
for(let teamApid of teams){
let newProfileResponse = await sails.helpers.http.sendHttpRequest.with({
method: 'POST',
baseUrl: sails.config.custom.fleetBaseUrl,
url: `/api/v1/fleet/configuration_profiles?team_id=${teamApid}`,
enctype: 'multipart/form-data',
body: {
team_id: teamApid,// eslint-disable-line camelcase
profile: {
options: {
filename: profileFileName,
contentType: 'application/octet-stream'
},
value: profileContents,
}
},
headers: {
Authorization: `Bearer ${sails.config.custom.fleetApiToken}`,
},
});
let parsedJsonResponse = JSON.parse(newProfileResponse.body);
let uuidForThisProfile = parsedJsonResponse.profile_uuid;
// send a request to the Fleet instance to get the bundleId of the new profile.
await sails.helpers.http.get.with({
baseUrl: sails.config.custom.fleetBaseUrl,
url: `/api/v1/fleet/configuration_profiles/${uuidForThisProfile}`,
headers: {
Authorization: `Bearer ${sails.config.custom.fleetApiToken}`,
}
});
newTeams.push({
fleetApid: teamApid,
uuid: JSON.parse(newProfileResponse.body).profile_uuid
});
}
newProfileInfo.teams = newTeams;
profileToReturn = newProfileInfo;
}
// All done.
return profileToReturn;
}
};