-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ACL-checking to turn off exec button (#7919)
This closes #7453. It adds an abstraction to handle the common needs of ability-determination.
- Loading branch information
Showing
8 changed files
with
366 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Ability } from 'ember-can'; | ||
import { inject as service } from '@ember/service'; | ||
import { computed, get } from '@ember/object'; | ||
import { equal, not } from '@ember/object/computed'; | ||
|
||
export default Ability.extend({ | ||
system: service(), | ||
token: service(), | ||
|
||
bypassAuthorization: not('token.aclEnabled'), | ||
selfTokenIsManagement: equal('token.selfToken.type', 'management'), | ||
|
||
activeNamespace: computed('system.activeNamespace.name', function() { | ||
return this.get('system.activeNamespace.name') || 'default'; | ||
}), | ||
|
||
rulesForActiveNamespace: computed('activeNamespace', 'token.selfTokenPolicies.[]', function() { | ||
let activeNamespace = this.activeNamespace; | ||
|
||
return (this.get('token.selfTokenPolicies') || []).toArray().reduce((rules, policy) => { | ||
let policyNamespaces = get(policy, 'rulesJSON.Namespaces') || []; | ||
|
||
let matchingNamespace = this._findMatchingNamespace(policyNamespaces, activeNamespace); | ||
|
||
if (matchingNamespace) { | ||
rules.push(policyNamespaces.find(namespace => namespace.Name === matchingNamespace)); | ||
} | ||
|
||
return rules; | ||
}, []); | ||
}), | ||
|
||
// Chooses the closest namespace as described at the bottom here: | ||
// https://www.nomadproject.io/guides/security/acl.html#namespace-rules | ||
_findMatchingNamespace(policyNamespaces, activeNamespace) { | ||
let namespaceNames = policyNamespaces.mapBy('Name'); | ||
|
||
if (namespaceNames.includes(activeNamespace)) { | ||
return activeNamespace; | ||
} | ||
|
||
let globNamespaceNames = namespaceNames.filter(namespaceName => namespaceName.includes('*')); | ||
|
||
let matchingNamespaceName = globNamespaceNames.reduce( | ||
(mostMatching, namespaceName) => { | ||
// Convert * wildcards to .* for regex matching | ||
let namespaceNameRegExp = new RegExp(namespaceName.replace(/\*/g, '.*')); | ||
let characterDifference = activeNamespace.length - namespaceName.length; | ||
|
||
if ( | ||
characterDifference < mostMatching.mostMatchingCharacterDifference && | ||
activeNamespace.match(namespaceNameRegExp) | ||
) { | ||
return { | ||
mostMatchingNamespaceName: namespaceName, | ||
mostMatchingCharacterDifference: characterDifference, | ||
}; | ||
} else { | ||
return mostMatching; | ||
} | ||
}, | ||
{ mostMatchingNamespaceName: null, mostMatchingCharacterDifference: Number.MAX_SAFE_INTEGER } | ||
).mostMatchingNamespaceName; | ||
|
||
if (matchingNamespaceName) { | ||
return matchingNamespaceName; | ||
} else if (namespaceNames.includes('default')) { | ||
return 'default'; | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import AbstractAbility from './abstract'; | ||
import { computed, get } from '@ember/object'; | ||
import { or } from '@ember/object/computed'; | ||
|
||
export default AbstractAbility.extend({ | ||
canExec: or('bypassAuthorization', 'selfTokenIsManagement', 'policiesSupportExec'), | ||
|
||
policiesSupportExec: computed('rulesForActiveNamespace.@each.capabilities', function() { | ||
return this.rulesForActiveNamespace.some(rules => { | ||
let capabilities = get(rules, 'Capabilities') || []; | ||
return capabilities.includes('alloc-exec'); | ||
}); | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,14 @@ | ||
import { Ability } from 'ember-can'; | ||
import { inject as service } from '@ember/service'; | ||
import AbstractAbility from './abstract'; | ||
import { computed, get } from '@ember/object'; | ||
import { equal, or, not } from '@ember/object/computed'; | ||
|
||
export default Ability.extend({ | ||
system: service(), | ||
token: service(), | ||
import { or } from '@ember/object/computed'; | ||
|
||
export default AbstractAbility.extend({ | ||
canRun: or('bypassAuthorization', 'selfTokenIsManagement', 'policiesSupportRunning'), | ||
|
||
bypassAuthorization: not('token.aclEnabled'), | ||
selfTokenIsManagement: equal('token.selfToken.type', 'management'), | ||
|
||
activeNamespace: computed('system.activeNamespace.name', function() { | ||
return this.get('system.activeNamespace.name') || 'default'; | ||
}), | ||
|
||
rulesForActiveNamespace: computed('activeNamespace', 'token.selfTokenPolicies.[]', function() { | ||
let activeNamespace = this.activeNamespace; | ||
|
||
return (this.get('token.selfTokenPolicies') || []).toArray().reduce((rules, policy) => { | ||
let policyNamespaces = get(policy, 'rulesJSON.Namespaces') || []; | ||
|
||
let matchingNamespace = this._findMatchingNamespace(policyNamespaces, activeNamespace); | ||
|
||
if (matchingNamespace) { | ||
rules.push(policyNamespaces.find(namespace => namespace.Name === matchingNamespace)); | ||
} | ||
|
||
return rules; | ||
}, []); | ||
}), | ||
|
||
policiesSupportRunning: computed('rulesForActiveNamespace.@each.capabilities', function() { | ||
return this.rulesForActiveNamespace.some(rules => { | ||
let capabilities = get(rules, 'Capabilities') || []; | ||
return capabilities.includes('submit-job'); | ||
}); | ||
}), | ||
|
||
// Chooses the closest namespace as described at the bottom here: | ||
// https://www.nomadproject.io/guides/security/acl.html#namespace-rules | ||
_findMatchingNamespace(policyNamespaces, activeNamespace) { | ||
let namespaceNames = policyNamespaces.mapBy('Name'); | ||
|
||
if (namespaceNames.includes(activeNamespace)) { | ||
return activeNamespace; | ||
} | ||
|
||
let globNamespaceNames = namespaceNames.filter(namespaceName => namespaceName.includes('*')); | ||
|
||
let matchingNamespaceName = globNamespaceNames.reduce( | ||
(mostMatching, namespaceName) => { | ||
// Convert * wildcards to .* for regex matching | ||
let namespaceNameRegExp = new RegExp(namespaceName.replace(/\*/g, '.*')); | ||
let characterDifference = activeNamespace.length - namespaceName.length; | ||
|
||
if ( | ||
characterDifference < mostMatching.mostMatchingCharacterDifference && | ||
activeNamespace.match(namespaceNameRegExp) | ||
) { | ||
return { | ||
mostMatchingNamespaceName: namespaceName, | ||
mostMatchingCharacterDifference: characterDifference, | ||
}; | ||
} else { | ||
return mostMatching; | ||
} | ||
}, | ||
{ mostMatchingNamespaceName: null, mostMatchingCharacterDifference: Number.MAX_SAFE_INTEGER } | ||
).mostMatchingNamespaceName; | ||
|
||
if (matchingNamespaceName) { | ||
return matchingNamespaceName; | ||
} else if (namespaceNames.includes('default')) { | ||
return 'default'; | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,12 @@ | ||
<button | ||
data-test-exec-button | ||
type="button" | ||
class="button exec-button is-outline is-small" | ||
{{action "open"}}> | ||
{{x-icon "console"}} | ||
<span>Exec</span> | ||
</button> | ||
{{#let (cannot "exec allocation") as |cannotExec|}} | ||
<button | ||
data-test-exec-button | ||
type="button" | ||
class="button exec-button is-outline is-small {{if cannotExec "tooltip"}}" | ||
disabled={{if cannotExec 'disabled'}} | ||
aria-label={{if cannotExec "You don’t have permission to exec"}} | ||
{{action "open"}}> | ||
{{x-icon "console"}} | ||
<span>Exec</span> | ||
</button> | ||
{{/let}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.