Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling and change config #4

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ Examles of config:
"password": "changeme"
},
"kibana": {
"node": "http://localhost:9200",
"node": "http://127.0.0.1:5601",
"username": "elastic",
"password": "changeme"
}
},
"eventIndex": ""
}
```

Expand Down
90 changes: 51 additions & 39 deletions commands/api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,38 @@ import fetch, { Headers } from "node-fetch";
import config from "../config.json" assert { type: "json" };

export const kibanaFetch = async (url, params, apiVersion = 1) => {
let headers = new Headers();
try {
let headers = new Headers();

headers.append("Content-Type", "application/json");
headers.append("kbn-xsrf", "true");
if(config.kibana.apiKey) {
headers.set("Authorization", "ApiKey " + config.kibana.apiKey)
} else {
headers.set(
"Authorization",
"Basic " +
Buffer.from(
config.kibana.username + ":" + config.kibana.password
).toString("base64")
);
headers.append("Content-Type", "application/json");
headers.append("kbn-xsrf", "true");
if (config.kibana.apiKey) {
headers.set("Authorization", "ApiKey " + config.kibana.apiKey);
} else {
headers.set(
"Authorization",
"Basic " +
Buffer.from(
config.kibana.username + ":" + config.kibana.password
).toString("base64")
);
}

headers.set("x-elastic-internal-origin", "kibana");
headers.set("elastic-api-version", apiVersion);
const result = await fetch(new URL(url, config.kibana.node), {
headers: headers,
...params,
});
const data = await result.json();
if(data.statusCode && data.statusCode !== 200) {
console.log(data)
throw new Error(data.message)
}
return data;
} catch (e) {
console.log(e);
}

headers.set("x-elastic-internal-origin", "kibana");
headers.set("elastic-api-version", apiVersion);
const result = await fetch(new URL(url, config.kibana.node), {
headers: headers,
...params,
});
const data = await result.json();
return data;
};

export const fetchRiskScore = async () => {
Expand Down Expand Up @@ -59,21 +67,25 @@ export const assignAssetCriticality = async ({
};

export const createRule = () => {
return kibanaFetch(`/api/detection_engine/rules`, {
method: "POST",
body: JSON.stringify({
name: "Alert Testing Query",
description: "Tests a simple query",
enabled: true,
risk_score: 70,
rule_id: 'rule-1',
severity: "high",
index: ['auditbeat-*'],
type: "query",
query: "*:*",
from: "2024-01-01T00:00:00.000Z",
from: 'now-1h',
interval: '1m',
}),
}, "2023-10-31");
return kibanaFetch(
`/api/detection_engine/rules`,
{
method: "POST",
body: JSON.stringify({
name: "Alert Testing Query",
description: "Tests a simple query",
enabled: true,
risk_score: 70,
rule_id: "rule-1",
severity: "high",
index: ["auditbeat-*"],
type: "query",
query: "*:*",
from: "2024-01-01T00:00:00.000Z",
from: "now-1h",
interval: "1m",
}),
},
"2023-10-31"
);
};
2 changes: 1 addition & 1 deletion config.dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"password": "changeme"
},
"kibana": {
"node": "http://localhost:9200",
"node": "http://127.0.0.1:5601",
"username": "elastic",
"password": "changeme"
},
Expand Down