diff --git a/ui/app/abilities/job.js b/ui/app/abilities/job.js index bb467f23683..d1d64b5337e 100644 --- a/ui/app/abilities/job.js +++ b/ui/app/abilities/job.js @@ -17,17 +17,17 @@ export default Ability.extend({ rulesForActiveNamespace: computed( 'activeNamespace', - 'token.selfTokenPolicies.@each.namespace' /* FIXME not quite */, + 'token.selfTokenPolicies.@each.Namespaces' /* FIXME not quite */, function() { const activeNamespace = this.activeNamespace; return (this.get('token.selfTokenPolicies') || []).toArray().reduce((rules, policy) => { - const policyNamespaces = getWithDefault(policy, 'rulesJson.namespace', {}); + const policyNamespaces = getWithDefault(policy, 'rulesJSON.Namespaces', []); const matchingNamespace = this._findMatchingNamespace(policyNamespaces, activeNamespace); if (matchingNamespace) { - rules.push(policyNamespaces[matchingNamespace]); + rules.push(policyNamespaces.find(namespace => namespace.Name === matchingNamespace)); } return rules; @@ -40,8 +40,8 @@ export default Ability.extend({ 'rulesForActiveNamespace.@each.capabilities', function() { return this.rulesForActiveNamespace.some(rules => { - const policy = rules.policy; - const capabilities = getWithDefault(rules, 'capabilities', []); + const policy = rules.Policy; + const capabilities = getWithDefault(rules, 'Capabilities', []); return policy == 'write' || capabilities.includes('submit-job'); }); @@ -49,11 +49,12 @@ export default Ability.extend({ ), _findMatchingNamespace(policyNamespaces, activeNamespace) { - if (policyNamespaces[activeNamespace]) { + const namespaceNames = policyNamespaces.mapBy('Name'); + + if (namespaceNames.includes(activeNamespace)) { return activeNamespace; } - const namespaceNames = Object.keys(policyNamespaces); const globNamespaceNames = namespaceNames.filter(namespaceName => namespaceName.includes('*')); const matchingNamespaceName = globNamespaceNames.reduce( @@ -80,7 +81,7 @@ export default Ability.extend({ if (matchingNamespaceName) { return matchingNamespaceName; - } else if (policyNamespaces.default) { + } else if (namespaceNames.includes('default')) { return 'default'; } }, diff --git a/ui/app/models/policy.js b/ui/app/models/policy.js index 4efdf790be8..8b333617bd2 100644 --- a/ui/app/models/policy.js +++ b/ui/app/models/policy.js @@ -1,19 +1,9 @@ import Model from 'ember-data/model'; import attr from 'ember-data/attr'; -import { computed } from '@ember/object'; -import hclToJson from 'hcl-to-json'; export default Model.extend({ name: attr('string'), description: attr('string'), rules: attr('string'), - - // FIXME remove if/when API can return rules in JSON - rulesJson: computed('rules', function() { - try { - return hclToJson(this.get('rules')); - } catch (e) { - return null; - } - }), + rulesJSON: attr(), }); diff --git a/ui/package.json b/ui/package.json index e4c7be12631..d35b10c85ef 100644 --- a/ui/package.json +++ b/ui/package.json @@ -86,7 +86,6 @@ "eslint": "^5.16.0", "flat": "^4.0.0", "fuse.js": "^3.4.4", - "hcl-to-json": "^0.1.1", "husky": "^1.3.1", "ivy-codemirror": "^2.1.0", "lint-staged": "^8.1.5", diff --git a/ui/tests/acceptance/jobs-list-test.js b/ui/tests/acceptance/jobs-list-test.js index b424c795048..7a542f65987 100644 --- a/ui/tests/acceptance/jobs-list-test.js +++ b/ui/tests/acceptance/jobs-list-test.js @@ -105,6 +105,19 @@ module('Acceptance | jobs list', function(hooks) { node { policy = "read" }`, + // TODO worth keeping HCL rules for comparison? + rulesJSON: { + Namespaces: [ + { + Name: job1.namespaceId, + Policy: 'write', + }, + { + Name: job2.namespaceId, + Capabilities: ['list-jobs'], + }, + ], + }, }); clientToken.policyIds = [policy.id]; @@ -133,6 +146,14 @@ module('Acceptance | jobs list', function(hooks) { node { policy = "read" }`, + rulesJSON: { + Namespaces: [ + { + Name: 'default', + Policy: 'write', + }, + ], + }, }); await JobsList.visit(); diff --git a/ui/tests/unit/abilities/job-test.js b/ui/tests/unit/abilities/job-test.js index 661369714ff..061c04f7dbb 100644 --- a/ui/tests/unit/abilities/job-test.js +++ b/ui/tests/unit/abilities/job-test.js @@ -27,12 +27,13 @@ module('Unit | Ability | job run FIXME just for ease of filtering', function(hoo selfToken: { type: 'client' }, selfTokenPolicies: [ { - rulesJson: { - namespace: { - aNamespace: { - policy: 'write', + rulesJSON: { + Namespaces: [ + { + Name: 'aNamespace', + Policy: 'write', }, - }, + ], }, }, ], @@ -56,15 +57,17 @@ module('Unit | Ability | job run FIXME just for ease of filtering', function(hoo selfToken: { type: 'client' }, selfTokenPolicies: [ { - rulesJson: { - namespace: { - aNamespace: { - policy: 'read', + rulesJSON: { + Namespaces: [ + { + Name: 'aNamespace', + Policy: 'read', }, - default: { - policy: 'write', + { + Name: 'default', + Policy: 'write', }, - }, + ], }, }, ], @@ -88,12 +91,13 @@ module('Unit | Ability | job run FIXME just for ease of filtering', function(hoo selfToken: { type: 'client' }, selfTokenPolicies: [ { - rulesJson: { - namespace: { - aNamespace: { - policy: 'read', + rulesJSON: { + Namespaces: [ + { + Name: 'aNamespace', + Policy: 'read', }, - }, + ], }, }, ], @@ -117,27 +121,33 @@ module('Unit | Ability | job run FIXME just for ease of filtering', function(hoo selfToken: { type: 'client' }, selfTokenPolicies: [ { - rulesJson: { - namespace: { - 'production-*': { - policy: 'write', + rulesJSON: { + Namespaces: [ + { + Name: 'production-*', + Policy: 'write', }, - 'production-api': { - policy: 'write', + { + Name: 'production-api', + Policy: 'write', }, - 'production-web': { - policy: 'deny', + { + Name: 'production-web', + Policy: 'deny', }, - '*-suffixed': { - policy: 'write', + { + Name: '*-suffixed', + Policy: 'write', }, - '*-more-suffixed': { - policy: 'deny', + { + Name: '*-more-suffixed', + Policy: 'deny', }, - '*-abc-*': { - policy: 'write', + { + Name: '*-abc-*', + Policy: 'write', }, - }, + ], }, }, ], diff --git a/ui/yarn.lock b/ui/yarn.lock index 8e846d688cb..93c69e8ce73 100644 --- a/ui/yarn.lock +++ b/ui/yarn.lock @@ -6200,15 +6200,6 @@ hash.js@^1.0.0, hash.js@^1.0.3: inherits "^2.0.3" minimalistic-assert "^1.0.1" -hcl-to-json@^0.1.1: - version "0.1.1" - resolved "https://registry.yarnpkg.com/hcl-to-json/-/hcl-to-json-0.1.1.tgz#39674aa2a5a4d2b4c2b6762c8196af4af4f12903" - integrity sha512-sj1RPsdgX/ilBGZGnyjbSHQbRe20hyA6VDXYBGJedHSCdwSWkr/7tr85N7FGeM7KvBjIQX7Gl897bo0Ug73Z/A== - dependencies: - debug "^3.0.1" - lodash.get "^4.4.2" - lodash.set "^4.3.2" - heimdalljs-fs-monitor@^0.2.2: version "0.2.2" resolved "https://registry.yarnpkg.com/heimdalljs-fs-monitor/-/heimdalljs-fs-monitor-0.2.2.tgz#a76d98f52dbf3aa1b7c20cebb0132e2f5eeb9204" @@ -7537,11 +7528,6 @@ lodash.restparam@^3.0.0: resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805" integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU= -lodash.set@^4.3.2: - version "4.3.2" - resolved "https://registry.yarnpkg.com/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" - integrity sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM= - lodash.support@~2.3.0: version "2.3.0" resolved "https://registry.yarnpkg.com/lodash.support/-/lodash.support-2.3.0.tgz#7eaf038af4f0d6aab776b44aa6dcfc80334c9bfd"