Skip to content

Commit

Permalink
Merge pull request #87 from 4urcloud/dev-esteban-optimization
Browse files Browse the repository at this point in the history
Dev esteban optimization
  • Loading branch information
estebanmathia authored Dec 5, 2023
2 parents 7d8908a + 907cf7c commit a6375e2
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 24 deletions.
3 changes: 1 addition & 2 deletions Kexa/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export async function main() {
let settings = await gatheringRules(await getEnvVar("RULESDIRECTORY")??"./Kexa/rules");
context?.log("settings", settings);
if(settings.length != 0){
let resources = {};
resources = await loadAddOns(resources);
let resources = await loadAddOns(settings);
context?.log("resources", resources);
if(args.o) createFileSync(JSON.stringify(resources), folderOutput + "/resources/"+ new Date().toISOString().slice(0, 16).replace(/[-T:/]/g, '') +".json", true);
context?.log("good");
Expand Down
1 change: 1 addition & 0 deletions Kexa/models/settingFile/config.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ export interface Config {
name?: string;
description?: string;
prefix?: string;
ObjectNameNeed?: Array<string>;
}
20 changes: 13 additions & 7 deletions Kexa/services/addOn.service.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import { Provider, ProviderResource } from "../models/providerResource.models";
import { Header } from "../models/settingFile/header.models";
import { writeStringToJsonFile } from "../helpers/files"
const configuration = require('node-config-ts').config;
import { Capacity } from "../models/settingFile/capacity.models";
import {getContext, getNewLogger} from "./logger.service";
import { SettingFile } from "../models/settingFile/settingFile.models";

const configuration = require('node-config-ts').config;
const mainFolder = 'Kexa';
const serviceAddOnPath = './' + mainFolder + '/services/addOn';
const fs = require('fs');

import {getContext, getNewLogger} from "./logger.service";
import { Capacity } from "../models/settingFile/capacity.models";
const logger = getNewLogger("LoaderAddOnLogger");

export async function loadAddOns(resources: ProviderResource): Promise<ProviderResource>{

export async function loadAddOns(settings:SettingFile[]): Promise<ProviderResource>{
let resources: ProviderResource = {};
let context = getContext();
logger.info("Loading addOns");
context?.log("Loading addOns");
const addOnNeed = require('../../config/addOnNeed.json');
const files = fs.readdirSync(serviceAddOnPath);
const promises = files.map(async (file: string) => {
return await loadAddOn(file, addOnNeed);
return await loadAddOn(file, addOnNeed, settings);
});
const results = await Promise.all(promises);
results.forEach((result: { key: string; data: Provider[]; }) => {
Expand All @@ -29,7 +31,7 @@ export async function loadAddOns(resources: ProviderResource): Promise<ProviderR
return resources;
}

async function loadAddOn(file: string, addOnNeed: any): Promise<{ key: string; data: Provider|null; } | null> {
async function loadAddOn(file: string, addOnNeed: any, settings:SettingFile[]): Promise<{ key: string; data: Provider|null; } | null> {
let context = getContext();
try{
if (file.endsWith('Gathering.service.ts')){
Expand All @@ -43,6 +45,10 @@ async function loadAddOn(file: string, addOnNeed: any): Promise<{ key: string; d
const { collectData } = await import(`./addOn/${file.replace(".ts", ".js") }`);
let start = Date.now();
const addOnConfig = (configuration.hasOwnProperty(nameAddOn)) ? configuration[nameAddOn] : null;
addOnConfig?.forEach((config: any) => {
config.ObjectNameNeed = []
config.rules.forEach((rulesName: string) => config.ObjectNameNeed = [...addOnNeed["objectNameNeed"][rulesName][nameAddOn], ...config.ObjectNameNeed]);
});
const data = await collectData(addOnConfig);
let delta = Date.now() - start;
context?.log(`AddOn ${nameAddOn} collect in ${delta}ms`);
Expand Down
11 changes: 11 additions & 0 deletions Kexa/services/addOn/awsGathering.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ let rdsClient: RDS;
let s3Client: S3;
let ecsClient: ECS;
let ecrClient: ECR;
let currentConfig: AwsConfig;
////////////////////////////////////////////////////////////////////////////////////////////////////////
//// LISTING CLOUD RESOURCES ///////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////
export async function collectData(awsConfig: AwsConfig[]): Promise<AWSResources[] | null> {
let context = getContext();
let resources = new Array<AWSResources>();
for (let oneConfig of awsConfig ?? []) {
currentConfig = oneConfig;
let awsResource = {
"ec2Instance": null,
"ec2SG": null,
Expand Down Expand Up @@ -169,6 +171,7 @@ function addRegion(resources:any, region:string) {
}

async function ec2SGListing(client: EC2, region: string): Promise<any> {
if(!currentConfig.ObjectNameNeed?.includes("ec2SG")) return null;
try {
const data = await client.describeSecurityGroups().promise();
let jsonData = JSON.parse(JSON.stringify(data.SecurityGroups));
Expand All @@ -182,6 +185,7 @@ async function ec2SGListing(client: EC2, region: string): Promise<any> {
}

async function ec2VolumesListing(client: EC2, region: string): Promise<any> {
if(!currentConfig.ObjectNameNeed?.includes("ec2Volume")) return null;
try {
const data = await client.describeVolumes().promise();
let jsonData = JSON.parse(JSON.stringify(data.Volumes));
Expand All @@ -195,6 +199,7 @@ async function ec2VolumesListing(client: EC2, region: string): Promise<any> {
}

async function ec2InstancesListing(client: EC2, region: string): Promise<Array<EC2.Instance> | null> {
if(!currentConfig.ObjectNameNeed?.includes("ec2Instance")) return null;
try {
const data = await client.describeInstances().promise();
let jsonData = JSON.parse(JSON.stringify(data.Reservations));
Expand All @@ -208,6 +213,7 @@ async function ec2InstancesListing(client: EC2, region: string): Promise<Array<E
}

async function rdsInstancesListing(client: RDS, region: string): Promise<any> {
if(!currentConfig.ObjectNameNeed?.includes("rds")) return null;
try {
const data = await client.describeDBInstances().promise();
let jsonData = JSON.parse(JSON.stringify(data.DBInstances));
Expand All @@ -221,6 +227,7 @@ async function rdsInstancesListing(client: RDS, region: string): Promise<any> {
}

async function resourceGroupsListing(client: ResourceGroups, region: string): Promise<any> {
if(!currentConfig.ObjectNameNeed?.includes("resourceGroup")) return null;
try {
const data = await client.listGroups().promise();
let jsonData = JSON.parse(JSON.stringify(data.Groups));
Expand All @@ -234,6 +241,7 @@ async function resourceGroupsListing(client: ResourceGroups, region: string): Pr
}

async function tagsValueListing(client: ResourceGroupsTaggingAPI, region: string): Promise<any> {
if(!currentConfig.ObjectNameNeed?.includes("tagsValue")) return null;
try {
interface TagParams {Key: string;}
const dataKeys = await client.getTagKeys().promise();
Expand All @@ -255,6 +263,7 @@ async function tagsValueListing(client: ResourceGroupsTaggingAPI, region: string
}

async function s3BucketsListing(client: S3, region: string): Promise<Array<S3> | null> {
if(!currentConfig.ObjectNameNeed?.includes("s3")) return null;
try {
const data = await client.listBuckets().promise();
let jsonData = JSON.parse(JSON.stringify(data.Buckets));
Expand All @@ -268,6 +277,7 @@ async function s3BucketsListing(client: S3, region: string): Promise<Array<S3> |
}

async function ecsClusterListing(client: ECS, region: string): Promise<any> {
if(!currentConfig.ObjectNameNeed?.includes("ecsCluster")) return null;
try {
const data = await client.describeClusters().promise();
let jsonData = JSON.parse(JSON.stringify(data.clusters));
Expand All @@ -281,6 +291,7 @@ async function ecsClusterListing(client: ECS, region: string): Promise<any> {
}

async function ecrImagesListing(client: ECR, region: string): Promise<any> {
if(!currentConfig.ObjectNameNeed?.includes("ecrImage")) return null;
try {
const data = await client.describeRepositories().promise();
let jsonData = JSON.parse(JSON.stringify(data.repositories));
Expand Down
23 changes: 22 additions & 1 deletion Kexa/services/addOn/azureGathering.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ const logger = getNewLogger("AzureLogger");
let computeClient: ComputeManagementClient;
let resourcesClient : ResourceManagementClient ;
let networkClient: NetworkManagementClient;
let currentConfig: AzureConfig;
////////////////////////////////////////////////////////////////////////////////////////////////////////
//// LISTING CLOUD RESOURCES
////////////////////////////////////////////////////////////////////////////////////////////////////////
export async function collectData(azureConfig:AzureConfig[]): Promise<AzureResources[]|null>{
let context = getContext();
let resources = new Array<AzureResources>();
for(let config of azureConfig??[]){
currentConfig = config;
let azureResource = {
"vm": null,
"rg": null,
Expand Down Expand Up @@ -132,6 +134,7 @@ export async function collectData(azureConfig:AzureConfig[]): Promise<AzureResou

//get service principal key information
export async function getSPKeyInformation(credential: DefaultAzureCredential, subscriptionId: string): Promise<any> {
if(!currentConfig.ObjectNameNeed?.includes("sp")) return null;
const { GraphRbacManagementClient } = require("@azure/graph");
logger.info("starting getSPKeyInformation");
try {
Expand All @@ -149,6 +152,7 @@ export async function getSPKeyInformation(credential: DefaultAzureCredential, su

//ip list
export async function ipListing(client:NetworkManagementClient): Promise<Array<any>|null> {
if(!currentConfig.ObjectNameNeed?.includes("ip")) return null;
logger.info("starting ipListing");
try{
const resultList = new Array<any>;
Expand All @@ -164,6 +168,7 @@ export async function ipListing(client:NetworkManagementClient): Promise<Array<a

//aks list
export async function aksListing(credential: DefaultAzureCredential, subscriptionId: string): Promise<any> {
if(!currentConfig.ObjectNameNeed?.includes("aks")) return null;
logger.info("starting aksListing");
try{
const client = new ContainerServiceClient(credential, subscriptionId);
Expand All @@ -180,6 +185,7 @@ export async function aksListing(credential: DefaultAzureCredential, subscriptio

//network security group list
export async function networkSecurityGroupListing(client:NetworkManagementClient): Promise<Array<NetworkSecurityGroup>|null> {
if(!currentConfig.ObjectNameNeed?.includes("nsg")) return null;
logger.info("starting networkSecurityGroupListing");
try {
const resultList = new Array<NetworkSecurityGroup>;
Expand All @@ -196,6 +202,7 @@ export async function networkSecurityGroupListing(client:NetworkManagementClient

//virtual network list
export async function virtualNetworksListing(client:NetworkManagementClient): Promise<Array<VirtualNetwork>|null> {
if(!currentConfig.ObjectNameNeed?.includes("virtualNetwork")) return null;
logger.info("starting virtualNetworksListing");
try {
const resultList = new Array<VirtualNetwork>;
Expand All @@ -212,6 +219,7 @@ export async function virtualNetworksListing(client:NetworkManagementClient): Pr

//network list
export async function networkInterfacesListing(client:NetworkManagementClient): Promise<Array<NetworkInterface>|null> {
if(!currentConfig.ObjectNameNeed?.includes("networkInterfaces")) return null;
logger.info("starting networkInterfacesListing");
try {
const resultList = new Array<NetworkInterface>;
Expand All @@ -227,6 +235,7 @@ export async function networkInterfacesListing(client:NetworkManagementClient):

//disks.list
export async function disksListing(client:ComputeManagementClient): Promise<Array<Disk>|null> {
if(!currentConfig.ObjectNameNeed?.includes("disk")) return null;
logger.info("starting disksListing");
try {
const resultList = new Array<Disk>;
Expand All @@ -242,6 +251,7 @@ export async function disksListing(client:ComputeManagementClient): Promise<Arra

//virtualMachines.listAll
export async function virtualMachinesListing(client:ComputeManagementClient): Promise<Array<VirtualMachine>|null> {
if(!currentConfig.ObjectNameNeed?.includes("vm")) return null;
logger.info("starting virtualMachinesListing");
try {
const resultList = new Array<VirtualMachine>;
Expand All @@ -256,6 +266,7 @@ export async function virtualMachinesListing(client:ComputeManagementClient): Pr
}

export async function resourceGroupListing(client:ResourceManagementClient): Promise<Array<ResourceGroup>|null> {
if(!currentConfig.ObjectNameNeed?.includes("rg")) return null;
logger.info("starting resourceGroupListing");
try {
const resultList = new Array<ResourceGroup>;
Expand All @@ -269,7 +280,8 @@ export async function resourceGroupListing(client:ResourceManagementClient): Pro
}
}

export async function networkSecurityGroup_analyse(nsgList: Array<NetworkSecurityGroup>): Promise<Array<ckiNetworkSecurityClass.CkiNetworkSecurityGroupClass>|null> {
export async function networkSecurityGroup_analyze(nsgList: Array<NetworkSecurityGroup>): Promise<Array<ckiNetworkSecurityClass.CkiNetworkSecurityGroupClass>|null> {
if(!currentConfig.ObjectNameNeed?.includes("nsg_analyze")) return null;
try {
const resultList = new Array<ckiNetworkSecurityClass.CkiNetworkSecurityGroupClass>;
for await (let item of nsgList){
Expand All @@ -291,6 +303,12 @@ export async function networkSecurityGroup_analyse(nsgList: Array<NetworkSecurit

import { AzureMachineLearningWorkspaces } from "@azure/arm-machinelearning";
export async function mlListing(credential: DefaultAzureCredential, subscriptionId: string): Promise<any> {
if(
!currentConfig.ObjectNameNeed?.includes("mlWorkspaces")
&& !currentConfig.ObjectNameNeed?.includes("mlJobs")
&& !currentConfig.ObjectNameNeed?.includes("mlComputes")
&& !currentConfig.ObjectNameNeed?.includes("mlSchedules")
) return null;
logger.info("starting mlListing");
try{
const client = new AzureMachineLearningWorkspaces(credential, subscriptionId);
Expand Down Expand Up @@ -322,6 +340,7 @@ export async function mlListing(credential: DefaultAzureCredential, subscription
}

export async function jobsListing(client: AzureMachineLearningWorkspaces, resourceGroupName: string, workspaceName: string): Promise<any[]> {
if(!currentConfig.ObjectNameNeed?.includes("mlJobs")) return [];
//logger.info("starting jobsListing");
try{
const resArray = new Array();
Expand All @@ -339,6 +358,7 @@ export async function jobsListing(client: AzureMachineLearningWorkspaces, resour
}

export async function computeOperationsListing(client: AzureMachineLearningWorkspaces, resourceGroupName: string, workspaceName: string): Promise<any[]> {
if(!currentConfig.ObjectNameNeed?.includes("mlComputes")) return [];
//logger.info("starting computeOperationsListing");
try{
const resArray = new Array();
Expand All @@ -356,6 +376,7 @@ export async function computeOperationsListing(client: AzureMachineLearningWorks
}

export async function schedulesListing(client: AzureMachineLearningWorkspaces, resourceGroupName: string, workspaceName: string): Promise<any[]> {
if(!currentConfig.ObjectNameNeed?.includes("mlSchedules")) return [];
//logger.info("starting schedulesListing");
try{
const resArray = new Array();
Expand Down
Loading

0 comments on commit a6375e2

Please sign in to comment.