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

add vault support #1

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
feat: add support for setting ca certificate
  • Loading branch information
helder-moreira committed Feb 13, 2023
commit f167e6594ee4bf38554421415f29738b8e562b2d
2 changes: 2 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ aes-kw = { version = "0.2.1", features = ["std"], optional = true}
rsa = { version = "0.6.1", default = true, optional = true}
rand = { version = "0.7", optional = true}
aes-gcm = { version = "0.10.1", optional = true}
rustls = "0.20.8"
rustls-pemfile = "1.0.2"

[dev-dependencies]
abscissa_core = { version = "0.7", features = ["testing"] }
Expand Down
11 changes: 11 additions & 0 deletions Dockerfile.hashicorp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM rust:1.66-alpine AS builder

RUN apk add --no-cache g++ zlib zlib-dev

WORKDIR /app
COPY . /app
RUN cargo build --release --features=hashicorp

FROM alpine
COPY --from=builder /app/target/release/tmkms /usr/local/bin/
ENTRYPOINT ["/usr/local/bin/tmkms"]
1 change: 1 addition & 0 deletions src/commands/hashicorp/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl Runnable for TestCommand {
&config.api_endpoint,
&config.access_token,
&self.pk_name,
"",
)
.unwrap_or_else(|e| panic!("Unable to connect to Vault {} {}", config.api_endpoint, e));

Expand Down
1 change: 1 addition & 0 deletions src/commands/hashicorp/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl UploadCommand {
&config.api_endpoint,
&vault_token,
&self.pk_name,
&config.ca_cert,
)
.unwrap_or_else(|_| panic!("Unable to connect to Vault at {}", config.api_endpoint));

Expand Down
2 changes: 2 additions & 0 deletions src/commands/init/templates/keyring/hashicorp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ api_endpoint= "http://127.0.0.1:8200"
access_token="hvs.CAESINi91lCOFj-_dOGiUfpdZUPKk93LD8YyHz-qZcYLVwH_Gh4KHGh2cy5kdXV1T2tpcXliakFFblU1SUpqanczYjU"
#Vault's transit secret engine key - vault write transit/keys/<cosmoshub-sign-key> type=ed25519
pk_name="cosmoshub-sign-key"
#Path to CA certificate to be used to connect to vault
ca_cert=""
3 changes: 3 additions & 0 deletions src/config/provider/hashicorp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ pub struct HashiCorpConfig {

/// Vault's key name with ed25519 pub+priv key
pub pk_name: String,

/// Path to CA certificate to connect to vault
pub ca_cert: String,
}
1 change: 1 addition & 0 deletions src/keyring/providers/hashicorp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub fn init(
&config.api_endpoint,
&config.access_token,
&config.pk_name,
&config.ca_cert,
)
.unwrap_or_else(|_| {
panic!(
Expand Down
32 changes: 25 additions & 7 deletions src/keyring/providers/hashicorp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use std::collections::{BTreeMap, HashMap};
use super::error::Error;

use std::time::Duration;
use std::{fs, io, sync};
use ureq::Agent;
use rustls::RootCertStore;
use rustls_pemfile;

use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -120,20 +123,35 @@ impl std::fmt::Display for CreateKeyType {
}

impl TendermintValidatorApp {
pub fn connect(api_endpoint: &str, token: &str, key_name: &str) -> Result<Self, Error> {
//this call performs token self lookup, to fail fast
//let mut client = Client::new(host, token)?;

pub fn connect(api_endpoint: &str, token: &str, key_name: &str, ca_cert: &str) -> Result<Self, Error> {
//default conect timeout is 30s, this should be ok, since we block
let agent: Agent = ureq::AgentBuilder::new()
let mut agent_builder = ureq::AgentBuilder::new()
.timeout_read(Duration::from_secs(5))
.timeout_write(Duration::from_secs(5))
.user_agent(&format!(
"{}/{}",
env!("CARGO_PKG_NAME"),
env!("CARGO_PKG_VERSION")
))
.build();
));

if !ca_cert.is_empty() {
debug!("Using provided CA certificate at {}", ca_cert);
let cert_file = fs::File::open(ca_cert).expect("Cant open certificate file");
let mut cert_rd = io::BufReader::new(cert_file);
let certs = rustls_pemfile::certs(&mut cert_rd).expect("Invalid certificate");

let mut roots = RootCertStore::empty();
roots.add_parsable_certificates(&certs);

let tls_config = rustls::ClientConfig::builder()
.with_safe_defaults()
.with_root_certificates(roots)
.with_no_client_auth();

agent_builder = agent_builder.tls_config(sync::Arc::new(tls_config))
}

let agent = agent_builder.build();

let app = TendermintValidatorApp {
agent,
Expand Down