Skip to content

Commit e7fc4d9

Browse files
committed
feat(providers): add Slack example profile with rotating user tokens
Add `providers/slack.yaml`, an example profile for Slack apps that have token rotation enabled. Slack issues user access tokens that expire after 43200 seconds together with a single-use refresh token, and requires the app client ID and client secret on every refresh, so the profile declares an `oauth2_refresh_token` credential against oauth.v2.access with a matching 43200-second lifetime cap, a 21600-second refresh lead, and client_id / client_secret / refresh_token material. Slack Web API methods are POST even for reads, so the endpoint uses the read-write method preset; deployers add `rules` for the methods their workloads need. Tighten `ProviderTypeProfile::adc_credential` to require the Google token endpoint in addition to the three ADC material keys, so that `--from-gcloud-adc` keeps refusing profiles whose refresh material merely has the same shape. Document the profile under how-it-works/providers. Refs #3729 Signed-off-by: Jason T. Greene <jason.greene@redhat.com>
1 parent 42cc663 commit e7fc4d9

5 files changed

Lines changed: 261 additions & 7 deletions

File tree

‎crates/openshell-providers/src/profiles.rs‎

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -962,16 +962,19 @@ impl ProviderTypeProfile {
962962

963963
/// Returns the credential suitable for `--from-gcloud-adc` bootstrap, if any.
964964
///
965-
/// A credential qualifies when its refresh strategy is `Oauth2RefreshToken`
966-
/// and its material declares the three gcloud ADC keys (`client_id`,
967-
/// `client_secret`, `refresh_token`).
965+
/// A credential qualifies when its refresh strategy is `Oauth2RefreshToken`,
966+
/// its token endpoint is the Google token endpoint that `gcloud` ADC
967+
/// refresh tokens are redeemable at, and its material declares the three
968+
/// gcloud ADC keys (`client_id`, `client_secret`, `refresh_token`).
968969
#[must_use]
969970
pub fn adc_credential(&self) -> Option<&CredentialProfile> {
970971
const ADC_MATERIAL_KEYS: &[&str] = &["client_id", "client_secret", "refresh_token"];
972+
const GOOGLE_TOKEN_URL: &str = "https://oauth2.googleapis.com/token";
971973

972974
self.credentials.iter().find(|cred| {
973975
cred.refresh.as_ref().is_some_and(|refresh| {
974976
refresh.strategy == ProviderCredentialRefreshStrategy::Oauth2RefreshToken
977+
&& refresh.token_url == GOOGLE_TOKEN_URL
975978
&& ADC_MATERIAL_KEYS
976979
.iter()
977980
.all(|key| refresh.material.iter().any(|m| m.name == *key))
@@ -3738,7 +3741,10 @@ mod tests {
37383741
use std::collections::HashMap;
37393742

37403743
use openshell_core::mcp::{DEFAULT_MCP_PROTOCOL_VERSION, McpProtocolVersion};
3741-
use openshell_core::proto::{ProviderCredentialTokenGrantType, ProviderProfileCategory};
3744+
use openshell_core::proto::{
3745+
ProviderCredentialRefreshStrategy, ProviderCredentialTokenGrantType,
3746+
ProviderProfileCategory,
3747+
};
37423748

37433749
use super::{
37443750
DiscoveryProfile, EndpointProfile, L7AllowProfile, L7QueryMatcherProfile,
@@ -4292,6 +4298,52 @@ endpoints:
42924298
assert_eq!(adc.env_vars[0], "GOOGLE_VERTEX_AI_TOKEN");
42934299
}
42944300

4301+
#[test]
4302+
fn slack_profile_declares_rotating_user_token() {
4303+
let profile = example_profile("slack");
4304+
let proto = profile.to_proto();
4305+
4306+
assert_eq!(proto.category, ProviderProfileCategory::Messaging as i32);
4307+
assert_eq!(profile.credentials.len(), 1);
4308+
let credential = &profile.credentials[0];
4309+
assert_eq!(credential.name, "user_token");
4310+
assert_eq!(credential.env_vars, vec!["SLACK_USER_TOKEN", "SLACK_TOKEN"]);
4311+
assert!(credential.required);
4312+
4313+
let refresh = credential
4314+
.refresh
4315+
.as_ref()
4316+
.expect("slack user token should declare refresh metadata");
4317+
assert_eq!(
4318+
refresh.strategy,
4319+
ProviderCredentialRefreshStrategy::Oauth2RefreshToken
4320+
);
4321+
assert_eq!(refresh.token_url, "https://slack.com/api/oauth.v2.access");
4322+
assert!(refresh.scopes.is_empty(), "no scope parameter on refresh");
4323+
assert_eq!(refresh.max_lifetime_seconds, 43_200);
4324+
assert!(
4325+
refresh.refresh_before_seconds < refresh.max_lifetime_seconds,
4326+
"lead time must leave a positive refresh interval"
4327+
);
4328+
let material = refresh
4329+
.material
4330+
.iter()
4331+
.map(|entry| (entry.name.as_str(), entry.required, entry.secret))
4332+
.collect::<Vec<_>>();
4333+
assert_eq!(
4334+
material,
4335+
vec![
4336+
("client_id", true, false),
4337+
("client_secret", true, true),
4338+
("refresh_token", true, true),
4339+
]
4340+
);
4341+
assert!(
4342+
profile.adc_credential().is_none(),
4343+
"slack material is not gcloud ADC material"
4344+
);
4345+
}
4346+
42954347
#[test]
42964348
fn adc_credential_returns_none_for_profiles_without_adc() {
42974349
let profile = example_profile("github");
@@ -6539,7 +6591,7 @@ binaries:
65396591
let refresh = access_key.refresh.as_ref().unwrap();
65406592
assert_eq!(
65416593
refresh.strategy,
6542-
openshell_core::proto::ProviderCredentialRefreshStrategy::AwsStsAssumeRole
6594+
ProviderCredentialRefreshStrategy::AwsStsAssumeRole
65436595
);
65446596
assert!(
65456597
refresh
@@ -6641,7 +6693,7 @@ binaries:
66416693
#[test]
66426694
fn is_gateway_mintable_strategy_includes_aws_sts() {
66436695
assert!(super::is_gateway_mintable_strategy(
6644-
openshell_core::proto::ProviderCredentialRefreshStrategy::AwsStsAssumeRole
6696+
ProviderCredentialRefreshStrategy::AwsStsAssumeRole
66456697
));
66466698
}
66476699

‎crates/openshell-server/src/grpc/provider.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7417,7 +7417,8 @@ mod tests {
74177417
"oci-genai",
74187418
"openai",
74197419
"openrouter",
7420-
"pypi"
7420+
"pypi",
7421+
"slack"
74217422
]
74227423
);
74237424

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3+
# SPDX-License-Identifier: Apache-2.0
4+
title: "Slack"
5+
sidebar-title: "Slack"
6+
description: "Give sandboxes governed Slack Web API access with gateway-managed user token rotation."
7+
keywords: "Slack, OAuth2, Token Rotation, Credentials, Provider Profile, Sandbox"
8+
---
9+
10+
The `slack` example profile injects a Slack user token into sandboxes as a stable
11+
`SLACK_USER_TOKEN` placeholder and keeps the real token short lived. Slack
12+
apps with token rotation enabled issue access tokens that expire after
13+
12 hours together with a single-use refresh token. The gateway stores the
14+
refresh material, refreshes the access token through `oauth.v2.access`
15+
before it expires, and swaps the value behind the placeholder without
16+
restarting sandbox processes.
17+
18+
<Note>
19+
Token rotation is an app-level setting in Slack and cannot be turned off
20+
once enabled. Apps without rotation issue non-expiring tokens; for those,
21+
create the provider with the static token and skip the refresh
22+
configuration below.
23+
</Note>
24+
25+
## Prerequisites
26+
27+
- A Slack app with token rotation enabled and the user scopes your workload
28+
needs.
29+
- The app's client ID and client secret. Slack requires both on every
30+
refresh, even for user tokens.
31+
- A completed OAuth authorization for the user, which returns an access
32+
token, a refresh token, and `expires_in` under `authed_user`.
33+
34+
| Variable | Value |
35+
|---|---|
36+
| `SLACK_CLIENT_ID` | Slack app client ID. |
37+
| `SLACK_CLIENT_SECRET` | Slack app client secret. |
38+
| `SLACK_USER_TOKEN` | Current user access token (`xoxe.xoxp-...`). |
39+
| `SLACK_REFRESH_TOKEN` | Current refresh token (`xoxe-1-...`). |
40+
| `SLACK_USER_TOKEN_EXPIRES_AT` | Absolute expiry of the current access token (RFC3339 or epoch milliseconds). |
41+
42+
<Warning>
43+
Slack refresh tokens are single use. Every refresh returns a new refresh
44+
token and invalidates the previous one, so only the gateway should refresh a
45+
rotating grant. Do not reuse the refresh token you hand to the gateway
46+
elsewhere, and avoid manual `refresh rotate` calls that could race the
47+
gateway's own scheduled refresh.
48+
</Warning>
49+
50+
## Import the Example Profile
51+
52+
The repository ships `providers/slack.yaml` as a reviewable example. Lint and
53+
import it (drop `--global` to import into the current workspace only):
54+
55+
```shell
56+
openshell provider profile lint -f providers/slack.yaml
57+
openshell provider profile import -f providers/slack.yaml --global
58+
```
59+
60+
Copy and adapt it if your workload needs narrower `endpoints` rules or a
61+
different `binaries` list; the example allows the Slack Web API host for
62+
any binary in the sandbox.
63+
64+
## Create the Provider
65+
66+
```shell
67+
openshell provider create \
68+
--name my-slack \
69+
--type slack \
70+
--credential SLACK_USER_TOKEN="$SLACK_USER_TOKEN"
71+
```
72+
73+
## Configure Refresh
74+
75+
```shell
76+
openshell provider refresh configure my-slack \
77+
--credential-key SLACK_USER_TOKEN \
78+
--strategy oauth2-refresh-token \
79+
--material client_id="$SLACK_CLIENT_ID" \
80+
--secret-material-env client_secret=SLACK_CLIENT_SECRET \
81+
--secret-material-env refresh_token=SLACK_REFRESH_TOKEN \
82+
--credential-expires-at "$SLACK_USER_TOKEN_EXPIRES_AT"
83+
```
84+
85+
The profile owns the token endpoint, the 43200-second lifetime cap, and a
86+
21600-second refresh lead time. The gateway refreshes the token on its own
87+
schedule; confirm the configuration took effect with:
88+
89+
```shell
90+
openshell provider refresh status my-slack
91+
```
92+
93+
## Failure Handling
94+
95+
Slack reports token endpoint errors with an HTTP 200 status and an `error`
96+
field. The gateway classifies those like standard OAuth errors:
97+
98+
| Slack error | `FAILURE_CODE` | Recovery |
99+
|---|---|---|
100+
| `invalid_refresh_token`, `token_revoked`, `token_expired`, `account_inactive`, `invalid_auth` | `oauth_invalid_grant` | `reauthorize`: the user must authorize the app again. |
101+
| `invalid_client_id`, `bad_client_secret` | `oauth_invalid_client` | `fix_configuration`: correct the client material. |
102+
| `ratelimited` and other transient Slack errors | `oauth_token_endpoint_retryable` | `retry`: the gateway retries automatically. |
103+
104+
The Slack error name is kept in the provider error subtype of the refresh
105+
status so operators can see the original cause.
106+
107+
## Use the Provider in a Sandbox
108+
109+
```shell
110+
openshell sandbox create --name slack-sandbox --provider my-slack
111+
openshell sandbox exec slack-sandbox -- \
112+
curl -s -X POST https://slack.com/api/auth.test \
113+
-H "Authorization: Bearer $SLACK_USER_TOKEN"
114+
```
115+
116+
The sandbox sees only the placeholder; the sandbox proxy substitutes the
117+
current access token for requests to `slack.com`. Restrict the allowed
118+
methods with a custom profile that adds `rules` for the Slack Web API paths
119+
your workload needs.

‎docs/index.yml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ navigation:
5555
path: how-it-works/providers/aws.mdx
5656
- page: "Google"
5757
path: how-it-works/providers/google.mdx
58+
- page: "Slack"
59+
path: how-it-works/providers/slack.mdx
5860
- section: "Policies"
5961
slug: policies
6062
contents:

‎providers/slack.yaml‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
# Example provider profile. OpenShell does not load it; import it explicitly:
5+
# openshell provider profile lint -f providers/slack.yaml
6+
# openshell provider profile import -f providers/slack.yaml --global
7+
#
8+
# Copy and edit this file rather than importing it unchanged. `binaries` is the
9+
# least-privilege control that decides which processes may reach the endpoints
10+
# below, so it has to name the paths in *your* image.
11+
#
12+
# Client binaries: curl (or your own Slack client; edit `binaries` to match).
13+
# Reference layout: curl on PATH at /usr/bin or /usr/local/bin.
14+
# Credential scope: a Slack user access token from an app with token rotation
15+
# enabled, as SLACK_USER_TOKEN (alias SLACK_TOKEN), sent as a
16+
# bearer authorization header to slack.com and nowhere else.
17+
# Configure the refresh material (client_id, client_secret,
18+
# refresh_token) with `openshell provider refresh configure`;
19+
# it stays at the gateway and is never injected. Slack access
20+
# tokens live 43200 s and refresh tokens are single use, so
21+
# only the gateway should refresh them.
22+
# Endpoint access: slack.com:443 with the read-write method preset (Slack Web
23+
# API methods are POST even for reads). Add `rules` for the
24+
# methods your workload needs.
25+
# Smoke test: openshell sandbox create --provider <name> -- \
26+
# curl -s -X POST https://slack.com/api/auth.test \
27+
# -H "Authorization: Bearer $SLACK_USER_TOKEN"
28+
29+
id: slack
30+
display_name: Slack
31+
description: Slack Web API access with gateway-managed user token rotation
32+
category: messaging
33+
34+
credentials:
35+
# Rotating user token (xoxe.xoxp-...). Slack apps with token rotation
36+
# enabled issue 12-hour access tokens together with a single-use refresh
37+
# token on every rotation; the gateway refreshes through oauth.v2.access.
38+
# Configure with `openshell provider refresh configure`.
39+
- name: user_token
40+
description: Slack user access token
41+
env_vars: [SLACK_USER_TOKEN, SLACK_TOKEN]
42+
required: true
43+
auth_style: bearer
44+
header_name: authorization
45+
refresh:
46+
strategy: oauth2_refresh_token
47+
token_url: https://slack.com/api/oauth.v2.access
48+
# Slack access tokens live exactly 43200 seconds. The cap must match,
49+
# otherwise the default 3600-second cap would schedule refreshes far
50+
# too often and burn single-use refresh tokens.
51+
refresh_before_seconds: 21600
52+
max_lifetime_seconds: 43200
53+
material:
54+
- name: client_id
55+
description: Slack app client ID
56+
required: true
57+
- name: client_secret
58+
description: Slack app client secret (required by Slack on refresh)
59+
required: true
60+
secret: true
61+
- name: refresh_token
62+
description: Slack rotating refresh token (xoxe-1-...)
63+
required: true
64+
secret: true
65+
66+
discovery:
67+
credentials: [user_token]
68+
69+
endpoints:
70+
# Slack Web API methods are POST even for reads, so the read-only
71+
# method preset is unusable; read-write is the minimum working preset.
72+
- host: slack.com
73+
port: 443
74+
protocol: rest
75+
access: read-write
76+
enforcement: enforce
77+
78+
binaries:
79+
- /usr/bin/curl
80+
- /usr/local/bin/curl

0 commit comments

Comments
 (0)