Skip to content

Commit 67f3bab

Browse files
Saas 3234 openapi scope (#351)
* add scopes to token creation * token patch command * update description * bump version * move scopes display to formatter * allow empty scopes * any scope message * update docs
1 parent afe2f4f commit 67f3bab

File tree

6 files changed

+95
-3
lines changed

6 files changed

+95
-3
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const _ = require('lodash');
2+
const Command = require('../../Command');
3+
const applyCmd = require('../root/apply.cmd');
4+
const CFError = require('cf-errors');
5+
const { sdk } = require('../../../../logic');
6+
7+
8+
const command = new Command({
9+
command: 'tokens <id>',
10+
aliases: ['token'],
11+
parent: applyCmd,
12+
description: 'Patch Codefresh token',
13+
usage: 'Update token name or scopes using token id. Id can be retrieved using "get" command',
14+
webDocs: {
15+
category: 'Tokens',
16+
title: 'Patch token',
17+
weight: 40,
18+
},
19+
builder: (yargs) => {
20+
yargs
21+
.positional('id', {
22+
describe: 'token id',
23+
required: true,
24+
})
25+
.option('name', {
26+
describe: 'token name or ids',
27+
})
28+
.option('scope', {
29+
alias: 's',
30+
describe: 'token names or ids',
31+
array: true,
32+
})
33+
.example('codefresh patch token [token_id] --name new-name', 'Update token name')
34+
.example(
35+
'codefresh patch token [token_id] -s pipeline -s project',
36+
'Set token scopes to ["pipeline", "project"] (will replace old scopes with new)',
37+
);
38+
},
39+
handler: async (argv) => {
40+
if (argv.scope && _.isEmpty(argv.scope)) {
41+
throw new CFError('Token must have at least one scope');
42+
}
43+
const { id, name, scope } = argv;
44+
await sdk.tokens.patch({ id }, { name, scopes: scope });
45+
console.log(`Token ${id} updated`);
46+
},
47+
});
48+
49+
module.exports = command;
50+

lib/interface/cli/commands/tokens/create.cmd.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ const command = new Command({
2525
choices: ['runtime-environment', 'user'],
2626
default: 'user',
2727
})
28+
.option('scope', {
29+
alias: 's',
30+
describe: 'user scopes',
31+
array: true,
32+
default: [],
33+
})
2834
.positional('name', {
2935
describe: 'token name',
3036
required: true,
@@ -49,6 +55,7 @@ const command = new Command({
4955
subjectReference: argv.subject,
5056
}, {
5157
name: argv.name,
58+
scopes: argv.scope,
5259
});
5360

5461
console.log('Token created:');

lib/logic/entities/Token.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ class Token extends Entity {
66
super();
77
this.entityType = 'token';
88
this.info = data;
9-
this.defaultColumns = ['id', 'name', 'token_prefix', 'created'];
10-
this.wideColumns = ['id', 'name', 'token_prefix', 'created', 'subject_type', 'subject'];
9+
this.defaultColumns = ['id', 'name', 'token_prefix', 'created', 'scopes'];
10+
this.wideColumns = ['id', 'name', 'token_prefix', 'created', 'subject_type', 'subject', 'scopes'];
1111
}
1212

1313
static fromResponse(response) {
1414
const data = Object.assign({}, response);
1515
data.id = data._id;
1616
data.token_prefix = response.tokenPrefix;
17+
data.scopes = _.isArray(response.scopes) ? response.scopes : ['any'];
1718
data.created = data.created ? new Date(data.created) : undefined;
1819
data.subject_type = _.get(data, 'subject.type');
1920
data.subject = _.get(data, 'subject.ref');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
const _ = require('lodash');
12
const { Formatter, Style } = require('../Formatter');
23

34

45
const FORMATTER = Formatter.build()
56
.style('token_prefix', val => `${val}**************`)
67
.style('created', Style.date)
8+
.style('scopes', data => (_.isArray(data) && data.join('\n')) || data)
79
.style('name', Style.cyan);
810

911
module.exports = FORMATTER;

openapi.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,6 +1854,38 @@
18541854
],
18551855
"summary": "Delete",
18561856
"x-sdk-interface": "tokens.delete"
1857+
},
1858+
"patch": {
1859+
"responses": {
1860+
"200": {
1861+
"description": "ok"
1862+
}
1863+
},
1864+
"tags": [
1865+
"tokens"
1866+
],
1867+
"operationId": "tokens-patch",
1868+
"parameters": [
1869+
{
1870+
"in": "path",
1871+
"name": "id",
1872+
"schema": {
1873+
"type": "string"
1874+
},
1875+
"required": true
1876+
}
1877+
],
1878+
"summary": "Patch",
1879+
"x-sdk-interface": "tokens.patch",
1880+
"requestBody": {
1881+
"content": {
1882+
"application/json": {
1883+
"schema": {
1884+
"type": "object"
1885+
}
1886+
}
1887+
}
1888+
}
18571889
}
18581890
},
18591891
"/auth/keys": {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.28.1",
3+
"version": "0.29.0",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)