2021-05-10 04:48:59 +00:00
module . exports = {
friendlyName : 'View query detail' ,
description : 'Display "Query detail" page.' ,
2021-05-26 08:24:38 +00:00
inputs : {
slug : { type : 'string' , required : true , description : 'A slug uniquely identifying this query in the library.' , example : 'get-macos-disk-free-space-percentage' } ,
} ,
2021-05-10 04:48:59 +00:00
2021-05-26 08:24:38 +00:00
exits : {
success : { viewTemplatePath : 'pages/query-detail' } ,
notFound : { responseType : 'notFound' } ,
badConfig : { responseType : 'badConfig' } ,
2025-01-23 03:10:17 +00:00
redirectToPolicy : {
description : 'The requesting user has been redirected to a policy page.' ,
responseType : 'redirect'
} ,
2021-05-10 04:48:59 +00:00
} ,
2021-05-26 08:24:38 +00:00
fn : async function ( { slug } ) {
2021-05-10 04:48:59 +00:00
2021-05-26 08:24:38 +00:00
if ( ! _ . isObject ( sails . config . builtStaticContent ) || ! _ . isArray ( sails . config . builtStaticContent . queries ) ) {
throw { badConfig : 'builtStaticContent.queries' } ;
2025-01-23 03:10:17 +00:00
} else if ( ! _ . isObject ( sails . config . builtStaticContent ) || ! _ . isArray ( sails . config . builtStaticContent . policies ) ) {
throw { badConfig : 'builtStaticContent.policies' } ;
2021-05-26 08:24:38 +00:00
} else if ( ! _ . isString ( sails . config . builtStaticContent . queryLibraryYmlRepoPath ) ) {
throw { badConfig : 'builtStaticContent.queryLibraryYmlRepoPath' } ;
}
// Serve appropriate content for query.
// > Inspired by https://github.com/sailshq/sailsjs.com/blob/b53c6e6a90c9afdf89e5cae00b9c9dd3f391b0e7/api/controllers/documentation/view-documentation.js
2025-01-23 04:10:43 +00:00
let query = _ . find ( sails . config . builtStaticContent . queries , { kind : 'query' , slug : slug } ) ;
2021-05-26 08:24:38 +00:00
if ( ! query ) {
2025-01-23 03:10:17 +00:00
// If we didn't find a query matching this slug, check to see if there is a policy with a matching slug.
// Note: We do this because policies used to be on /queries/* pages. This way, all old URLs that policies used to live at will still bring users to the correct page.
let policyWithThisSlug = _ . find ( sails . config . builtStaticContent . policies , { kind : 'policy' , slug : slug } ) ;
if ( policyWithThisSlug ) {
// If we foudn a matchign policy, redirect the user.
2025-01-23 03:41:16 +00:00
throw { redirectToPolicy : ` /policies/ ${ policyWithThisSlug . slug } ` } ;
2025-01-23 03:10:17 +00:00
} else {
throw 'notFound' ;
}
2021-05-26 08:24:38 +00:00
}
2021-05-20 08:22:42 +00:00
2024-11-07 21:49:16 +00:00
// Find the related osquery table documentation for tables used in this query, and grab the keywordsForSyntaxHighlighting from each table used.
let allTablesInformation = _ . filter ( sails . config . builtStaticContent . markdownPages , ( pageInfo ) => {
return _ . startsWith ( pageInfo . url , '/tables/' ) ;
} ) ;
// Get all the osquery table names, we'll use this list to determine which tables are used.
let allTableNames = _ . pluck ( allTablesInformation , 'title' ) ;
// Create an array of words in the query.
2025-01-23 03:10:17 +00:00
let queryWords = _ . words ( query . query , /[^ \n;]+/g ) ;
2024-11-07 21:49:16 +00:00
let columnNamesForSyntaxHighlighting = [ ] ;
let tableNamesForSyntaxHighlighting = [ ] ;
// Get all of the words that appear in both arrays
let intersectionBetweenQueryWordsAndTableNames = _ . intersection ( queryWords , allTableNames ) ;
// For each matched osquery table, add the keywordsForSyntaxHighlighting and the names of the tables used into two arrays.
for ( let tableName of intersectionBetweenQueryWordsAndTableNames ) {
let tableMentionedInThisQuery = _ . find ( sails . config . builtStaticContent . markdownPages , { title : tableName } ) ;
tableNamesForSyntaxHighlighting . push ( tableMentionedInThisQuery . title ) ;
let keyWordsForThisTable = tableMentionedInThisQuery . keywordsForSyntaxHighlighting ;
columnNamesForSyntaxHighlighting = columnNamesForSyntaxHighlighting . concat ( keyWordsForThisTable ) ;
}
// Remove the table names from the array of column names to highlight.
columnNamesForSyntaxHighlighting = _ . difference ( columnNamesForSyntaxHighlighting , tableNamesForSyntaxHighlighting ) ;
2021-10-01 02:14:02 +00:00
// Setting the meta title and description of this page using the query object, and falling back to a generic title or description if query.name or query.description are missing.
2024-07-31 19:50:42 +00:00
let pageTitleForMeta = query . name ? query . name + ' | Query details' : 'Query details' ;
2021-10-01 02:14:02 +00:00
let pageDescriptionForMeta = query . description ? query . description : 'View more information about a query in Fleet\'s standard query library' ;
2021-05-10 04:48:59 +00:00
// Respond with view.
2021-05-26 08:24:38 +00:00
return {
query ,
2021-10-01 02:14:02 +00:00
queryLibraryYmlRepoPath : sails . config . builtStaticContent . queryLibraryYmlRepoPath ,
pageTitleForMeta ,
pageDescriptionForMeta ,
2024-11-07 21:49:16 +00:00
columnNamesForSyntaxHighlighting ,
tableNamesForSyntaxHighlighting ,
2024-11-04 19:31:36 +00:00
algoliaPublicKey : sails . config . custom . algoliaPublicKey ,
2021-05-26 08:24:38 +00:00
} ;
2021-05-10 04:48:59 +00:00
}
} ;