-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor signing key loading into module
Move the function to load the signing keys into a module. Add errors messages with information about which file/dir failed to open/read.
- Loading branch information
Felix Obenhuber
committed
Sep 7, 2020
1 parent
490ba7d
commit 89009a8
Showing
3 changed files
with
62 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) 2020 ESRLabs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use crate::SETTINGS; | ||
use anyhow::{Context, Result}; | ||
use async_std::fs; | ||
use ed25519_dalek::PublicKey; | ||
use futures::{AsyncReadExt, StreamExt}; | ||
use log::info; | ||
use std::collections::HashMap; | ||
|
||
pub async fn load() -> Result<HashMap<String, PublicKey>> { | ||
let key_dir = &SETTINGS.directories.key_dir; | ||
let mut signing_keys = HashMap::new(); | ||
let mut key_dir = fs::read_dir(&key_dir) | ||
.await | ||
.with_context(|| format!("Failed to open {}", key_dir.display()))?; | ||
while let Some(entry) = key_dir.next().await { | ||
let entry = entry.context("Invalid key dir entry")?; | ||
let path = entry.path(); | ||
if let Some(extension) = path.extension() { | ||
if extension == "pub" && path.is_file().await { | ||
if let Some(key_id) = path.file_stem().map(|s| s.to_string_lossy().to_string()) { | ||
info!("Loading signing key {}", key_id); | ||
let mut sign_key_file = fs::File::open(&path) | ||
.await | ||
.with_context(|| format!("Failed to open {}", path.display()))?; | ||
let mut key_bytes = Vec::new(); | ||
sign_key_file | ||
.read_to_end(&mut key_bytes) | ||
.await | ||
.with_context(|| format!("Failed to read {}", path.display()))?; | ||
let key = PublicKey::from_bytes(&key_bytes) | ||
.with_context(|| format!("Failed to load key from {}", path.display()))?; | ||
signing_keys.insert(key_id, key); | ||
} | ||
} | ||
} | ||
} | ||
Ok(signing_keys) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters