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 TLS support #81

Merged
merged 30 commits into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3324575
WIP(tls): add TlsStream support
compressed Jan 8, 2017
5fa4359
Work around some type issues
tikue Jan 11, 2017
6fc9469
Remove stream type param
tikue Jan 11, 2017
31b993f
Update pre-push to test tls
tikue Jan 11, 2017
7e4257f
chore(deps): use crates.io versions for tls dependencies
compressed Jan 12, 2017
31c2c88
chore(error): native2io -> native_to_io
compressed Jan 12, 2017
9a21c25
docs: update examples
compressed Jan 12, 2017
f296219
test: add tls and tcp at the same time test case
compressed Jan 12, 2017
49d19a8
chore: Merge branch 'google/master' into tls-support-v2
compressed Jan 19, 2017
ce132a6
chore: cleanup
compressed Jan 19, 2017
a26805a
chore: update travis-cargo command
compressed Jan 19, 2017
87a21ee
docs(README): formatting for TLS section
compressed Jan 19, 2017
d7c2c33
chore: rm unneeded comment
compressed Jan 19, 2017
65c68be
chore: correct client visibility
compressed Jan 19, 2017
c5a5dd6
docs(client): add comment for `mod tls`
compressed Jan 19, 2017
16625f5
refactor: address review comments
compressed Jan 23, 2017
64c87ca
chore: Merge branch 'master' into tls-support-v4
compressed Jan 23, 2017
61acb5f
chore: rustfmt
compressed Jan 23, 2017
6e4c22b
chore: rm extra newline
compressed Jan 24, 2017
e9653f5
chore: use re-exported `NativeTlsError` type
compressed Jan 24, 2017
ce5cbca
refactor: `TlsClientContext` -> `tls::client::Context`
compressed Jan 24, 2017
e1311e6
chore: clippy
compressed Jan 24, 2017
4831c27
docs(example): update formatting
compressed Jan 24, 2017
9f358a8
docs(TLS): set the TLS doctest to ignore
compressed Jan 26, 2017
f0dd32e
docs(client): correct comment about `tls::Context.domain`'s usage
compressed Jan 27, 2017
7c9cca8
fix(tls): change `tls` to `native_tls` for re-exports
compressed Jan 31, 2017
8b3f8d5
Merge branch 'master' into tls-support-v4
tikue Jan 31, 2017
bec30af
Fix merge fallout
tikue Jan 31, 2017
c2bcc88
Fix merge fallout
tikue Jan 31, 2017
ac353e0
Fix merge fallout.
tikue Jan 31, 2017
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
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ before_script:

script:
- |
travis-cargo build && travis-cargo test
travis-cargo build && travis-cargo test &&
travis-cargo build -- --features tls && travis-cargo test -- --features tls

after_success:
- travis-cargo coveralls --no-sudo
Copy link
Collaborator

Choose a reason for hiding this comment

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

How does this work with features?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These run just like cargo build --features tls. If you pass -- to travis-cargo then whatever follows is passed to cargo.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What about coveralls? Looks like it needs to have the tls feature enabled.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ideally yes... However, coveralls is broken already in travis-cargo. I think there are a few PRs to fix it in their repo. Maybe we should have a separate PR just to remove travis-cargo. It may be easier than using that tool.

Expand Down
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ license = "MIT"
documentation = "https://docs.rs/tarpc"
homepage = "https://github.com/google/tarpc"
repository = "https://github.com/google/tarpc"
keywords = ["rpc", "protocol", "remote", "procedure", "serialize"]
keywords = ["rpc", "protocol", "remote", "procedure", "serialize", "tls"]
readme = "README.md"
description = "An RPC framework for Rust with a focus on ease of use."

[dependencies]
bincode = "0.6"
byteorder = "0.5"
cfg-if = "0.1.0"
bytes = "0.3"
futures = "0.1.7"
lazy_static = "0.2"
log = "0.3"
native-tls = { version = "0.1.1", optional = true }
scoped-pool = "1.0"
serde = "0.8"
serde_derive = "0.8"
Expand All @@ -25,6 +27,7 @@ take = "0.1"
tokio-service = "0.1"
tokio-proto = "0.1"
tokio-core = "0.1"
tokio-tls = { version = "0.1", optional = true }
net2 = "0.2"

[dev-dependencies]
Expand All @@ -33,7 +36,12 @@ env_logger = "0.3"
futures-cpupool = "0.1"
clap = "2.0"

