ui: Ensure we check intention service prefix permissions for per service (#11409)

Port of: Ensure we check intention service prefix permissions for per service (#11270)

Previously, when showing some action buttons for 'per service intentions' we used a global 'can I do something with any intention' permission to decide whether to show a certain button or not. If a user has a token that does not have 'global' intention permissions, but does have intention permissions on one or more specific services (for example via service / service_prefix), this meant that we did not show them certain buttons required to create/edit the intentions for this specific service.

This PR adds that extra permissions check so we now check the intentions permissions per service instead of using the 'global' "can I edit intentions" question/request.

**Notes:**

- If a HTML button is `disabled` this means tippy.js doesn't adopt the
popover properly and subsequently hide it from the user, so aswell as
just disabling the button so you can't active the popover, we also don't
even put the popover on the page
- If `ability.item` or `ability.item.Resources` are empty then assume no access

**We don't try to disable service > right hand side intention actions here**

Whether you can create intentions for a service depends on the
_destination_ of the intention you would like to create. For the
topology view going from the LHS to the center, this is straightforwards
as we only need to know the permissions for the central service, as when
you are going from the LHS to the center, the center is the
_destination_.

When going from the center to the RHS the _destination[s]_ are on the
RHS. This means we need to know the permissions for potentially 1000s of
services all in one go in order to know when to show a button or not.

We can't realistically discover the permissions for service > RHS
services as we'd have either make a HTTP request per right hand service,
or potentially make an incredibly large POST request for all the
potentially 1000s of services on the right hand side (more preferable to
1000s of HTTP requests).

Therefore for the moment at least we keep the old functionality (thin client)
for the middle to RHS here. If you do go to click on the button and you
don't have permissions to update the intention you will still not be
able to update it, only you won't know this until you click the button
(at which point you'll get a UI visible 403 error)

Note: We reversed the conditional here between 1.10 and 1.11

So this make 100% sense that the port is different here to 1.11
This commit is contained in:
John Cowen 2021-11-04 12:10:28 +00:00 committed by GitHub
parent 8f8055352d
commit 72be2dd204
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 213 additions and 141 deletions

4
.changelog/11409.txt Normal file
View File

@ -0,0 +1,4 @@
```release-note:bug
ui: Ensure we check intention permissions for specific services when deciding
whether to show action buttons for per service intention actions
```

View File

@ -1,5 +1,14 @@
import BaseAbility from './base'; import BaseAbility, { ACCESS_READ, ACCESS_WRITE } from './base';
export default class ServiceInstanceAbility extends BaseAbility { export default class ServiceInstanceAbility extends BaseAbility {
resource = 'service'; resource = 'service';
generateForSegment(segment) {
// When we ask for service-instances its almost like a request for a single service
// When we do that we also want to know if we can read/write intentions for services
// so here we add intentions read/write for the specific segment/service prefix
return super.generateForSegment(...arguments).concat([
this.permissions.generate('intention', ACCESS_READ, segment),
this.permissions.generate('intention', ACCESS_WRITE, segment),
]);
}
} }

View File

@ -6,4 +6,32 @@ export default class ServiceAbility extends BaseAbility {
get isLinkable() { get isLinkable() {
return this.item.InstanceCount > 0; return this.item.InstanceCount > 0;
} }
get canReadIntention() {
if (typeof this.item === 'undefined' || typeof this.item.Resources === 'undefined') {
return false;
}
const found = this.item.Resources.find(
item => item.Resource === 'intention' && item.Access === 'read' && item.Allow === true
);
return typeof found !== 'undefined';
}
get canWriteIntention() {
if (typeof this.item === 'undefined' || typeof this.item.Resources === 'undefined') {
return false;
}
const found = this.item.Resources.find(
item => item.Resource === 'intention' && item.Access === 'write' && item.Allow === true
);
return typeof found !== 'undefined';
}
get canCreateIntention() {
return this.canWriteIntention;
}
get canUpdateIntention() {
return this.canWriteIntention;
}
} }

View File

@ -33,7 +33,7 @@ as |api|>
<BlockSlot @name="form"> <BlockSlot @name="form">
{{#let api.data as |item|}} {{#let api.data as |item|}}
{{#if (can 'write intention' item=item)}} {{#if (not readonly)}}
{{#let (changeset-get item 'Action') as |newAction|}} {{#let (changeset-get item 'Action') as |newAction|}}
<ModalDialog <ModalDialog

View File

@ -82,12 +82,14 @@
</svg> </svg>
{{/if}} {{/if}}
{{#let (not (can 'update intention for service' item=@service.Service)) as |disabled|}}
{{#each @items as |item|}} {{#each @items as |item|}}
{{#if (or (not item.Intention.Allowed) item.Intention.HasPermissions)}} {{#if (or (not item.Intention.Allowed) item.Intention.HasPermissions)}}
<TopologyMetrics::Popover <TopologyMetrics::Popover
@type={{if item.Intention.HasPermissions 'l7' 'deny'}} @type={{if item.Intention.HasPermissions 'l7' 'deny'}}
@position={{find-by 'id' (concat this.guid item.Namespace item.Name) this.iconPositions}} @position={{find-by 'id' (concat this.guid item.Namespace item.Name) this.iconPositions}}
@item={{item}} @item={{item}}
@disabled={{disabled}}
@oncreate={{action @oncreate item @service}} @oncreate={{action @oncreate item @service}}
/> />
{{else if (and item.Intention.Allowed (not item.TransparentProxy) (eq item.Source 'specific-intention'))}} {{else if (and item.Intention.Allowed (not item.TransparentProxy) (eq item.Source 'specific-intention'))}}
@ -96,8 +98,10 @@
@service={{@service}} @service={{@service}}
@position={{find-by 'id' (concat this.guid item.Namespace item.Name) this.iconPositions}} @position={{find-by 'id' (concat this.guid item.Namespace item.Name) this.iconPositions}}
@item={{item}} @item={{item}}
@disabled={{disabled}}
@oncreate={{action @oncreate item @service}} @oncreate={{action @oncreate item @service}}
/> />
{{/if}} {{/if}}
{{/each}} {{/each}}
{{/let}}

View File

@ -2,133 +2,147 @@
class="topology-metrics-popover {{@type}}" class="topology-metrics-popover {{@type}}"
...attributes ...attributes
> >
{{#let
{{#if (eq @type 'deny')}} (concat 'top:' @position.y 'px;left:' @position.x 'px;')
<InformedAction (if (eq @type 'deny') 'Add intention' 'View intention')
class="dangerous" as |style label|}}
{{did-insert (set this 'popover')}} {{#if (not @disabled)}}
> {{#if (eq @type 'deny')}}
<:header> <InformedAction
<h3> class="dangerous"
{{t "components.consul.topology-metrics.popover.deny.header"}} {{did-insert (set this 'popover')}}
</h3> >
</:header> <:header>
<:body> <h3>
<p> {{t "components.consul.topology-metrics.popover.deny.header"}}
{{#if @item.Intention.HasExact}} </h3>
{{t "components.consul.topology-metrics.popover.deny.body.isExact"}} </:header>
{{else}} <:body>
{{t "components.consul.topology-metrics.popover.deny.body.notExact"}} <p>
{{/if}} {{#if @item.Intention.HasExact}}
</p> {{t "components.consul.topology-metrics.popover.deny.body.isExact"}}
</:body> {{else}}
<:actions as |Actions|> {{t "components.consul.topology-metrics.popover.deny.body.notExact"}}
<Actions.Action class="action"> {{/if}}
<button </p>
{{on "click" @oncreate}} </:body>
data-test-confirm <:actions as |Actions|>
type="button" <Actions.Action class="action">
> <button
{{#if @item.Intention.HasExact}} {{on "click" @oncreate}}
{{t "components.consul.topology-metrics.popover.deny.action.isExact"}} data-test-confirm
{{else}} type="button"
{{t "components.consul.topology-metrics.popover.deny.action.notExact"}} >
{{/if}} {{#if @item.Intention.HasExact}}
</button> {{t "components.consul.topology-metrics.popover.deny.action.isExact"}}
</Actions.Action> {{else}}
<Actions.Action> {{t "components.consul.topology-metrics.popover.deny.action.notExact"}}
<button {{/if}}
{{on 'click' (fn (optional this.popoverController.hide))}} </button>
class="cancel" </Actions.Action>
type="button" <Actions.Action>
> <button
Cancel {{on 'click' (fn (optional this.popoverController.hide))}}
</button> class="cancel"
</Actions.Action> type="button"
</:actions> >
</InformedAction> Cancel
{{else if (eq @type 'not-defined')}} </button>
<InformedAction </Actions.Action>
class="warning documentation" </:actions>
{{did-insert (set this 'popover')}} </InformedAction>
> {{else if (eq @type 'not-defined')}}
<:header> <InformedAction
<h3> class="warning documentation"
{{t "components.consul.topology-metrics.popover.not-defined.header"}} {{did-insert (set this 'popover')}}
</h3> >
</:header> <:header>
<:body> <h3>
<p> {{t "components.consul.topology-metrics.popover.not-defined.header"}}
{{t "components.consul.topology-metrics.popover.not-defined.body" downstream=@item.Name upstream=@service.Name }} </h3>
</p> </:header>
</:body> <:body>
<:actions as |Actions|> <p>
<Actions.Action class="action"> {{t "components.consul.topology-metrics.popover.not-defined.body" downstream=@item.Name upstream=@service.Name }}
<a href="{{env 'CONSUL_DOCS_URL'}}/connect/registration/service-registration#upstreams" rel="noopener noreferrer" target="_blank"> </p>
{{t "components.consul.topology-metrics.popover.not-defined.action"}} </:body>
</a> <:actions as |Actions|>
</Actions.Action> <Actions.Action class="action">
<Actions.Action> <a href="{{env 'CONSUL_DOCS_URL'}}/connect/registration/service-registration#upstreams" rel="noopener noreferrer" target="_blank">
<button {{t "components.consul.topology-metrics.popover.not-defined.action"}}
{{on 'click' (fn (optional this.popoverController.hide))}} </a>
class="cancel" </Actions.Action>
type="button" <Actions.Action>
> <button
Close {{on 'click' (fn (optional this.popoverController.hide))}}
</button> class="cancel"
</Actions.Action> type="button"
</:actions> >
</InformedAction> Close
{{else}} </button>
<InformedAction </Actions.Action>
class="info" </:actions>
{{did-insert (set this 'popover')}} </InformedAction>
> {{else}}
<:header> <InformedAction
<h3> class="info"
{{t "components.consul.topology-metrics.popover.l7.header"}} {{did-insert (set this 'popover')}}
</h3> >
</:header> <:header>
<:body> <h3>
<p> {{t "components.consul.topology-metrics.popover.l7.header"}}
{{t "components.consul.topology-metrics.popover.l7.body"}} </h3>
</p> </:header>
</:body> <:body>
<:actions as |Actions|> <p>
<Actions.Action class="action"> {{t "components.consul.topology-metrics.popover.l7.body"}}
<a href={{href-to 'dc.services.show.intentions.edit' (concat @item.Intention.ID)}}> </p>
{{t "components.consul.topology-metrics.popover.l7.action"}} </:body>
</a> <:actions as |Actions|>
</Actions.Action> <Actions.Action class="action">
<Actions.Action> <a href={{href-to 'dc.services.show.intentions.edit' (concat @item.Intention.ID)}}>
<button {{t "components.consul.topology-metrics.popover.l7.action"}}
{{on 'click' (fn (optional this.popoverController.hide))}} </a>
class="cancel" </Actions.Action>
type="button" <Actions.Action>
> <button
Close {{on 'click' (fn (optional this.popoverController.hide))}}
</button> class="cancel"
</Actions.Action> type="button"
</:actions> >
</InformedAction> Close
{{/if}} </button>
<button </Actions.Action>
{{with-overlay </:actions>
this.popover </InformedAction>
options=(hash {{/if}}
theme="square-tail" {{! TODO: Once we can conditionally add a modifier these two buttons }}
placement="bottom-start" {{! should be replaced with a conditional modifer instead }}
) <button
returns=(set this 'popoverController') {{with-overlay
}} this.popover
{{on 'click' (fn (optional this.popoverController.show))}} options=(hash
{{disabled (cannot 'update intention' item=item.Intention)}} theme="square-tail"
type="button" placement="bottom-start"
style={{{concat 'top:' @position.y 'px;left:' @position.x 'px;'}}} )
aria-label={{if (eq @type 'deny') 'Add intention' 'View intention'}} returns=(set this 'popoverController')
data-test-action }}
> {{on 'click' (fn (optional this.popoverController.show))}}
</button> type="button"
style={{{style}}}
aria-label={{label}}
data-test-action
>
</button>
{{else}}
<button
{{disabled true}}
type="button"
style={{{style}}}
aria-label={{label}}
data-test-action
>
</button>
{{/if}}
{{/let}}
</div> </div>

View File

@ -82,13 +82,13 @@
{{/each}} {{/each}}
</svg> </svg>
{{/if}} {{/if}}
{{#each @items as |item|}} {{#each @items as |item|}}
{{#if (or (not item.Intention.Allowed) item.Intention.HasPermissions)}} {{#if (or (not item.Intention.Allowed) item.Intention.HasPermissions)}}
<TopologyMetrics::Popover <TopologyMetrics::Popover
@type={{if item.Intention.HasPermissions 'l7' 'deny'}} @type={{if item.Intention.HasPermissions 'l7' 'deny'}}
@position={{find-by 'id' (concat this.guid item.Namespace item.Name) this.iconPositions}} @position={{find-by 'id' (concat this.guid item.Namespace item.Name) this.iconPositions}}
@item={{item}} @item={{item}}
@disabled={{false}}
@oncreate={{action @oncreate @service item}} @oncreate={{action @oncreate @service item}}
/> />
{{/if}} {{/if}}

View File

@ -79,9 +79,12 @@ export default class RepositoryService extends Service {
throw e; throw e;
} }
} }
const item = await cb(); const item = await cb(params.resources);
// add the `Resource` information to the record/model so we can inspect // add the `Resource` information to the record/model so we can inspect
// them in other places like templates etc // them in other places like templates etc
// TODO: We mostly use this to authorize single items but we do
// occasionally get an array back here e.g. service-instances, so we
// should make this fact more obvious
if (get(item, 'Resources')) { if (get(item, 'Resources')) {
set(item, 'Resources', params.resources); set(item, 'Resources', params.resources);
} }

View File

@ -21,12 +21,15 @@ export default class ServiceInstanceService extends RepositoryService {
params.index = configuration.cursor; params.index = configuration.cursor;
params.uri = configuration.uri; params.uri = configuration.uri;
} }
const instances = await this.authorizeBySlug( return this.authorizeBySlug(
async () => this.query(params), async (resources) => {
const instances = await this.query(params);
set(instances, 'firstObject.Service.Resources', resources);
return instances;
},
ACCESS_READ, ACCESS_READ,
params params
); );
return instances;
} }
@dataSource('/:partition/:ns/:dc/service-instance/:serviceId/:node/:id') @dataSource('/:partition/:ns/:dc/service-instance/:serviceId/:node/:id')

View File

@ -22,7 +22,8 @@ as |route|>
<BlockSlot @name="loaded"> <BlockSlot @name="loaded">
{{#let {{#let
loader.data loader.data
as |item|}} (not (can "write intention" item=item))
as |item readOnly|}}
<AppView> <AppView>
<BlockSlot @name="breadcrumbs"> <BlockSlot @name="breadcrumbs">
<ol> <ol>
@ -31,7 +32,7 @@ as |item|}}
</BlockSlot> </BlockSlot>
<BlockSlot @name="header"> <BlockSlot @name="header">
<h1> <h1>
{{#if (can "write intention" item=item)}} {{#if (not readOnly)}}
{{#if item.ID}} {{#if item.ID}}
<route.Title @title="Edit Intention" /> <route.Title @title="Edit Intention" />
{{else}} {{else}}
@ -44,6 +45,7 @@ as |item|}}
</BlockSlot> </BlockSlot>
<BlockSlot @name="content"> <BlockSlot @name="content">
<Consul::Intention::Form <Consul::Intention::Form
@readonly={{readOnly}}
@item={{item}} @item={{item}}
@dc={{route.model.dc}} @dc={{route.model.dc}}
@nspace={{route.params.nspace}} @nspace={{route.params.nspace}}

View File

@ -90,7 +90,7 @@ as |items item dc|}}
services=(eq item.Service.Kind 'terminating-gateway') services=(eq item.Service.Kind 'terminating-gateway')
upstreams=(eq item.Service.Kind 'ingress-gateway') upstreams=(eq item.Service.Kind 'ingress-gateway')
instances=true instances=true
intentions=(not-eq item.Service.Kind 'terminating-gateway') intentions=(and (not-eq item.Service.Kind 'terminating-gateway') (can 'read intention for service' item=item.Service))
routing=(and dc.MeshEnabled item.IsOrigin) routing=(and dc.MeshEnabled item.IsOrigin)
tags=(not item.Service.Kind) tags=(not item.Service.Kind)
) )
@ -177,6 +177,7 @@ as |items item dc|}}
@name={{routeName}} @name={{routeName}}
@model={{assign (hash @model={{assign (hash
items=items items=items
item=item
tabs=tabs tabs=tabs
) route.model}} ) route.model}}
as |o|> as |o|>

View File

@ -1,14 +1,17 @@
<Route <Route
@name={{routeName}} @name={{routeName}}
as |route|> as |route|>
{{#let (not (can "write intention for service" item=item.Service)) as |readOnly|}}
<Consul::Intention::Form <Consul::Intention::Form
@nspace={{'*'}} @nspace={{'*'}}
@dc={{route.params.dc}} @dc={{route.params.dc}}
@partition={{route.params.partition}} @partition={{route.params.partition}}
@src={{route.params.intention_id}} @src={{route.params.intention_id}}
@readonly={{readOnly}}
@autofill={{hash @autofill={{hash
DestinationName=route.params.name DestinationName=route.params.name
}} }}
@onsubmit={{transition-to 'dc.services.show.intentions.index'}} @onsubmit={{transition-to 'dc.services.show.intentions.index'}}
/> />
{{/let}}
</Route> </Route>

View File

@ -38,10 +38,11 @@ as |route|>
) )
api.data api.data
route.model.item
as |sort filters items|}} as |sort filters items item|}}
<div class="tab-section"> <div class="tab-section">
{{#if (can 'create intentions')}} {{#if (can 'create intention for service' item=item.Service)}}
<Portal @target="app-view-actions"> <Portal @target="app-view-actions">
<a data-test-create href={{href-to 'dc.services.show.intentions.create'}} class="type-create">Create</a> <a data-test-create href={{href-to 'dc.services.show.intentions.create'}} class="type-create">Create</a>
</Portal> </Portal>