Skip to content

Commit

Permalink
Merge pull request #152 from lkatalin/parse_logentry
Browse files Browse the repository at this point in the history
Updates for parsing hashedrekord LogEntry
  • Loading branch information
flavio authored Oct 24, 2022
2 parents a606ece + 112ccee commit 0725c0d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 29 deletions.
9 changes: 1 addition & 8 deletions examples/rekor/create_log_entry/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,14 @@ async fn main() {
.unwrap_or(&HASH.to_string())
.to_owned(),
);
let data = Data::new(
hash,
Url::parse(flags.get_one::<String>("url").unwrap_or(&URL.to_string())).unwrap(),
);
let data = Data::new(hash);
let public_key = PublicKey::new(
flags
.get_one::<String>("public_key")
.unwrap_or(&PUBLIC_KEY.to_string())
.to_owned(),
);
let signature = Signature::new(
flags
.get_one::<String>("key_format")
.unwrap_or(&KEY_FORMAT.to_string())
.to_owned(),
flags
.get_one("signature")
.unwrap_or(&SIGNATURE.to_string())
Expand Down
9 changes: 1 addition & 8 deletions examples/rekor/search_log_query/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,14 @@ async fn main() {
.unwrap_or(&HASH.to_string())
.to_owned(),
);
let data = Data::new(
hash,
Url::parse(flags.get_one::<String>("url").unwrap_or(&URL.to_string())).unwrap(),
);
let data = Data::new(hash);
let public_key = PublicKey::new(
flags
.get_one::<String>("public_key")
.unwrap_or(&PUBLIC_KEY.to_string())
.to_owned(),
);
let signature = Signature::new(
flags
.get_one::<String>("key_format")
.unwrap_or(&KEY_FORMAT.to_string())
.to_owned(),
flags
.get_one::<String>("signature")
.unwrap_or(&SIGNATURE.to_string())
Expand Down
28 changes: 16 additions & 12 deletions src/rekor/models/hashedrekord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
* Generated by: https://openapi-generator.tech
*/

use base64::decode;
use serde::{Deserialize, Serialize};
use url::Url;

use crate::errors::SigstoreError;

/// Hashedrekord : Hashed Rekord object
Expand Down Expand Up @@ -38,8 +40,8 @@ impl Hashedrekord {
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Spec {
signature: Signature,
data: Data,
pub signature: Signature,
pub data: Data,
}

// Design a SPEC struct
Expand All @@ -53,15 +55,13 @@ impl Spec {
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Signature {
format: String,
content: String,
public_key: PublicKey,
pub content: String,
pub public_key: PublicKey,
}

impl Signature {
pub fn new(format: String, content: String, public_key: PublicKey) -> Signature {
pub fn new(content: String, public_key: PublicKey) -> Signature {
Signature {
format,
content,
public_key,
}
Expand All @@ -79,18 +79,22 @@ impl PublicKey {
pub fn new(content: String) -> PublicKey {
PublicKey { content }
}

pub fn decode(&self) -> Result<String, SigstoreError> {
let decoded = decode(&self.content)?;
String::from_utf8(decoded).map_err(|e| SigstoreError::from(e.utf8_error()))
}
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Data {
hash: Hash,
url: Url,
pub hash: Hash,
}

impl Data {
pub fn new(hash: Hash, url: Url) -> Data {
Data { hash, url }
pub fn new(hash: Hash) -> Data {
Data { hash }
}
}

Expand Down
23 changes: 22 additions & 1 deletion src/rekor/models/log_entry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::rekor::TreeSize;
use base64::decode;
use serde::{Deserialize, Serialize};

use crate::errors::SigstoreError;
use crate::rekor::models::hashedrekord::Spec;
use crate::rekor::TreeSize;

/// Stores the response returned by Rekor after making a new entry
#[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand All @@ -15,6 +19,23 @@ pub struct LogEntry {
pub verification: Verification,
}

impl LogEntry {
pub fn decode_body(&self) -> Result<Body, SigstoreError> {
let decoded = decode(&self.body)?;
serde_json::from_slice(&decoded).map_err(SigstoreError::from)
}
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Body {
#[serde(rename = "kind")]
pub kind: String,
#[serde(rename = "apiVersion")]
pub api_version: String,
#[serde(rename = "spec")]
pub spec: Spec,
}

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

0 comments on commit 0725c0d

Please sign in to comment.