plugin-workflow-portainer 0.0.0-stage → 0.1.1

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.
Files changed (98) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +31 -3
  3. package/client-v2.js +1 -0
  4. package/client.js +1 -0
  5. package/dist/client/127.5012da93825a6e5c.js +10 -0
  6. package/dist/client/57.ace9538136438718.js +10 -0
  7. package/dist/client/65.ec4a55173511c06d.js +10 -0
  8. package/dist/client/660.546c702d1ddf7eff.js +10 -0
  9. package/dist/client/index.js +10 -0
  10. package/dist/client-v2/127.0ec4dce7b13750e9.js +10 -0
  11. package/dist/client-v2/57.e7cfc50fb651ac14.js +10 -0
  12. package/dist/client-v2/65.9c745ea32ac96e98.js +10 -0
  13. package/dist/client-v2/660.29ae3c3b4040039d.js +10 -0
  14. package/dist/client-v2/index.js +10 -0
  15. package/dist/externalVersion.js +20 -0
  16. package/dist/index.js +48 -0
  17. package/dist/locale/en-US.json +151 -0
  18. package/dist/locale/vi-VN.json +151 -0
  19. package/dist/locale/zh-CN.json +151 -0
  20. package/dist/node_modules/joi/dist/joi-browser.min.js +1 -0
  21. package/dist/node_modules/joi/lib/annotate.js +175 -0
  22. package/dist/node_modules/joi/lib/base.js +1069 -0
  23. package/dist/node_modules/joi/lib/cache.js +143 -0
  24. package/dist/node_modules/joi/lib/common.js +216 -0
  25. package/dist/node_modules/joi/lib/compile.js +283 -0
  26. package/dist/node_modules/joi/lib/errors.js +271 -0
  27. package/dist/node_modules/joi/lib/extend.js +312 -0
  28. package/dist/node_modules/joi/lib/index.d.ts +2365 -0
  29. package/dist/node_modules/joi/lib/index.js +1 -0
  30. package/dist/node_modules/joi/lib/manifest.js +476 -0
  31. package/dist/node_modules/joi/lib/messages.js +207 -0
  32. package/dist/node_modules/joi/lib/modify.js +267 -0
  33. package/dist/node_modules/joi/lib/ref.js +414 -0
  34. package/dist/node_modules/joi/lib/schemas.js +302 -0
  35. package/dist/node_modules/joi/lib/state.js +166 -0
  36. package/dist/node_modules/joi/lib/template.js +463 -0
  37. package/dist/node_modules/joi/lib/trace.js +346 -0
  38. package/dist/node_modules/joi/lib/types/alternatives.js +364 -0
  39. package/dist/node_modules/joi/lib/types/any.js +174 -0
  40. package/dist/node_modules/joi/lib/types/array.js +809 -0
  41. package/dist/node_modules/joi/lib/types/binary.js +100 -0
  42. package/dist/node_modules/joi/lib/types/boolean.js +150 -0
  43. package/dist/node_modules/joi/lib/types/date.js +233 -0
  44. package/dist/node_modules/joi/lib/types/function.js +93 -0
  45. package/dist/node_modules/joi/lib/types/keys.js +1084 -0
  46. package/dist/node_modules/joi/lib/types/link.js +184 -0
  47. package/dist/node_modules/joi/lib/types/number.js +363 -0
  48. package/dist/node_modules/joi/lib/types/object.js +22 -0
  49. package/dist/node_modules/joi/lib/types/string.js +850 -0
  50. package/dist/node_modules/joi/lib/types/symbol.js +102 -0
  51. package/dist/node_modules/joi/lib/validator.js +750 -0
  52. package/dist/node_modules/joi/lib/values.js +263 -0
  53. package/dist/node_modules/joi/node_modules/@hapi/topo/lib/index.d.ts +60 -0
  54. package/dist/node_modules/joi/node_modules/@hapi/topo/lib/index.js +225 -0
  55. package/dist/node_modules/joi/node_modules/@hapi/topo/package.json +30 -0
  56. package/dist/node_modules/joi/package.json +1 -0
  57. package/dist/server/actions/portainer.js +192 -0
  58. package/dist/server/collections/portainer-instances.js +54 -0
  59. package/dist/server/index.js +54 -0
  60. package/dist/server/instructions/PortainerStackControlInstruction.js +156 -0
  61. package/dist/server/instructions/PortainerStackRedeployInstruction.js +254 -0
  62. package/dist/server/instructions/helpers.js +211 -0
  63. package/dist/server/plugin.js +170 -0
  64. package/dist/server/services/PortainerClient.js +497 -0
  65. package/dist/shared/collection.js +57 -0
  66. package/dist/shared/constants.js +66 -0
  67. package/dist/shared/env.js +221 -0
  68. package/dist/shared/types.js +24 -0
  69. package/package.json +50 -4
  70. package/server.js +1 -0
  71. package/src/client/index.tsx +50 -0
  72. package/src/client/instructions.tsx +76 -0
  73. package/src/client-v2/__tests__/workflow-registration.test.tsx +132 -0
  74. package/src/client-v2/components/ControlFieldset.tsx +46 -0
  75. package/src/client-v2/components/PortainerEnvEditor.tsx +223 -0
  76. package/src/client-v2/components/PortainerSettingsPage.tsx +671 -0
  77. package/src/client-v2/components/PortainerTargetFields.tsx +317 -0
  78. package/src/client-v2/components/RedeployFieldset.tsx +123 -0
  79. package/src/client-v2/index.tsx +50 -0
  80. package/src/client-v2/instructions.tsx +162 -0
  81. package/src/client-v2/locale.ts +24 -0
  82. package/src/index.ts +11 -0
  83. package/src/locale/en-US.json +151 -0
  84. package/src/locale/vi-VN.json +151 -0
  85. package/src/locale/zh-CN.json +151 -0
  86. package/src/server/__tests__/portainer-workflow.test.ts +455 -0
  87. package/src/server/actions/portainer.ts +196 -0
  88. package/src/server/collections/portainer-instances.ts +34 -0
  89. package/src/server/index.ts +13 -0
  90. package/src/server/instructions/PortainerStackControlInstruction.ts +139 -0
  91. package/src/server/instructions/PortainerStackRedeployInstruction.ts +255 -0
  92. package/src/server/instructions/helpers.ts +237 -0
  93. package/src/server/plugin.ts +167 -0
  94. package/src/server/services/PortainerClient.ts +589 -0
  95. package/src/shared/collection.ts +32 -0
  96. package/src/shared/constants.ts +25 -0
  97. package/src/shared/env.ts +228 -0
  98. package/src/shared/types.ts +105 -0
