Skip to content

Commit 8570151

Browse files
saas 1218-1221 - create, get, delete tokens (#260)
* create, get, delete, replace + formatters * docs corrected + replace -> recreate + delete on ids only now + additional validation * remove "recreate" + make validation throw + update version
1 parent 32424df commit 8570151

File tree

10 files changed

+165
-20
lines changed

10 files changed

+165
-20
lines changed

lib/interface/cli/Command.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class Command {
296296
yargsCommand.description = this.description;
297297
yargsCommand.command = this.command;
298298
yargsCommand.builder = this._createBuilderFunction();
299-
yargsCommand.handler = wrapHandler(this.handler, this.requiresAuthentication);
299+
yargsCommand.handler = wrapHandler(this.handler || (() => yargs.showHelp()), this.requiresAuthentication);
300300
return yargsCommand;
301301

302302
}

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,56 @@
1-
const debug = require('debug')('codefresh:cli:create:token');
1+
const _ = require('lodash');
22
const Command = require('../../Command');
33
const createCmd = require('../root/create.cmd');
4-
const {
5-
generateToken,
6-
} = require('../../../../logic/api/token');
4+
const { token } = require('../../../../logic').api;
5+
const CFError = require('cf-errors');
6+
77

88
const command = new Command({
9-
command: 'tokens',
10-
aliases: ['token'],
9+
command: 'token [name]',
1110
parent: createCmd,
12-
description: 'Create Codefresh token',
13-
usage: 'Create Codefresh token',
11+
description: 'Generate Codefresh token',
12+
usage: 'Create default token or provide --type and --subject. See options for more details.',
1413
webDocs: {
1514
category: 'Tokens',
16-
title: 'Tokens tokens',
15+
title: 'Create tokens',
16+
weight: 10,
1717
},
1818
builder: (yargs) => {
1919
return yargs
2020
.option('subject', {
21-
describe: 'name of the token subject',
22-
required: true,
21+
describe: 'Name of the token subject',
2322
})
2423
.option('type', {
2524
describe: 'Type of the subject',
26-
default: 'runtime-environment',
27-
options: ['runtime-environment'],
25+
choices: ['runtime-environment', 'user'],
26+
default: 'user',
2827
})
29-
.option('name', {
28+
.positional('name', {
3029
describe: 'token name',
3130
required: true,
3231
})
33-
.example('codefresh create token --subject my-k8s-cluster/namespace --name new-token', 'Create token form runtime environment');
32+
.example('codefresh create token some-token', 'Create token with default type')
33+
.example(
34+
'codefresh create token some-token --subject my-k8s-cluster/namespace --type runtime-environment',
35+
'Create token from runtime environment',
36+
);
3437
},
3538
handler: async (argv) => {
36-
const res = await generateToken({
39+
if (_.isEmpty(argv.name)) {
40+
throw new CFError('Token name must be provided');
41+
}
42+
43+
if (argv.type === 'runtime-environment' && !argv.subject) {
44+
throw new CFError('Type "runtime-environment" needs --subject to be provided');
45+
}
46+
47+
const res = await token.generateToken({
3748
subjectType: argv.type,
3849
subjectReference: argv.subject,
3950
name: argv.name,
4051
});
41-
console.log('Token created');
52+
53+
console.log('Token created:');
4254
console.log(res.token);
4355
},
4456
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const _ = require('lodash');
2+
const Command = require('../../Command');
3+
const deleteCmd = require('../root/delete.cmd');
4+
const { token } = require('../../../../logic').api;
5+
const Promise = require('bluebird');
6+
const CFError = require('cf-errors');
7+
8+
9+
const command = new Command({
10+
command: 'tokens [ids..]',
11+
aliases: ['token'],
12+
parent: deleteCmd,
13+
description: 'Revoke Codefresh token',
14+
usage: 'Provide one or many token ids to delete. Ids can be retrieved with "get" command',
15+
webDocs: {
16+
category: 'Tokens',
17+
title: 'Delete tokens',
18+
weight: 40,
19+
},
20+
builder: (yargs) => {
21+
yargs
22+
.positional('ids', {
23+
describe: 'token names or ids',
24+
required: true,
25+
})
26+
.example('codefresh delete token [token_id]', 'Delete one token')
27+
.example('codefresh delete tokens [token_id_1] [token_id_2]', 'Delete many tokens');
28+
},
29+
handler: async (argv) => {
30+
if (_.isEmpty(argv.ids)) {
31+
throw new CFError('Token ids must be provided');
32+
}
33+
34+
await Promise.map(argv.ids, (id) => {
35+
return token.deleteToken(id)
36+
.then(() => console.log(`Deleted token: '${id}'`))
37+
.catch(err => console.log(`Not deleted: '${id}' -- ${err}`));
38+
});
39+
},
40+
});
41+
42+
module.exports = command;
43+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const _ = require('lodash');
2+
const Command = require('../../Command');
3+
const getCmd = require('../root/get.cmd');
4+
const { token } = require('../../../../logic').api;
5+
const { specifyOutputForArray } = require('../../helpers/get');
6+
7+
const command = new Command({
8+
command: 'tokens [names|ids..]',
9+
aliases: ['token'],
10+
parent: getCmd,
11+
description: 'Get Codefresh tokens',
12+
usage: 'Provide names/ids to filter results by them',
13+
webDocs: {
14+
category: 'Tokens',
15+
title: 'Get tokens',
16+
weight: 20,
17+
},
18+
builder: (yargs) => {
19+
return yargs
20+
.positional('names', {
21+
describe: 'Token names or ids',
22+
})
23+
.example('codefresh get tokens', 'Get all tokens')
24+
.example('codefresh get tokens [token_1_name] [token_2_id]', 'Get tokens filtered by names or ids');
25+
},
26+
handler: async (argv) => {
27+
let res = await token.getTokens();
28+
if (!_.isEmpty(argv.names)) {
29+
res = res.filter(t => argv.names.includes(t.info.name) || argv.names.includes(t.info.id));
30+
}
31+
specifyOutputForArray(argv.output, res, argv.pretty);
32+
},
33+
});
34+
35+
module.exports = command;
36+

lib/logic/api/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const board = require('./board');
1515
const section = require('./section');
1616
const cluster = require('./cluster');
1717
const repository = require('./repository');
18+
const token = require('./token');
1819

1920
module.exports = {
2021
user,
@@ -34,4 +35,5 @@ module.exports = {
3435
section,
3536
cluster,
3637
repository,
38+
token,
3739
};

lib/logic/api/token.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const _ = require('lodash');
21
const { sendHttpRequest } = require('./helper');
2+
const Token = require('../entities/Token');
33

44
const generateToken = async (info) => {
55
const options = {
@@ -19,7 +19,26 @@ const generateToken = async (info) => {
1919
};
2020
};
2121

22+
const getTokens = async () => {
23+
const options = {
24+
url: '/api/auth/keys',
25+
method: 'GET',
26+
};
27+
const result = await sendHttpRequest(options);
28+
return result.map(Token.fromResponse);
29+
};
30+
31+
const deleteToken = (id) => {
32+
const options = {
33+
url: `/api/auth/key/${id}`,
34+
method: 'DELETE',
35+
};
36+
return sendHttpRequest(options);
37+
};
38+
2239

2340
module.exports = {
2441
generateToken,
42+
getTokens,
43+
deleteToken,
2544
};

lib/logic/entities/Token.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const Entity = require('./Entity');
2+
const _ = require('lodash');
3+
4+
class Token extends Entity {
5+
constructor(data) {
6+
super();
7+
this.entityType = 'token';
8+
this.info = data;
9+
this.defaultColumns = ['id', 'name', 'token_prefix', 'created'];
10+
this.wideColumns = ['id', 'name', 'token_prefix', 'created', 'subject_type', 'subject'];
11+
}
12+
13+
static fromResponse(response) {
14+
const data = Object.assign({}, response);
15+
data.id = data._id;
16+
data.token_prefix = response.tokenPrefix;
17+
data.created = new Date(data.created).toLocaleDateString();
18+
data.subject_type = _.get(data, 'subject.type');
19+
data.subject = _.get(data, 'subject.ref');
20+
return new Token(data);
21+
}
22+
}
23+
24+
module.exports = Token;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { Formatter, Style } = require('../../output');
2+
3+
4+
const FORMATTER = Formatter.build()
5+
.style('token_prefix', val => `${val}**************`)
6+
.style('name', Style.cyan);
7+
8+
module.exports = FORMATTER;

lib/output/formatters/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const formatters = {
1717
Environment: require('./Environment.formatter'),
1818
GitRepo: require('./GitRepo.formatter'),
1919
CodefreshRepo: require('./CodefreshRepo.formatter'),
20+
Token: require('./Token.formatter'),
2021
};
2122
/* eslint-enable */
2223

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.9.14",
3+
"version": "0.9.15",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)