@flowfuse/file-server 1.14.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,67 @@
1
+ #### 1.14.0: Release
2
+
3
+
4
+ #### 1.13.0: Release
5
+
6
+ - Bump workflows to v0.1.1 version (#90) @ppawlowski
7
+ - Pin reusable workflows to v0.1.0 (#89) @ppawlowski
8
+ - Allow config to be passed via env for testing (#88) @knolleary
9
+ - Add /metrics endpoint (#87) @hardillb
10
+ - Fix run serve (#84) @hardillb
11
+ - Fix doule export (#83) @hardillb
12
+ - Allow JSON HTTP Request logging from the FileServer (#82) @hardillb
13
+ - Update ff references in package.json (#80) @knolleary
14
+ - Change repo references in workflows after github org rename (#78) @ppawlowski
15
+
16
+ #### 1.12.0: Release
17
+
18
+ - Publish nightly package to npmjs (#77) @ppawlowski
19
+ - Bump the semver dep (#76) @hardillb
20
+ - Update dependencies to clear CVEs (#75) @hardillb
21
+ - Pin reusable workflow to commit SHA (#74) @ppawlowski
22
+ - Disable scheduled package build (#73) @ppawlowski
23
+
24
+ #### 1.11.0: Release
25
+
26
+ - Bump nodemon to allow other deps to be updated (#71) @hardillb
27
+ - First pass at variable quotas (#70) @hardillb
28
+ - Move to SQLite from Memory context (#68) @hardillb
29
+ - Add file-server container build pipeline dispatcher (#69) @ppawlowski
30
+ - FIX: Publish package on schedule (#65) @ppawlowski
31
+ - FIX: Allow publish only when changes are pushed to `main` branch (#64) @ppawlowski
32
+ - Allow publish only when PR is merged (#63) @ppawlowski
33
+ - Update dependencies (#62) @knolleary
34
+ - Remove defunct docs tasks (#61) @knolleary
35
+ - Introduce build and publish workflow (#59) @ppawlowski
36
+
37
+ #### 1.10.0: Release
38
+
39
+ - Chore: Set root flag in eslint (#57) @Pezmc
40
+
41
+ #### 1.9.0: Release
42
+
43
+ - Add package-lock.json (#55) @Pezmc
44
+
45
+ #### 1.8.0: Release
46
+
47
+
48
+ #### 1.7.0: Release
49
+
50
+
51
+ #### 1.6.0: Release
52
+
53
+
54
+ #### 1.5.0: Release
55
+
56
+
57
+ #### 1.4.0: Release
58
+
59
+ - Add APIs to support synchronous context (#48) @Steve-Mcl
60
+
61
+ #### 1.3.0: Release
62
+
63
+
64
+ #### 1.2.0: Release
65
+
66
+ - S3 quota check (#42) @hardillb
67
+
package/LICENSE ADDED
@@ -0,0 +1,178 @@
1
+ Copyright FlowForge Inc, and other contributors, https://flowforge.com/
2
+
3
+ Apache License
4
+ Version 2.0, January 2004
5
+ http://www.apache.org/licenses/
6
+
7
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
8
+
9
+ 1. Definitions.
10
+
11
+ "License" shall mean the terms and conditions for use, reproduction,
12
+ and distribution as defined by Sections 1 through 9 of this document.
13
+
14
+ "Licensor" shall mean the copyright owner or entity authorized by
15
+ the copyright owner that is granting the License.
16
+
17
+ "Legal Entity" shall mean the union of the acting entity and all
18
+ other entities that control, are controlled by, or are under common
19
+ control with that entity. For the purposes of this definition,
20
+ "control" means (i) the power, direct or indirect, to cause the
21
+ direction or management of such entity, whether by contract or
22
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
23
+ outstanding shares, or (iii) beneficial ownership of such entity.
24
+
25
+ "You" (or "Your") shall mean an individual or Legal Entity
26
+ exercising permissions granted by this License.
27
+
28
+ "Source" form shall mean the preferred form for making modifications,
29
+ including but not limited to software source code, documentation
30
+ source, and configuration files.
31
+
32
+ "Object" form shall mean any form resulting from mechanical
33
+ transformation or translation of a Source form, including but
34
+ not limited to compiled object code, generated documentation,
35
+ and conversions to other media types.
36
+
37
+ "Work" shall mean the work of authorship, whether in Source or
38
+ Object form, made available under the License, as indicated by a
39
+ copyright notice that is included in or attached to the work
40
+ (an example is provided in the Appendix below).
41
+
42
+ "Derivative Works" shall mean any work, whether in Source or Object
43
+ form, that is based on (or derived from) the Work and for which the
44
+ editorial revisions, annotations, elaborations, or other modifications
45
+ represent, as a whole, an original work of authorship. For the purposes
46
+ of this License, Derivative Works shall not include works that remain
47
+ separable from, or merely link (or bind by name) to the interfaces of,
48
+ the Work and Derivative Works thereof.
49
+
50
+ "Contribution" shall mean any work of authorship, including
51
+ the original version of the Work and any modifications or additions
52
+ to that Work or Derivative Works thereof, that is intentionally
53
+ submitted to Licensor for inclusion in the Work by the copyright owner
54
+ or by an individual or Legal Entity authorized to submit on behalf of
55
+ the copyright owner. For the purposes of this definition, "submitted"
56
+ means any form of electronic, verbal, or written communication sent
57
+ to the Licensor or its representatives, including but not limited to
58
+ communication on electronic mailing lists, source code control systems,
59
+ and issue tracking systems that are managed by, or on behalf of, the
60
+ Licensor for the purpose of discussing and improving the Work, but
61
+ excluding communication that is conspicuously marked or otherwise
62
+ designated in writing by the copyright owner as "Not a Contribution."
63
+
64
+ "Contributor" shall mean Licensor and any individual or Legal Entity
65
+ on behalf of whom a Contribution has been received by Licensor and
66
+ subsequently incorporated within the Work.
67
+
68
+ 2. Grant of Copyright License. Subject to the terms and conditions of
69
+ this License, each Contributor hereby grants to You a perpetual,
70
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
71
+ copyright license to reproduce, prepare Derivative Works of,
72
+ publicly display, publicly perform, sublicense, and distribute the
73
+ Work and such Derivative Works in Source or Object form.
74
+
75
+ 3. Grant of Patent License. Subject to the terms and conditions of
76
+ this License, each Contributor hereby grants to You a perpetual,
77
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
78
+ (except as stated in this section) patent license to make, have made,
79
+ use, offer to sell, sell, import, and otherwise transfer the Work,
80
+ where such license applies only to those patent claims licensable
81
+ by such Contributor that are necessarily infringed by their
82
+ Contribution(s) alone or by combination of their Contribution(s)
83
+ with the Work to which such Contribution(s) was submitted. If You
84
+ institute patent litigation against any entity (including a
85
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
86
+ or a Contribution incorporated within the Work constitutes direct
87
+ or contributory patent infringement, then any patent licenses
88
+ granted to You under this License for that Work shall terminate
89
+ as of the date such litigation is filed.
90
+
91
+ 4. Redistribution. You may reproduce and distribute copies of the
92
+ Work or Derivative Works thereof in any medium, with or without
93
+ modifications, and in Source or Object form, provided that You
94
+ meet the following conditions:
95
+
96
+ (a) You must give any other recipients of the Work or
97
+ Derivative Works a copy of this License; and
98
+
99
+ (b) You must cause any modified files to carry prominent notices
100
+ stating that You changed the files; and
101
+
102
+ (c) You must retain, in the Source form of any Derivative Works
103
+ that You distribute, all copyright, patent, trademark, and
104
+ attribution notices from the Source form of the Work,
105
+ excluding those notices that do not pertain to any part of
106
+ the Derivative Works; and
107
+
108
+ (d) If the Work includes a "NOTICE" text file as part of its
109
+ distribution, then any Derivative Works that You distribute must
110
+ include a readable copy of the attribution notices contained
111
+ within such NOTICE file, excluding those notices that do not
112
+ pertain to any part of the Derivative Works, in at least one
113
+ of the following places: within a NOTICE text file distributed
114
+ as part of the Derivative Works; within the Source form or
115
+ documentation, if provided along with the Derivative Works; or,
116
+ within a display generated by the Derivative Works, if and
117
+ wherever such third-party notices normally appear. The contents
118
+ of the NOTICE file are for informational purposes only and
119
+ do not modify the License. You may add Your own attribution
120
+ notices within Derivative Works that You distribute, alongside
121
+ or as an addendum to the NOTICE text from the Work, provided
122
+ that such additional attribution notices cannot be construed
123
+ as modifying the License.
124
+
125
+ You may add Your own copyright statement to Your modifications and
126
+ may provide additional or different license terms and conditions
127
+ for use, reproduction, or distribution of Your modifications, or
128
+ for any such Derivative Works as a whole, provided Your use,
129
+ reproduction, and distribution of the Work otherwise complies with
130
+ the conditions stated in this License.
131
+
132
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
133
+ any Contribution intentionally submitted for inclusion in the Work
134
+ by You to the Licensor shall be under the terms and conditions of
135
+ this License, without any additional terms or conditions.
136
+ Notwithstanding the above, nothing herein shall supersede or modify
137
+ the terms of any separate license agreement you may have executed
138
+ with Licensor regarding such Contributions.
139
+
140
+ 6. Trademarks. This License does not grant permission to use the trade
141
+ names, trademarks, service marks, or product names of the Licensor,
142
+ except as required for reasonable and customary use in describing the
143
+ origin of the Work and reproducing the content of the NOTICE file.
144
+
145
+ 7. Disclaimer of Warranty. Unless required by applicable law or
146
+ agreed to in writing, Licensor provides the Work (and each
147
+ Contributor provides its Contributions) on an "AS IS" BASIS,
148
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
149
+ implied, including, without limitation, any warranties or conditions
150
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
151
+ PARTICULAR PURPOSE. You are solely responsible for determining the
152
+ appropriateness of using or redistributing the Work and assume any
153
+ risks associated with Your exercise of permissions under this License.
154
+
155
+ 8. Limitation of Liability. In no event and under no legal theory,
156
+ whether in tort (including negligence), contract, or otherwise,
157
+ unless required by applicable law (such as deliberate and grossly
158
+ negligent acts) or agreed to in writing, shall any Contributor be
159
+ liable to You for damages, including any direct, indirect, special,
160
+ incidental, or consequential damages of any character arising as a
161
+ result of this License or out of the use or inability to use the
162
+ Work (including but not limited to damages for loss of goodwill,
163
+ work stoppage, computer failure or malfunction, or any and all
164
+ other commercial damages or losses), even if such Contributor
165
+ has been advised of the possibility of such damages.
166
+
167
+ 9. Accepting Warranty or Additional Liability. While redistributing
168
+ the Work or Derivative Works thereof, You may choose to offer,
169
+ and charge a fee for, acceptance of support, warranty, indemnity,
170
+ or other liability obligations and/or rights consistent with this
171
+ License. However, in accepting such obligations, You may act only
172
+ on Your own behalf and on Your sole responsibility, not on behalf
173
+ of any other Contributor, and only if You agree to indemnify,
174
+ defend, and hold each Contributor harmless for any liability
175
+ incurred by, or claims asserted against, such Contributor by reason
176
+ of your accepting any such warranty or additional liability.
177
+
178
+ END OF TERMS AND CONDITIONS
package/README.md ADDED
@@ -0,0 +1,231 @@
1
+ # A Basic Object Store for use with FlowFuse
2
+
3
+ ## Authorisation
4
+
5
+ All requests should include a `Authorization` header with a Bearer token assigned by the FlowFuse platform to identify
6
+ ## End Points
7
+
8
+ ### File Storage
9
+
10
+ - Create/Replace
11
+
12
+ **POST** */v1/files/:teamId/:projectId/[path and filename]*
13
+
14
+ Content-Type: application/octet-stream
15
+
16
+ - Append
17
+
18
+ **POST** */v1/files/:teamId/:projectId/[path and filename]*
19
+
20
+ With Header `FF_MODE: append`
21
+
22
+ Content-Type: application/octet-stream
23
+ - Read File
24
+
25
+ **GET** */v1/files/:teamId/:projectId/[path and filename]*
26
+
27
+ Content-Type: application/octet-stream
28
+
29
+ - Delete File
30
+
31
+ **DELETE** */v1/files/:teamId/:projectId/[path and filename]*
32
+
33
+ - Check team quota usage
34
+
35
+ **GET** */v1/quota/:teamId*
36
+
37
+ Content-Type: application/json
38
+
39
+ ### Context Store
40
+
41
+ - Set stored values
42
+
43
+ **POST** */v1/context/:projectId/:scope*
44
+
45
+ Content-Type: application/json
46
+
47
+ Body:
48
+ ```json
49
+ [
50
+ { "key": "x", "value": { "foo": "bar" } },
51
+ { "key": "y.y", "value": 100 },
52
+ ]
53
+ ```
54
+
55
+ - Get stored values
56
+
57
+ **GET** */v1/context/:projectId/:scope?key=x[&key=y.y]*
58
+
59
+ Content-Type: application/json
60
+
61
+ Response:
62
+ ```json
63
+ [
64
+ { "key": "x", "value": { "foo": "bar" } },
65
+ { "key": "y.y", "value": 100 }
66
+ ]
67
+ ```
68
+
69
+ - Get keys for a scope
70
+
71
+ **GET** */v1/context/:projectId/:scope/keys*
72
+
73
+ Content-Type: application/json
74
+
75
+ Response:
76
+ ```json
77
+ [
78
+ "x",
79
+ "y"
80
+ ]
81
+ ```
82
+
83
+ - Delete scope
84
+
85
+ **DELETE** */v1/context/:projectId/:scope*
86
+
87
+ - Clean unused scopes from the store
88
+
89
+ **POST** */v1/context/:projectId/clean*
90
+
91
+ Content-Type: application/json
92
+
93
+ Body:
94
+ ```json
95
+ [
96
+ "nodeId", "flowId"
97
+ ]
98
+ ```
99
+
100
+ ## Configuration
101
+
102
+ Configuration is read from `etc/flowforge-storage.yml`
103
+
104
+ ```yaml
105
+ host: 0.0.0.0
106
+ port: 3001
107
+ base_url: http://flowforge:3000
108
+ driver:
109
+ type: localfs
110
+ options:
111
+ root: var/root
112
+ telemetry:
113
+ backend:
114
+ prometheus:
115
+ enabled: true
116
+ ```
117
+
118
+ - base_url - Where to reach the core FlowForge platform
119
+ - driver
120
+ - type - can be `s3`, `localfs` or `memory` (for testing)
121
+ - options - will vary by driver
122
+ - telemetry
123
+ - backend
124
+ - prometheus
125
+ - enabled - turns on the `/metrics` endpoint to track resource usage
126
+
127
+ ### File Storage
128
+ #### S3
129
+
130
+ The following can be any of the options for the S3Client Contructor, see [here](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/s3clientconfig.html)
131
+
132
+ - options
133
+ - bucket - name of S3 Bucket (required)
134
+ - region - AWS Region
135
+ - endpoint - S3 ObjectStore Endpoint (if not using AWS S3)
136
+ - forcePathStyle: true/false
137
+ - credential
138
+ - accessKeyId - AccountID/Username
139
+ - secretAccessKey - SecretKey/Password
140
+
141
+ ```yaml
142
+ host: '0.0.0.0'
143
+ port: 3001
144
+ base_url: http://forge.default
145
+ driver:
146
+ type: s3
147
+ options:
148
+ bucket: flowforge-files
149
+ credentials:
150
+ accessKeyId: XXXXXXXXXXXXXXXXXXX
151
+ secretAccessKey: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
152
+ forcePathStyle: true
153
+ region: us-east-1
154
+ ```
155
+
156
+ #### LocalFS
157
+
158
+ - options
159
+ - root - path to store team files, relative path will apply to FLOWFORGE_HOME
160
+
161
+ #### Memory
162
+
163
+ This driver is purely to make testing easier, it has no configuration
164
+ options.
165
+
166
+ ### Context Storage
167
+
168
+ #### Sequelize
169
+
170
+ This driver can use either PostgreSQL or SQLite to hold context values.
171
+
172
+ To use with PostgreSQL configure as follows:
173
+
174
+ ```yaml
175
+ context:
176
+ type: sequelize
177
+ options:
178
+ type: postgres
179
+ host: 127.0.0.1
180
+ port: 5432
181
+ database: ff-context
182
+ username: user
183
+ password: password
184
+ ```
185
+
186
+ To use with SQLite configure as follows:
187
+
188
+ ```yaml
189
+ context:
190
+ type: sequelize
191
+ options:
192
+ type: sqlite
193
+ storage: ff-context.db
194
+ ```
195
+
196
+ Where `context.options.storage` is the filename of the SQLite database, by default it will be written to
197
+ the `var` directory if a fully qualified path is not provided.
198
+
199
+ ### Environment variables
200
+
201
+ - FLOWFORGE_HOME default `/opt/flowforge-file-storage`
202
+ - PORT overrides value in config file, default 3001
203
+
204
+ ## Development
205
+
206
+ ### Testing
207
+
208
+ ```bash
209
+ npm run test
210
+ ```
211
+
212
+ NOTE: This will run all tests for all backends and requires a running postgres database
213
+
214
+ To prepare postgres for the tests, use the following procedure (tested on Linux and WSL2 ubuntu + docker)...
215
+
216
+ ```bash
217
+ docker run --rm --name postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=postgres -p 5432:5432 postgres:14
218
+ ```
219
+
220
+
221
+ ### Testing (without postgres)
222
+
223
+ ```
224
+ npm run test:nopg
225
+ ```
226
+
227
+ Alternatively, you can set env variable TEST_POSTGRES=false
228
+ ```
229
+ export TEST_POSTGRES=false
230
+ npm run test
231
+ ```
@@ -0,0 +1,22 @@
1
+ host: '0.0.0.0'
2
+ port: 3001
3
+ base_url: http://localhost:3000
4
+ driver:
5
+ # localfs, s3, memory
6
+ type: localfs
7
+ options:
8
+ root: var/root
9
+ # type: s3
10
+ # options:
11
+ # bucket: test
12
+ # credentials:
13
+ # accessKeyId: test
14
+ # secretAccessKey: password
15
+ # endpoint: http://localhost:9000
16
+ # forcePathStyle: true
17
+ # region: eu-west-1
18
+ context:
19
+ type: sequelize
20
+ options:
21
+ type: sqlite
22
+ storage: ff-context.db
@@ -0,0 +1,26 @@
1
+ host: '0.0.0.0'
2
+ port: 3001
3
+ base_url: http://localhost:3000
4
+ driver:
5
+ # localfs, s3, memory
6
+ type: localfs
7
+ options:
8
+ root: var/root
9
+ # type: s3
10
+ # options:
11
+ # bucket: test
12
+ # credentials:
13
+ # accessKeyId: test
14
+ # secretAccessKey: password
15
+ # endpoint: http://localhost:9000
16
+ # forcePathStyle: true
17
+ # region: eu-west-1
18
+ context:
19
+ type: sequelize
20
+ options:
21
+ type: sqlite
22
+ storage: ff-context.db
23
+ # telemetry:
24
+ # backend:
25
+ # prometheus:
26
+ # enabled: true
Binary file
package/forge/auth.js ADDED
@@ -0,0 +1,106 @@
1
+ const fp = require('fastify-plugin')
2
+ const got = require('got')
3
+
4
+ const authCache = {}
5
+ const ttl = 90 * 1000
6
+
7
+ module.exports = fp(async function (app, opts, done) {
8
+ const client = got.extend({
9
+ prefixUrl: `${app.config.base_url}/account/check/project`,
10
+ headers: {
11
+ 'user-agent': 'FlowForge Storage Server',
12
+ 'ff-quota': true
13
+ },
14
+ timeout: {
15
+ request: 500
16
+ }
17
+ })
18
+
19
+ async function checkToken (projectId, token) {
20
+ const project = await client.get(projectId, {
21
+ headers: {
22
+ authorization: `Bearer ${token}`
23
+ }
24
+ })
25
+ return project.body
26
+ }
27
+
28
+ async function checkAuth (request, reply) {
29
+ if (request.url === '/metrics') {
30
+ return
31
+ }
32
+ try {
33
+ const token = getAuthToken(request)
34
+ const cacheOk = checkCache(token, request.params.projectId)
35
+ if (!cacheOk) {
36
+ const tokenResponse = await checkToken(request.params.projectId, token)
37
+ if (!tokenResponse) {
38
+ throw new Error('Invalid token')
39
+ }
40
+ request.quota = tokenResponse.quota
41
+ // update cache
42
+ authCache[token] = {
43
+ ttl: Date.now(),
44
+ projectId: request.params.projectId,
45
+ quota: tokenResponse.quota
46
+ }
47
+ } else {
48
+ request.quota = authCache[token].quota
49
+ }
50
+ } catch (error) {
51
+ // always send 401 for security reasons
52
+ reply.code(401).send({ code: 'unauthorized', error: 'unauthorized' })
53
+ }
54
+ }
55
+
56
+ /**
57
+ * Extract the token from the request.
58
+ * @throws {Error} if the token is not found or the authorization header is invalid
59
+ * @param {Object} request The request object
60
+ * @returns {string} The token
61
+ */
62
+ function getAuthToken (request) {
63
+ if (!request?.headers?.authorization) {
64
+ throw new Error('Missing authorization header')
65
+ }
66
+ const parts = request.headers.authorization.split(' ')
67
+ if (parts.length !== 2) {
68
+ throw new Error('Invalid authorization header')
69
+ }
70
+ if (parts[0] !== 'Bearer') {
71
+ throw new Error('Invalid authorization header')
72
+ }
73
+ if (!parts[1]) {
74
+ throw new Error('Invalid authorization header')
75
+ }
76
+ return parts[1]
77
+ }
78
+
79
+ /**
80
+ * Check if the token is in the cache and if it is still valid
81
+ * @param {string} token The token to check
82
+ * @param {string} projectId The projectId to check
83
+ * @returns {boolean} true if the token is valid for the projectId
84
+ */
85
+ function checkCache (token, projectId) {
86
+ const cacheExists = authCache[token] && authCache[token].projectId && authCache[token].ttl
87
+ let projectMatch = false
88
+ let ttlOk = false
89
+ if (cacheExists) {
90
+ const cacheEntry = authCache[token]
91
+ // ensure projectId matches the cached projectId
92
+ projectMatch = cacheEntry.projectId === projectId
93
+ // check cache ttl
94
+ ttlOk = (Date.now() - cacheEntry.ttl) < ttl
95
+ }
96
+ const result = cacheExists && projectMatch && ttlOk
97
+ // delete only if expired
98
+ if (!ttlOk && projectMatch) {
99
+ delete authCache[token]
100
+ }
101
+ return result
102
+ }
103
+
104
+ app.decorate('checkAuth', checkAuth)
105
+ done()
106
+ })