1xops 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/commands.mjs +19 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "1xops",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "1xops by OceanSoft.io: set up the 1xops plugin, settings and read-only cloud connectors for an organisation, team or person. Native Node.js, no dependencies.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Nhat-Thanh Nguyen <nnthanh101@gmail.com> (https://www.oceansoft.io)",
package/src/commands.mjs CHANGED
@@ -30,6 +30,16 @@ export function run(cmd, args, timeout = 20000) {
30
30
  }
31
31
 
32
32
  export const mask = (account) => (account.length >= 4 ? `****${account.slice(-4)}` : '****');
33
+ // Any 12-digit run (an AWS account ID) in any text, masked: doctor output is pasted onto issues as evidence.
34
+ export const scrub = (text) => String(text).replace(/\d{12}/g, (d) => mask(d));
35
+ // read: the role cannot change anything; write: it can; unknown: cannot tell from the name.
36
+ export function accessFromRole(arn = '') {
37
+ const role = arn.match(/:assumed-role\/([^/]+)\//)?.[1] ?? arn.match(/:(user|root)\b/)?.[1] ?? '';
38
+ if (/readonly|viewonly|securityaudit|readaccess|viewer/i.test(role)) return 'read';
39
+ if (/admin|poweruser|fullaccess|root/i.test(role)) return 'write';
40
+ return 'unknown';
41
+ }
42
+ const ACCESS = { read: 'read-only', write: 'can change things: use a read-only role', unknown: 'access unknown: use a read-only role' };
33
43
 
34
44
  function table(title, columns, rows) {
35
45
  const width = columns.map((c, i) => Math.max(c.length, ...rows.map((r) => String(r[i]).length)));
@@ -58,9 +68,11 @@ export function checkSignIn(awsProfile) {
58
68
  const args = ['sts', 'get-caller-identity', '--output', 'json', ...(awsProfile ? ['--profile', awsProfile] : [])];
59
69
  const { code, out } = run('aws', args);
60
70
  if (code === 0) {
61
- let account = '';
62
- try { account = JSON.parse(out).Account ?? ''; } catch { /* not JSON */ }
63
- rows.push(['aws sign-in', false, true, `signed in, account ${mask(account)}`]);
71
+ let id = {};
72
+ try { id = JSON.parse(out); } catch { /* not JSON */ }
73
+ const access = accessFromRole(id.Arn);
74
+ rows.push(['aws sign-in', false, true, `signed in, account ${mask(id.Account ?? '')}`]);
75
+ rows.push(['aws access', false, access === 'read', ACCESS[access]]);
64
76
  } else rows.push(['aws sign-in', false, false, 'not signed in (aws sso login)']);
65
77
  }
66
78
  if (which('az')) {
@@ -79,8 +91,8 @@ export function doctor(opts, version) {
79
91
  const shown = rows.map(([name, required, ok, detail]) => [
80
92
  name,
81
93
  required ? 'required' : 'optional',
82
- ok ? color('green', 'ok') : required ? color('red', 'missing') : color('yellow', 'optional'),
83
- detail,
94
+ ok ? color('green', 'ok') : required ? color('red', 'missing') : color('yellow', 'check'),
95
+ scrub(detail),
84
96
  ]);
85
97
  table(`1xops ${version} doctor`, ['check', 'needed', 'result', 'detail'], shown);
86
98
  const failed = rows.filter((r) => r[1] && !r[2]).map((r) => r[0]);
@@ -150,7 +162,7 @@ function showChange(file, before, after) {
150
162
  function write(file, data) {
151
163
  mkdirSync(path.dirname(file), { recursive: true });
152
164
  const tmp = `${file}.tmp`;
153
- writeFileSync(tmp, s.dumps(data));
165
+ writeFileSync(tmp, typeof data === 'string' ? data : s.dumps(data), { flush: true }); // on disk before the swap
154
166
  renameSync(tmp, file);
155
167
  }
156
168
 
@@ -320,7 +332,7 @@ export async function setup(opts) {
320
332
  console.log(`\n${file}\n${text}`);
321
333
  if (existsSync(file) && !(await yes(`${file} exists. Replace it?`, false))) return 1;
322
334
  if (!(await yes(`Write ${file}?`, true))) return 1;
323
- writeFileSync(file, text);
335
+ write(file, text);
324
336
  cf.load(file); // same checks as `1xops config --file`
325
337
 
326
338
  console.log('\nChanges this would make:');