Skip to content

Commit

Permalink
feat(proof_storage): azure auth and get_keys_count implementation
Browse files Browse the repository at this point in the history
Co-authored-by: Aneta Tsvetkova <anetastsvetkova@gmail.com>
  • Loading branch information
Xearty and Owliie committed Mar 18, 2024
1 parent 5943c6c commit ea0c8cc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@ use std::env;
use super::proof_storage::ProofStorage;
use anyhow::Result;
use async_trait::async_trait;
use azure_storage::StorageCredentials;
use azure_storage_blobs::prelude::{ClientBuilder, ContainerClient};
use azure_storage::ConnectionString;

use azure_storage_blobs::container::operations::BlobItem;
use azure_storage_blobs::prelude::*;
use futures::StreamExt;

pub struct AzureStorage {
container_client: ContainerClient,
}

impl AzureStorage {
pub fn new(account: String, container: String) -> AzureStorage {
let access_key = env::var("STORAGE_ACCESS_KEY").expect("missing STORAGE_ACCOUNT_KEY");

let storage_credentials =
StorageCredentials::access_key(account.clone(), access_key.clone());
pub fn new(container: String) -> AzureStorage {
let connection_string = env::var("AZURE_CONNECTION_STRING").unwrap();
let account = env::var("STORAGE_ACCOUNT").unwrap();

let container_client =
ClientBuilder::new(account, storage_credentials).container_client(&container);
let container_client = ClientBuilder::new(
account,
ConnectionString::new(connection_string.as_str())
.unwrap()
.storage_credentials()
.unwrap(),
)
.container_client(&container);

AzureStorage { container_client }
}
Expand Down Expand Up @@ -60,7 +66,26 @@ impl ProofStorage for AzureStorage {
Ok(())
}

async fn get_keys_count(&mut self, _pattern: String) -> usize {
unimplemented!()
async fn get_keys_count(&mut self, pattern: String) -> usize {
let mut count = 0;
let pattern = glob::Pattern::new(&pattern).unwrap();
let mut iter = self.container_client.list_blobs().into_stream();

while let Some(Ok(reponse)) = iter.next().await {
count += reponse
.blobs
.items
.iter()
.filter(|&item| {
if let BlobItem::Blob(blob) = item {
pattern.matches(&blob.name)
} else {
false
}
})
.count();
}

count
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ pub async fn create_proof_storage(args: &ArgMatches) -> Box<dyn ProofStorage> {
}
ProofStorageType::Azure => {
dotenv::from_path("../.env").ok();
Box::new(AzureStorage::new(
args.value_of("azure_account").unwrap().to_string(),
args.value_of("azure_container").unwrap().to_string(),
))
Box::new(AzureStorage::new(env::var("STORAGE_CONTAINER").unwrap()))
}
ProofStorageType::Aws => {
dotenv::from_path("../.env").ok();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BlobServiceClient, ContainerClient } from "@azure/storage-blob";
import { IProofStorage } from "./proof_storage";

export class AzureStorage implements IProofStorage {
export class AzureBlobStorage implements IProofStorage {
private containerClient: ContainerClient;

constructor(connectionString: string, containerName: string) {
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
this.containerClient = blobServiceClient.getContainerClient(containerName);
constructor(container: string) {
const blobServiceClient = BlobServiceClient.fromConnectionString(process.env.AZURE_CONNECTION_STRING!);
this.containerClient = blobServiceClient.getContainerClient(container);
}

async getProof(key: string): Promise<Buffer | null> {
Expand Down
12 changes: 4 additions & 8 deletions beacon-light-client/plonky2/proof_storage/proof_storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { S3Storage } from './aws_proof_storage';
import { AzureStorage } from './azure_proof_storage';
import { AzureBlobStorage } from './azure_proof_storage';
import { FileStorage } from './file_proof_storage';
import { RedisStorage } from './redis_proof_storage';

Expand Down Expand Up @@ -38,17 +38,13 @@ export function createProofStorage(options: any): IProofStorage {
return new FileStorage(folder);
}
case 'azure': {
const account = options['azure-account'];
const container = options['azure-container'];
const container = process.env.STORAGE_CONTAINER;

if (account === undefined) {
throw new Error('azure-account was not provided');
}
if (container === undefined) {
throw new Error('azure-container was not provided');
throw new Error('STORAGE_CONTAINER env var was not provided')
}

return new AzureStorage('placeholder', 'placeholder');
return new AzureBlobStorage(container);

}
case 'aws': {
Expand Down

0 comments on commit ea0c8cc

Please sign in to comment.