@faable/faable 4.18.5 → 4.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -379,6 +379,13 @@ class FaableApi {
379
379
  async setDeployBranch(app_id, github_branch) {
380
380
  return data(this.client.post(`/app/${app_id}/deploy-branch`, { github_branch }));
381
381
  }
382
+ // The GitLab project (numeric id) whose CI may deploy the app keyless; null
383
+ // revokes it.
384
+ async setCiTrust(app_id, gitlab_project_id) {
385
+ return data(this.client.post(`/app/${app_id}/ci-trust`, {
386
+ gitlab_project_id
387
+ }));
388
+ }
382
389
  // null clears the platform override (faable.json's rootDir applies again).
383
390
  async setRootDir(app_id, root_dir) {
384
391
  return data(this.client.post(`/app/${app_id}/root-dir`, { root_dir }));
@@ -38,6 +38,21 @@ const context = async (targetAppId) => {
38
38
  console.error("Error fetching token, configure 'permissions: id-token: write'");
39
39
  }
40
40
  }
41
+ else if (process.env.GITLAB_CI === "true" && process.env.FAABLE_ID_TOKEN) {
42
+ // GitLab CI: the job declares
43
+ // id_tokens: { FAABLE_ID_TOKEN: { aud: https://faable.com } }
44
+ // and the app must trust its project (`deploy apps set --gitlab-project`).
45
+ // Without FAABLE_ID_TOKEN a GitLab job falls through to an API key or
46
+ // the local credentials, as before.
47
+ api = FaableApi.create({
48
+ authStrategy: oidc_strategy,
49
+ auth: {
50
+ idToken: process.env.FAABLE_ID_TOKEN,
51
+ appId: targetAppId,
52
+ provider: "gitlab",
53
+ },
54
+ });
55
+ }
41
56
  else {
42
57
  const store = new CredentialsStore();
43
58
  // Auto-refreshes an expired access token via the stored refresh_token.
@@ -1,6 +1,31 @@
1
1
  import axios from 'axios';
2
2
  import { create_base_client } from '../base_client.js';
3
3
 
4
+ // GitLab has no linked repository to infer the app from: the job names it
5
+ // with `faable deploy --app`, and the api checks the app trusts the token's
6
+ // project (`faable deploy apps set --gitlab-project <id>`).
7
+ const exchangeGitlabOidcToken = async (gl_token, target_app_id) => {
8
+ if (!target_app_id) {
9
+ throw new Error("Keyless deploys from GitLab CI need the app: run `faable deploy --app <app_id>`.");
10
+ }
11
+ const client = create_base_client();
12
+ try {
13
+ const res = await client.post("/auth/gitlab-oidc", {
14
+ token: gl_token,
15
+ app_id: target_app_id,
16
+ });
17
+ const { access_token, app_id } = res.data;
18
+ return { access_token, app_id };
19
+ }
20
+ catch (err) {
21
+ if (axios.isAxiosError(err)) {
22
+ const status = err.response?.status;
23
+ const serverMessage = err.response?.data?.message;
24
+ throw new Error(`Faable OIDC token exchange failed (${status ?? "network error"})${serverMessage ? `: ${serverMessage}` : ""}`, { cause: err });
25
+ }
26
+ throw err;
27
+ }
28
+ };
4
29
  const exchangeGithubOidcToken = async (gh_token, target_app_id) => {
5
30
  const client = create_base_client();
6
31
  try {
@@ -35,14 +60,16 @@ const exchangeGithubOidcToken = async (gh_token, target_app_id) => {
35
60
  }
36
61
  };
37
62
  const oidc_strategy = (config) => {
38
- const { idToken, appId } = config;
63
+ const { idToken, appId, provider = "github" } = config;
39
64
  if (!idToken) {
40
65
  throw new Error("Missing idToken.");
41
66
  }
42
67
  let token_ex;
43
68
  const ensure = async () => {
44
69
  if (!token_ex)
45
- token_ex = await exchangeGithubOidcToken(idToken, appId);
70
+ token_ex = await (provider === "gitlab"
71
+ ? exchangeGitlabOidcToken(idToken, appId)
72
+ : exchangeGithubOidcToken(idToken, appId));
46
73
  return token_ex;
47
74
  };
48
75
  return {
@@ -31,16 +31,25 @@ const apps_set = {
31
31
  choices: MODES,
32
32
  description: 'push: every push deploys · ci: once CI tags a release · workflow: your own GitHub workflow runs faable deploy'
33
33
  })
34
- .check(({ branch, rootDir, mode }) => {
34
+ .option('gitlab-project', {
35
+ type: 'string',
36
+ description: 'GitLab project ID (the number on its page) whose CI may deploy this app without an API key ("" revokes it)'
37
+ })
38
+ .check(({ branch, rootDir, mode, gitlabProject }) => {
35
39
  if (branch === undefined &&
36
40
  rootDir === undefined &&
37
- mode === undefined) {
38
- throw new Error('Nothing to change: pass --branch, --root-dir or --mode.');
41
+ mode === undefined &&
42
+ gitlabProject === undefined) {
43
+ throw new Error('Nothing to change: pass --branch, --root-dir, --mode or --gitlab-project.');
44
+ }
45
+ if (gitlabProject && !/^[0-9]+$/.test(String(gitlabProject))) {
46
+ throw new Error('--gitlab-project takes the numeric Project ID shown on the GitLab project page, not its path.');
39
47
  }
40
48
  return true;
41
49
  })
42
50
  .example('$0 deploy apps set --branch release', 'Deploy from "release"')
43
51
  .example('$0 deploy apps set --root-dir apps/web', 'A monorepo app')
52
+ .example('$0 deploy apps set --gitlab-project 12345678', 'Let that GitLab project deploy keyless')
44
53
  .showHelpOnFail(false),
45
54
  handler: async (args) => {
46
55
  const ctx = await requireApi();
@@ -60,6 +69,12 @@ const apps_set = {
60
69
  await ctx.api.setDeployMode(app_id, args.mode);
61
70
  log.info(`⚙️ Deploy mode: ${args.mode}`);
62
71
  }
72
+ if (args.gitlabProject !== undefined) {
73
+ await ctx.api.setCiTrust(app_id, String(args.gitlabProject) || null);
74
+ log.info(args.gitlabProject
75
+ ? `🦊 GitLab project ${args.gitlabProject} can deploy this app without an API key`
76
+ : '🦊 GitLab keyless deploys revoked');
77
+ }
63
78
  const app = await ctx.api.getApp(app_id);
64
79
  if (args.json)
65
80
  return print_json(app);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faable/faable",
3
- "version": "4.18.5",
3
+ "version": "4.19.0",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "author": "Marc Pomar <marc@faable.com>",