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

Add: openvasd_get_performance() #900

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
91 changes: 69 additions & 22 deletions openvasd/openvasd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1124,8 +1124,7 @@
goto res_cleanup;

item = cJSON_GetObjectItem (result_obj, "detail");
if (item != NULL
&& cJSON_IsObject (item))
if (item != NULL && cJSON_IsObject (item))
{
cJSON *detail_obj = NULL;

Expand All @@ -1137,21 +1136,21 @@
{
detail_source_type = gvm_json_obj_str (detail_obj, "type");
detail_source_name = gvm_json_obj_str (detail_obj, "name");
detail_source_description = gvm_json_obj_str (detail_obj, "description");
detail_source_description =
gvm_json_obj_str (detail_obj, "description");
}
}

result = openvasd_result_new (gvm_json_obj_double (result_obj, "id"),
gvm_json_obj_str (result_obj, "type"),
gvm_json_obj_str (result_obj, "ip_address"),
gvm_json_obj_str (result_obj, "hostname"),
gvm_json_obj_str (result_obj, "oid"),
gvm_json_obj_int (result_obj, "port"),
gvm_json_obj_str (result_obj, "protocol"),
gvm_json_obj_str (result_obj, "message"),
detail_name, detail_value,
detail_source_type, detail_source_name,
detail_source_description);
result = openvasd_result_new (
gvm_json_obj_double (result_obj, "id"),
gvm_json_obj_str (result_obj, "type"),
gvm_json_obj_str (result_obj, "ip_address"),
gvm_json_obj_str (result_obj, "hostname"),
gvm_json_obj_str (result_obj, "oid"),
gvm_json_obj_int (result_obj, "port"),
gvm_json_obj_str (result_obj, "protocol"),
gvm_json_obj_str (result_obj, "message"), detail_name, detail_value,
detail_source_type, detail_source_name, detail_source_description);

*results = g_slist_append (*results, result);
ret = 200;
Expand Down Expand Up @@ -1310,8 +1309,7 @@
// read progress of single running hosts
cJSON *scanning;
scanning = cJSON_GetObjectItem (reader, "scanning");
if (scanning != NULL
&& cJSON_IsObject (scanning))
if (scanning != NULL && cJSON_IsObject (scanning))
{
cJSON *host = scanning->child;
while (host)
Expand Down Expand Up @@ -1591,6 +1589,56 @@
return response;
}

openvasd_resp_t
openvasd_get_performance (openvasd_connector_t conn,

Check warning on line 1593 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1593

Added line #L1593 was not covered by tests
openvasd_get_performance_opts_t opts)
{
openvasd_resp_t response = NULL;
gchar *err = NULL;
CURL *hnd = NULL;
struct curl_slist *customheader = NULL;

Check warning on line 1599 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1596-L1599

Added lines #L1596 - L1599 were not covered by tests
time_t now;
time (&now);

Check warning on line 1601 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1601

Added line #L1601 was not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
time (&now);
time (&now);


response = g_malloc0 (sizeof (struct openvasd_response));

Check warning on line 1603 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1603

Added line #L1603 was not covered by tests

if (!opts.titles || !strcmp (opts.titles, "") || opts.start < 0
|| opts.start > now || opts.end < 0 || opts.end > now)
{
response->code = RESP_CODE_ERR;
response->body =
g_strdup ("{\"error\": \"Couldn't send get_performance command "

Check warning on line 1610 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1608-L1610

Added lines #L1608 - L1610 were not covered by tests
"to scanner. Bad or missing parameters.\"}");
return response;

Check warning on line 1612 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1612

Added line #L1612 was not covered by tests
}
gchar *query =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see more consistency with the declarations. All the others are at the top of the block, even for example hnd which is first used below this.

Personally I'd also like you to drop the excess inits in general. Like response is set to NULL then it's set again basically straight away.

g_strdup_printf ("/health/performance?start=%d&end=%d&titles=%s",

Check warning on line 1615 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1615

Added line #L1615 was not covered by tests
opts.start, opts.end, opts.titles);
customheader = init_customheader (conn->apikey, FALSE);
hnd = handler (conn, GET, query, NULL, customheader, &err);

Check warning on line 1618 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1617-L1618

Added lines #L1617 - L1618 were not covered by tests
if (hnd == NULL)
{
curl_slist_free_all (customheader);
response->code = RESP_CODE_ERR;
response->body = err;
return response;

Check warning on line 1624 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1621-L1624

Added lines #L1621 - L1624 were not covered by tests
}

openvasd_send_request (hnd, NULL, response);
curl_slist_free_all (customheader);

Check warning on line 1628 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1627-L1628

Added lines #L1627 - L1628 were not covered by tests
if (response->code != RESP_CODE_ERR)
response->body = g_strdup (openvasd_vt_stream_str (conn));

Check warning on line 1630 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1630

Added line #L1630 was not covered by tests
else if (response->code == RESP_CODE_ERR)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could drop the if and just have the else because the condition is always true.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this is like this everywhere in this file. I'm happy to do them all as a PR.

{
response->body = g_strdup (

Check warning on line 1633 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1633

Added line #L1633 was not covered by tests
"{\"error\": \"Not possible to get performance information.\"}");
g_warning ("%s: Not possible to get performance information", __func__);

Check warning on line 1635 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1635

Added line #L1635 was not covered by tests
}

openvasd_reset_vt_stream (conn);
return response;

Check warning on line 1639 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1638-L1639

Added lines #L1638 - L1639 were not covered by tests
}

openvasd_resp_t
openvasd_get_scan_preferences (openvasd_connector_t conn)
{
Expand Down Expand Up @@ -1809,12 +1857,11 @@
}
}

param =
openvasd_param_new (g_strdup (gvm_json_obj_str (param_obj, "id")),
g_strdup (gvm_json_obj_str (param_obj, "name")),
g_strdup (defval),
g_strdup (gvm_json_obj_str (param_obj, "description")),
g_strdup (param_type), mandatory);
param = openvasd_param_new (
g_strdup (gvm_json_obj_str (param_obj, "id")),
g_strdup (gvm_json_obj_str (param_obj, "name")), g_strdup (defval),
g_strdup (gvm_json_obj_str (param_obj, "description")),

Check warning on line 1863 in openvasd/openvasd.c

View check run for this annotation

Codecov / codecov/patch

openvasd/openvasd.c#L1860-L1863

Added lines #L1860 - L1863 were not covered by tests
g_strdup (param_type), mandatory);
g_free (defval);
g_free (param_type);
*params = g_slist_append (*params, param);
Expand Down
10 changes: 10 additions & 0 deletions openvasd/openvasd.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ struct openvasd_scan_status
long response_code;
};

typedef struct
{
int start; /**< Start interval. */
int end; /**< End interval. */
char *titles; /**< Graph title. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
char *titles; /**< Graph title. */
gchar *titles; /**< Graph title. */

} openvasd_get_performance_opts_t;

typedef struct openvasd_response *openvasd_resp_t;

typedef enum OPENVASD_RESULT_MEMBER_INT openvasd_result_member_int_t;
Expand Down Expand Up @@ -182,6 +189,9 @@ openvasd_resp_t openvasd_get_health_ready (openvasd_connector_t);

openvasd_resp_t openvasd_get_health_started (openvasd_connector_t);

openvasd_resp_t openvasd_get_performance (openvasd_connector_t,
openvasd_get_performance_opts_t);

/* Scanner preferences */

typedef struct openvasd_param openvasd_param_t;
Expand Down