Skip to content

Commit

Permalink
feat: accept self-signed certificates (#2801) (#2923)
Browse files Browse the repository at this point in the history
  • Loading branch information
asimpleidea authored Sep 29, 2024
1 parent a302df1 commit 35796f1
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 6 deletions.
3 changes: 2 additions & 1 deletion benches/impl_path_string_for_evaluation_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ impl Http {
.http2_keep_alive_while_idle(upstream.keep_alive_while_idle)
.pool_idle_timeout(Some(Duration::from_secs(upstream.pool_idle_timeout)))
.pool_max_idle_per_host(upstream.pool_max_idle_per_host)
.user_agent(upstream.user_agent.clone());
.user_agent(upstream.user_agent.clone())
.danger_accept_invalid_certs(!upstream.verify_ssl);

// Add Http2 Prior Knowledge
if upstream.http2_only {
Expand Down
7 changes: 7 additions & 0 deletions generated/.tailcallrc.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,13 @@ directive @upstream(
The User-Agent header value to be used in HTTP requests. @default `Tailcall/1.0`
"""
userAgent: String
"""
A boolean value that determines whether to verify certificates. Setting this as `false`
will make tailcall accept self-signed certificates. NOTE: use this *only* during
development or testing. It is highly recommended to keep this enabled (`true`) in
production.
"""
verifySSL: Boolean
) on SCHEMA

"""
Expand Down
7 changes: 7 additions & 0 deletions generated/.tailcallrc.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,13 @@
"string",
"null"
]
},
"verifySSL": {
"description": "A boolean value that determines whether to verify certificates. Setting this as `false` will make tailcall accept self-signed certificates. NOTE: use this *only* during development or testing. It is highly recommended to keep this enabled (`true`) in production.",
"type": [
"boolean",
"null"
]
}
},
"additionalProperties": false
Expand Down
3 changes: 2 additions & 1 deletion src/cli/runtime/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ impl NativeHttp {
.http2_keep_alive_while_idle(upstream.keep_alive_while_idle)
.pool_idle_timeout(Some(Duration::from_secs(upstream.pool_idle_timeout)))
.pool_max_idle_per_host(upstream.pool_max_idle_per_host)
.user_agent(upstream.user_agent.clone());
.user_agent(upstream.user_agent.clone())
.danger_accept_invalid_certs(!upstream.verify_ssl);

// Add Http2 Prior Knowledge
if upstream.http2_only {
Expand Down
2 changes: 2 additions & 0 deletions src/core/blueprint/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Upstream {
pub batch: Option<Batch>,
pub http2_only: bool,
pub on_request: Option<String>,
pub verify_ssl: bool,
}

impl Upstream {
Expand Down Expand Up @@ -82,6 +83,7 @@ impl TryFrom<&ConfigModule> for Upstream {
batch,
http2_only: (config_upstream).get_http_2_only(),
on_request: (config_upstream).get_on_request(),
verify_ssl: (config_upstream).get_verify_ssl(),
})
.to_result()
}
Expand Down
18 changes: 16 additions & 2 deletions src/core/config/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use derive_setters::Setters;
use serde::{Deserialize, Serialize};
use tailcall_macros::{DirectiveDefinition, InputDefinition};

use crate::core::is_default;
use crate::core::macros::MergeRight;
use crate::core::merge_right::MergeRight;
use crate::core::{default_verify_ssl, is_default, verify_ssl_is_default};

const DEFAULT_MAX_SIZE: usize = 100;

Expand Down Expand Up @@ -144,6 +144,18 @@ pub struct Upstream {
/// The User-Agent header value to be used in HTTP requests. @default
/// `Tailcall/1.0`
pub user_agent: Option<String>,

#[serde(
default = "default_verify_ssl",
rename = "verifySSL",
skip_serializing_if = "verify_ssl_is_default"
)]
/// A boolean value that determines whether to verify certificates.
/// Setting this as `false` will make tailcall accept self-signed
/// certificates. NOTE: use this *only* during development or testing.
/// It is highly recommended to keep this enabled (`true`) in
/// production.
pub verify_ssl: Option<bool>,
}

impl Upstream {
Expand Down Expand Up @@ -191,14 +203,16 @@ impl Upstream {
.as_ref()
.map_or(DEFAULT_MAX_SIZE, |b| b.max_size.unwrap_or(DEFAULT_MAX_SIZE))
}

pub fn get_http_2_only(&self) -> bool {
self.http2_only.unwrap_or(false)
}

pub fn get_on_request(&self) -> Option<String> {
self.on_request.clone()
}
pub fn get_verify_ssl(&self) -> bool {
self.verify_ssl.unwrap_or(true)
}
}

#[cfg(test)]
Expand Down
9 changes: 9 additions & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ pub use mustache::Mustache;
pub use tailcall_macros as macros;
pub use transform::Transform;

const DEFAULT_VERIFY_SSL: bool = true;
pub const fn default_verify_ssl() -> Option<bool> {
Some(DEFAULT_VERIFY_SSL)
}

pub trait EnvIO: Send + Sync + 'static {
fn get(&self, key: &str) -> Option<Cow<'_, str>>;
}
Expand Down Expand Up @@ -102,6 +107,10 @@ pub fn is_default<T: Default + Eq>(val: &T) -> bool {
*val == T::default()
}

pub fn verify_ssl_is_default(val: &Option<bool>) -> bool {
val.is_none() || val.unwrap()
}

#[cfg(test)]
pub mod tests {
use std::collections::HashMap;
Expand Down
3 changes: 2 additions & 1 deletion src/core/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ pub mod test {
.http2_keep_alive_while_idle(upstream.keep_alive_while_idle)
.pool_idle_timeout(Some(Duration::from_secs(upstream.pool_idle_timeout)))
.pool_max_idle_per_host(upstream.pool_max_idle_per_host)
.user_agent(upstream.user_agent.clone());
.user_agent(upstream.user_agent.clone())
.danger_accept_invalid_certs(!upstream.verify_ssl);

// Add Http2 Prior Knowledge
if upstream.http2_only {
Expand Down
3 changes: 2 additions & 1 deletion tests/server_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub mod test {
.http2_keep_alive_while_idle(upstream.keep_alive_while_idle)
.pool_idle_timeout(Some(Duration::from_secs(upstream.pool_idle_timeout)))
.pool_max_idle_per_host(upstream.pool_max_idle_per_host)
.user_agent(upstream.user_agent.clone());
.user_agent(upstream.user_agent.clone())
.danger_accept_invalid_certs(!upstream.verify_ssl);

// Add Http2 Prior Knowledge
if upstream.http2_only {
Expand Down

1 comment on commit 35796f1

@github-actions
Copy link

Choose a reason for hiding this comment

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

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 11.83ms 4.74ms 195.67ms 88.79%
Req/Sec 2.15k 255.44 2.75k 85.67%

256727 requests in 30.03s, 1.29GB read

Requests/sec: 8549.53

Transfer/sec: 43.88MB

Please sign in to comment.