Add MS Graph Security node (#2307)

*  Create MS Graph Security node

*  General update

* 📦 Update package-lock.json

* 👕 Fix lint

* 🔥 Remove Reviewed field

*  Set max limit to 1000

*  Add limit to 1000 to second resource
This commit is contained in:
Iván Ovejero 2022-01-08 10:53:10 +01:00 committed by GitHub
parent 8e708f3d3d
commit 77a05976ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 762 additions and 1 deletions

View file

@ -0,0 +1,21 @@
import {
ICredentialType,
INodeProperties,
} from 'n8n-workflow';
export class MicrosoftGraphSecurityOAuth2Api implements ICredentialType {
name = 'microsoftGraphSecurityOAuth2Api';
displayName = 'Microsoft Graph Security OAuth2 API';
extends = [
'microsoftOAuth2Api',
];
documentationUrl = 'microsoft';
properties: INodeProperties[] = [
{
displayName: 'Scope',
name: 'scope',
type: 'hidden',
default: 'SecurityEvents.ReadWrite.All',
},
];
}

View file

@ -0,0 +1,88 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
import {
OptionsWithUri,
} from 'request';
export async function msGraphSecurityApiRequest(
this: IExecuteFunctions,
method: string,
endpoint: string,
body: IDataObject = {},
qs: IDataObject = {},
headers: IDataObject = {},
) {
const {
oauthTokenData: {
access_token, // tslint:disable-line variable-name
},
} = await this.getCredentials('microsoftGraphSecurityOAuth2Api') as {
oauthTokenData: {
access_token: string;
}
};
const options: OptionsWithUri = {
headers: {
Authorization: `Bearer ${access_token}`,
},
method,
body,
qs,
uri: `https://graph.microsoft.com/v1.0/security${endpoint}`,
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
}
if (!Object.keys(qs).length) {
delete options.qs;
}
if (Object.keys(headers).length) {
options.headers = { ...options.headers, ...headers };
}
try {
return await this.helpers.request(options);
} catch (error) {
const nestedMessage = error?.error?.error?.message;
if (nestedMessage.startsWith('{"')) {
error = JSON.parse(nestedMessage);
}
if (nestedMessage.startsWith('Http request failed with statusCode=BadRequest')) {
error.error.error.message = 'Request failed with bad request';
} else if (nestedMessage.startsWith('Http request failed with')) {
const stringified = nestedMessage.split(': ').pop();
if (stringified) {
error = JSON.parse(stringified);
}
}
if (['Invalid filter clause', 'Invalid ODATA query filter'].includes(nestedMessage)) {
error.error.error.message += ' - Please check that your query parameter syntax is correct: https://docs.microsoft.com/en-us/graph/query-parameters#filter-parameter';
}
throw new NodeApiError(this.getNode(), error);
}
}
export function tolerateDoubleQuotes(filterQueryParameter: string) {
return filterQueryParameter.replace(/"/g, `'`);
}
export function throwOnEmptyUpdate(this: IExecuteFunctions) {
throw new NodeOperationError(this.getNode(), 'Please enter at least one field to update');
}

View file

@ -0,0 +1,241 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
msGraphSecurityApiRequest,
throwOnEmptyUpdate,
tolerateDoubleQuotes,
} from './GenericFunctions';
import {
secureScoreControlProfileFields,
secureScoreControlProfileOperations,
secureScoreFields,
secureScoreOperations,
} from './descriptions';
export class MicrosoftGraphSecurity implements INodeType {
description: INodeTypeDescription = {
displayName: 'Microsoft Graph Security',
name: 'microsoftGraphSecurity',
icon: 'file:microsoftGraph.svg',
group: ['transform'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume the Microsoft Graph Security API',
defaults: {
name: 'Microsoft Graph Security',
color: '#0078d4',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'microsoftGraphSecurityOAuth2Api',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
noDataExpression: true,
options: [
{
name: 'Secure Score',
value: 'secureScore',
},
{
name: 'Secure Score Control Profile',
value: 'secureScoreControlProfile',
},
],
default: 'secureScore',
},
...secureScoreOperations,
...secureScoreFields,
...secureScoreControlProfileOperations,
...secureScoreControlProfileFields,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const resource = this.getNodeParameter('resource', 0) as 'secureScore'| 'secureScoreControlProfile';
const operation = this.getNodeParameter('operation', 0) as 'get' | 'getAll' | 'update';
let responseData;
for (let i = 0; i < items.length; i++) {
try {
if (resource === 'secureScore') {
// **********************************************************************
// secureScore
// **********************************************************************
if (operation === 'get') {
// ----------------------------------------
// secureScore: get
// ----------------------------------------
// https://docs.microsoft.com/en-us/graph/api/securescore-get
const secureScoreId = this.getNodeParameter('secureScoreId', i);
responseData = await msGraphSecurityApiRequest.call(this, 'GET', `/secureScores/${secureScoreId}`);
delete responseData['@odata.context'];
} else if (operation === 'getAll') {
// ----------------------------------------
// secureScore: getAll
// ----------------------------------------
// https://docs.microsoft.com/en-us/graph/api/security-list-securescores
const qs: IDataObject = {};
const {
filter,
includeControlScores,
} = this.getNodeParameter('filters', i) as {
filter?: string;
includeControlScores?: boolean;
};
if (filter) {
qs.$filter = tolerateDoubleQuotes(filter);
}
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
if (!returnAll) {
qs.$count = true;
qs.$top = this.getNodeParameter('limit', 0);
}
responseData = await msGraphSecurityApiRequest
.call(this, 'GET', '/secureScores', {}, qs)
.then(response => response.value) as Array<{ controlScores: object[] }>;
if (!includeControlScores) {
responseData = responseData.map(({ controlScores, ...rest }) => rest);
}
}
} else if (resource === 'secureScoreControlProfile') {
// **********************************************************************
// secureScoreControlProfile
// **********************************************************************
if (operation === 'get') {
// ----------------------------------------
// secureScoreControlProfile: get
// ----------------------------------------
// https://docs.microsoft.com/en-us/graph/api/securescorecontrolprofile-get
const secureScoreControlProfileId = this.getNodeParameter('secureScoreControlProfileId', i);
const endpoint = `/secureScoreControlProfiles/${secureScoreControlProfileId}`;
responseData = await msGraphSecurityApiRequest.call(this, 'GET', endpoint);
delete responseData['@odata.context'];
} else if (operation === 'getAll') {
// ----------------------------------------
// secureScoreControlProfile: getAll
// ----------------------------------------
// https://docs.microsoft.com/en-us/graph/api/security-list-securescorecontrolprofiles
const qs: IDataObject = {};
const { filter } = this.getNodeParameter('filters', i) as { filter?: string };
if (filter) {
qs.$filter = tolerateDoubleQuotes(filter);
}
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
if (!returnAll) {
qs.$count = true;
qs.$top = this.getNodeParameter('limit', 0);
}
responseData = await msGraphSecurityApiRequest
.call(this, 'GET', '/secureScoreControlProfiles', {}, qs)
.then(response => response.value);
} else if (operation === 'update') {
// ----------------------------------------
// secureScoreControlProfile: update
// ----------------------------------------
// https://docs.microsoft.com/en-us/graph/api/securescorecontrolprofile-update
const body: IDataObject = {
vendorInformation: {
provider: this.getNodeParameter('provider', i),
vendor: this.getNodeParameter('vendor', i),
},
};
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
if (!Object.keys(updateFields).length) {
throwOnEmptyUpdate.call(this);
}
if (Object.keys(updateFields).length) {
Object.assign(body, updateFields);
}
const id = this.getNodeParameter('secureScoreControlProfileId', i);
const endpoint = `/secureScoreControlProfiles/${id}`;
const headers = { Prefer: 'return=representation' };
responseData = await msGraphSecurityApiRequest.call(this, 'PATCH', endpoint, body, {}, headers);
delete responseData['@odata.context'];
}
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
continue;
}
throw error;
}
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -0,0 +1,230 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const secureScoreControlProfileOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: [
'secureScoreControlProfile',
],
},
},
options: [
{
name: 'Get',
value: 'get',
},
{
name: 'Get All',
value: 'getAll',
},
{
name: 'Update',
value: 'update',
},
],
default: 'get',
},
];
export const secureScoreControlProfileFields: INodeProperties[] = [
// ----------------------------------------
// secureScore: get
// ----------------------------------------
{
displayName: 'Secure Score Control Profile ID',
name: 'secureScoreControlProfileId',
description: 'ID of the secure score control profile to retrieve',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'secureScoreControlProfile',
],
operation: [
'get',
],
},
},
},
// ----------------------------------------
// secureScoreControlProfile: getAll
// ----------------------------------------
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
default: false,
description: 'Whether to return all results or only up to a given limit',
displayOptions: {
show: {
resource: [
'secureScoreControlProfile',
],
operation: [
'getAll',
],
},
},
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
default: 50,
description: 'Max number of results to return',
typeOptions: {
minValue: 1,
maxValue: 1000,
},
displayOptions: {
show: {
resource: [
'secureScoreControlProfile',
],
operation: [
'getAll',
],
returnAll: [
false,
],
},
},
},
{
displayName: 'Filters',
name: 'filters',
type: 'collection',
default: {},
placeholder: 'Add Filter',
displayOptions: {
show: {
resource: [
'secureScoreControlProfile',
],
operation: [
'getAll',
],
},
},
options: [
{
displayName: 'Filter Query Parameter',
name: 'filter',
description: '<a href="https://docs.microsoft.com/en-us/graph/query-parameters#filter-parameter">Query parameter</a> to filter results by',
type: 'string',
default: '',
placeholder: 'startsWith(id, \'AATP\')',
},
],
},
// ----------------------------------------
// secureScoreControlProfile: update
// ----------------------------------------
{
displayName: 'Secure Score Control Profile ID',
name: 'secureScoreControlProfileId',
description: 'ID of the secure score control profile to update',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'secureScoreControlProfile',
],
operation: [
'update',
],
},
},
},
{
displayName: 'Provider',
name: 'provider',
type: 'string',
description: 'Name of the provider of the security product or service',
default: '',
placeholder: 'SecureScore',
required: true,
displayOptions: {
show: {
resource: [
'secureScoreControlProfile',
],
operation: [
'update',
],
},
},
},
{
displayName: 'Vendor',
name: 'vendor',
type: 'string',
description: 'Name of the vendor of the security product or service',
default: '',
placeholder: 'Microsoft',
required: true,
displayOptions: {
show: {
resource: [
'secureScoreControlProfile',
],
operation: [
'update',
],
},
},
},
{
displayName: 'Update Fields',
name: 'updateFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'secureScoreControlProfile',
],
operation: [
'update',
],
},
},
options: [
{
displayName: 'State',
name: 'state',
type: 'options',
default: 'Default',
description: 'Analyst driven setting on the control',
options: [
{
name: 'Default',
value: 'Default',
},
{
name: 'Ignored',
value: 'Ignored',
},
{
name: 'Third Party',
value: 'ThirdParty',
},
],
},
],
},
];

