screwdriver-api 8.0.197 → 8.0.199

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screwdriver-api",
3
- "version": "8.0.197",
3
+ "version": "8.0.199",
4
4
  "description": "API server for the Screwdriver.cd service",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -6,6 +6,37 @@ const schema = require('screwdriver-data-schema');
6
6
  const getSchema = schema.models.pipeline.get;
7
7
  const idSchema = schema.models.pipeline.base.extract('id');
8
8
 
9
+ /**
10
+ * Get a pipeline when the current user is an explicitly registered admin from another SCM context.
11
+ *
12
+ * @param {Object} credentials Request credentials
13
+ * @param {String} pipelineId Target pipeline ID
14
+ * @param {Object} app Server app object
15
+ * @return {Promise<Object|null>} Pipeline when access is allowed, otherwise null
16
+ */
17
+ async function canAccessPipelineForScmMigration(credentials, pipelineId, app) {
18
+ const { username, scmContext, scope } = credentials;
19
+ const { pipelineFactory, userFactory } = app;
20
+
21
+ if (!scope.includes('user')) {
22
+ return null;
23
+ }
24
+
25
+ const [pipeline, user] = await Promise.all([
26
+ pipelineFactory.get(pipelineId),
27
+ userFactory.get({ username, scmContext })
28
+ ]);
29
+
30
+ if (!pipeline || !user) {
31
+ return null;
32
+ }
33
+
34
+ const isDifferentScm = user.scmContext !== pipeline.scmContext;
35
+ const isExplicitAdmin = (pipeline.adminUserIds || []).includes(user.id);
36
+
37
+ return isDifferentScm && isExplicitAdmin ? pipeline : null;
38
+ }
39
+
9
40
  module.exports = () => ({
10
41
  method: 'GET',
11
42
  path: '/pipelines/{id}',
@@ -26,18 +57,28 @@ module.exports = () => ({
26
57
  handler: async (request, h) => {
27
58
  const { canAccessPipeline } = request.server.plugins.pipelines;
28
59
  const { credentials } = request.auth;
60
+ const pipelineId = request.params.id;
61
+ let pipeline;
62
+
63
+ try {
64
+ pipeline = await canAccessPipeline(credentials, pipelineId, 'pull', request.server.app);
65
+ } catch (err) {
66
+ if (!boom.isBoom(err, 403)) {
67
+ throw err;
68
+ }
29
69
 
30
- return canAccessPipeline(credentials, request.params.id, 'pull', request.server.app)
31
- .then(pipeline => {
32
- if (!pipeline) {
33
- throw boom.notFound('Pipeline does not exist');
34
- }
70
+ pipeline = await canAccessPipelineForScmMigration(credentials, pipelineId, request.server.app);
35
71
 
36
- return h.response(pipeline.toJson());
37
- })
38
- .catch(err => {
72
+ if (!pipeline) {
39
73
  throw err;
40
- });
74
+ }
75
+ }
76
+
77
+ if (!pipeline) {
78
+ throw boom.notFound('Pipeline does not exist');
79
+ }
80
+
81
+ return h.response(pipeline.toJson());
41
82
  },
42
83
  response: {
43
84
  schema: getSchema
@@ -8,7 +8,7 @@ const { getUserPermissions, getScmUri } = require('../../helper');
8
8
 
9
9
  module.exports = () => ({
10
10
  method: 'DELETE',
11
- path: '/pipelines/{id}/tokens',
11
+ path: '/pipelines/{pipelineId}/tokens',
12
12
  options: {
13
13
  description: 'Remove all tokens for a specific pipeline',
14
14
  notes: 'Returns null if successful',
@@ -54,7 +54,7 @@ module.exports = () => ({
54
54
  },
55
55
  validate: {
56
56
  params: joi.object({
57
- id: idSchema
57
+ pipelineId: idSchema
58
58
  })
59
59
  }
60
60
  }