All files / src/tools utils.ts

98.63% Statements 144/146
86.76% Branches 59/68
97.67% Functions 42/43
99.2% Lines 124/125

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 2691x 1x 1x 1x 1x   1x   1x 465x   465x 465x   465x     1x 463x     1x 200x   285x   285x   200x     1x 199x 283x   283x   199x     1x   287x 196x 196x   287x           1x 4x 4x 5x         4x     1x 7x 4x     1x 30x 30x 54x 37x 23x 23x   31x   30x   30x     1x               204x 204x 204x 202x 198x   4x   2x       1x   996x     1x 147x     1x   47x   47x 47x 65x 50x 50x       47x 188x 47x   47x     1x               15x   10x 10x 10x   14x         10x 10x 6x 4x 2x         10x     1x   145x   204x 204x 130x 130x   204x       145x     1x 20x   20x 17x     14x   3x               1x       1x       189x 188x 80x     108x 108x 12x 8x       108x       1x       51x 51x 3x     50x 50x 14x 14x 10x       50x     1x                 126x 126x 126x 124x           2x 1x 1x           1x      
import path from 'path';
import fs, { constants as fsConstants } from 'fs';
import dotProp from 'dot-prop';
import _ from 'lodash';
import log from '../logger';
import { Asset, Assets, CalculatedChanges, KeywordMappings } from '../types';
import constants from './constants';
 
export const keywordReplaceArrayRegExp = (key) => {
  const pattern = `@@${key}@@`;
  //YAML format supports both single and double quotes for strings
  const patternWithSingleQuotes = `'${pattern}'`;
  const patternWithDoubleQuotes = `"${pattern}"`;
 
  return new RegExp(`${patternWithSingleQuotes}|${patternWithDoubleQuotes}|${pattern}`, 'g');
};
 
export const keywordReplaceStringRegExp = (key) => {
  return new RegExp(`##${key}##`, 'g');
};
 
export function keywordArrayReplace(input: string, mappings: KeywordMappings): string {
  Object.keys(mappings).forEach(function (key) {
    // Matching against two sets of patterns because a developer may provide their array replacement keyword with or without wrapping quotes. It is not obvious to the developer which to do depending if they're operating in YAML or JSON.
    const regex = keywordReplaceArrayRegExp(key);
 
    input = input.replace(regex, JSON.stringify(mappings[key]));
  });
  return input;
}
 
export function keywordStringReplace(input: string, mappings: KeywordMappings): string {
  Object.keys(mappings).forEach(function (key) {
    const regex = keywordReplaceStringRegExp(key);
    // @ts-ignore TODO: come back and distinguish strings vs array replacement.
    input = input.replace(regex, mappings[key]);
  });
  return input;
}
 
export function keywordReplace(input: string, mappings: KeywordMappings): string {
  // Replace keywords with mappings within input.
  if (mappings && Object.keys(mappings).length > 0) {
    input = keywordArrayReplace(input, mappings);
    input = keywordStringReplace(input, mappings);
  }
  return input;
}
 
// wrapArrayReplaceMarkersInQuotes will wrap array replacement markers in quotes.
// This is necessary for YAML format in the context of keyword replacement
// to preserve the keyword markers while also maintaining valid YAML syntax.
export function wrapArrayReplaceMarkersInQuotes(body: string, mappings: KeywordMappings): string {
  let newBody = body;
  Object.keys(mappings).forEach((keyword) => {
    newBody = newBody.replace(
      new RegExp('(?<![\'"])@@' + keyword + '@@(?![\'"])', 'g'),
      `"@@${keyword}@@"`
    );
  });
  return newBody;
}
 
export function convertClientNameToId(name: string, clients: Asset[]): string {
  const found = clients.find((c) => c.name === name);
  return (found && found.client_id) || name;
}
 
export function convertClientNamesToIds(names: string[], clients: Asset[]): string[] {
  const resolvedNames = names.map((name) => ({ name, resolved: false }));
  const result = clients.reduce((acc: string[], client): string[] => {
    if (names.includes(client.name)) {
      const index = resolvedNames.findIndex((item) => item.name === client.name);
      resolvedNames[index].resolved = true;
      return [...acc, client.client_id];
    }
    return [...acc];
  }, []);
  const unresolved = resolvedNames.filter((item) => !item.resolved).map((item) => item.name);
  // @ts-ignore TODO: come back and refactor to use map instead of reduce.
  return [...unresolved, ...result];
}
 
export function loadFileAndReplaceKeywords(
  file: string,
  {
    mappings,
    disableKeywordReplacement = false,
  }: { mappings: KeywordMappings; disableKeywordReplacement: boolean }
): string {
  // Load file and replace keyword mappings
  const f = path.resolve(file);
  try {
    fs.accessSync(f, fsConstants.F_OK);
    if (mappings && !disableKeywordReplacement) {
      return keywordReplace(fs.readFileSync(f, 'utf8'), mappings);
    }
    return fs.readFileSync(f, 'utf8');
  } catch (error) {
    throw new Error(`Unable to load file ${f} due to ${error}`);
  }
}
 
