Skip to content

Commit

Permalink
Merge pull request #207 from Neccolini/rekor_logentry_body_info
Browse files Browse the repository at this point in the history
Changed the type of LogEntry.body from String to Body
  • Loading branch information
flavio authored Feb 7, 2023
2 parents cff5d2f + a45a504 commit 91dcdf0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
7 changes: 4 additions & 3 deletions src/rekor/apis/entries_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::{configuration, Error};
use crate::rekor::apis::ResponseContent;
use crate::rekor::models::log_entry::LogEntry;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

/// struct for typed errors of method [`create_log_entry`]
#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down Expand Up @@ -93,7 +94,7 @@ pub async fn create_log_entry(
let local_var_content = local_var_resp.text().await?;

if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
serde_json::from_str::<LogEntry>(&(parse_response(local_var_content))).map_err(Error::from)
LogEntry::from_str(&(parse_response(local_var_content))).map_err(Error::from)
} else {
let local_var_entity: Option<CreateLogEntryError> =
serde_json::from_str(&local_var_content).ok();
Expand Down Expand Up @@ -130,7 +131,7 @@ pub async fn get_log_entry_by_index(
let local_var_content = local_var_resp.text().await?;

if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
serde_json::from_str::<LogEntry>(&(parse_response(local_var_content))).map_err(Error::from)
LogEntry::from_str(&(parse_response(local_var_content))).map_err(Error::from)
} else {
let local_var_entity: Option<GetLogEntryByIndexError> =
serde_json::from_str(&local_var_content).ok();
Expand Down Expand Up @@ -170,7 +171,7 @@ pub async fn get_log_entry_by_uuid(
let local_var_content = local_var_resp.text().await?;

if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
serde_json::from_str::<LogEntry>(&(parse_response(local_var_content))).map_err(Error::from)
LogEntry::from_str(&(parse_response(local_var_content))).map_err(Error::from)
} else {
let local_var_entity: Option<GetLogEntryByUuidError> =
serde_json::from_str(&local_var_content).ok();
Expand Down
13 changes: 7 additions & 6 deletions src/rekor/models/hashedrekord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Hashedrekord {
}

/// Stores the Signature and Data struct
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Spec {
pub signature: Signature,
Expand All @@ -52,7 +52,7 @@ impl Spec {
}

/// Stores the signature format, signature of the artifact and the PublicKey struct
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Signature {
pub content: String,
Expand All @@ -69,7 +69,7 @@ impl Signature {
}

/// Stores the public key used to sign the artifact
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKey {
content: String,
Expand All @@ -86,7 +86,7 @@ impl PublicKey {
}
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Data {
pub hash: Hash,
Expand All @@ -98,15 +98,16 @@ impl Data {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[allow(non_camel_case_types)]
pub enum AlgorithmKind {
#[default]
sha256,
sha1,
}

/// Stores the algorithm used to hash the artifact and the value of the hash
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Hash {
pub algorithm: AlgorithmKind,
Expand Down
36 changes: 27 additions & 9 deletions src/rekor/models/log_entry.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use base64::{engine::general_purpose::STANDARD as BASE64_STD_ENGINE, Engine as _};
use serde::{Deserialize, Serialize};

use crate::errors::SigstoreError;
use crate::rekor::models::hashedrekord::Spec;
use crate::rekor::TreeSize;
use base64::{engine::general_purpose::STANDARD as BASE64_STD_ENGINE, Engine as _};

use serde::{Deserialize, Serialize};
use serde_json::{json, Error, Value};
use std::collections::HashMap;
use std::str::FromStr;

/// Stores the response returned by Rekor after making a new entry
#[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
Expand All @@ -12,21 +15,31 @@ pub struct LogEntry {
pub uuid: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub attestation: Option<Attestation>,
pub body: String,
pub body: Body,
pub integrated_time: i64,
pub log_i_d: String,
pub log_index: i64,
pub verification: Verification,
}

impl LogEntry {
pub fn decode_body(&self) -> Result<Body, SigstoreError> {
let decoded = BASE64_STD_ENGINE.decode(&self.body)?;
serde_json::from_slice(&decoded).map_err(SigstoreError::from)
impl FromStr for LogEntry {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut log_entry_map = serde_json::from_str::<HashMap<&str, Value>>(s)?;
log_entry_map.entry("body").and_modify(|body| {
let decoded_body = serde_json::to_value(
decode_body(body.as_str().expect("Failed to parse Body"))
.expect("Failed to decode Body"),
)
.expect("Serialization failed");
*body = json!(decoded_body);
});
let log_entry_str = serde_json::to_string(&log_entry_map)?;
Ok(serde_json::from_str::<LogEntry>(&log_entry_str).expect("Serialization failed"))
}
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Default, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Body {
#[serde(rename = "kind")]
pub kind: String,
Expand All @@ -36,6 +49,11 @@ pub struct Body {
pub spec: Spec,
}

fn decode_body(s: &str) -> Result<Body, SigstoreError> {
let decoded = BASE64_STD_ENGINE.decode(s)?;
serde_json::from_slice(&decoded).map_err(SigstoreError::from)
}

#[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Attestation {
Expand Down

0 comments on commit 91dcdf0

Please sign in to comment.