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