[target.'cfg(target_os = "macos")'.dev-dependencies]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this stuff work on osx/linux/windows? Is OSX the only one that needs an extra dependency like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line will make this dependency apply only for osx. For linux, it internally uses the openssl crate, but we can make required data for testing purposes without needing to bring in the openssl crate.

For windows, I didn't write the test case since I don't actually have access to a windows machine and it seemed a little complicated based on the tests I read in the native-tls and tokio-tls crates.

security-framework = "0.1"

[features]
default = []
tls = ["tokio-tls", "native-tls"]
unstable = ["serde/unstable"]

[workspace]
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,85 @@ fn main() {
}
```

## Example: Futures + TLS

By default, tarpc internally uses a [`TcpStream`] for communication between your clients and
servers. However, TCP by itself has no encryption. As a result, your communication will be sent in
the clear. If you want your RPC communications to be encrypted, you can choose to use [TLS]. TLS
operates as an encryption layer on top of TCP. When using TLS, your communication will occur over a
[`TlsStream<TcpStream>`]. You can add the ability to make TLS clients and servers by adding `tarpc`
with the `tls` feature flag enabled.

When using TLS, some additional information is required. You will need to make [`TlsAcceptor`] and
`client::tls::Context` structs; `client::tls::Context` requires a [`TlsConnector`]. The
[`TlsAcceptor`] and [`TlsConnector`] types are defined in the [native-tls]. tarpc re-exports
external TLS-related types in its `tls` module (`tarpc::tls`).

[TLS]: https://en.wikipedia.org/wiki/Transport_Layer_Security
[`TcpStream`]: https://docs.rs/tokio-core/0.1/tokio_core/net/struct.TcpStream.html
[`TlsStream<TcpStream>`]: https://docs.rs/native-tls/0.1/native_tls/struct.TlsStream.html
[`TlsAcceptor`]: https://docs.rs/native-tls/0.1/native_tls/struct.TlsAcceptor.html
[`TlsConnector`]: https://docs.rs/native-tls/0.1/native_tls/struct.TlsConnector.html
[native-tls]: https://github.com/sfackler/rust-native-tls

Both TLS streams and TCP streams are supported in the same binary when the `tls` feature is enabled.
However, if you are working with both stream types, ensure that you use the TLS clients with TLS
servers and TCP clients with TCP servers.

```rust
#![feature(conservative_impl_trait, plugin)]
#![plugin(tarpc_plugins)]

extern crate futures;
#[macro_use]
extern crate tarpc;
extern crate tokio_core;

use futures::Future;
use tarpc::{client, server};
use tarpc::client::future::Connect;
use tarpc::util::{FirstSocketAddr, Never};
use tokio_core::reactor;
use tarpc::tls::{Pkcs12, TlsAcceptor};

service! {
rpc hello(name: String) -> String;
}

#[derive(Clone)]
struct HelloServer;

impl FutureService for HelloServer {
type HelloFut = futures::Finished<String, Never>;

fn hello(&mut self, name: String) -> Self::HelloFut {
futures::finished(format!("Hello, {}!", name))
}
}

fn get_acceptor() -> TlsAcceptor {
let buf = include_bytes!("test/identity.p12");
let pkcs12 = Pkcs12::from_der(buf, "password").unwrap();
TlsAcceptor::builder(pkcs12).unwrap().build().unwrap()
}

fn main() {
let addr = "localhost:10000".first_socket_addr();
let mut core = reactor::Core::new().unwrap();
let acceptor = get_acceptor();
HelloServer.listen(addr, server::Options::default()
.handle(core.handle())
.tls(acceptor)).wait().unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Building the client_cx seems pretty straightforward; maybe we could just inline it as .tls(TlsClientContext::new("foobar.com"))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea!

let options = client::Options::default().handle(core.handle()
.tls(client::tls::Context::new("foobar.com").unwrap()));
core.run(FutureClient::connect(addr, options)
.map_err(tarpc::Error::from)
.and_then(|client| client.hello("Mom".to_string()))
.map(|resp| println!("{}", resp)))
.unwrap();
}
```

## Tips

### Sync vs Futures
Expand Down
2 changes: 2 additions & 0 deletions hooks/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ run_cargo() {
rustup run $2 cargo $1 &>/dev/null
else
rustup run nightly cargo $1 --features unstable &>/dev/null
rustup run nightly cargo $1 --features unstable,tls &>/dev/null
fi
else
printf "${PREFIX} $VERB... "
cargo $1 &>/dev/null
cargo $1 --features tls &>/dev/null
fi
if [ "$?" != "0" ]; then
printf "${FAILURE}\n"
Expand Down
Loading