View file

@ -0,0 +1,132 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const secureScoreOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
noDataExpression: true,
displayOptions: {
show: {
resource: [
'secureScore',
],
},
},
options: [
{
name: 'Get',
value: 'get',
},
{
name: 'Get All',
value: 'getAll',
},
],
default: 'get',
},
];
export const secureScoreFields: INodeProperties[] = [
// ----------------------------------------
// secureScore: get
// ----------------------------------------
{
displayName: 'Secure Score ID',
name: 'secureScoreId',
description: 'ID of the secure score to retrieve',
type: 'string',
required: true,
default: '',
displayOptions: {
show: {
resource: [
'secureScore',
],
operation: [
'get',
],
},
},
},
// ----------------------------------------
// secureScore: getAll
// ----------------------------------------
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
default: false,
description: 'Whether to return all results or only up to a given limit',
displayOptions: {
show: {
resource: [
'secureScore',
],
operation: [
'getAll',
],
},
},
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
default: 50,
description: 'Max number of results to return',
typeOptions: {
minValue: 1,
maxValue: 1000,
},
displayOptions: {
show: {
resource: [
'secureScore',
],
operation: [
'getAll',
],
returnAll: [
false,
],
},
},
},
{
displayName: 'Filters',
name: 'filters',
type: 'collection',
default: {},
placeholder: 'Add Filter',
displayOptions: {
show: {
resource: [
'secureScore',
],
operation: [
'getAll',
],
},
},
options: [
{
displayName: 'Filter Query Parameter',
name: 'filter',
description: '<a href="https://docs.microsoft.com/en-us/graph/query-parameters#filter-parameter">Query parameter</a> to filter results by',
type: 'string',
default: '',
placeholder: 'currentScore eq 13',
},
{
displayName: 'Include Control Scores',
name: 'includeControlScores',
type: 'boolean',
default: false,
},
],
},
];