export function flatten(list: any[]): any[] {
  // Flatten an multiple arrays to single array
  return list.reduce((a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []);
}
 
export function convertJsonToString(obj: { [key: string]: any }, spacing = 0): string {
  return JSON.stringify(obj, null, spacing);
}
 
export function stripFields(obj: Asset, fields: string[]): Asset {
  // Strip object fields supporting dot notation (ie: a.deep.field)
  const stripped: string[] = [];
 
  const newObj = { ...obj };
  fields.forEach((f) => {
    if (dotProp.get(newObj, f) !== undefined) {
      dotProp.delete(newObj, f);
      stripped.push(f);
    }
  });
 
  Eif (stripped) {
    const name = ['id', 'client_id', 'template', 'name'].reduce((n, k) => newObj[k] || n, '');
    log.debug(`Stripping "${name}" read-only fields ${JSON.stringify(stripped)}`);
  }
  return newObj;
}
 
export function getEnabledClients(
  assets: Assets,
  connection: Asset,
  existing: Asset[],
  clients: Asset[]
): string[] | undefined {
  // Convert enabled_clients by name to the id
 
  if (connection.enabled_clients === undefined) return undefined; // If no enabled clients passed in, explicitly ignore from management, preventing unintentional disabling of connection.
 
  const excludedClientsByNames = (assets.exclude && assets.exclude.clients) || [];
  const excludedClients = convertClientNamesToIds(excludedClientsByNames, clients);
  const enabledClients = [
    ...convertClientNamesToIds(connection.enabled_clients || [], clients).filter(
      (item) => ![...excludedClientsByNames, ...excludedClients].includes(item)
    ),
  ];
  // If client is excluded and in the existing connection this client is enabled, it should keep enabled
  // If client is excluded and in the existing connection this client is disabled, it should keep disabled
  existing.forEach((conn) => {
    if (conn.name === connection.name) {
      excludedClients.forEach((excludedClient) => {
        if (conn.enabled_clients.includes(excludedClient)) {
          enabledClients.push(excludedClient);
        }
      });
    }
  });
  return enabledClients;
}
 
export function duplicateItems(arr: Asset[], key: string): Asset[] {
  // Find duplicates objects within array that have the same key value
  const duplicates = arr.reduce(
    (accum: { [key: string]: Asset[] }, obj): { [key: string]: Asset[] } => {
      const keyValue = obj[key];
      if (keyValue) {
        if (!(keyValue in accum)) accum[keyValue] = [];
        accum[keyValue].push(obj);
      }
      return accum;
    },
    {}
  );
  return Object.values(duplicates).filter((g) => g.length > 1);
}
 
export function filterExcluded(changes: CalculatedChanges, exclude: string[]): CalculatedChanges {
  const { del, update, create, conflicts } = changes;
 
  if (!exclude.length) {
    return changes;
  }
 
  const filter = (list: Asset[]) => list.filter((item) => !exclude.includes(item.name));
 
  return {
    del: filter(del),
    update: filter(update),
    create: filter(create),
    conflicts: filter(conflicts),
  };
}
 
export function areArraysEquals(x: any[], y: any[]): boolean {
  return _.isEqual(x && x.sort(), y && y.sort());
}
 
export const obfuscateSensitiveValues = (
  data: Asset | Asset[] | null,
  sensitiveFieldsToObfuscate: string[]
): Asset | Asset[] | null => {
  if (data === null) return data;
  if (Array.isArray(data)) {
    return data.map((asset) => obfuscateSensitiveValues(asset, sensitiveFieldsToObfuscate));
  }
 
  const newAsset = { ...data };
  sensitiveFieldsToObfuscate.forEach((sensitiveField) => {
    if (dotProp.get(newAsset, sensitiveField) !== undefined) {
      dotProp.set(newAsset, sensitiveField, constants.OBFUSCATED_SECRET_VALUE);
    }
  });
 
  return newAsset;
};
 
// The reverse of `obfuscateSensitiveValues()`, preventing an obfuscated value from being passed to the API
export const stripObfuscatedFieldsFromPayload = (
  data: Asset | Asset[] | null,
  obfuscatedFields: string[]
): Asset | Asset[] | null => {
  Iif (data === null) return data;
  if (Array.isArray(data)) {
    return data.map((asset) => stripObfuscatedFieldsFromPayload(asset, obfuscatedFields));
  }
 
  const newAsset = { ...data };
  obfuscatedFields.forEach((sensitiveField) => {
    const obfuscatedFieldValue = dotProp.get(newAsset, sensitiveField);
    if (obfuscatedFieldValue === constants.OBFUSCATED_SECRET_VALUE) {
      dotProp.delete(newAsset, sensitiveField);
    }
  });
 
  return newAsset;
};
 
export const detectInsufficientScopeError = async <T>(
  fn: Function
): Promise<
  | {
      hadSufficientScopes: true;
      data: T;
      requiredScopes: [];
    }
  | { hadSufficientScopes: false; requiredScopes: string[]; data: null }
> => {
  try {
    const data = await fn();
    return {
      hadSufficientScopes: true,
      data,
      requiredScopes: [],
    };
  } catch (err) {
    if (err.statusCode === 403 && err.message.includes('Insufficient scope')) {
      const requiredScopes = err.message?.split('Insufficient scope, expected any of: ')?.slice(1);
      return {
        hadSufficientScopes: false,
        requiredScopes,
        data: null,
      };
    }
    throw err;
  }
};