Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 1 | // Copyright 2021, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 15 | //! This module implements the IKeystoreMetrics AIDL interface, which exposes the API method for the |
| 16 | //! proxy in the system server to pull the aggregated metrics in keystore. |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 17 | use crate::error::into_logged_binder; |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 18 | use crate::ks_err; |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 19 | use crate::metrics_store::METRICS_STORE; |
| 20 | use crate::permission::KeystorePerm; |
| 21 | use crate::utils::{check_keystore_permission, watchdog as wd}; |
| 22 | use android_security_metrics::aidl::android::security::metrics::{ |
| 23 | AtomID::AtomID, |
| 24 | IKeystoreMetrics::{BnKeystoreMetrics, IKeystoreMetrics}, |
| 25 | KeystoreAtom::KeystoreAtom, |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 26 | }; |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 27 | use android_security_metrics::binder::{BinderFeatures, Interface, Result as BinderResult, Strong}; |
| 28 | use anyhow::{Context, Result}; |
Hasini Gunasinghe | 0aba68a | 2021-03-19 00:43:52 +0000 | [diff] [blame] | 29 | |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 30 | /// This struct is defined to implement IKeystoreMetrics AIDL interface. |
| 31 | pub struct Metrics; |
| 32 | |
| 33 | impl Metrics { |
| 34 | /// Create a new instance of Keystore Metrics service. |
| 35 | pub fn new_native_binder() -> Result<Strong<dyn IKeystoreMetrics>> { |
| 36 | Ok(BnKeystoreMetrics::new_binder( |
| 37 | Self, |
| 38 | BinderFeatures { set_requesting_sid: true, ..BinderFeatures::default() }, |
| 39 | )) |
| 40 | } |
| 41 | |
| 42 | fn pull_metrics(&self, atom_id: AtomID) -> Result<Vec<KeystoreAtom>> { |
| 43 | // Check permission. Function should return if this failed. Therefore having '?' at the end |
| 44 | // is very important. |
Shaquille Johnson | 9da2e1c | 2022-09-19 12:39:01 +0000 | [diff] [blame] | 45 | check_keystore_permission(KeystorePerm::PullMetrics).context(ks_err!())?; |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 46 | METRICS_STORE.get_atoms(atom_id) |
Hasini Gunasinghe | 69f0bc5 | 2021-05-15 02:08:39 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
Hasini Gunasinghe | 15891e6 | 2021-06-10 16:23:27 +0000 | [diff] [blame] | 50 | impl Interface for Metrics {} |
| 51 | |
| 52 | impl IKeystoreMetrics for Metrics { |
| 53 | fn pullMetrics(&self, atom_id: AtomID) -> BinderResult<Vec<KeystoreAtom>> { |
David Drysdale | 703bcc1 | 2024-11-26 14:15:03 +0000 | [diff] [blame] | 54 | let _wp = wd::watch_millis_with("IKeystoreMetrics::pullMetrics", 500, atom_id); |
David Drysdale | db7ddde | 2024-06-07 16:22:49 +0100 | [diff] [blame] | 55 | self.pull_metrics(atom_id).map_err(into_logged_binder) |
Hasini Gunasinghe | b714297 | 2021-02-20 03:11:27 +0000 | [diff] [blame] | 56 | } |
| 57 | } |