View file

@ -0,0 +1,2 @@
export * from './SecureScoreDescription';
export * from './SecureScoreControlProfileDescription';

View file

@ -0,0 +1,45 @@
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 374.85 330">
<defs>
<style>
.cls-1 {
fill: #0078d4;
}
.cls-2 {
fill: #28a8ea;
}
.cls-3 {
fill: #14447d;
}
.cls-4 {
fill: #50d9ff;
}
.cls-5 {
fill: #0364b8;
}
.cls-6 {
fill: #0f335e;
}
</style>
</defs>
<g>
<path class="cls-1" d="M1043.7,375H876.3A20,20,0,0,0,859,385L775.26,530a20,20,0,0,0,0,20L859,695a20,20,0,0,0,17.35,10h167.4a20,20,0,0,0,17.35-10l83.69-145a20,20,0,0,0,0-20l-83.69-145A20,20,0,0,0,1043.7,375Z" transform="translate(-772.57 -375)"/>
<g>
<path class="cls-2" d="M1144.74,530,1061,385a19.87,19.87,0,0,0-6-6.5l6.77,161.17,85.63.27A20,20,0,0,0,1144.74,530Z" transform="translate(-772.57 -375)"/>
<path class="cls-3" d="M775.26,550,859,695a20.09,20.09,0,0,0,5.43,6.08l36.39-58.56L774.15,547.78A20.26,20.26,0,0,0,775.26,550Z" transform="translate(-772.57 -375)"/>
<path class="cls-1" d="M859,385,775.26,530a19.18,19.18,0,0,0-1.46,3.12l120.66-95.83-30.35-58.13A19.89,19.89,0,0,0,859,385Z" transform="translate(-772.57 -375)"/>
<path class="cls-2" d="M1043.7,375H876.3a20,20,0,0,0-12.19,4.14l30.35,58.13,153.91-61.71A20.16,20.16,0,0,0,1043.7,375Z" transform="translate(-772.57 -375)"/>
<polygon class="cls-2" points="128.19 267.5 289.22 164.69 121.89 62.27 128.19 267.5"/>
<path class="cls-4" d="M1048.37,375.56,894.46,437.27l167.33,102.42L1055,378.52A20,20,0,0,0,1048.37,375.56Z" transform="translate(-772.57 -375)"/>
<path class="cls-5" d="M773.8,533.1a20.07,20.07,0,0,0,.35,14.68L900.77,642.5l-6.31-205.23Z" transform="translate(-772.57 -375)"/>
<polygon class="cls-1" points="128.19 267.5 128.19 267.5 128.19 267.5 128.19 267.5"/>
<path class="cls-6" d="M864.38,701.06A20.07,20.07,0,0,0,876.3,705h167.4a20,20,0,0,0,3.8-.38L900.77,642.5Z" transform="translate(-772.57 -375)"/>
<path class="cls-5" d="M900.77,642.5l146.73,62.12a20,20,0,0,0,7.54-3.15l6.75-161.78Z" transform="translate(-772.57 -375)"/>
<path class="cls-1" d="M1061.79,539.69,1055,701.47a19.88,19.88,0,0,0,6-6.49l83.7-145a20,20,0,0,0,2.68-10.06Z" transform="translate(-772.57 -375)"/>
</g>
</g>
</svg>

