2023-04-10 15:36:59 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2022-08-05 14:45:22 +00:00
|
|
|
// @ts-check
|
2022-05-17 18:52:14 +00:00
|
|
|
import { computed, get } from '@ember/object';
|
|
|
|
import { or } from '@ember/object/computed';
|
2022-07-06 14:48:22 +00:00
|
|
|
import AbstractAbility from './abstract';
|
2023-03-06 15:06:31 +00:00
|
|
|
import doesMatchPattern from 'nomad-ui/utils/match-glob';
|
2022-07-06 14:48:22 +00:00
|
|
|
|
|
|
|
const WILDCARD_GLOB = '*';
|
|
|
|
const WILDCARD_PATTERN = '/';
|
|
|
|
const GLOBAL_FLAG = 'g';
|
|
|
|
const WILDCARD_PATTERN_REGEX = new RegExp(WILDCARD_PATTERN, GLOBAL_FLAG);
|
|
|
|
|
|
|
|
export default class Variable extends AbstractAbility {
|
|
|
|
// Pass in a namespace to `can` or `cannot` calls to override
|
|
|
|
// https://github.com/minutebase/ember-can#additional-attributes
|
|
|
|
path = '*';
|
|
|
|
|
|
|
|
get _path() {
|
|
|
|
if (!this.path) return '*';
|
|
|
|
return this.path;
|
|
|
|
}
|
2022-05-17 18:52:14 +00:00
|
|
|
|
|
|
|
@or(
|
|
|
|
'bypassAuthorization',
|
|
|
|
'selfTokenIsManagement',
|
2022-08-05 14:45:22 +00:00
|
|
|
'policiesSupportVariableList'
|
2022-05-17 18:52:14 +00:00
|
|
|
)
|
|
|
|
canList;
|
|
|
|
|
2022-05-30 17:10:44 +00:00
|
|
|
@or(
|
|
|
|
'bypassAuthorization',
|
|
|
|
'selfTokenIsManagement',
|
2022-07-06 14:48:22 +00:00
|
|
|
'policiesSupportVariableWriting'
|
2022-05-30 17:10:44 +00:00
|
|
|
)
|
2022-07-06 14:48:22 +00:00
|
|
|
canWrite;
|
|
|
|
|
|
|
|
@or(
|
|
|
|
'bypassAuthorization',
|
|
|
|
'selfTokenIsManagement',
|
|
|
|
'policiesSupportVariableDestroy'
|
|
|
|
)
|
|
|
|
canDestroy;
|
2022-05-30 17:10:44 +00:00
|
|
|
|
2022-08-09 17:17:55 +00:00
|
|
|
@or(
|
|
|
|
'bypassAuthorization',
|
|
|
|
'selfTokenIsManagement',
|
|
|
|
'policiesSupportVariableRead'
|
|
|
|
)
|
|
|
|
canRead;
|
|
|
|
|
2022-08-05 14:45:22 +00:00
|
|
|
@computed('token.selfTokenPolicies')
|
|
|
|
get policiesSupportVariableList() {
|
2022-08-29 18:31:16 +00:00
|
|
|
return this.policyNamespacesIncludeVariablesCapabilities(
|
2022-08-05 14:45:22 +00:00
|
|
|
this.token.selfTokenPolicies,
|
|
|
|
['list', 'read', 'write', 'destroy']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-09 17:17:55 +00:00
|
|
|
@computed('path', 'allPaths')
|
|
|
|
get policiesSupportVariableRead() {
|
|
|
|
const matchingPath = this._nearestMatchingPath(this.path);
|
|
|
|
return this.allPaths
|
|
|
|
.find((path) => path.name === matchingPath)
|
|
|
|
?.capabilities?.includes('read');
|
|
|
|
}
|
|
|
|
|
2022-08-05 14:45:22 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Map to your policy's namespaces,
|
2022-08-29 18:31:16 +00:00
|
|
|
* and each of their Variables blocks' paths,
|
2022-08-05 14:45:22 +00:00
|
|
|
* and each of their capabilities.
|
|
|
|
* Then, check to see if any of the permissions you're looking for
|
|
|
|
* are contained within at least one of them.
|
|
|
|
*
|
|
|
|
* @param {Object} policies
|
|
|
|
* @param {string[]} capabilities
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2022-08-29 18:31:16 +00:00
|
|
|
policyNamespacesIncludeVariablesCapabilities(
|
2022-08-05 14:45:22 +00:00
|
|
|
policies = [],
|
2022-08-09 20:56:18 +00:00
|
|
|
capabilities = [],
|
|
|
|
path
|
2022-08-05 14:45:22 +00:00
|
|
|
) {
|
2022-08-29 18:31:16 +00:00
|
|
|
const namespacesWithVariableCapabilities = policies
|
2022-08-05 14:45:22 +00:00
|
|
|
.toArray()
|
2022-08-16 20:13:36 +00:00
|
|
|
.filter((policy) => get(policy, 'rulesJSON.Namespaces'))
|
2022-08-05 14:45:22 +00:00
|
|
|
.map((policy) => get(policy, 'rulesJSON.Namespaces'))
|
|
|
|
.flat()
|
|
|
|
.map((namespace = {}) => {
|
2022-08-29 18:31:16 +00:00
|
|
|
return namespace.Variables?.Paths;
|
2022-08-05 14:45:22 +00:00
|
|
|
})
|
|
|
|
.flat()
|
|
|
|
.compact()
|
2022-08-29 18:31:16 +00:00
|
|
|
.filter((varsBlock = {}) => {
|
2022-08-09 20:56:18 +00:00
|
|
|
if (!path || path === WILDCARD_GLOB) {
|
|
|
|
return true;
|
|
|
|
} else {
|
2022-08-29 18:31:16 +00:00
|
|
|
return varsBlock.PathSpec === path;
|
2022-08-09 20:56:18 +00:00
|
|
|
}
|
|
|
|
})
|
2022-08-29 18:31:16 +00:00
|
|
|
.map((varsBlock = {}) => {
|
|
|
|
return varsBlock.Capabilities;
|
2022-08-05 14:45:22 +00:00
|
|
|
})
|
|
|
|
.flat()
|
|
|
|
.compact();
|
|
|
|
|
|
|
|
// Check for requested permissions
|
2022-08-29 18:31:16 +00:00
|
|
|
return namespacesWithVariableCapabilities.some((abilityList) => {
|
2022-08-05 14:45:22 +00:00
|
|
|
return capabilities.includes(abilityList);
|
2022-05-17 18:52:14 +00:00
|
|
|
});
|
|
|
|
}
|
2022-05-30 17:10:44 +00:00
|
|
|
|
2022-08-09 20:56:18 +00:00
|
|
|
@computed('allPaths', 'namespace', 'path', 'token.selfTokenPolicies')
|
2022-07-06 14:48:22 +00:00
|
|
|
get policiesSupportVariableWriting() {
|
2022-08-09 20:56:18 +00:00
|
|
|
if (this.namespace === WILDCARD_GLOB && this.path === WILDCARD_GLOB) {
|
|
|
|
// If you're checking if you can write from root, and you don't specify a namespace,
|
|
|
|
// Then if you can write in ANY path in ANY namespace, you can get to /new.
|
2022-08-29 18:31:16 +00:00
|
|
|
return this.policyNamespacesIncludeVariablesCapabilities(
|
2022-08-09 20:56:18 +00:00
|
|
|
this.token.selfTokenPolicies,
|
|
|
|
['write'],
|
|
|
|
this._nearestMatchingPath(this.path)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// Checking a specific path in a specific namespace.
|
|
|
|
// TODO: This doesn't cover the case when you're checking for the * namespace at a specific path.
|
|
|
|
// Right now we require you to specify yournamespace to enable the button.
|
|
|
|
const matchingPath = this._nearestMatchingPath(this.path);
|
|
|
|
return this.allPaths
|
|
|
|
.find((path) => path.name === matchingPath)
|
|
|
|
?.capabilities?.includes('write');
|
|
|
|
}
|
2022-07-06 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@computed('path', 'allPaths')
|
|
|
|
get policiesSupportVariableDestroy() {
|
|
|
|
const matchingPath = this._nearestMatchingPath(this.path);
|
|
|
|
return this.allPaths
|
|
|
|
.find((path) => path.name === matchingPath)
|
|
|
|
?.capabilities?.includes('destroy');
|
|
|
|
}
|
|
|
|
|
2022-08-01 19:33:47 +00:00
|
|
|
@computed('token.selfTokenPolicies.[]', 'namespace')
|
2022-07-06 14:48:22 +00:00
|
|
|
get allPaths() {
|
|
|
|
return (get(this, 'token.selfTokenPolicies') || [])
|
|
|
|
.toArray()
|
|
|
|
.reduce((paths, policy) => {
|
2022-08-05 20:40:22 +00:00
|
|
|
const namespaces = get(policy, 'rulesJSON.Namespaces');
|
|
|
|
const matchingNamespace = this._nearestMatchingNamespace(
|
|
|
|
namespaces,
|
|
|
|
this.namespace
|
|
|
|
);
|
2022-07-06 14:48:22 +00:00
|
|
|
|
2022-08-05 20:40:22 +00:00
|
|
|
const variables = (namespaces || []).find(
|
2022-07-06 14:48:22 +00:00
|
|
|
(namespace) => namespace.Name === matchingNamespace
|
2022-08-29 18:31:16 +00:00
|
|
|
)?.Variables;
|
2022-07-06 14:48:22 +00:00
|
|
|
|
|
|
|
const pathNames = variables?.Paths?.map((path) => ({
|
|
|
|
name: path.PathSpec,
|
|
|
|
capabilities: path.Capabilities,
|
|
|
|
}));
|
|
|
|
|
|
|
|
if (pathNames) {
|
|
|
|
paths = [...paths, ...pathNames];
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
2022-08-05 20:40:22 +00:00
|
|
|
_nearestMatchingNamespace(policyNamespaces, namespace) {
|
|
|
|
if (!namespace || !policyNamespaces) return 'default';
|
|
|
|
|
|
|
|
return this._findMatchingNamespace(policyNamespaces, namespace);
|
|
|
|
}
|
|
|
|
|
2022-07-06 14:48:22 +00:00
|
|
|
_formatMatchingPathRegEx(path, wildCardPlacement = 'end') {
|
|
|
|
const replacer = () => '\\/';
|
|
|
|
if (wildCardPlacement === 'end') {
|
|
|
|
const trimmedPath = path.slice(0, path.length - 1);
|
|
|
|
const pattern = trimmedPath.replace(WILDCARD_PATTERN_REGEX, replacer);
|
|
|
|
return pattern;
|
|
|
|
} else {
|
|
|
|
const trimmedPath = path.slice(1, path.length);
|
|
|
|
const pattern = trimmedPath.replace(WILDCARD_PATTERN_REGEX, replacer);
|
|
|
|
return pattern;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_computeAllMatchingPaths(pathNames, path) {
|
|
|
|
const matches = [];
|
|
|
|
|
|
|
|
for (const pathName of pathNames) {
|
2023-03-06 15:06:31 +00:00
|
|
|
if (doesMatchPattern(pathName, path)) matches.push(pathName);
|
2022-07-06 14:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
|
|
|
_nearestMatchingPath(path) {
|
|
|
|
const pathNames = this.allPaths.map((path) => path.name);
|
|
|
|
if (pathNames.includes(path)) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
const allMatchingPaths = this._computeAllMatchingPaths(pathNames, path);
|
|
|
|
|
|
|
|
if (!allMatchingPaths.length) return WILDCARD_GLOB;
|
|
|
|
|
|
|
|
return this._smallestDifference(allMatchingPaths, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
_computeLengthDiff(pattern, path) {
|
|
|
|
const countGlobsInPattern = pattern
|
|
|
|
?.split('')
|
|
|
|
.filter((el) => el === WILDCARD_GLOB).length;
|
|
|
|
|
|
|
|
return path?.length - pattern?.length + countGlobsInPattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
_smallestDifference(matches, path) {
|
|
|
|
const sortingCallBack = (patternA, patternB) =>
|
|
|
|
this._computeLengthDiff(patternA, path) -
|
|
|
|
this._computeLengthDiff(patternB, path);
|
|
|
|
|
|
|
|
const sortedMatches = matches?.sort(sortingCallBack);
|
|
|
|
const isTie =
|
|
|
|
this._computeLengthDiff(sortedMatches[0], path) ===
|
|
|
|
this._computeLengthDiff(sortedMatches[1], path);
|
|
|
|
const doesFirstMatchHaveLeadingGlob = sortedMatches[0][0] === WILDCARD_GLOB;
|
|
|
|
|
|
|
|
return isTie && doesFirstMatchHaveLeadingGlob
|
|
|
|
? sortedMatches[1]
|
|
|
|
: sortedMatches[0];
|
2022-05-30 17:10:44 +00:00
|
|
|
}
|
2022-05-17 18:52:14 +00:00
|
|
|
}
|