-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.js
70 lines (64 loc) · 2.86 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
// Modules
const _ = require('lodash');
const PantheonApiClient = require('./lib/client');
const utils = require('./lib/utils');
/**
* Pantheon plugin for Lando that handles authentication and token management
* between Lando and Pantheon hosting platform.
*
* @param {Object} app - The Lando app object
* @param {Object} lando - The Lando config object
* @return {void}
*/
module.exports = async (app, lando) => {
// Add additional things to cleanse
app.log.alsoSanitize('pantheon-auth');
// Only do this on pantheon recipes
if (_.get(app, 'config.recipe') === 'pantheon') {
// Set the app caches, validate tokens and update token cache
_.forEach(['pull', 'push', 'switch'], command => {
/**
* Post-command handler for Pantheon authentication
* Validates and updates Pantheon machine tokens after certain commands
*
* @param {Object} config - Command configuration
* @param {Object} answers - User provided answers/input
* @return {Promise} Resolves when token validation and caching is complete
*/
app.events.on(`post-${command}`, async (config, answers) => {
// get existing token and email
const {token, email} = lando.cache.get(app.metaCache);
// Only run if answer.auth is set, the tokens are different or the email is blank
// this allows these commands to all be overriden without causing a failure here
if (answers.auth && (answers.auth !== token || !email)) {
const api = new PantheonApiClient(answers.auth, app.log);
try {
await api.auth();
const results = await api.getUser();
const cache = {token: answers.auth, email: results.email, date: _.toInteger(_.now() / 1000)};
// Reset this apps metacache
lando.cache.set(app.metaCache, _.merge({}, app.meta, cache), {persist: true});
// Set lando's store of pantheon machine tokens
lando.cache.set(app.pantheonTokenCache, utils.sortTokens(app.pantheonTokens, [cache]), {persist: true});
// Wipe out the apps tooling cache to reset with the new MT
lando.cache.remove(`${app.name}.tooling.cache`);
// Throw some sort of error
// NOTE: this provides some error handling when we are completely non-interactive
} catch (error) {
throw (_.has(error, 'response.data')) ? new Error(error.response.data) : error;
}
}
});
});
/**
* Pre-init handler to load Pantheon tokens and metadata
* Sets up token caches and loads existing tokens before landofile initialization
*/
app.events.on('pre-init', 1, () => {
app.pantheonTokenCache = 'pantheon.tokens';
app.pantheonTokens = lando.cache.get(app.pantheonTokenCache) || [];
app.terminusTokens = utils.getTerminusTokens(lando.config.home);
});
}
};