2020-09-25 09:00:18 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2016-2020 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-09-06 14:39:31 +00:00
|
|
|
import { Flags, Args } from '@oclif/core';
|
2023-11-07 22:32:25 +00:00
|
|
|
import Command from '../../command';
|
|
|
|
import { ExpectedError } from '../../errors';
|
|
|
|
import * as cf from '../../utils/common-flags';
|
|
|
|
import { getBalenaSdk, getCliUx, stripIndent } from '../../utils/lazy';
|
|
|
|
import { applicationIdInfo } from '../../utils/messages';
|
2020-09-25 09:00:18 +00:00
|
|
|
|
|
|
|
export default class SupportCmd extends Command {
|
|
|
|
public static description = stripIndent`
|
2021-07-15 13:41:38 +00:00
|
|
|
Grant or revoke support access for devices or fleets.
|
2020-09-25 09:00:18 +00:00
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
Grant or revoke balena support agent access to devices or fleets
|
2020-09-25 09:00:18 +00:00
|
|
|
on balenaCloud. (This command does not apply to openBalena.)
|
|
|
|
Access will be automatically revoked once the specified duration has elapsed.
|
|
|
|
|
|
|
|
Duration defaults to 24h, but can be specified using --duration flag in days
|
|
|
|
or hours, e.g. '12h', '2d'.
|
|
|
|
|
2021-07-15 13:41:38 +00:00
|
|
|
Both --device and --fleet flags accept multiple values, specified as
|
2020-09-25 09:00:18 +00:00
|
|
|
a comma-separated list (with no spaces).
|
2020-12-10 12:30:17 +00:00
|
|
|
|
|
|
|
${applicationIdInfo.split('\n').join('\n\t\t')}
|
2020-09-25 09:00:18 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
public static examples = [
|
|
|
|
'balena support enable --device ab346f,cd457a --duration 3d',
|
2021-07-15 13:41:38 +00:00
|
|
|
'balena support enable --fleet myFleet --duration 12h',
|
|
|
|
'balena support disable -f myorg/myfleet',
|
2020-09-25 09:00:18 +00:00
|
|
|
];
|
|
|
|
|
2023-09-06 14:39:31 +00:00
|
|
|
public static args = {
|
|
|
|
action: Args.string({
|
2020-09-25 09:00:18 +00:00
|
|
|
description: 'enable|disable support access',
|
|
|
|
options: ['enable', 'disable'],
|
2023-09-06 14:39:31 +00:00
|
|
|
}),
|
|
|
|
};
|
2020-09-25 09:00:18 +00:00
|
|
|
|
|
|
|
public static usage = 'support <action>';
|
|
|
|
|
2023-09-06 14:39:31 +00:00
|
|
|
public static flags = {
|
|
|
|
device: Flags.string({
|
2020-09-25 09:00:18 +00:00
|
|
|
description: 'comma-separated list (no spaces) of device UUIDs',
|
|
|
|
char: 'd',
|
|
|
|
}),
|
2021-07-15 13:41:38 +00:00
|
|
|
fleet: {
|
|
|
|
...cf.fleet,
|
2020-12-10 12:30:17 +00:00
|
|
|
description:
|
2021-12-21 00:02:45 +00:00
|
|
|
'comma-separated list (no spaces) of fleet names or slugs (preferred)',
|
2020-12-10 12:30:17 +00:00
|
|
|
},
|
2023-09-06 14:39:31 +00:00
|
|
|
duration: Flags.string({
|
2020-09-25 09:00:18 +00:00
|
|
|
description:
|
|
|
|
'length of time to enable support for, in (h)ours or (d)ays, e.g. 12h, 2d',
|
|
|
|
char: 't',
|
|
|
|
}),
|
|
|
|
help: cf.help,
|
|
|
|
};
|
|
|
|
|
|
|
|
public static authenticated = true;
|
|
|
|
|
|
|
|
public async run() {
|
2023-09-06 14:39:31 +00:00
|
|
|
const { args: params, flags: options } = await this.parse(SupportCmd);
|
2020-09-25 09:00:18 +00:00
|
|
|
|
|
|
|
const balena = getBalenaSdk();
|
|
|
|
const ux = getCliUx();
|
|
|
|
|
|
|
|
const enabling = params.action === 'enable';
|
|
|
|
|
|
|
|
// Validation
|
2021-12-19 20:50:03 +00:00
|
|
|
if (!options.device && !options.fleet) {
|
2021-07-15 13:41:38 +00:00
|
|
|
throw new ExpectedError('At least one device or fleet must be specified');
|
2020-09-25 09:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (options.duration != null && !enabling) {
|
|
|
|
throw new ExpectedError(
|
|
|
|
'--duration option is only applicable when enabling support',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate expiry ts
|
|
|
|
const durationDefault = '24h';
|
|
|
|
const duration = options.duration || durationDefault;
|
|
|
|
const expiryTs = Date.now() + this.parseDuration(duration);
|
|
|
|
|
|
|
|
const deviceUuids = options.device?.split(',') || [];
|
2021-12-19 20:50:03 +00:00
|
|
|
const appNames = options.fleet?.split(',') || [];
|
2020-09-25 09:00:18 +00:00
|
|
|
|
|
|
|
const enablingMessage = 'Enabling support access for';
|
|
|
|
const disablingMessage = 'Disabling support access for';
|
|
|
|
|
|
|
|
// Process devices
|
|
|
|
for (const deviceUuid of deviceUuids) {
|
|
|
|
if (enabling) {
|
|
|
|
ux.action.start(`${enablingMessage} device ${deviceUuid}`);
|
|
|
|
await balena.models.device.grantSupportAccess(deviceUuid, expiryTs);
|
|
|
|
} else if (params.action === 'disable') {
|
|
|
|
ux.action.start(`${disablingMessage} device ${deviceUuid}`);
|
|
|
|
await balena.models.device.revokeSupportAccess(deviceUuid);
|
|
|
|
}
|
|
|
|
ux.action.stop();
|
|
|
|
}
|
|
|
|
|
2023-11-07 22:32:25 +00:00
|
|
|
const { getFleetSlug } = await import('../../utils/sdk');
|
2021-12-21 00:02:45 +00:00
|
|
|
|
2020-09-25 09:00:18 +00:00
|
|
|
// Process applications
|
|
|
|
for (const appName of appNames) {
|
2021-12-21 00:02:45 +00:00
|
|
|
const slug = await getFleetSlug(balena, appName);
|
2020-09-25 09:00:18 +00:00
|
|
|
if (enabling) {
|
2021-12-21 00:02:45 +00:00
|
|
|
ux.action.start(`${enablingMessage} fleet ${slug}`);
|
|
|
|
await balena.models.application.grantSupportAccess(slug, expiryTs);
|
2020-09-25 09:00:18 +00:00
|
|
|
} else if (params.action === 'disable') {
|
2021-12-21 00:02:45 +00:00
|
|
|
ux.action.start(`${disablingMessage} fleet ${slug}`);
|
|
|
|
await balena.models.application.revokeSupportAccess(slug);
|
2020-09-25 09:00:18 +00:00
|
|
|
}
|
|
|
|
ux.action.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enabling) {
|
|
|
|
console.log(
|
|
|
|
`Access has been granted for ${duration}, expiring ${new Date(
|
|
|
|
expiryTs,
|
2023-05-12 15:59:47 +00:00
|
|
|
).toISOString()}`,
|
2020-09-25 09:00:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parseDuration(duration: string): number {
|
|
|
|
const parseErrorMsg =
|
|
|
|
'Duration must be specified as number followed by h or d, e.g. 24h, 1d';
|
|
|
|
const unit = duration.slice(duration.length - 1);
|
|
|
|
const amount = Number(duration.substring(0, duration.length - 1));
|
|
|
|
|
|
|
|
if (isNaN(amount)) {
|
|
|
|
throw new ExpectedError(parseErrorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
let durationMs;
|
|
|
|
if (['h', 'H'].includes(unit)) {
|
|
|
|
durationMs = amount * 60 * 60 * 1000;
|
|
|
|
} else if (['d', 'D'].includes(unit)) {
|
|
|
|
durationMs = amount * 24 * 60 * 60 * 1000;
|
|
|
|
} else {
|
|
|
|
throw new ExpectedError(parseErrorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return durationMs;
|
|
|
|
}
|
|
|
|
}
|