No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

displayExtensionInfo.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.retrieveRoleInfo = exports.printSourceDownloadLink = exports.displayExtInfo = void 0;
  4. const clc = require("colorette");
  5. const { marked } = require("marked");
  6. const TerminalRenderer = require("marked-terminal");
  7. const utils = require("../utils");
  8. const extensionsHelper_1 = require("./extensionsHelper");
  9. const logger_1 = require("../logger");
  10. const error_1 = require("../error");
  11. const types_1 = require("./types");
  12. const iam = require("../gcp/iam");
  13. const secretsUtils_1 = require("./secretsUtils");
  14. marked.setOptions({
  15. renderer: new TerminalRenderer(),
  16. });
  17. const TASKS_ROLE = "cloudtasks.enqueuer";
  18. const TASKS_API = "cloudtasks.googleapis.com";
  19. async function displayExtInfo(extensionName, publisher, spec, published = false) {
  20. const lines = [];
  21. lines.push(`**Name**: ${spec.displayName}`);
  22. if (publisher) {
  23. lines.push(`**Publisher**: ${publisher}`);
  24. }
  25. if (spec.description) {
  26. lines.push(`**Description**: ${spec.description}`);
  27. }
  28. if (published) {
  29. if (spec.license) {
  30. lines.push(`**License**: ${spec.license}`);
  31. }
  32. if (spec.sourceUrl) {
  33. lines.push(`**Source code**: ${spec.sourceUrl}`);
  34. }
  35. }
  36. const apis = impliedApis(spec);
  37. if (apis.length) {
  38. lines.push(displayApis(apis));
  39. }
  40. const roles = impliedRoles(spec);
  41. if (roles.length) {
  42. lines.push(await displayRoles(roles));
  43. }
  44. if (lines.length > 0) {
  45. utils.logLabeledBullet(extensionsHelper_1.logPrefix, `information about '${clc.bold(extensionName)}':`);
  46. const infoStr = lines.join("\n");
  47. const formatted = marked(infoStr).replace(/\n+$/, "\n");
  48. logger_1.logger.info(formatted);
  49. return lines;
  50. }
  51. else {
  52. throw new error_1.FirebaseError("Error occurred during installation: cannot parse info from source spec", {
  53. context: {
  54. spec: spec,
  55. extensionName: extensionName,
  56. },
  57. });
  58. }
  59. }
  60. exports.displayExtInfo = displayExtInfo;
  61. function printSourceDownloadLink(sourceDownloadUri) {
  62. const sourceDownloadMsg = `Want to review the source code that will be installed? Download it here: ${sourceDownloadUri}`;
  63. utils.logBullet(marked(sourceDownloadMsg));
  64. }
  65. exports.printSourceDownloadLink = printSourceDownloadLink;
  66. async function retrieveRoleInfo(role) {
  67. const res = await iam.getRole(role);
  68. return ` ${res.title} (${res.description})`;
  69. }
  70. exports.retrieveRoleInfo = retrieveRoleInfo;
  71. async function displayRoles(roles) {
  72. const lines = await Promise.all(roles.map((role) => {
  73. return retrieveRoleInfo(role.role);
  74. }));
  75. return clc.bold("**Roles granted to this Extension**:\n") + lines.join("\n");
  76. }
  77. function displayApis(apis) {
  78. const lines = apis.map((api) => {
  79. return ` ${api.apiName} (${api.reason})`;
  80. });
  81. return "**APIs used by this Extension**:\n" + lines.join("\n");
  82. }
  83. function usesTasks(spec) {
  84. return spec.resources.some((r) => { var _a; return r.type === types_1.FUNCTIONS_RESOURCE_TYPE && ((_a = r.properties) === null || _a === void 0 ? void 0 : _a.taskQueueTrigger) !== undefined; });
  85. }
  86. function impliedRoles(spec) {
  87. var _a, _b, _c;
  88. const roles = [];
  89. if ((0, secretsUtils_1.usesSecrets)(spec) && !((_a = spec.roles) === null || _a === void 0 ? void 0 : _a.some((r) => r.role === secretsUtils_1.SECRET_ROLE))) {
  90. roles.push({
  91. role: secretsUtils_1.SECRET_ROLE,
  92. reason: "Allows the extension to read secret values from Cloud Secret Manager",
  93. });
  94. }
  95. if (usesTasks(spec) && !((_b = spec.roles) === null || _b === void 0 ? void 0 : _b.some((r) => r.role === TASKS_ROLE))) {
  96. roles.push({
  97. role: TASKS_ROLE,
  98. reason: "Allows the extension to enqueue Cloud Tasks",
  99. });
  100. }
  101. return roles.concat((_c = spec.roles) !== null && _c !== void 0 ? _c : []);
  102. }
  103. function impliedApis(spec) {
  104. var _a, _b;
  105. const apis = [];
  106. if (usesTasks(spec) && !((_a = spec.apis) === null || _a === void 0 ? void 0 : _a.some((a) => a.apiName === TASKS_API))) {
  107. apis.push({
  108. apiName: TASKS_API,
  109. reason: "Allows the extension to enqueue Cloud Tasks",
  110. });
  111. }
  112. return apis.concat((_b = spec.apis) !== null && _b !== void 0 ? _b : []);
  113. }