Skip to content

Commit

Permalink
feat(redis): add options to set username and password separately (#4453)
Browse files Browse the repository at this point in the history
Example of configuration:

```yaml title="router.yaml"
supergraph:
  query_planning:
    experimental_cache:
      redis: #highlight-line
        urls: ["redis://..."] #highlight-line
        username: admin/123 # Optional, can be part of the urls directly, mainly useful if you have special character like '/' in your password that doesn't work in url
        password: admin # Optional, can be part of the urls directly, mainly useful if you have special character like '/' in your password that doesn't work in url
        timeout: 5ms # Optional, by default: 2ms
        ttl: 24h # Optional, by default no expiration
```

Fixes #4346
---------

Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
  • Loading branch information
bnjjj authored Jan 12, 2024
1 parent 189423f commit 1cd670e
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .changesets/feat_bnjjj_feat_4346.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Add options to set username and password separately for Redis ([Issue #4346](https://github.com/apollographql/router/issues/4346))

Example of configuration:

```yaml title="router.yaml"
supergraph:
query_planning:
experimental_cache:
redis: #highlight-line
urls: ["redis://..."] #highlight-line
username: admin/123 # Optional, can be part of the urls directly, mainly useful if you have special character like '/' in your password that doesn't work in url. This field takes precedence over the username in the URL
password: admin # Optional, can be part of the urls directly, mainly useful if you have special character like '/' in your password that doesn't work in url. This field takes precedence over the password in the URL
timeout: 5ms # Optional, by default: 2ms
ttl: 24h # Optional, by default no expiration
```
By [@bnjjj](https://github.com/bnjjj) in https://github.com/apollographql/router/pull/4453
8 changes: 8 additions & 0 deletions apollo-router/src/cache/redis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ impl RedisCacheStorage {
let url = Self::preprocess_urls(config.urls)?;
let mut client_config = RedisConfig::from_url(url.as_str())?;

if let Some(username) = config.username {
client_config.username = Some(username);
}

if let Some(password) = config.password {
client_config.password = Some(password);
}

if let Some(tls) = config.tls.as_ref() {
let tls_cert_store = tls.create_certificate_store().transpose()?;
let client_cert_config = tls.client_authentication.as_ref();
Expand Down
5 changes: 5 additions & 0 deletions apollo-router/src/configuration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,11 @@ pub(crate) struct RedisCache {
/// List of URLs to the Redis cluster
pub(crate) urls: Vec<url::Url>,

/// Redis username if not provided in the URLs. This field takes precedence over the username in the URL
pub(crate) username: Option<String>,
/// Redis password if not provided in the URLs. This field takes precedence over the password in the URL
pub(crate) password: Option<String>,

#[serde(deserialize_with = "humantime_serde::deserialize", default)]
#[schemars(with = "Option<String>", default)]
/// Redis request timeout (default: 2ms)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ expression: "&schema"
"urls"
],
"properties": {
"password": {
"description": "Redis password if not provided in the URLs. This field takes precedence over the password in the URL",
"type": "string",
"nullable": true
},
"timeout": {
"description": "Redis request timeout (default: 2ms)",
"default": null,
Expand Down Expand Up @@ -140,6 +145,11 @@ expression: "&schema"
"type": "string",
"format": "uri"
}
},
"username": {
"description": "Redis username if not provided in the URLs. This field takes precedence over the username in the URL",
"type": "string",
"nullable": true
}
},
"additionalProperties": false,
Expand Down Expand Up @@ -1207,6 +1217,11 @@ expression: "&schema"
"urls"
],
"properties": {
"password": {
"description": "Redis password if not provided in the URLs. This field takes precedence over the password in the URL",
"type": "string",
"nullable": true
},
"timeout": {
"description": "Redis request timeout (default: 2ms)",
"default": null,
Expand Down Expand Up @@ -1264,6 +1279,11 @@ expression: "&schema"
"type": "string",
"format": "uri"
}
},
"username": {
"description": "Redis username if not provided in the URLs. This field takes precedence over the username in the URL",
"type": "string",
"nullable": true
}
},
"additionalProperties": false
Expand Down Expand Up @@ -2309,6 +2329,11 @@ expression: "&schema"
"urls"
],
"properties": {
"password": {
"description": "Redis password if not provided in the URLs. This field takes precedence over the password in the URL",
"type": "string",
"nullable": true
},
"timeout": {
"description": "Redis request timeout (default: 2ms)",
"default": null,
Expand Down Expand Up @@ -2366,6 +2391,11 @@ expression: "&schema"
"type": "string",
"format": "uri"
}
},
"username": {
"description": "Redis username if not provided in the URLs. This field takes precedence over the username in the URL",
"type": "string",
"nullable": true
}
},
"additionalProperties": false,
Expand Down
2 changes: 2 additions & 0 deletions docs/source/configuration/distributed-caching.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ supergraph:
experimental_cache:
redis: #highlight-line
urls: ["redis://..."] #highlight-line
username: admin/123 # Optional, can be part of the urls directly, mainly useful if you have special character like '/' in your password that doesn't work in url. This field takes precedence over the username in the URL
password: admin # Optional, can be part of the urls directly, mainly useful if you have special character like '/' in your password that doesn't work in url. This field takes precedence over the password in the URL
timeout: 5ms # Optional, by default: 2ms
ttl: 24h # Optional, by default no expiration
```
Expand Down

0 comments on commit 1cd670e

Please sign in to comment.