1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::v_authorization::common::AuthorizationContext;
use io::Error;
use lmdb_rs_m::core::{Database, EnvCreateNoLock, EnvCreateNoMetaSync, EnvCreateNoSync, EnvCreateReadOnly};
use lmdb_rs_m::{DbFlags, EnvBuilder, Environment, MdbError};
use std::io::ErrorKind;
use std::time;
use std::{io, thread};
use v_authorization::common::{Storage, Trace};
use v_authorization::*;

const DB_PATH: &str = "./data/acl-indexes/";

pub struct LmdbAzContext {
    env: Environment,
    authorize_counter: u64,
    max_authorize_counter: u64,
}

fn open(max_read_counter: u64) -> LmdbAzContext {
    let env_builder = EnvBuilder::new().flags(EnvCreateNoLock | EnvCreateReadOnly | EnvCreateNoMetaSync | EnvCreateNoSync);
    loop {
        match env_builder.open(DB_PATH, 0o644) {
            Ok(env) => {
                info!("LIB_AZ: Opened environment {}", DB_PATH);
                return LmdbAzContext {
                    env,
                    authorize_counter: 0,
                    max_authorize_counter: max_read_counter,
                };
            },
            Err(e) => {
                error!("Authorize: Err opening environment: {:?}", e);
                thread::sleep(time::Duration::from_secs(3));
                error!("Retry");
            },
        }
    }
}

impl LmdbAzContext {
    pub fn new(max_read_counter: u64) -> LmdbAzContext {
        open(max_read_counter)
    }
}

impl Default for LmdbAzContext {
    fn default() -> Self {
        Self::new(u64::MAX)
    }
}

impl AuthorizationContext for LmdbAzContext {
    fn authorize(&mut self, uri: &str, user_uri: &str, request_access: u8, _is_check_for_reload: bool) -> Result<u8, std::io::Error> {
        let mut t = Trace {
            acl: &mut String::new(),
            is_acl: false,
            group: &mut String::new(),
            is_group: false,
            info: &mut String::new(),
            is_info: false,
            str_num: 0,
        };

        self.authorize_and_trace(uri, user_uri, request_access, _is_check_for_reload, &mut t)
    }

    fn authorize_and_trace(&mut self, uri: &str, user_uri: &str, request_access: u8, _is_check_for_reload: bool, trace: &mut Trace) -> Result<u8, std::io::Error> {
        self.authorize_counter += 1;
        //info!("az counter={}", self.authorize_counter);
        if self.authorize_counter >= self.max_authorize_counter {
            info!("az reopen, counter > {}", self.max_authorize_counter);
            self.authorize_counter = 0;
            let env_builder = EnvBuilder::new().flags(EnvCreateNoLock | EnvCreateReadOnly | EnvCreateNoMetaSync | EnvCreateNoSync);

            match env_builder.open(DB_PATH, 0o644) {
                Ok(env1) => {
                    self.env = env1;
                },
                Err(e1) => {
                    return Err(Error::new(ErrorKind::Other, format!("Authorize: Err opening environment: {:?}", e1)));
                },
            }
        }

        match _f_authorize(&mut self.env, uri, user_uri, request_access, _is_check_for_reload, trace) {
            Ok(r) => {
                return Ok(r);
            },
            Err(e) => {
                info!("reopen");
                let env_builder = EnvBuilder::new().flags(EnvCreateNoLock | EnvCreateReadOnly | EnvCreateNoMetaSync | EnvCreateNoSync);

                match env_builder.open(DB_PATH, 0o644) {
                    Ok(env1) => {
                        self.env = env1;
                    },
                    Err(e1) => {
                        error!("Authorize: Err opening environment: {:?}", e1);
                        return Err(e);
                    },
                }
            },
        }
        _f_authorize(&mut self.env, uri, user_uri, request_access, _is_check_for_reload, trace)
    }
}

pub struct AzLmdbStorage<'a> {
    db: &'a Database<'a>,
}

impl<'a> Storage for AzLmdbStorage<'a> {
    fn get(&self, key: &str) -> io::Result<Option<String>> {
        match self.db.get::<String>(&key) {
            Ok(val) => Ok(Some(val)),
            Err(e) => match e {
                MdbError::NotFound => Ok(None),
                _ => Err(Error::new(ErrorKind::Other, format!("Authorize: db.get {:?}, {}", e, key))),
            },
        }
    }

    fn fiber_yield(&self) {}
}

fn _f_authorize(env: &mut Environment, uri: &str, user_uri: &str, request_access: u8, _is_check_for_reload: bool, trace: &mut Trace) -> Result<u8, std::io::Error> {
    let db_handle = match env.get_default_db(DbFlags::empty()) {
        Ok(db_handle_res) => db_handle_res,
        Err(e) => {
            return Err(Error::new(ErrorKind::Other, format!("Authorize: Err opening db handle: {:?}", e)));
        },
    };

    let txn = match env.get_reader() {
        Ok(txn1) => txn1,
        Err(e) => {
            return Err(Error::new(ErrorKind::Other, format!("Authorize:CREATING TRANSACTION {:?}", e)));
        },
    };

    let db = txn.bind(&db_handle);
    let storage = AzLmdbStorage {
        db: &db,
    };

    authorize(uri, user_uri, request_access, &storage, trace)
}