Skip to content

Commit f234c2b

Browse files
Revert "saas-1569 - auth to sdk (#298)" (#304)
This reverts commit e0a1d7e
1 parent a42d28d commit f234c2b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1064
-275
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:10.13.0-alpine
1+
FROM node:9.2.0-alpine
22

33
RUN apk add --update git curl jq py-pip && pip install yq
44

codefresh-release.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ steps:
55

66
fail_if_not_master:
77
title: "Validate running on master branch"
8-
image: codefresh/build-cli
8+
image: codefresh/cli-build
99
commands:
1010
- 'echo This pipeline should be run only on master'
1111
- 'exit 1'
@@ -15,7 +15,7 @@ steps:
1515

1616
extract_version:
1717
title: "Exporting package.json version"
18-
image: codefresh/build-cli
18+
image: codefresh/cli-build
1919
commands:
2020
- 'export PACKAGE_VERSION=$(jq -r ".version" package.json)'
2121
- "echo Current version: $PACKAGE_VERSION"
@@ -40,7 +40,7 @@ steps:
4040

4141
generate_comletion:
4242
title: "Generating commands completion tree"
43-
image: codefresh/build-cli
43+
image: codefresh/cli-build
4444
commands:
4545
- "yarn install"
4646
- "yarn generate-completion"
@@ -53,21 +53,21 @@ steps:
5353

5454
compile_executables:
5555
title: "Compiling executables"
56-
image: codefresh/build-cli
56+
image: codefresh/cli-build
5757
commands:
5858
- "rm -rf dist"
5959
- "yarn pkg"
6060

6161
create_release:
6262
title: "Create github release"
63-
image: codefresh/build-cli
63+
image: codefresh/cli-build
6464
fail_fast: false
6565
commands:
6666
- 'curl --fail -X POST -d ''{"tag_name":"v${{PACKAGE_VERSION}}","target_commitish":"${{CF_REVISION}}","name":"Codefresh V${{PACKAGE_VERSION}}"}'' -H "Content-Type: application/json" -H "Authorization: token ${{GITHUB_TOKEN}}" https://api.github.com/repos/codefresh-io/cli/releases'
6767

6868
get_release:
6969
title: "Get github release"
70-
image: codefresh/build-cli
70+
image: codefresh/cli-build
7171
commands:
7272
- "curl --fail https://api.github.com/repos/codefresh-io/cli/releases/tags/v${{PACKAGE_VERSION}}"
7373
- "export GITHUB_RELEASE_ID=$(curl --fail https://api.github.com/repos/codefresh-io/cli/releases/tags/v${{PACKAGE_VERSION}} | jq -r '.id')"
@@ -76,7 +76,7 @@ steps:
7676

7777
upload_executables:
7878
title: "Upload executables to github release"
79-
image: codefresh/cli-build # todo: move to new codefresh/build-cli
79+
image: codefresh/cli-build
8080
commands:
8181
# delete all previous .zip/.gz created files
8282
- "rm -rf *.gz"
@@ -119,7 +119,7 @@ steps:
119119

120120
update_brew_formula:
121121
title: "Updating homebrew formula"
122-
image: codefresh/build-cli
122+
image: codefresh/cli-build
123123
commands:
124124
- VERSION=v${{PACKAGE_VERSION}}
125125
- curl -L https://github.com/codefresh-io/cli/releases/download/$VERSION/codefresh-$VERSION-macos-x64.tar.gz > $VERSION.tar.gz

codefresh.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ steps:
55

66
install_dependencies:
77
title: 'Installing testing dependencies'
8-
image: codefresh/node-tester-image:10.13.0
8+
image: codefresh/node-tester-image:8.8.0
99
commands:
1010
- yarn install --frozen-lockfile
1111

1212
eslint:
1313
title: 'Running linting logic'
14-
image: codefresh/node-tester-image:10.13.0
14+
image: codefresh/node-tester-image:8.8.0
1515
commands:
1616
- yarn eslint
1717

1818
unit-tests:
1919
title: 'Running unit tests'
20-
image: codefresh/node-tester-image:10.13.0
20+
image: codefresh/node-tester-image:8.8.0
2121
commands:
2222
- yarn test
2323

lib/interface/cli/Command.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const yargs = require('yargs');
22
const CFError = require('cf-errors');
3+
const assert = require('assert');
34
const _ = require('lodash');
45
const { wrapHandler } = require('./helpers/general');
6+
const authManager = require('../../logic').auth.manager;
57
const Output = require('../../output/Output');
6-
const { sdk } = require('../../logic');
78

89

910
class Command {
@@ -74,17 +75,18 @@ class Command {
7475

7576
if (subCommand.isBetaCommand()) {
7677
// load beta commands only if authentication exists and it is beta enabled
77-
const currentContext = sdk.config && sdk.config.context;
78+
const currentContext = authManager.getCurrentContext();
7879
if (currentContext && currentContext.isBetaFeatEnabled()) {
7980
yargs.command(subCommand.toCommand());
8081
}
8182
} else if (subCommand.isOnPremCommand()) {
8283
// load onPrem commands only if authentication exists and it is onPrem enabled
83-
const currentContext = sdk.config && sdk.config.context;
84+
const currentContext = authManager.getCurrentContext();
8485
if (currentContext && currentContext.isOnPremFeatEnabled()) {
8586
yargs.command(subCommand.toCommand());
8687
}
87-
} else {
88+
}
89+
else {
8890
yargs.command(subCommand.toCommand());
8991
}
9092
});

lib/interface/cli/codefresh

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,62 @@ process.on('unhandledRejection', (err) => {
1313
});
1414

1515
if (process.argv.includes('--get-yargs-completions')) {
16-
const completionInterface = require('./completion');
17-
completionInterface().argv; // eslint-disable-line
16+
const initCompletion = require('./completion');
17+
initCompletion().argv;
1818
} else {
19-
const commandLineInterface = require('./commad-line-interface');
20-
commandLineInterface().catch((err) => {
21-
Output.printError(err);
22-
process.exit(1);
19+
const _ = require('lodash');
20+
const yargs = require('yargs');
21+
const path = require('path');
22+
const recursive = require('recursive-readdir');
23+
const DEFAULTS = require('./defaults');
24+
const authManager = require('../../logic').auth.manager;
25+
26+
recursive(path.resolve(__dirname, 'commands'), (err, files) => {
27+
const rootCommands = [];
28+
yargs
29+
.env('')
30+
.options('cfconfig', {
31+
default: DEFAULTS.CFCONFIG,
32+
global: false,
33+
})
34+
.config('cfconfig', 'Custom path for authentication contexts config file', (configFilePath) => {
35+
try {
36+
authManager.loadContexts(process.env.CF_API_KEY, process.env.CF_URL || DEFAULTS.URL, configFilePath);
37+
_.forEach(files, (file) => {
38+
if (file.endsWith('.cmd.js')) {
39+
const command = require(file);
40+
if (command.isRoot()) {
41+
if (command.isBetaCommand()) {
42+
const currentContext = authManager.getCurrentContext();
43+
if (currentContext && currentContext.isBetaFeatEnabled()) {
44+
rootCommands.push(command);
45+
}
46+
} else {
47+
rootCommands.push(command);
48+
}
49+
}
50+
}
51+
});
52+
_.forEach(rootCommands, (command) => {
53+
yargs.command(command.toCommand());
54+
});
55+
} catch (err) {
56+
Output.printError(err);
57+
process.exit(1);
58+
}
59+
});
60+
61+
62+
yargs // eslint-disable-line
63+
.demandCommand(1, 'You need at least one command before moving on')
64+
.wrap(null)
65+
.version(false)
66+
.help('help')
67+
.epilogue('For more information, find our official documentation at http://cli.codefresh.io')
68+
// .option('help', {
69+
// global: false,
70+
// })
71+
.argv;
2372
});
2473
}
2574

lib/interface/cli/commad-line-interface.js

Lines changed: 0 additions & 77 deletions
This file was deleted.

lib/interface/cli/commands/auth/create-context.cmd.js

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
1+
const debug = require('debug')('codefresh:auth:login');
12
const Command = require('../../Command');
3+
const _ = require('lodash');
4+
const CFError = require('cf-errors');
25
const DEFAULTS = require('../../defaults');
3-
const { Config } = require('codefresh-sdk');
6+
const { auth } = require('../../../../logic');
7+
const { JWTContext, APIKeyContext } = auth.contexts;
8+
const authManager = auth.manager;
49
const authRoot = require('../root/auth.cmd');
10+
const { createContext } = require('../../../../logic/api/auth');
11+
12+
const _loginWithToken = async (url, token) => {
13+
let authContext;
14+
try {
15+
authContext = JWTContext.createFromToken(token, url);
16+
return authContext;
17+
18+
} catch (err) {
19+
try {
20+
authContext = APIKeyContext.createFromToken(token, url);
21+
return authContext;
22+
23+
} catch (err) {
24+
const error = new CFError({
25+
cause: err,
26+
message: 'Failed to login with api key',
27+
});
28+
throw error;
29+
}
30+
}
31+
};
532

633
const command = new Command({
734
command: 'create-context [name]',
@@ -13,7 +40,7 @@ const command = new Command({
1340
title: 'Create Context',
1441
weight: 20,
1542
},
16-
builder: (yargs) => { // eslint-disable-line
43+
builder: (yargs) => {
1744
return yargs
1845
.option('url', {
1946
describe: 'Codefresh system custom url',
@@ -29,25 +56,38 @@ const command = new Command({
2956
})
3057
.example('codefresh auth create-context --api-key KEY', 'Creating a default context using KEY as the api-key')
3158
.example('codefresh auth create-context my-context --api-key KEY', 'Creating a named context');
59+
3260
},
3361
handler: async (argv) => {
34-
const configManager = Config.manager();
35-
await configManager.loadConfig({ configFilePath: argv.cfconfig });
62+
const authContext = await _loginWithToken(argv.url, argv['api-key']);
63+
const userData = await authContext.validate();
64+
const roles = _.get(userData,'roles');
3665

37-
const updatedExistingContext = configManager.getContextByName(argv.name);
66+
if (roles.indexOf('Admin') > -1) {
67+
authContext.onPrem = true;
68+
}
69+
70+
if (argv.name) {
71+
authContext.setName(argv.name);
72+
}
73+
74+
let updatedExistingContext = false;
75+
if (authManager.getContextByName(authContext.getName())) {
76+
updatedExistingContext = true;
77+
}
3878

39-
const context = await configManager.createContext({ apiKey: argv.apiKey, url: argv.url, name: argv.name });
40-
configManager.addContext(context);
41-
configManager.setCurrentContext(context);
42-
await configManager.persistConfig();
79+
await authManager.addContext(authContext);
80+
await authManager.setCurrentContext(authContext);
81+
await authManager.persistContexts(authContext);
82+
await createContext();
4383

4484
if (updatedExistingContext) {
45-
console.log(`Updated context: ${context.name}`);
85+
console.log(`Updated context: ${authContext.name}`);
4686
} else {
47-
console.log(`Created new context: ${context.name}`);
87+
console.log(`Created new context: ${authContext.name}`);
4888
}
4989

50-
console.log(`Switched to context: ${context.name}`);
90+
console.log(`Switched to context: ${authContext.name}`);
5191
},
5292
});
5393

lib/interface/cli/commands/auth/current-context.cmd.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
const debug = require('debug')('codefresh:auth:current-context');
12
const Command = require('../../Command');
3+
const CFError = require('cf-errors');
4+
const { auth } = require('../../../../logic');
5+
const authManager = auth.manager;
26
const authRoot = require('../root/auth.cmd');
3-
const { Config } = require('codefresh-sdk');
47
const { printTableForAuthContexts } = require('../../helpers/auth');
8+
const _ = require('lodash');
59

610
const command = new Command({
711
command: 'current-context',
@@ -17,7 +21,6 @@ const command = new Command({
1721
.example('codefresh auth current-context', 'Show active authentication context');
1822
},
1923
handler: async (argv) => {
20-
await Config.manager().loadConfig({ configFilePath: argv.cfconfig });
2124
await printTableForAuthContexts({ filter: 'current' });
2225
},
2326
});

0 commit comments

Comments
 (0)