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

Change: make async the methods for preference handling. #1572

Merged
merged 2 commits into from Feb 21, 2024

Conversation

jjnicola
Copy link
Member

@jjnicola jjnicola commented Feb 16, 2024

What:
Jira: SC-1026
Also, improve the init of RedisHelper, since at this point the nvti cache and a main kb for the scan should have been created.

Usage example. (Needs a running redis instance with an up-to-date nvticache)

use std::sync::{Arc, Mutex};

use models::{AliveTestMethods, Credential, PortRange, Port, Scan, Parameter}; use openvas::{pref_handler::PreferenceHandler, openvas_redis::RedisHelper}; use redis_storage::{NameSpaceSelector, RedisCtx};

async fn main() -> Result<(), std::fmt::Error> {

    // Create an scan config
    let mut scan = Scan::default();
    scan.scan_id = Some("123-456".to_string());
    scan.target.alive_test_methods = vec![AliveTestMethods::Icmp, AliveTestMethods::TcpSyn];
    scan.target.credentials = vec![Credential {
        service: models::Service::SSH,
        port: Some(22),
        credential_type: models::CredentialType::UP {
            username: "user".to_string(),
            password: "pass".to_string(),
            privilege_credential: None,
        },
    }];
    scan.vts.push(models::VT {
        oid: "1.3.6.1.4.1.25623.1.0.112771".to_string(),
        parameters: vec![Parameter {
            id: 1,
            value: "llala".to_string()

        }]
    });
    scan.target.excluded_hosts = vec!["127.0.0.1".to_string()];
    scan.target.hosts = vec!["127.0.0.2".to_string()];
    scan.target.ports = vec![Port {
        protocol: Some(models::Protocol::TCP),
        range: vec![
            PortRange {
                start: 22,
                end: Some(25),
            },
            PortRange {
                start: 80,
                end: None,
            },
        ],
    }];

    let redis_url = "unix:///run/redis-openvas/redis.sock";
    // In this example, a fix db is taken,but the next free can be taken, using the name space selector NameSpaceSelector::Free
    let mut rctx = RedisCtx::open(redis_url, &[NameSpaceSelector::Fix(6)]).unwrap();
    rctx.delete_namespace().unwrap();
    // Create a redis connection to the nvticache, and a connection to the mainkb().
    let cache = RedisCtx::open(redis_url, &[NameSpaceSelector::Key("nvticache")]).unwrap();

    // New redis helper with access to the main kb for storing the preferences, and access to the nvticache
    // for getting info to build preferences.
    let rc = RedisHelper::<RedisCtx>::init(Arc::new(Mutex::new(cache)),Arc::new(Mutex::new(rctx))).unwrap();

    let mut p = PreferenceHandler::new(scan, rc);
    Ok(p.prepare_preferences_for_openvas().await.expect("aaa"))

}

Why:

How:

Checklist:

  • Tests
  • PR merge commit message adjusted

@jjnicola jjnicola requested a review from a team as a code owner February 16, 2024 14:01
Also, improve the creating the RedisHelper, since at this point the nvti cache and a main kb for the scan should have been created.

Usage example. (Needs a running redis instance with an up-to-date nvticache)

```

```use std::sync::{Arc, Mutex};

use models::{AliveTestMethods, Credential, PortRange, Port, Scan, Parameter};
use openvas::{pref_handler::PreferenceHandler, openvas_redis::RedisHelper};
use redis_storage::{NameSpaceSelector, RedisCtx};

async fn main() -> Result<(), std::fmt::Error> {

    // Create an scan config
    let mut scan = Scan::default();
    scan.scan_id = Some("123-456".to_string());
    scan.target.alive_test_methods = vec![AliveTestMethods::Icmp, AliveTestMethods::TcpSyn];
    scan.target.credentials = vec![Credential {
        service: models::Service::SSH,
        port: Some(22),
        credential_type: models::CredentialType::UP {
            username: "user".to_string(),
            password: "pass".to_string(),
            privilege_credential: None,
        },
    }];
    scan.vts.push(models::VT {
        oid: "1.3.6.1.4.1.25623.1.0.112771".to_string(),
        parameters: vec![Parameter {
            id: 1,
            value: "llala".to_string()

        }]
    });
    scan.target.excluded_hosts = vec!["127.0.0.1".to_string()];
    scan.target.hosts = vec!["127.0.0.2".to_string()];
    scan.target.ports = vec![Port {
        protocol: Some(models::Protocol::TCP),
        range: vec![
            PortRange {
                start: 22,
                end: Some(25),
            },
            PortRange {
                start: 80,
                end: None,
            },
        ],
    }];

    let redis_url = "unix:///run/redis-openvas/redis.sock";
    // In this example, a fix db is taken,but the next free can be taken, using the name space selector NameSpaceSelector::Free
    let mut rctx = RedisCtx::open(redis_url, &[NameSpaceSelector::Fix(6)]).unwrap();
    rctx.delete_namespace().unwrap();
    // Create a redis connection to the nvticache, and a connection to the mainkb().
    let cache = RedisCtx::open(redis_url, &[NameSpaceSelector::Key("nvticache")]).unwrap();

    // New redis helper with access to the main kb for storing the preferences, and access to the nvticache
    // for getting info to build preferences.
    let rc = RedisHelper::<RedisCtx>::init(Arc::new(Mutex::new(cache)),Arc::new(Mutex::new(rctx))).unwrap();

    let mut p = PreferenceHandler::new(scan, rc);
    Ok(p.prepare_preferences_for_openvas().await.expect("aaa"))

}
@jjnicola jjnicola enabled auto-merge (squash) February 19, 2024 09:00
@jjnicola jjnicola mentioned this pull request Feb 20, 2024
2 tasks
Copy link
Member

@nichtsfrei nichtsfrei left a comment

Choose a reason for hiding this comment

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

Some minor things.

@jjnicola jjnicola merged commit c741857 into main Feb 21, 2024
18 checks passed
@jjnicola jjnicola deleted the async-prefs-handler branch February 21, 2024 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants