Skip to content

Commit

Permalink
Merge pull request #78 from 4urcloud/dev-adrien
Browse files Browse the repository at this point in the history
replace config by node-config-ts
  • Loading branch information
aeppling authored Nov 24, 2023
2 parents 61f6ea2 + 534e795 commit f65a934
Show file tree
Hide file tree
Showing 16 changed files with 10,244 additions and 9,319 deletions.
9 changes: 8 additions & 1 deletion Kexa/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ const args = yargs(hideBin(process.argv)).argv
const folderOutput = process.env.OUTPUT??"./output";

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
env.config(); // reading environnement vars // file system
env.config();// reading environnement vars
// file system

let config = require('node-config-ts');

export async function main() {


env.config();

let context = getContext();
context?.log("entering main");
const logger = getNewLogger("MainLogger");
Expand Down
4 changes: 2 additions & 2 deletions Kexa/services/addOn.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Provider, ProviderResource } from "../models/providerResource.models";
import { Header } from "../models/settingFile/header.models";
import { writeStringToJsonFile } from "../helpers/files"
const configuration = require('config');
const configuration = require('node-config-ts').config;

const mainFolder = 'Kexa';
const serviceAddOnPath = './' + mainFolder + '/services/addOn';
Expand Down Expand Up @@ -42,7 +42,7 @@ 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.has(nameAddOn))?configuration.get(nameAddOn):null;
const addOnConfig = (configuration.hasOwnProperty(nameAddOn)) ? configuration[nameAddOn] : null;
const data = await collectData(addOnConfig);
let delta = Date.now() - start;
context?.log(`AddOn ${nameAddOn} collect in ${delta}ms`);
Expand Down
4 changes: 0 additions & 4 deletions Kexa/services/addOn/azureGathering.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ export async function virtualMachinesListing(client:ComputeManagementClient): Pr
}
}

