Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the gcp option in cast wallet list #8232

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/cast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ serde.workspace = true
# aws-kms
aws-sdk-kms = { version = "1", default-features = false, optional = true }

# gcp-kms
gcloud-sdk = { version = "0.24", default-features = false, optional = true }

# bin
foundry-cli.workspace = true

Expand Down Expand Up @@ -96,6 +99,7 @@ openssl = ["foundry-cli/openssl"]
asm-keccak = ["alloy-primitives/asm-keccak"]
jemalloc = ["dep:tikv-jemallocator"]
aws-kms = ["foundry-wallets/aws-kms", "dep:aws-sdk-kms"]
gcp-kms = ["foundry-wallets/gcp-kms", "dep:gcloud-sdk"]
isolate-by-default = ["foundry-config/isolate-by-default"]

[[bench]]
Expand Down
18 changes: 17 additions & 1 deletion crates/cast/bin/cmd/wallet/list.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Parser;
use eyre::Result;
use std::env;

use foundry_common::fs;
use foundry_config::Config;
Expand All @@ -25,6 +26,10 @@ pub struct ListArgs {
#[arg(long, hide = !cfg!(feature = "aws-kms"))]
aws: bool,

/// List accounts from Google Cloud KMS.
#[arg(long, hide = !cfg!(feature = "gcp-kms"))]
gcp: bool,

/// List all configured accounts.
#[arg(long, group = "hw-wallets")]
all: bool,
Expand All @@ -37,7 +42,10 @@ pub struct ListArgs {
impl ListArgs {
pub async fn run(self) -> Result<()> {
// list local accounts as files in keystore dir, no need to unlock / provide password
if self.dir.is_some() || self.all || (!self.ledger && !self.trezor && !self.aws) {
if self.dir.is_some() ||
self.all ||
(!self.ledger && !self.trezor && !self.aws && !self.gcp)
{
let _ = self.list_local_senders();
}

Expand All @@ -47,6 +55,7 @@ impl ListArgs {
.mnemonic_indexes(Some(vec![0]))
.trezor(self.trezor || self.all)
.aws(self.aws || self.all)
.gcp(self.gcp || (self.all && gcp_env_vars_set()))
.interactives(0)
.build()
.expect("build multi wallet");
Expand Down Expand Up @@ -106,3 +115,10 @@ impl ListArgs {
Ok(())
}
}

fn gcp_env_vars_set() -> bool {
let required_vars =
["GCP_PROJECT_ID", "GCP_LOCATION", "GCP_KEY_RING", "GCP_KEY_NAME", "GCP_KEY_VERSION"];

required_vars.iter().all(|&var| env::var(var).is_ok())
}
44 changes: 44 additions & 0 deletions crates/wallets/src/multi_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ pub struct MultiWalletOpts {
/// Use AWS Key Management Service.
#[arg(long, help_heading = "Wallet options - remote", hide = !cfg!(feature = "aws-kms"))]
pub aws: bool,

/// Use Google Cloud Key Management Service.
#[arg(long, help_heading = "Wallet options - remote", hide = !cfg!(feature = "gcp-kms"))]
pub gcp: bool,
}

impl MultiWalletOpts {
Expand All @@ -238,6 +242,9 @@ impl MultiWalletOpts {
if let Some(aws_signers) = self.aws_signers().await? {
signers.extend(aws_signers);
}
if let Some(gcp_signer) = self.gcp_signers().await? {
signers.extend(gcp_signer);
}
if let Some((pending_keystores, unlocked)) = self.keystores()? {
pending.extend(pending_keystores);
signers.extend(unlocked);
Expand Down Expand Up @@ -394,6 +401,43 @@ impl MultiWalletOpts {

Ok(None)
}

/// Returns a list of GCP signers if the GCP flag is set.
///
/// The GCP signers are created from the following environment variables:
/// - GCP_PROJECT_ID: The GCP project ID. e.g. `my-project-123456`.
/// - GCP_LOCATION: The GCP location. e.g. `us-central1`.
/// - GCP_KEY_RING: The GCP key ring name. e.g. `my-key-ring`.
/// - GCP_KEY_NAME: The GCP key name. e.g. `my-key`.
/// - GCP_KEY_VERSION: The GCP key version. e.g. `1`.
///
/// For more information on GCP KMS, see the [official documentation](https://cloud.google.com/kms/docs).
pub async fn gcp_signers(&self) -> Result<Option<Vec<WalletSigner>>> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a few docs here that describe what this does?

#[cfg(feature = "gcp-kms")]
if self.gcp {
let mut wallets = vec![];

let project_id = std::env::var("GCP_PROJECT_ID")?;
let location = std::env::var("GCP_LOCATION")?;
let key_ring = std::env::var("GCP_KEY_RING")?;
let key_names = std::env::var("GCP_KEY_NAME")?;
let key_version = std::env::var("GCP_KEY_VERSION")?;

let gcp_signer = WalletSigner::from_gcp(
project_id,
location,
key_ring,
key_names,
key_version.parse()?,
)
.await?;
wallets.push(gcp_signer);

return Ok(Some(wallets));
}

Ok(None)
}
}

#[cfg(test)]
Expand Down
Loading