@extrahorizon/exh-cli 1.8.2-feat-84-88251d7 → 1.9.0-dev-85-4c62b0a

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Extra Horizon CLI changelog
2
2
 
3
+ ### v1.9.0
4
+ * Introduced the `executionCredentials` field in the task configuration:
5
+ * Specify the permissions your task needs
6
+ * The CLI automatically creates a user, role, and credentials for your task
7
+ * These credentials are injected as environment variables into the task automatically
8
+
3
9
  ### v1.8.2
4
10
  * Updated the ExH SDK to `8.6.0` to fix a security warning from `axios`
5
11
 
@@ -5,6 +5,7 @@ const fs = require("fs/promises");
5
5
  const chalk = require("chalk");
6
6
  const constants_1 = require("../../constants");
7
7
  const util_1 = require("../../helpers/util");
8
+ const authRepository = require("../../repositories/auth");
8
9
  const functionRepository = require("../../repositories/functions");
9
10
  const taskConfig_1 = require("./taskConfig");
10
11
  const util_2 = require("./util");
@@ -107,7 +108,7 @@ async function syncSingleTask(sdk, config) {
107
108
  });
108
109
  config.environment = {
109
110
  ...config.environment,
110
- API_HOST: process.env.API_HOST,
111
+ API_HOST: authRepository.getHost(sdk),
111
112
  API_OAUTH_CONSUMER_KEY: process.env.API_OAUTH_CONSUMER_KEY,
112
113
  API_OAUTH_CONSUMER_SECRET: process.env.API_OAUTH_CONSUMER_SECRET,
113
114
  API_OAUTH_TOKEN: credentials.token,
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.syncFunctionUser = exports.zipFileFromDirectory = void 0;
4
4
  const fs_1 = require("fs");
5
5
  const os_1 = require("os");
6
+ const javascript_sdk_1 = require("@extrahorizon/javascript-sdk");
6
7
  const archiver = require("archiver");
7
8
  const chalk = require("chalk");
8
9
  const uuid_1 = require("uuid");
@@ -37,10 +38,13 @@ async function syncFunctionUser(sdk, data) {
37
38
  const roleName = `exh.tasks.${taskName}`;
38
39
  const role = await syncRoleWithPermissions(sdk, taskName, roleName, targetPermissions);
39
40
  let user = await userRepository.findUserByEmail(sdk, email);
40
- console.group(chalk.white(`🔄 Syncing user: ${email}`));
41
41
  if (!user) {
42
- console.log(chalk.white('⚙️ Creating the user...'));
43
- user = await userRepository.createUser(sdk, {
42
+ console.log(chalk.white('⚙️ Creating a user for the task'));
43
+ const { emailAvailable } = await sdk.users.isEmailAvailable(email);
44
+ if (!emailAvailable) {
45
+ throw new Error('❌ The user could not be created as the email address is already in use');
46
+ }
47
+ user = await sdk.users.createAccount({
44
48
  firstName: `${taskName}`,
45
49
  lastName: 'exh.tasks',
46
50
  email,
@@ -48,13 +52,11 @@ async function syncFunctionUser(sdk, data) {
48
52
  phoneNumber: '0000000000',
49
53
  language: 'EN',
50
54
  });
55
+ console.log(chalk.green('✅ Successfully created a user for task'));
51
56
  await assignRoleToUser(sdk, user.id, role.id);
52
- const oAuth1Tokens = await createOAuth1Tokens(sdk, email, password);
53
- console.groupEnd();
54
- console.log(chalk.green('✅ Successfully synced user'));
55
- console.log('');
56
- return oAuth1Tokens;
57
+ return await createOAuth1Tokens(sdk, email, password);
57
58
  }
59
+ console.log(chalk.white('⚙️ Checking for the existing users credentials'));
58
60
  const currentFunction = await functionRepository.findByName(sdk, taskName);
59
61
  const hasExistingCredentials = (currentFunction?.environmentVariables?.API_HOST?.value &&
60
62
  currentFunction?.environmentVariables?.API_OAUTH_TOKEN_SECRET?.value &&
@@ -64,14 +66,11 @@ async function syncFunctionUser(sdk, data) {
64
66
  if (!hasExistingCredentials) {
65
67
  throw new Error('❌ No credentials were found for the existing user');
66
68
  }
67
- console.log(chalk.white('⚙️ Reusing existing user credentials...'));
68
69
  const userRole = user.roles.find(({ name }) => name === roleName);
69
70
  if (!userRole) {
70
71
  await assignRoleToUser(sdk, user.id, role.id);
71
72
  }
72
- console.groupEnd();
73
- console.log(chalk.green('✅ Successfully synced user'));
74
- console.log('');
73
+ console.log(chalk.green('✅ Using existing credentials for the user'));
75
74
  return {
76
75
  token: currentFunction.environmentVariables.API_OAUTH_TOKEN.value,
77
76
  tokenSecret: currentFunction.environmentVariables.API_OAUTH_TOKEN_SECRET.value,
@@ -79,50 +78,38 @@ async function syncFunctionUser(sdk, data) {
79
78
  }
80
79
  exports.syncFunctionUser = syncFunctionUser;
81
80
  async function syncRoleWithPermissions(sdk, taskName, roleName, targetPermissions) {
82
- console.group(chalk.white(`🔄 Syncing role: ${roleName}`));
83
- if (targetPermissions.length === 0) {
84
- console.log(chalk.yellow('⚠️ No permissions have been defined for the role'));
85
- }
86
- let role = await userRepository.findGlobalRoleByName(sdk, roleName);
81
+ console.log(chalk.white('⚙️ Checking if the role exists'));
82
+ let role = await sdk.users.globalRoles.findByName(roleName);
87
83
  if (!role) {
88
- console.log(chalk.white('⚙️ Creating the role...'));
89
- const roleDescription = `A role created by the CLI for the execution of the task ${taskName}`;
90
- role = await userRepository.createGlobalRole(sdk, roleName, roleDescription);
91
- if (targetPermissions.length !== 0) {
92
- await userRepository.addPermissionsToGlobalRole(sdk, roleName, targetPermissions);
93
- }
94
- console.log(chalk.white(`🔐 Permissions added: [${targetPermissions.join(', ')}]`));
95
- console.groupEnd();
96
- console.log(chalk.green('✅ Successfully synced role'));
97
- console.log('');
84
+ console.log(chalk.white('⚙️ Role does not exist, creating a new role'));
85
+ role = await sdk.users.globalRoles.create({
86
+ name: roleName,
87
+ description: `A role created by the CLI for the execution of the task ${taskName}`,
88
+ });
89
+ console.log(chalk.white('⚙️ Assigning permissions to the role'));
90
+ await sdk.users.globalRoles.addPermissions((0, javascript_sdk_1.rqlBuilder)().eq('name', roleName).build(), { permissions: targetPermissions });
91
+ console.log(chalk.green('✅ Successfully assigned permissions to the role'));
98
92
  return role;
99
93
  }
100
- console.log(chalk.white('⚙️ Updating the role...'));
101
94
  const currentPermissions = role.permissions?.flatMap(permission => permission.name) || [];
102
95
  const permissionsToAdd = targetPermissions.filter(targetPermission => !currentPermissions.includes(targetPermission));
103
96
  const permissionsToRemove = currentPermissions.filter(currentPermission => !targetPermissions.includes(currentPermission));
104
97
  if (permissionsToAdd.length > 0) {
105
- await userRepository.addPermissionsToGlobalRole(sdk, roleName, permissionsToAdd);
106
- console.log(chalk.white(`🔐 Permissions added: [${permissionsToAdd.join(',')}]`));
98
+ console.log(chalk.white('⚙️ Adding missing permissions to the role'));
99
+ await sdk.users.globalRoles.addPermissions((0, javascript_sdk_1.rqlBuilder)().eq('name', roleName).build(), { permissions: permissionsToAdd });
100
+ console.log(chalk.green('✅ Successfully added missing permissions to the role'));
107
101
  }
108
102
  if (permissionsToRemove.length > 0) {
109
- await userRepository.removePermissionsFromGlobalRole(sdk, roleName, permissionsToRemove);
110
- console.log(chalk.white(`🔐 Permissions removed: [${permissionsToRemove.join(',')}]`));
103
+ console.log(chalk.white('⚙️ Removing excess permissions from the role'));
104
+ await sdk.users.globalRoles.removePermissions((0, javascript_sdk_1.rqlBuilder)().eq('name', roleName).build(), { permissions: permissionsToRemove });
105
+ console.log(chalk.green('✅ Successfully removed excess permissions from the role'));
111
106
  }
112
- console.groupEnd();
113
- console.log(chalk.green('✅ Successfully synced role'));
114
- console.log('');
115
107
  return role;
116
108
  }
117
109
  async function assignRoleToUser(sdk, userId, roleId) {
118
- console.log(chalk.white('⚙️ Assigning the role to the user...'));
119
- await userRepository.addGlobalRoleToUser(sdk, userId, roleId);
120
- }
121
- async function createOAuth1Tokens(sdk, email, password) {
122
- console.log(chalk.white('⚙️ Creating credentials...'));
123
- const response = await authRepository.createOAuth1Tokens(sdk, email, password);
124
- const { token, tokenSecret } = response;
125
- return { token, tokenSecret };
110
+ console.log(chalk.white('⚙️ Assigning the role to the user'));
111
+ await sdk.users.globalRoles.addToUsers((0, javascript_sdk_1.rqlBuilder)().eq('id', userId).build(), { roles: [roleId] });
112
+ console.log(chalk.green('✅ Successfully assigned the role to the user'));
126
113
  }
127
114
  function validateEmail(email) {
128
115
  const emailRegex = /.+@.+\..+/;
@@ -130,3 +117,10 @@ function validateEmail(email) {
130
117
  throw new Error('Invalid email address');
131
118
  }
132
119
  }
120
+ async function createOAuth1Tokens(sdk, email, password) {
121
+ console.log(chalk.white('⚙️ Creating OAuth1 tokens for the user', email));
122
+ const response = await authRepository.createOAuth1Tokens(sdk, email, password);
123
+ const { token, tokenSecret } = response.data;
124
+ console.log(chalk.green('✅ Successfully created OAuth1 tokens for the user', email));
125
+ return { token, tokenSecret };
126
+ }
@@ -7,11 +7,7 @@ async function find(sdk) {
7
7
  }
8
8
  exports.find = find;
9
9
  async function findByName(sdk, name) {
10
- const response = await sdk.raw.get(`/tasks/v1/functions/${name}`, { customResponseKeys: ['*'] })
11
- .catch(e => e);
12
- if (response.status === 404) {
13
- return undefined;
14
- }
10
+ const response = await sdk.raw.get(`/tasks/v1/functions/${name}`, { customResponseKeys: ['*'] });
15
11
  return response.data;
16
12
  }
17
13
  exports.findByName = findByName;
@@ -1,9 +1,2 @@
1
- import { OAuth1Client, RegisterUserData } from '@extrahorizon/javascript-sdk';
1
+ import { OAuth1Client } from '@extrahorizon/javascript-sdk';
2
2
  export declare function findUserByEmail(sdk: OAuth1Client, email: string): Promise<import("@extrahorizon/javascript-sdk").UserData>;
3
- export declare function isEmailAvailable(sdk: OAuth1Client, email: string): Promise<boolean>;
4
- export declare function createUser(sdk: OAuth1Client, data: RegisterUserData): Promise<import("@extrahorizon/javascript-sdk").UserData>;
5
- export declare function findGlobalRoleByName(sdk: OAuth1Client, name: string): Promise<import("@extrahorizon/javascript-sdk").Role>;
6
- export declare function createGlobalRole(sdk: OAuth1Client, name: string, description: string): Promise<import("@extrahorizon/javascript-sdk").Role>;
7
- export declare function addPermissionsToGlobalRole(sdk: OAuth1Client, name: string, permissions: string[]): Promise<import("@extrahorizon/javascript-sdk").AffectedRecords>;
8
- export declare function removePermissionsFromGlobalRole(sdk: OAuth1Client, name: string, permissions: string[]): Promise<import("@extrahorizon/javascript-sdk").AffectedRecords>;
9
- export declare function addGlobalRoleToUser(sdk: OAuth1Client, userId: string, roleId: string): Promise<import("@extrahorizon/javascript-sdk").AffectedRecords>;
@@ -1,38 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.addGlobalRoleToUser = exports.removePermissionsFromGlobalRole = exports.addPermissionsToGlobalRole = exports.createGlobalRole = exports.findGlobalRoleByName = exports.createUser = exports.isEmailAvailable = exports.findUserByEmail = void 0;
3
+ exports.findUserByEmail = void 0;
4
4
  const javascript_sdk_1 = require("@extrahorizon/javascript-sdk");
5
5
  async function findUserByEmail(sdk, email) {
6
6
  const rql = (0, javascript_sdk_1.rqlBuilder)().eq('email', email).build();
7
7
  return await sdk.users.findFirst({ rql });
8
8
  }
9
9
  exports.findUserByEmail = findUserByEmail;
10
- async function isEmailAvailable(sdk, email) {
11
- const { emailAvailable } = await sdk.users.isEmailAvailable(email);
12
- return emailAvailable;
13
- }
14
- exports.isEmailAvailable = isEmailAvailable;
15
- async function createUser(sdk, data) {
16
- return await sdk.users.createAccount(data);
17
- }
18
- exports.createUser = createUser;
19
- async function findGlobalRoleByName(sdk, name) {
20
- return await sdk.users.globalRoles.findByName(name);
21
- }
22
- exports.findGlobalRoleByName = findGlobalRoleByName;
23
- async function createGlobalRole(sdk, name, description) {
24
- return await sdk.users.globalRoles.create({ name, description });
25
- }
26
- exports.createGlobalRole = createGlobalRole;
27
- async function addPermissionsToGlobalRole(sdk, name, permissions) {
28
- return await sdk.users.globalRoles.addPermissions((0, javascript_sdk_1.rqlBuilder)().eq('name', name).build(), { permissions });
29
- }
30
- exports.addPermissionsToGlobalRole = addPermissionsToGlobalRole;
31
- async function removePermissionsFromGlobalRole(sdk, name, permissions) {
32
- return await sdk.users.globalRoles.removePermissions((0, javascript_sdk_1.rqlBuilder)().eq('name', name).build(), { permissions });
33
- }
34
- exports.removePermissionsFromGlobalRole = removePermissionsFromGlobalRole;
35
- async function addGlobalRoleToUser(sdk, userId, roleId) {
36
- return await sdk.users.globalRoles.addToUsers((0, javascript_sdk_1.rqlBuilder)().eq('id', userId).build(), { roles: [roleId] });
37
- }
38
- exports.addGlobalRoleToUser = addGlobalRoleToUser;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@extrahorizon/exh-cli",
3
- "version": "1.8.2-feat-84-88251d7",
3
+ "version": "1.9.0-dev-85-4c62b0a",
4
4
  "main": "build/index.js",
5
5
  "exports": "./build/index.js",
6
6
  "license": "MIT",