Skip to content

Commit

Permalink
genpolicy: adjust format
Browse files Browse the repository at this point in the history
Signed-off-by: Saul Paredes <[email protected]>
  • Loading branch information
Redent0r committed Dec 20, 2023
1 parent 74a3ba5 commit 37552e3
Showing 1 changed file with 11 additions and 24 deletions.
35 changes: 11 additions & 24 deletions src/tools/genpolicy/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@ use crate::verity;

use anyhow::{anyhow, Result};
use docker_credential::{CredentialRetrievalError, DockerCredential};
use fs2::FileExt;
use log::warn;
use log::{debug, info, LevelFilter};
use oci_distribution::client::{linux_amd64_resolver, ClientConfig};
use oci_distribution::{manifest, secrets::RegistryAuth, Client, Reference};
use serde::{Deserialize, Serialize};
use sha2::{digest::typenum::Unsigned, digest::OutputSizeUser, Sha256};
use std::{io, io::Seek, io::Write, path::Path};
use tokio::{io::AsyncWriteExt};
use std::io::{BufWriter};
use std::fs::OpenOptions;
use fs2::FileExt;

use std::io::BufWriter;
use std::{io, io::Seek, io::Write, path::Path};
use tokio::io::AsyncWriteExt;

/// Container image properties obtained from an OCI repository.
#[derive(Clone, Debug, Default)]
Expand Down Expand Up @@ -232,10 +231,8 @@ async fn get_image_layers(
for layer in &manifest.layers {
if layer
.media_type
.eq(manifest::IMAGE_DOCKER_LAYER_GZIP_MEDIA_TYPE) ||
layer
.media_type
.eq(manifest::IMAGE_LAYER_GZIP_MEDIA_TYPE)
.eq(manifest::IMAGE_DOCKER_LAYER_GZIP_MEDIA_TYPE)
|| layer.media_type.eq(manifest::IMAGE_LAYER_GZIP_MEDIA_TYPE)
{
if layer_index < config_layer.rootfs.diff_ids.len() {
layers.push(ImageLayer {
Expand Down Expand Up @@ -300,9 +297,7 @@ async fn get_verity_hash(
)
.await
{
error_message = format!(
"Failed to create verity hash for {layer_digest}, error {e}"
);
error_message = format!("Failed to create verity hash for {layer_digest}, error {e}");
error = true
};

Expand Down Expand Up @@ -335,11 +330,7 @@ async fn get_verity_hash(
}

// the store is a json file that matches layer hashes to verity hashes
fn add_verity_to_store(
cache_file: &str,
diff_id: &str,
verity_hash: &str,
) -> Result<()> {
fn add_verity_to_store(cache_file: &str, diff_id: &str, verity_hash: &str) -> Result<()> {
// open the json file in read mode, create it if it doesn't exist
let read_file = OpenOptions::new()
.read(true)
Expand All @@ -355,7 +346,7 @@ fn add_verity_to_store(
};

// Add new data to the deserialized JSON
data.push(ImageLayer{
data.push(ImageLayer {
diff_id: diff_id.to_string(),
verity_hash: verity_hash.to_string(),
});
Expand All @@ -364,9 +355,7 @@ fn add_verity_to_store(
let serialized = serde_json::to_string_pretty(&data)?;

// Open the JSON file to write
let file = OpenOptions::new()
.write(true)
.open(cache_file)?;
let file = OpenOptions::new().write(true).open(cache_file)?;

// try to lock the file, if it fails, get the error
let result = file.try_lock_exclusive();
Expand All @@ -390,9 +379,7 @@ fn read_verity_from_store(cache_file: &str, diff_id: &str) -> Result<String> {
return Ok("".to_string());
}

let file = OpenOptions::new()
.read(true)
.open(cache_file)?;
let file = OpenOptions::new().read(true).open(cache_file)?;

// If the file is empty, return empty string
if file.metadata()?.len() == 0 {
Expand Down

0 comments on commit 37552e3

Please sign in to comment.