@@ -0,0 +1,167 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ import { resolve } from 'node:path';
11
+ import type { Context } from '@nocobase/actions';
12
+ import type { Model } from '@nocobase/database';
13
+ import type WorkflowPlugin from '@nocobase/plugin-workflow';
14
+ import { Plugin } from '@nocobase/server';
15
+ import {
16
+ INSTRUCTION_TYPES,
17
+ PORTAINER_ACL_SNIPPET,
18
+ PORTAINER_INSTANCES_COLLECTION,
19
+ PORTAINER_RESOURCE,
20
+ SECRET_MASK,
21
+ } from '../shared/constants';
22
+ import { createPortainerResourceActions } from './actions/portainer';
23
+ import { PortainerStackControlInstruction } from './instructions/PortainerStackControlInstruction';
24
+ import { PortainerStackRedeployInstruction } from './instructions/PortainerStackRedeployInstruction';
25
+ import { assertValidPortainerUrl, clampTimeoutMs } from './services/PortainerClient';
26
+
27
+ const ENCRYPTED_FIELDS = ['apiKey', 'gitAccessToken'] as const;
28
+ const METADATA_READ_ACTIONS = ['listInstances', 'listEndpoints', 'listStacks', 'getStackDetail'] as const;
29
+
30
+ function maskSecretFields(payload: unknown): unknown {
31
+ if (!payload || typeof payload !== 'object') {
32
+ return payload;
33
+ }
34
+ if (Array.isArray(payload)) {
35
+ return payload.map(maskSecretFields);
36
+ }
37
+ const obj = payload as Record<string, unknown>;
38
+ if (typeof obj.toJSON === 'function') {
39
+ return maskSecretFields((obj as { toJSON: () => unknown }).toJSON());
40
+ }
41
+ const clone: Record<string, unknown> = { ...obj };
42
+ for (const field of ENCRYPTED_FIELDS) {
43
+ if (typeof clone[field] === 'string' && clone[field]) {
44
+ clone[field] = SECRET_MASK;
45
+ }
46
+ }
47
+ if (Array.isArray(clone.data)) {
48
+ clone.data = clone.data.map(maskSecretFields);
49
+ }
50
+ if (clone.data && typeof clone.data === 'object' && !Array.isArray(clone.data)) {
51
+ clone.data = maskSecretFields(clone.data);
52
+ }
53
+ if (Array.isArray(clone.rows)) {
54
+ clone.rows = clone.rows.map(maskSecretFields);
55
+ }
56
+ return clone;
57
+ }
58
+
59
+ export class PluginWorkflowPortainerServer extends Plugin {
60
+ private canReadWorkflowMetadata(ctx: Context, action: (typeof METADATA_READ_ACTIONS)[number]): boolean {
61
+ const userId = ctx.state?.currentUser?.id;
62
+ const roles = ctx.state?.currentRoles;
63
+ if (!userId || !Array.isArray(roles)) {
64
+ return false;
65
+ }
66
+ return roles.some((role) => {
67
+ if (typeof role !== 'string') {
68
+ return false;
69
+ }
70
+ if (role === 'root') {
71
+ return true;
72
+ }
73
+ if (this.app.acl.can({ role, resource: PORTAINER_RESOURCE, action })) {
74
+ return true;
75
+ }
76
+ if (this.app.acl.can({ role, resource: 'workflows', action: 'update' })) {
77
+ return true;
78
+ }
79
+ if (this.app.acl.can({ role, resource: 'flow_nodes', action: 'update' })) {
80
+ return true;
81
+ }
82
+ return false;
83
+ });
84
+ }
85
+
86
+ async beforeLoad() {
87
+ await this.db.import({
88
+ directory: resolve(__dirname, 'collections'),
89
+ });
90
+
91
+ this.db.on(`${PORTAINER_INSTANCES_COLLECTION}.beforeSave`, async (model: Model) => {
92
+ if (model.changed('baseUrl') && model.get('baseUrl')) {
93
+ assertValidPortainerUrl(String(model.get('baseUrl')));
94
+ }
95
+ if (model.changed('defaultTimeoutMs') && model.get('defaultTimeoutMs') !== null) {
96
+ model.set('defaultTimeoutMs', clampTimeoutMs(Number(model.get('defaultTimeoutMs'))));
97
+ }
98
+ const encryptor = (this.app as unknown as { aesEncryptor?: { encrypt(v: string): Promise<string> | string } })
99
+ .aesEncryptor;
100
+
101
+ for (const field of ENCRYPTED_FIELDS) {
102
+ if (!model.changed(field)) {
103
+ continue;
104
+ }
105
+ const rawVal = model.get(field);
106
+ if (rawVal === SECRET_MASK) {
107
+ model.set(field, model.previous(field));
108
+ continue;
109
+ }
110
+ if (typeof rawVal === 'string' && rawVal.trim() && encryptor) {
111
+ const encrypted = await encryptor.encrypt(rawVal.trim());
112
+ model.set(field, encrypted);
113
+ }
114
+ }
115
+ });
116
+
117
+ this.db.on(`${PORTAINER_INSTANCES_COLLECTION}.afterSave`, async (model: Model) => {
118
+ if (model.get('isDefault')) {
119
+ const currentId = Number(model.get('id'));
120
+ const repo = this.db.getRepository(PORTAINER_INSTANCES_COLLECTION);
121
+ const others = await repo.find({
122
+ filter: {
123
+ isDefault: true,
124
+ id: { $ne: currentId },
125
+ },
126
+ });
127
+ for (const item of others) {
128
+ await item.update({ isDefault: false });
129
+ }
130
+ }
131
+ });
132
+ }
133
+
134
+ async load() {
135
+ const workflow = (this.app.pm.get('workflow') || this.app.pm.get('@nocobase/plugin-workflow')) as
136
+ | WorkflowPlugin
137
+ | undefined;
138
+
139
+ if (workflow?.registerInstruction) {
140
+ workflow.registerInstruction(INSTRUCTION_TYPES.redeploy, new PortainerStackRedeployInstruction(workflow));
141
+ workflow.registerInstruction(INSTRUCTION_TYPES.control, new PortainerStackControlInstruction(workflow));
142
+ }
143
+
144
+ this.app.resourceManager.define({
145
+ name: PORTAINER_RESOURCE,
146
+ actions: createPortainerResourceActions(this.app),
147
+ });
148
+
149
+ this.app.resourceManager.use(async (ctx, next) => {
150
+ await next();
151
+ if (ctx.action?.resourceName === PORTAINER_INSTANCES_COLLECTION && ctx.body) {
152
+ ctx.body = maskSecretFields(ctx.body);
153
+ }
154
+ });
155
+
156
+ this.app.acl.registerSnippet({
157
+ name: PORTAINER_ACL_SNIPPET,
158
+ actions: [`${PORTAINER_INSTANCES_COLLECTION}:*`, `${PORTAINER_RESOURCE}:*`],
159
+ });
160
+
161
+ for (const action of METADATA_READ_ACTIONS) {
162
+ this.app.acl.allow(PORTAINER_RESOURCE, action, (ctx: Context) => this.canReadWorkflowMetadata(ctx, action));
163
+ }
164
+ }
165
+ }
166
+
167
+ export default PluginWorkflowPortainerServer;