From 147476c10dd39ac940c60b6cac158c1b26256057 Mon Sep 17 00:00:00 2001 From: John Cowen Date: Thu, 9 Jun 2022 18:15:52 +0100 Subject: [PATCH] ui: Disable licensing tab if the UI is running via HCP (#12771) Co-authored-by: Evan Rowe --- .circleci/config.yml | 2 +- ui/package.json | 2 +- ui/packages/consul-hcp/package.json | 5 +++++ .../consul-hcp/vendor/consul-hcp/routes.js | 11 ++++++++++ .../consul-hcp/vendor/consul-hcp/services.js | 7 +++++++ ui/packages/consul-ui/app/abilities/hcp.js | 11 ---------- ui/packages/consul-ui/app/abilities/server.js | 6 ++++++ .../consul-ui/app/components/route/index.hbs | 1 + .../consul-ui/app/components/route/index.js | 6 ++++++ ui/packages/consul-ui/app/services/routlet.js | 20 +++++++++++++++++++ .../consul-ui/app/templates/dc/show.hbs | 4 ++-- .../consul-ui/app/utils/routing/walk.js | 3 +++ ui/packages/consul-ui/ember-cli-build.js | 3 ++- .../lib/startup/templates/body.html.js | 6 +++++- ui/packages/consul-ui/package.json | 1 + .../consul-ui/vendor/consul-ui/routes.js | 4 ++-- 16 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 ui/packages/consul-hcp/package.json create mode 100644 ui/packages/consul-hcp/vendor/consul-hcp/routes.js create mode 100644 ui/packages/consul-hcp/vendor/consul-hcp/services.js delete mode 100644 ui/packages/consul-ui/app/abilities/hcp.js create mode 100644 ui/packages/consul-ui/app/abilities/server.js diff --git a/.circleci/config.yml b/.circleci/config.yml index 0286ec3679..7edc711e5f 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -30,7 +30,7 @@ references: ember: &EMBER_IMAGE docker.mirror.hashicorp.services/circleci/node:14-browsers ubuntu: &UBUNTU_CI_IMAGE ubuntu-2004:202201-02 cache: - yarn: &YARN_CACHE_KEY consul-ui-v7-{{ checksum "ui/yarn.lock" }} + yarn: &YARN_CACHE_KEY consul-ui-v8-{{ checksum "ui/yarn.lock" }} steps: install-gotestsum: &install-gotestsum diff --git a/ui/package.json b/ui/package.json index a474ecf731..edfb47355c 100644 --- a/ui/package.json +++ b/ui/package.json @@ -11,7 +11,7 @@ "scripts": { "doc:toc": "doctoc README.md", "compliance": "npm-run-all compliance:*", - "compliance:licenses": "license-checker --summary --onlyAllow 'Python-2.0;Apache*;Apache License, Version 2.0;Apache-2.0;Apache 2.0;Artistic-2.0;BSD;BSD-3-Clause;CC-BY-3.0;CC-BY-4.0;CC0-1.0;ISC;MIT;MPL-2.0;Public Domain;Unicode-TOU;Unlicense;WTFPL' --excludePackages 'consul-ui@2.2.0;consul-acls@0.1.0;consul-lock-sessions@0.1.0;consul-partitions@0.1.0;consul-nspaces@0.1.0'" + "compliance:licenses": "license-checker --summary --onlyAllow 'Python-2.0;Apache*;Apache License, Version 2.0;Apache-2.0;Apache 2.0;Artistic-2.0;BSD;BSD-3-Clause;CC-BY-3.0;CC-BY-4.0;CC0-1.0;ISC;MIT;MPL-2.0;Public Domain;Unicode-TOU;Unlicense;WTFPL' --excludePackages 'consul-ui@2.2.0;consul-acls@0.1.0;consul-lock-sessions@0.1.0;consul-partitions@0.1.0;consul-nspaces@0.1.0;consul-hcp@0.1.0'" }, "devDependencies": { diff --git a/ui/packages/consul-hcp/package.json b/ui/packages/consul-hcp/package.json new file mode 100644 index 0000000000..bb35cc6f6e --- /dev/null +++ b/ui/packages/consul-hcp/package.json @@ -0,0 +1,5 @@ +{ + "name": "consul-hcp", + "version": "0.1.0", + "private": true +} diff --git a/ui/packages/consul-hcp/vendor/consul-hcp/routes.js b/ui/packages/consul-hcp/vendor/consul-hcp/routes.js new file mode 100644 index 0000000000..1b58d87ef3 --- /dev/null +++ b/ui/packages/consul-hcp/vendor/consul-hcp/routes.js @@ -0,0 +1,11 @@ +(routes => routes({ + dc: { + show: { + license: null, + }, + }, +}))( + (json, data = (typeof document !== 'undefined' ? document.currentScript.dataset : module.exports)) => { + data[`routes`] = JSON.stringify(json); + } +); diff --git a/ui/packages/consul-hcp/vendor/consul-hcp/services.js b/ui/packages/consul-hcp/vendor/consul-hcp/services.js new file mode 100644 index 0000000000..159a7a96ec --- /dev/null +++ b/ui/packages/consul-hcp/vendor/consul-hcp/services.js @@ -0,0 +1,7 @@ +(services => services({ + +}))( + (json, data = (typeof document !== 'undefined' ? document.currentScript.dataset : module.exports)) => { + data[`services`] = JSON.stringify(json); + } +); diff --git a/ui/packages/consul-ui/app/abilities/hcp.js b/ui/packages/consul-ui/app/abilities/hcp.js deleted file mode 100644 index 80f364cad0..0000000000 --- a/ui/packages/consul-ui/app/abilities/hcp.js +++ /dev/null @@ -1,11 +0,0 @@ -import BaseAbility from './base'; -import { inject as service } from '@ember/service'; - -export default class HcpAbility extends BaseAbility { - @service('env') env; - - get is() { - return false; - // return this.env.var('CONSUL_HCP'); - } -} diff --git a/ui/packages/consul-ui/app/abilities/server.js b/ui/packages/consul-ui/app/abilities/server.js new file mode 100644 index 0000000000..8952fe65fa --- /dev/null +++ b/ui/packages/consul-ui/app/abilities/server.js @@ -0,0 +1,6 @@ +import BaseAbility from './base'; + +export default class ServerAbility extends BaseAbility { + resource = 'operator'; + segmented = false; +} diff --git a/ui/packages/consul-ui/app/components/route/index.hbs b/ui/packages/consul-ui/app/components/route/index.hbs index 7af35e58f4..4e8f485e7f 100644 --- a/ui/packages/consul-ui/app/components/route/index.hbs +++ b/ui/packages/consul-ui/app/components/route/index.hbs @@ -7,6 +7,7 @@ refresh=this.refresh t=this.t + exists=this.exists Title=(component "route/title") Announcer=(component "route/announcer") diff --git a/ui/packages/consul-ui/app/components/route/index.js b/ui/packages/consul-ui/app/components/route/index.js index 999ae7fd63..32065ca473 100644 --- a/ui/packages/consul-ui/app/components/route/index.js +++ b/ui/packages/consul-ui/app/components/route/index.js @@ -31,6 +31,12 @@ export default class RouteComponent extends Component { } return undefined; } + + @action + exists(str) { + return this.routlet.exists(`${this.args.name}.${str}`); + } + @action t(str, options) { if (str.includes('${')) { diff --git a/ui/packages/consul-ui/app/services/routlet.js b/ui/packages/consul-ui/app/services/routlet.js index 4ff011158e..2fb805b543 100644 --- a/ui/packages/consul-ui/app/services/routlet.js +++ b/ui/packages/consul-ui/app/services/routlet.js @@ -1,5 +1,6 @@ import Service, { inject as service } from '@ember/service'; import { schedule } from '@ember/runloop'; +import { get } from '@ember/object'; import wildcard from 'consul-ui/utils/routing/wildcard'; import { routes } from 'consul-ui/router'; @@ -57,10 +58,29 @@ export default class RoutletService extends Service { @service('env') env; @service('router') router; + @service('repository/permission') permissions; + ready() { return this._transition; } + exists(routeName) { + if(get(routes, routeName)) { + return this.allowed(routeName); + } + return false; + } + + allowed(routeName) { + const abilities = get(routes, `${routeName}._options.abilities`) || []; + if (abilities.length > 0) { + if (!abilities.every(ability => this.permissions.can(ability))) { + return false; + } + } + return true; + } + transition() { let endTransition; this._transition = new Promise(resolve => { diff --git a/ui/packages/consul-ui/app/templates/dc/show.hbs b/ui/packages/consul-ui/app/templates/dc/show.hbs index 1391997da3..497c2a100e 100644 --- a/ui/packages/consul-ui/app/templates/dc/show.hbs +++ b/ui/packages/consul-ui/app/templates/dc/show.hbs @@ -13,9 +13,9 @@ as |route|> {{#let (from-entries (array - (array 'serverstatus' true) + (array 'serverstatus' (compute (fn route.exists 'serverstatus'))) (array 'cataloghealth' false) - (array 'license' (can 'read license')) + (array 'license' (compute (fn route.exists 'license'))) )) as |tabs|}} diff --git a/ui/packages/consul-ui/app/utils/routing/walk.js b/ui/packages/consul-ui/app/utils/routing/walk.js index 0eef8a71c2..388f4ce219 100644 --- a/ui/packages/consul-ui/app/utils/routing/walk.js +++ b/ui/packages/consul-ui/app/utils/routing/walk.js @@ -6,6 +6,9 @@ export const walk = function(routes) { if (item === '_options') { return; } + if(routes[item] === null) { + return; + } const options = routes[item]._options; let cb; if (Object.keys(routes[item]).length > 1) { diff --git a/ui/packages/consul-ui/ember-cli-build.js b/ui/packages/consul-ui/ember-cli-build.js index 65c72fe9b3..8f2a92e152 100644 --- a/ui/packages/consul-ui/ember-cli-build.js +++ b/ui/packages/consul-ui/ember-cli-build.js @@ -31,7 +31,8 @@ module.exports = function(defaults, $ = process.env) { 'consul-acls', 'consul-lock-sessions', 'consul-partitions', - 'consul-nspaces' + 'consul-nspaces', + 'consul-hcp' ].map(item => { return { name: item, diff --git a/ui/packages/consul-ui/lib/startup/templates/body.html.js b/ui/packages/consul-ui/lib/startup/templates/body.html.js index 6956c24b93..0722a60d6a 100644 --- a/ui/packages/consul-ui/lib/startup/templates/body.html.js +++ b/ui/packages/consul-ui/lib/startup/templates/body.html.js @@ -66,6 +66,9 @@ ${ {{if .NamespacesEnabled}} {{end}} +{{if .HCPEnabled}} + +{{end}} ` : ` diff --git a/ui/packages/consul-ui/package.json b/ui/packages/consul-ui/package.json index 515398314e..e40b6fe280 100644 --- a/ui/packages/consul-ui/package.json +++ b/ui/packages/consul-ui/package.json @@ -79,6 +79,7 @@ "chalk": "^4.1.0", "clipboard": "^2.0.4", "consul-acls": "*", + "consul-hcp": "*", "consul-lock-sessions": "*", "consul-nspaces": "*", "consul-partitions": "*", diff --git a/ui/packages/consul-ui/vendor/consul-ui/routes.js b/ui/packages/consul-ui/vendor/consul-ui/routes.js index b5bec8a609..9191b87268 100644 --- a/ui/packages/consul-ui/vendor/consul-ui/routes.js +++ b/ui/packages/consul-ui/vendor/consul-ui/routes.js @@ -18,7 +18,7 @@ serverstatus: { _options: { path: '/server-status', - abilities: ['access overview', 'read zones'], + abilities: ['read servers'] }, }, cataloghealth: { @@ -30,7 +30,7 @@ license: { _options: { path: '/license', - abilities: ['access overview', 'read licence'], + abilities: ['read license'] }, }, },