2024-08-22 20:59:15 +00:00
module . exports = {
friendlyName : 'Get profiles' ,
description : 'Builds and returns an array of deployed configuration profiles on the Fleet instance and undeployed profiles stored in the dashboard\'s datastore.' ,
exits : {
success : {
outputType : [ { } ] ,
}
} ,
2024-12-05 23:39:47 +00:00
2024-08-22 20:59:15 +00:00
fn : async function ( ) {
2024-12-05 23:39:47 +00:00
2024-08-22 20:59:15 +00:00
// Get all teams on the Fleet instance.
let teamsResponseData = await sails . helpers . http . get . with ( {
url : '/api/v1/fleet/teams' ,
baseUrl : sails . config . custom . fleetBaseUrl ,
headers : {
Authorization : ` Bearer ${ sails . config . custom . fleetApiToken } `
}
} )
. timeout ( 120000 )
. retry ( [ 'requestFailed' , { name : 'TimeoutError' } ] ) ;
let allTeams = teamsResponseData . teams ;
2025-01-14 18:09:23 +00:00
let teams = allTeams . map ( ( team ) => {
return {
2024-08-22 20:59:15 +00:00
fleetApid : team . id ,
2025-01-14 18:09:23 +00:00
teamName : team . name
} ;
} ) ;
2024-08-22 20:59:15 +00:00
// Add the "team" for hosts with no team
teams . push ( {
fleetApid : 0 ,
teamName : 'No team' ,
} ) ;
let allProfiles = [ ] ;
let teamApids = _ . pluck ( allTeams , 'id' ) ;
// Get all of the configuration profiles on the Fleet instance.
2025-01-14 18:09:23 +00:00
await sails . helpers . flow . simultaneouslyForEach ( teamApids , async ( teamApid ) => {
2024-08-22 20:59:15 +00:00
let configurationProfilesResponseData = await sails . helpers . http . get . with ( {
url : ` /api/v1/fleet/configuration_profiles?team_id= ${ teamApid } ` ,
baseUrl : sails . config . custom . fleetBaseUrl ,
headers : {
Authorization : ` Bearer ${ sails . config . custom . fleetApiToken } `
}
} )
. timeout ( 120000 )
. retry ( [ 'requestFailed' , { name : 'TimeoutError' } ] ) ;
let profilesForThisTeam = configurationProfilesResponseData . profiles ;
allProfiles = allProfiles . concat ( profilesForThisTeam ) ;
2025-01-14 18:09:23 +00:00
} ) ;
2024-08-22 20:59:15 +00:00
// Add the configurations profiles that are assigned to the "no team" team.
let noTeamConfigurationProfilesResponseData = await sails . helpers . http . get . with ( {
url : '/api/v1/fleet/configuration_profiles' ,
baseUrl : sails . config . custom . fleetBaseUrl ,
headers : {
Authorization : ` Bearer ${ sails . config . custom . fleetApiToken } `
}
} )
. timeout ( 120000 )
. retry ( [ 'requestFailed' , { name : 'TimeoutError' } ] ) ;
2024-12-05 23:39:47 +00:00
let profilesForNoTeam = noTeamConfigurationProfilesResponseData . profiles ;
allProfiles = allProfiles . concat ( profilesForNoTeam ) ;
let profilesInformation = [ ] ;
2025-01-14 18:09:23 +00:00
await sails . helpers . flow . simultaneouslyForEach ( allProfiles , async ( profile ) => {
2024-08-22 20:59:15 +00:00
let profileInformation = {
name : profile . name ,
2024-12-05 23:39:47 +00:00
identifier : profile . identifier ,
2024-08-22 20:59:15 +00:00
platform : profile . platform ,
createdAt : new Date ( profile . created _at ) . getTime ( ) ,
2024-12-05 23:39:47 +00:00
team : {
uuid : profile . profile _uuid ,
fleetApid : profile . team _id ,
teamName : _ . find ( teams , { fleetApid : profile . team _id } ) . teamName ,
} ,
profileTarget : 'all' ,
2024-08-22 20:59:15 +00:00
} ;
2024-12-05 23:39:47 +00:00
if ( profile . labels _include _all ) {
profileInformation . labels = _ . pluck ( profile . labels _include _all , 'name' ) ;
profileInformation . profileTarget = 'custom' ;
profileInformation . labelTargetBehavior = 'include' ;
} else if ( profile . labels _exclude _any ) {
profileInformation . labels = _ . pluck ( profile . labels _exclude _any , 'name' ) ;
profileInformation . profileTarget = 'custom' ;
profileInformation . labelTargetBehavior = 'exclude' ;
}
profilesInformation . push ( profileInformation ) ;
2025-01-14 18:09:23 +00:00
} ) ;
2024-12-05 23:39:47 +00:00
// Group the profiles based on identifier, labels, and labelTargetBehavior
let profilesGroupedbyLabelsAndIdentifier = _ . groupBy ( profilesInformation , ( profile ) => {
return ` ${ profile . identifier } | ${ JSON . stringify ( profile . labels ) } | ${ profile . labelTargetBehavior } ` ;
} ) ;
// map the grouped profiles and merge profiles that have the same labels, target behavior, and identifier.
let allProfilesOnFleetInstance = Object . values ( profilesGroupedbyLabelsAndIdentifier ) . map ( profileGroup => {
return {
... profileGroup [ 0 ] , // Expand the first item in the profileGroup
teams : profileGroup . map ( item => item . team ) // Merge the teams arrays
} ;
} ) ;
2024-08-22 20:59:15 +00:00
// Get the undeployed profiles from the app's database.
let undeployedProfiles = await UndeployedProfile . find ( ) ;
2024-12-05 23:39:47 +00:00
allProfilesOnFleetInstance = _ . union ( allProfilesOnFleetInstance , undeployedProfiles ) ;
2024-08-22 20:59:15 +00:00
// Sort profiles by their name.
2024-12-05 23:39:47 +00:00
allProfilesOnFleetInstance = _ . sortByOrder ( allProfilesOnFleetInstance , 'name' , 'asc' ) ;
2024-08-22 20:59:15 +00:00
2024-12-05 23:39:47 +00:00
// return the updated list of profiles
return allProfilesOnFleetInstance ;
2024-08-22 20:59:15 +00:00
}
} ;