Skip to content

Commit

Permalink
Add updated date/time to secret meta data. (#76)
Browse files Browse the repository at this point in the history
Closes #20.
  • Loading branch information
tmpfs authored Jul 20, 2022
1 parent 1e896e4 commit 9e5faaa
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
5 changes: 4 additions & 1 deletion workspace/client/src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ fn exec_program(

drop(reader);

let (uuid, secret_meta, secret_data) =
let (uuid, mut secret_meta, secret_data) =
result.ok_or(Error::SecretNotAvailable(secret.clone()))?;

let result =
Expand Down Expand Up @@ -717,6 +717,8 @@ fn exec_program(

let summary = keeper.summary().clone();

secret_meta.touch();

let event = keeper
.update(&uuid, secret_meta, edited_secret)?
.ok_or(Error::SecretNotAvailable(secret))?;
Expand Down Expand Up @@ -792,6 +794,7 @@ fn exec_program(

let mut secret_meta = keeper.decrypt_meta(&meta_aead)?;
secret_meta.set_label(label);
secret_meta.touch();
let meta_aead = keeper.encrypt_meta(&secret_meta)?;

let (commit, _) = Vault::commit_hash(&meta_aead, &secret_aead)?;
Expand Down
5 changes: 3 additions & 2 deletions workspace/client/src/shell/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ pub(super) fn secret(
secret_data: &Secret,
) -> Result<()> {
let heading =
format!("[{}] {}", secret_meta.short_name(), secret_meta.label());
format!("[{}] {}", secret_meta.short_name(), secret_meta.last_updated().to_date_time()?);

let banner = Banner::new()
.padding(Padding::one())
.text(Cow::Owned(heading));
.text(Cow::Owned(heading))
.text(Cow::Borrowed(secret_meta.label()));

let banner = match secret_data {
Secret::Note(text) => {
Expand Down
4 changes: 4 additions & 0 deletions workspace/core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ pub enum Error {
#[error(transparent)]
TimeFormat(#[from] time::error::Format),

/// Error generated creating format descriptions for date formatting.
#[error(transparent)]
InvalidFormat(#[from] time::error::InvalidFormatDescription),

/// Error generated parsing PEM files.
#[error(transparent)]
Pem(#[from] pem::PemError),
Expand Down
20 changes: 18 additions & 2 deletions workspace/core/src/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{collections::HashMap, fmt, str::FromStr};
use url::Url;
use uuid::Uuid;

use crate::Error;
use crate::{Error, Timestamp};

fn serialize_secret_string<S>(
secret: &SecretString,
Expand Down Expand Up @@ -135,12 +135,14 @@ pub struct SecretMeta {
label: String,
/// Kind of the secret.
kind: u8,
/// Last updated timestamp.
last_updated: Timestamp,
}

impl SecretMeta {
/// Create new meta data for a secret.
pub fn new(label: String, kind: u8) -> Self {
Self { label, kind }
Self { label, kind, last_updated: Default::default() }
}

/// The label for the secret.
Expand All @@ -158,6 +160,16 @@ impl SecretMeta {
&self.kind
}

/// Update the last updated timestamp to now.
pub fn touch(&mut self) {
self.last_updated = Default::default();
}

/// The last updated date and time.
pub fn last_updated(&self) -> &Timestamp {
&self.last_updated
}

/// Get an abbreviated short name based
/// on the kind of secret.
pub fn short_name(&self) -> &str {
Expand All @@ -176,6 +188,7 @@ impl SecretMeta {
impl Encode for SecretMeta {
fn encode(&self, writer: &mut BinaryWriter) -> BinaryResult<()> {
writer.write_u8(self.kind)?;
self.last_updated.encode(&mut *writer)?;
writer.write_string(&self.label)?;
Ok(())
}
Expand All @@ -184,6 +197,9 @@ impl Encode for SecretMeta {
impl Decode for SecretMeta {
fn decode(&mut self, reader: &mut BinaryReader) -> BinaryResult<()> {
self.kind = reader.read_u8()?;
let mut last_updated: Timestamp = Default::default();
last_updated.decode(&mut *reader)?;
self.last_updated = last_updated;
self.label = reader.read_string()?;
Ok(())
}
Expand Down
14 changes: 12 additions & 2 deletions workspace/core/src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ use std::fmt;
use filetime::FileTime;

use time::{
format_description::well_known::{Rfc2822, Rfc3339},
format_description::{self, well_known::{Rfc2822, Rfc3339}},
Duration, OffsetDateTime, UtcOffset,
};

use crate::Result;

/// Timestamp for events and log records.
#[derive(Debug, Clone, Serialize, Deserialize, PartialOrd, Eq, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, Ord, PartialOrd, Eq, PartialEq)]
pub struct Timestamp(OffsetDateTime);

impl Default for Timestamp {
Expand All @@ -28,6 +28,16 @@ impl Default for Timestamp {
}

impl Timestamp {

/// Convert to a short human-readable date and time without
/// the timezone offset.
pub fn to_date_time(&self) -> Result<String> {
let format = format_description::parse(
"[day] [month repr:short] [year] [hour]:[minute]:[second]",
)?;
Ok(self.0.format(&format)?)
}

/// Convert this timestamp to a RFC2822 formatted string.
pub fn to_rfc2822(&self) -> Result<String> {
Ok(Timestamp::rfc2822(&self.0)?)
Expand Down

0 comments on commit 9e5faaa

Please sign in to comment.