Skip to content

Commit b9d5b3e

Browse files
test(e2e): build provider profiles with testkit::Profile
Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
1 parent f717bad commit b9d5b3e

4 files changed

Lines changed: 79 additions & 110 deletions

File tree

‎tests/suites/conformance/src/providers.rs‎

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
//! Providers attached to a sandbox: files a provider profile serves, and
55
//! providers that `sandbox create --provider` creates from local credentials.
66
7-
use testkit::{Context, ResultExt as _, YamlFile, policy};
7+
use testkit::policy::{self, Access, Endpoint as _};
8+
use testkit::{Context, Credential, Profile, ResultExt as _, YamlFile};
89

910
/// The example profile that serves `client.toml` from the provider's config.
1011
const FILES_PROFILE: &str =
@@ -96,7 +97,7 @@ async fn auto_created_provider_credential_available_in_sandbox(
9697
cx: &mut Context,
9798
) -> testkit::Result {
9899
let provider = cx
99-
.import_profile(&auto_create_profile(&cx.name("auto")))
100+
.import_profile(&auto_create_profile(&cx.name("auto")).to_yaml()?)
100101
.await?;
101102
// The CLI names the provider it creates after the profile.
102103
let cli = cx.cli_handle();
@@ -176,28 +177,21 @@ async fn expect_project(sandbox: &testkit::Sandbox, path: &str, project: &str) -
176177
/// A profile `id` whose credential the CLI discovers in [`AUTO_CREATE_ENV`].
177178
/// Its endpoint makes the gateway inject the credential without a policy
178179
/// binding; nothing connects to it.
179-
fn auto_create_profile(id: &str) -> String {
180-
format!(
181-
"id: {id}
182-
display_name: E2E Auto-created Provider
183-
category: other
184-
credentials:
185-
- name: api_key
186-
env_vars: [{AUTO_CREATE_ENV}]
187-
required: true
188-
auth_style: header
189-
header_name: x-api-key
190-
discovery:
191-
credentials: [api_key]
192-
endpoints:
193-
- host: api.auto-provider.example
194-
port: 443
195-
protocol: rest
196-
access: read-write
197-
enforcement: enforce
198-
binaries: [/usr/bin/curl]
199-
"
200-
)
180+
fn auto_create_profile(id: &str) -> Profile {
181+
Profile::new(id)
182+
.display_name("E2E Auto-created Provider")
183+
.credential(
184+
Credential::new("api_key")
185+
.env_var(AUTO_CREATE_ENV)
186+
.required()
187+
.auth_style("header")
188+
.header_name("x-api-key"),
189+
)
190+
.discover(["api_key"])
191+
.endpoint("api.auto-provider.example", 443, |ep| {
192+
ep.rest(Access::ReadWrite).enforce();
193+
})
194+
.binaries(["/usr/bin/curl"])
201195
}
202196

203197
/// Whether `value` is the placeholder for the credential in `key`, in the

‎tests/suites/features/Cargo.lock‎

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/suites/features/keycloak-provider-refresh/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ publish = false
1414
[dependencies]
1515
keycloak-support = { path = "../../../support/keycloak" }
1616
openshell-testkit = { path = "../../../testkit/testkit" }
17-
tempfile = "3"
17+
prost-types = "0.14"

‎tests/suites/features/keycloak-provider-refresh/tests/provider_refresh.rs‎

Lines changed: 43 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
//! Keycloak is only the provider's token issuer here; the CLI still reaches
77
//! the gateway the usual way.
88
9-
use std::io::Write as _;
10-
119
use keycloak_support::KeycloakContext;
12-
use testkit::{CliOutput, Context, ResultExt as _};
10+
use testkit::policy::{Access, Endpoint as _};
11+
use testkit::sdk::raw::proto;
12+
use testkit::{CliOutput, Context, Credential, Profile};
1313

1414
const CREDENTIAL_KEY: &str = "KEYCLOAK_ACCESS_TOKEN";
1515

@@ -20,34 +20,14 @@ const CREDENTIAL_KEY: &str = "KEYCLOAK_ACCESS_TOKEN";
2020
async fn revoked_refresh_grant_requires_user_reauthorization(cx: &mut Context) -> testkit::Result {
2121
let kc = KeycloakContext::new(cx).await?;
2222
let provider = kc.name("kc-refresh");
23-
let profile_id = kc.name("kc-refresh-profile");
2423
let grant = kc.keycloak().password_grant().await?;
25-
let profile = write_profile(&profile_id, kc.keycloak())?;
26-
let profile_path = profile.path().to_string_lossy().into_owned();
27-
28-
// Register each delete before its create, so cleanup also covers a create
29-
// the gateway applied but the CLI reported as failed. Both deletes succeed
30-
// when there is nothing to delete.
31-
delete_on_cleanup(&kc, "profile", &profile_id);
32-
kc.cli(["profile", "import", "--file", &profile_path])
33-
.await?
34-
.success()?;
35-
delete_on_cleanup(&kc, "provider", &provider);
36-
kc.cli_with_env(
24+
let profile = refresh_profile(&kc.name("kc-refresh-profile"), kc.keycloak()).to_yaml()?;
25+
kc.install_provider(
26+
&provider,
27+
&profile,
3728
&[(CREDENTIAL_KEY, &grant.access_token)],
38-
[
39-
"provider",
40-
"create",
41-
"--name",
42-
&provider,
43-
"--type",
44-
&profile_id,
45-
"--credential",
46-
CREDENTIAL_KEY,
47-
],
4829
)
49-
.await?
50-
.success()?;
30+
.await?;
5131
let client_id = format!("client_id={}", kc.keycloak().client_id());
5232
kc.cli_with_env(
5333
&[("KEYCLOAK_REFRESH_TOKEN", &grant.refresh_token)],
@@ -117,64 +97,43 @@ async fn revoked_refresh_grant_requires_user_reauthorization(cx: &mut Context) -
11797
Ok(())
11898
}
11999

120-
/// Delete the `kind` named `name` when the test ends, since testkit only
121-
/// cleans up sandboxes by itself.
122-
fn delete_on_cleanup(cx: &Context, kind: &'static str, name: &str) {
123-
let cli = cx.cli_handle();
124-
let name = name.to_string();
125-
cx.on_cleanup(format!("delete {kind} {name}"), async move || {
126-
cli.run([kind, "delete", &name]).await?.success()?;
127-
Ok(())
128-
});
129-
}
130-
131100
fn combined(output: &CliOutput) -> String {
132101
format!("{}{}", output.stdout, output.stderr)
133102
}
134103

135-
fn write_profile(
136-
profile_id: &str,
137-
keycloak: &keycloak_support::Keycloak,
138-
) -> testkit::Result<tempfile::NamedTempFile> {
139-
let issuer_token_url = keycloak.token_endpoint();
140-
let profile = format!(
141-
r#"id: {profile_id}
142-
display_name: Keycloak provider refresh test
143-
category: other
144-
credentials:
145-
- name: access_token
146-
env_vars: [{CREDENTIAL_KEY}]
147-
required: true
148-
auth_style: bearer
149-
header_name: authorization
150-
refresh:
151-
strategy: oauth2_refresh_token
152-
token_url: {issuer_token_url}
153-
scopes: [openid]
154-
refresh_before_seconds: 60
155-
max_lifetime_seconds: 3600
156-
material:
157-
- name: client_id
158-
required: true
159-
- name: refresh_token
160-
required: true
161-
secret: true
162-
endpoints:
163-
- host: keycloak.test.invalid
164-
port: 443
165-
protocol: rest
166-
access: read-only
167-
enforcement: enforce
168-
binaries:
169-
- /usr/bin/curl
170-
"#
171-
);
172-
let mut file = tempfile::Builder::new()
173-
.suffix(".yaml")
174-
.tempfile()
175-
.context("create provider profile")?;
176-
file.write_all(profile.as_bytes())
177-
.and_then(|()| file.flush())
178-
.context("write provider profile")?;
179-
Ok(file)
104+
/// A profile whose `access_token` the gateway refreshes from `keycloak`
105+
/// with the OAuth refresh-token grant.
106+
fn refresh_profile(id: &str, keycloak: &keycloak_support::Keycloak) -> Profile {
107+
let material = |name: &str, secret: bool| proto::ProviderCredentialRefreshMaterial {
108+
name: name.to_string(),
109+
required: true,
110+
secret,
111+
..proto::ProviderCredentialRefreshMaterial::default()
112+
};
113+
let seconds = |seconds| Some(prost_types::Duration { seconds, nanos: 0 });
114+
Profile::new(id)
115+
.display_name("Keycloak provider refresh test")
116+
.credential(
117+
Credential::new("access_token")
118+
.env_var(CREDENTIAL_KEY)
119+
.required()
120+
.auth_style("bearer")
121+
.header_name("authorization")
122+
.refresh(proto::ProviderCredentialRefresh {
123+
strategy: proto::ProviderCredentialRefreshStrategy::Oauth2RefreshToken as i32,
124+
token_url: keycloak.token_endpoint(),
125+
scopes: vec!["openid".to_string()],
126+
refresh_before: seconds(60),
127+
max_lifetime: seconds(3600),
128+
material: vec![
129+
material("client_id", false),
130+
material("refresh_token", true),
131+
],
132+
..proto::ProviderCredentialRefresh::default()
133+
}),
134+
)
135+
.endpoint("keycloak.test.invalid", 443, |ep| {
136+
ep.rest(Access::ReadOnly).enforce();
137+
})
138+
.binaries(["/usr/bin/curl"])
180139
}

0 commit comments

Comments
 (0)