After

Width:  |  Height:  |  Size: 2.2 KiB

View file

@ -178,6 +178,7 @@
"dist/credentials/MessageBirdApi.credentials.js",
"dist/credentials/MicrosoftDynamicsOAuth2Api.credentials.js",
"dist/credentials/MicrosoftExcelOAuth2Api.credentials.js",
"dist/credentials/MicrosoftGraphSecurityOAuth2Api.credentials.js",
"dist/credentials/MicrosoftOAuth2Api.credentials.js",
"dist/credentials/MicrosoftOneDriveOAuth2Api.credentials.js",
"dist/credentials/MicrosoftOutlookOAuth2Api.credentials.js",
@ -505,6 +506,7 @@
"dist/nodes/MessageBird/MessageBird.node.js",
"dist/nodes/Microsoft/Dynamics/MicrosoftDynamicsCrm.node.js",
"dist/nodes/Microsoft/Excel/MicrosoftExcel.node.js",
"dist/nodes/Microsoft/GraphSecurity/MicrosoftGraphSecurity.node.js",
"dist/nodes/Microsoft/OneDrive/MicrosoftOneDrive.node.js",
"dist/nodes/Microsoft/Outlook/MicrosoftOutlook.node.js",
"dist/nodes/Microsoft/Sql/MicrosoftSql.node.js",
@ -765,4 +767,4 @@
"json"
]
}
}
}