Skip to content

Commit

Permalink
feat: Add CredentialProvider & support v1 tokens (#146)
Browse files Browse the repository at this point in the history
Add the `CredentialProvider` construct that we have in other SDKs to parse auth tokens and get cache and control endpoints. This replaces the `EndpointResolver` and `jwt` modules.

Credential Providers can either be created from a string or an environment variable. They can optionally override the cache and control endpoints (or just one or the other).

```rust
    let credential_provider =
        CredentialProviderBuilder::new_from_environment_variable("TEST_AUTH_TOKEN")
            .build()
            .expect("TEST_AUTH_TOKEN must be set");

```

```rust
    let credential_provider =
        CredentialProviderBuilder::new_from_string("my_test_token")
            .with_cache_endpoint("cache.localhost")
            .with_control_endpoint("control.localhost")
            .build()
            .expect("could not parse token and endpoints")
```
then
```rust
 let mut momento = SimpleCacheClientBuilder::new(credential_provider, Duration::from_secs(30))?.build();
```

Co-authored-by: Kenny <[email protected]>
  • Loading branch information
rlinehan and kvcache authored Jun 9, 2023
1 parent ca2909d commit 2ee5c45
Show file tree
Hide file tree
Showing 10 changed files with 779 additions and 546 deletions.
21 changes: 7 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,11 @@ Rust SDK for Momento, a serverless cache that automatically scales without any o

<br/>

## Preview Features

This SDK contains APIs for interacting with collection data structures: Lists, Sets, and Dictionaries. These APIs
are currently in preview. If you would like to request early access to the data structure APIs, please contact us
at `[email protected]`.

**Note that if you call the List, Set, or Dictionary APIs without first signing up for our early access preview, you
the calls will result in an `Unsupported operation` error.**

## Getting Started 🏃

### Requirements

- A Momento Auth Token is required, you can generate one using the [Momento CLI](https://github.com/momentohq/momento-cli)
- A Momento Auth Token is required, you can generate one using the [Momento Console](https://console.gomomento.com)

<br/>

Expand All @@ -40,17 +31,19 @@ Check out [example](./example/) directory!
### Using Momento

```rust
use momento::simple_cache_client::SimpleCacheClientBuilder;
use momento::{CredentialProviderBuilder, SimpleCacheClientBuilder};
use std::env;
use std::num::NonZeroU64;

async fn demo_cache_usage() {
// Initialize Momento
let auth_token = env::var("MOMENTO_AUTH_TOKEN")
.expect("env var MOMENTO_AUTH_TOKEN must be set to your auth token");
let credential_provider =
CredentialProviderBuilder::new_from_environment_variable("MOMENTO_AUTH_TOKEN")
.build()
.expect("env var MOMENTO_AUTH_TOKEN must be set to your auth token");
let item_default_ttl_seconds = 60;
let mut cache_client = SimpleCacheClientBuilder::new(
auth_token,
credential_provider,
NonZeroU64::new(item_default_ttl_seconds).unwrap(),
)
.unwrap()
Expand Down
12 changes: 10 additions & 2 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use thiserror::Error;
use tonic::{codegen::http::uri::InvalidUri, transport::Channel, Streaming};

use crate::{
endpoint_resolver::MomentoEndpointsResolver,
response::MomentoError,
utils::{connect_channel_lazily, ChannelConnectError},
};
Expand Down Expand Up @@ -190,7 +189,16 @@ async fn consume_login_messages(
}

fn auth_client() -> Result<AuthClient<Channel>, AuthError> {
let hostname = MomentoEndpointsResolver::get_login_endpoint();
let hostname = get_login_endpoint();
let channel = connect_channel_lazily(&hostname)?;
Ok(AuthClient::new(channel))
}

fn get_login_endpoint() -> String {
const LOGIN_HOSTNAMES: &[&str] = &["control.cell-us-east-1-1.prod.a.momentohq.com"];
std::env::var("LOGIN_ENDPOINT").unwrap_or_else(|_| {
let random = rand::random::<usize>();
let hostname = LOGIN_HOSTNAMES[random % LOGIN_HOSTNAMES.len()].to_string();
format!("https://{hostname}:443")
})
}
Loading

0 comments on commit 2ee5c45

Please sign in to comment.