Skip to content

Commit c16a731

Browse files
authored
Fix for CSS7 including creating credentials via a script (#109)
1 parent c90f46d commit c16a731

3 files changed

Lines changed: 143 additions & 16 deletions

File tree

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ Use the script with the `-d` option to use the local tests:
7373
```shell
7474
./run.sh -d . css
7575
```
76+
77+
## Tips for CSS
78+
For testing CSS, the run script has the ability to register test accounts on a fresh server installation. It depends on
79+
node 18+ being available. It calls `clientCredentials.js` to add the required client ids and secrets to a temporary env
80+
file.
81+
7682
## Creating a script for a CI workflow
7783
If you just want to run tests against a single test subject, for example in a CI workflow, you can create a script such
7884
as this one which will run the tests embedded in the latest published CTH image:

createCredentials.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// This script is derived from a script created for CSS:
2+
// https://github.com/CommunitySolidServer/CommunitySolidServer/blob/main/test/deploy/createAccountCredentials.ts
3+
4+
if (process.argv.length !== 3) {
5+
throw new Error('Exactly 1 parameter is needed: the server URL.');
6+
}
7+
8+
const baseUrl = process.argv[2];
9+
10+
const alice = {
11+
email: 'alice@example.com',
12+
password: 'alice-secret',
13+
podName: 'alice',
14+
};
15+
16+
const bob = {
17+
email: 'bob@example.com',
18+
password: 'bob-secret',
19+
podName: 'bob',
20+
};
21+
22+
/**
23+
* Registers a user with the server and provides them with a pod.
24+
* @param user - The user settings necessary to register a user.
25+
*/
26+
async function register(user) {
27+
// Get controls
28+
let res = await fetch(new URL('.account/', baseUrl));
29+
let { controls } = await res.json();
30+
31+
// Create account
32+
res = await fetch(controls.account.create, { method: 'POST' });
33+
if (res.status !== 200) {
34+
throw new Error(`Account creation failed: ${await res.text()}`);
35+
}
36+
const authorization = `CSS-Account-Token ${(await res.json()).authorization}`;
37+
38+
// Get account controls
39+
res = await fetch(controls.main.index, {
40+
headers: { authorization },
41+
});
42+
({ controls } = await res.json());
43+
44+
// Add login method
45+
res = await fetch(controls.password.create, {
46+
method: 'POST',
47+
headers: { authorization, 'content-type': 'application/json' },
48+
body: JSON.stringify({
49+
email: user.email,
50+
password: user.password,
51+
}),
52+
});
53+
if (res.status !== 200) {
54+
throw new Error(`Login creation failed: ${await res.text()}`);
55+
}
56+
57+
// Create pod
58+
res = await fetch(controls.account.pod, {
59+
method: 'POST',
60+
headers: { authorization, 'content-type': 'application/json' },
61+
body: JSON.stringify({ name: user.podName }),
62+
});
63+
if (res.status !== 200) {
64+
throw new Error(`Pod creation failed: ${await res.text()}`);
65+
}
66+
const { webId } = await res.json();
67+
68+
return { webId, authorization };
69+
}
70+
71+
/**
72+
* Requests a client credentials API token.
73+
* @param webId - WebID to create credentials for.
74+
* @param authorization - Authorization header for the account that tries to create credentials.
75+
* @returns The id/secret for the client credentials request.
76+
*/
77+
async function createCredentials(webId, authorization) {
78+
let res = await fetch(new URL('.account/', baseUrl), {
79+
headers: { authorization },
80+
});
81+
const { controls } = await res.json();
82+
83+
res = await fetch(controls.account.clientCredentials, {
84+
method: 'POST',
85+
headers: { authorization, 'content-type': 'application/json' },
86+
body: JSON.stringify({ name: 'token', webId }),
87+
});
88+
if (res.status !== 200) {
89+
throw new Error(`Token generation failed: ${await res.text()}`);
90+
}
91+
92+
return res.json();
93+
}
94+
95+
/**
96+
* Generates all the necessary data and outputs the necessary lines
97+
* that need to be added to the CTH environment file
98+
* so it can use client credentials.
99+
* @param user - User for which data needs to be generated.
100+
*/
101+
async function outputCredentials(user) {
102+
const { webId, authorization } = await register(user);
103+
const { id, secret } = await createCredentials(webId, authorization);
104+
105+
const name = user.podName.toUpperCase();
106+
console.log(`USERS_${name}_CLIENTID=${id}`);
107+
console.log(`USERS_${name}_CLIENTSECRET=${secret}`);
108+
}
109+
110+
/**
111+
* Ends the process and writes out an error in case something goes wrong.
112+
*/
113+
function endProcess(error) {
114+
console.error(error);
115+
process.exit(1);
116+
}
117+
118+
// Create tokens for Alice and Bob
119+
outputCredentials(alice).catch(endProcess);
120+
outputCredentials(bob).catch(endProcess);

run.sh

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,22 @@ setup_css() {
2929
mkdir -p config
3030
cat > ./config/css-config.json <<EOF
3131
{
32-
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^5.0.0/components/context.jsonld",
32+
"@context": "https://linkedsoftwaredependencies.org/bundles/npm/@solid/community-server/^7.0.0/components/context.jsonld",
3333
"import": [
34+
"css:config/app/init/static-root.json",
3435
"css:config/app/main/default.json",
35-
"css:config/app/init/initialize-prefilled-root.json",
36-
"css:config/app/setup/optional.json",
3736
"css:config/app/variables/default.json",
3837
"css:config/http/handler/default.json",
39-
"css:config/http/middleware/websockets.json",
40-
"css:config/http/server-factory/https-websockets.json",
38+
"css:config/http/middleware/default.json",
39+
"css:config/http/notifications/all.json",
40+
"css:config/http/server-factory/https.json",
4141
"css:config/http/static/default.json",
4242
"css:config/identity/access/public.json",
4343
"css:config/identity/email/default.json",
4444
"css:config/identity/handler/default.json",
45+
"css:config/identity/oidc/default.json",
4546
"css:config/identity/ownership/token.json",
4647
"css:config/identity/pod/static.json",
47-
"css:config/identity/registration/enabled.json",
4848
"css:config/ldp/authentication/dpop-bearer.json",
4949
"css:config/ldp/authorization/webacl.json",
5050
"css:config/ldp/handler/default.json",
@@ -53,6 +53,7 @@ setup_css() {
5353
"css:config/ldp/modes/default.json",
5454
"css:config/storage/backend/memory.json",
5555
"css:config/storage/key-value/resource-store.json",
56+
"css:config/storage/location/pod.json",
5657
"css:config/storage/middleware/default.json",
5758
"css:config/util/auxiliary/acl.json",
5859
"css:config/util/identifiers/suffix.json",
@@ -62,13 +63,6 @@ setup_css() {
6263
"css:config/util/resource-locker/memory.json",
6364
"css:config/util/variables/default.json"
6465
],
65-
"@graph": [
66-
{
67-
"comment": [
68-
"Adds CLI options --httpsKey and --httpsCert and uses those to start an HTTPS server."
69-
]
70-
},
71-
]
7266
}
7367
EOF
7468

@@ -85,7 +79,7 @@ EOF
8579
docker run -d --name=server --network=testnet --env NODE_TLS_REJECT_UNAUTHORIZED=0 \
8680
-v "$(pwd)"/config:/config \
8781
-v "$(pwd)"/certs:/certs \
88-
-p 443:443 -it solidproject/community-server:5 \
82+
-p 443:443 -it solidproject/community-server:7 \
8983
-c /config/css-config.json \
9084
--httpsKey=/certs/server.key --httpsCert=/certs/server.cert \
9185
--port=443 --baseUrl=https://server/
@@ -96,6 +90,10 @@ EOF
9690
sleep 1
9791
done
9892
echo 'CSS is running'
93+
94+
echo 'Creating client credentials'
95+
cp css.env css-creds.env
96+
NODE_TLS_REJECT_UNAUTHORIZED=0 node createCredentials.js https://server >> css-creds.env
9997
}
10098

10199
stop_css() {
@@ -171,7 +169,7 @@ shift
171169

172170
echo "Running tests on $subject and reporting to $cwd/reports/$subject"
173171

174-
dockerargs+=('-v' "$cwd/reports/$outdir:/reports" "--env-file=$envfile")
172+
dockerargs+=('-v' "$cwd/reports/$outdir:/reports")
175173
if ! [[ "$*" == *"--target="* ]]; then
176174
harnessargs+=("--target=https://github.com/solid/conformance-test-harness/$subject")
177175
fi
@@ -183,7 +181,10 @@ mkdir -p reports/$subject
183181
if [ $subject == "css" ]
184182
then
185183
setup_css
186-
dockerargs+=('--network=testnet')
184+
dockerargs+=('--env-file=css-creds.env' '--network=testnet')
185+
harnessargs+=('--skip-teardown')
186+
else
187+
dockerargs+=('--env-file=$envfile')
187188
fi
188189

189190
# optionally pull published CTH image

0 commit comments

Comments
 (0)