//resourceGroups.list
export async function resourceGroupListing(client:ResourceManagementClient): Promise<Array<ResourceGroup>|null> {
logger.info("starting resourceGroupListing");
try {
Expand All @@ -257,9 +256,6 @@ export async function resourceGroupListing(client:ResourceManagementClient): Pro
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////


export async function networkSecurityGroup_analyse(nsgList: Array<NetworkSecurityGroup>): Promise<Array<ckiNetworkSecurityClass.CkiNetworkSecurityGroupClass>|null> {
try {
const resultList = new Array<ckiNetworkSecurityClass.CkiNetworkSecurityGroupClass>;
Expand Down
9 changes: 3 additions & 6 deletions Kexa/services/addOn/httpGathering.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let httpConfig: HttpConfig[] = [];
const jsome = require('jsome');
jsome.level.show = true;

import {getContext, getNewLogger} from "../logger.service";
import {getNewLogger} from "../logger.service";
const logger = getNewLogger("HttpLogger");

////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -31,12 +31,10 @@ export async function collectData(_httpConfig:HttpConfig[]) {
httpConfig = _httpConfig;
let resources = new Array<HttpResources>();
let promises: any = []
let context = getContext();
for(let config of httpConfig??[]){
let prefix = config.prefix??(httpConfig.indexOf(config).toString());
promises.push(
(async () => {
context?.log("- add one config for http -");
logger.info("- add one config for http -");
let httpResources = {
certificate: null,
Expand All @@ -53,7 +51,7 @@ export async function collectData(_httpConfig:HttpConfig[]) {

httpResources = await getDataHttp(url, config);

} catch (e) {
} catch (e:any) {
logger.error("error in collectHttpData with the url: " + ((await getConfigOrEnvVar(config, "URL", prefix)) ?? null));
logger.error(e);
}
Expand All @@ -66,7 +64,6 @@ export async function collectData(_httpConfig:HttpConfig[]) {
resources.push(...results);

logger.info("- listing http resources done -");
context?.log("- listing http resources done -");
return resources??null;
}

Expand Down Expand Up @@ -178,7 +175,7 @@ async function getDataHttp(url: string, config: HttpConfig): Promise<HttpRequest
httpResources.ip = await dnsLookup(URL.parse(url).hostname!);
httpResources.certificate = await getCertificateFromResponse(response);
httpResources.delays = response?.delays;
}catch(e){
}catch(e:any){
logger.error("error in getDataHttp with the url: " + url);
logger.error(e);
}
Expand Down
16 changes: 7 additions & 9 deletions Kexa/services/addOn/kubernetesGathering.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { deleteFile, getFile, writeStringToJsonFile } from "../../helpers/files"
import { KubernetesConfig } from "../../models/kubernetes/config.models";
const yaml = require('js-yaml');

import {getContext, getNewLogger} from "../logger.service";
import {getNewLogger} from "../logger.service";
const logger = getNewLogger("KubernetesLogger");

const k8s = require('@kubernetes/client-node');
Expand All @@ -40,8 +40,8 @@ export async function collectData(kubernetesConfig:KubernetesConfig[]): Promise<
"helm": kubernetesList["helm"],
} as KubernetesResources;
resources.push(kubernetesResource);
}catch(e){
logger.debug(e);
}catch(e:any){
logger.error(e);
}
deleteFile("./config/kubernetes.json");
}
Expand All @@ -50,8 +50,6 @@ export async function collectData(kubernetesConfig:KubernetesConfig[]): Promise<

//kubernetes list
export async function kubernetesListing(isPathKubeFile: boolean): Promise<any> {
let context = getContext();
context?.log("starting kubernetesListing");
logger.info("starting kubernetesListing");
const kc = new k8s.KubeConfig();
(isPathKubeFile)?kc.loadFromFile("./config/kubernetes.json"):kc.loadFromDefault();
Expand Down Expand Up @@ -83,8 +81,8 @@ async function collectHelm(namespace: string): Promise<any> {
try{
let helmData = await helm.list({ namespace: namespace });
return helmData;
}catch(e){
logger.debug(e);
}catch(e:any){
//logger.error(e);
return null;
}
}
Expand All @@ -93,8 +91,8 @@ async function collectPods(k8sApiCore: any, namespace: string): Promise<any> {
try{
const pods = await k8sApiCore.listNamespacedPod(namespace);
return pods;
}catch(e){
logger.debug(e);
}catch(e:any){
//logger.error(e);
return null;
}
}
2 changes: 1 addition & 1 deletion Kexa/services/alerte.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const request = require('request');
const nodemailer = require("nodemailer");
const levelAlert = ["info", "warning", "error", "critical"];
const colors = ["#4f5660", "#ffcc00", "#cc3300", "#cc3300"];
const config = require('config');
const config = require('node-config-ts');

import {getContext, getNewLogger} from "./logger.service";
const logger = getNewLogger("functionLogger");
Expand Down
10 changes: 5 additions & 5 deletions Kexa/services/analyse.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const varEnvMin = {
"email": ["EMAILPORT", "EMAILHOST", "EMAILUSER", "EMAILPWD", "EMAILFROM"],
"sms": ["SMSACCOUNTSID", "SMSAUTHTOKEN", "SMSFROM"],
}
const config = require('config');
const config = require('node-config-ts').config;
const levelAlert = ["info", "warning", "error", "critical"];
let headers: any;
//Analyse list
Expand Down Expand Up @@ -69,10 +69,10 @@ export function extractAddOnNeed(settingFileList: SettingFile[]){
}

function getListNeedRules(): string[]{
const config = require('config');
const config = require('node-config-ts').config;
let listNeedRules = new Array<string>();
for(let cloudProvider of Object.keys(config)){
let configAssign = config.get(cloudProvider);
let configAssign = config[cloudProvider];
for(let config of configAssign){
for(let rule of config.rules){
if(!listNeedRules.includes(rule)) listNeedRules.push(rule);
Expand Down Expand Up @@ -306,11 +306,11 @@ export function checkRules(rules:Rules[], resources:ProviderResource, alert: Ale
if(!rule.applied) return;
context?.log("check rule:"+rule.name);
logger.info("check rule:"+rule.name);
if(!config.has(rule.cloudProvider)){
if(!config.hasOwnProperty(rule.cloudProvider)){
logger.debug("cloud provider not found in config:"+rule.cloudProvider);
return;
}
const configAssign = config.get(rule.cloudProvider);
const configAssign = config[rule.cloudProvider];
let objectResources:any = []
for(let i = 0; i < configAssign.length; i++){
if(configAssign[i].rules.includes(alert.global.name)){
Expand Down
11 changes: 0 additions & 11 deletions config/default.json

This file was deleted.

15 changes: 15 additions & 0 deletions config/env/aws.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"aws": [
{
"name": "AWS Project A",
"prefix": "AWSPROJECTA_",
"description": "First subscription (0) : Project A subscription",
"rules": [
"Economy"
],
"regions": [
"us-east-1"
]
}
]
}
11 changes: 11 additions & 0 deletions config/env/azure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"azure": [
{
"description": "organization 4urcloud",
"prefix": "DEMO_",
"rules": [
"Economy"
]
}
]
}
16 changes: 16 additions & 0 deletions config/env/gcp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"gcp": [
{
"name": "GCP project B",
"prefix": "GCPPROJECTB_",
"description": "First subscription (0) : Project B subscription",
"rules": [
"Economy",
"OperationalExcellence"
],
"regions": [
"us-east1"
]
}
]
}
12 changes: 12 additions & 0 deletions config/env/office365.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"o365": [
{
"name": "O365 project A",
"description": "organization 4urcloud",
"prefix": "O365PROJECTA_",
"rules": [
"BenchmarkOffice365"
]
}
]
}
12 changes: 12 additions & 0 deletions config/env/workspace.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"googleWorkspace": [
{
"name": "Workspace project A",
"prefix": "WORKPROJECTA",
"description": "First subscription (0) : Project A subscription",
"rules": [
"BenchmarkWorkspace"
]
}
]
}
Loading

0 comments on commit f65a934

Please sign in to comment.