Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect: fix access request searching logic #43159

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { AccessRequest, Resource } from 'shared/services/accessRequests';

// TODO
// places that use this need to be replaced with server-side filtering like
// done in RequestList.tsx:
// https://github.com/gravitational/teleport.e/blob/a776b3e65e6e8e11ca6938025e63fc3676238fb2/web/teleport/src/Workflow/ReviewRequests/RequestList/RequestList.tsx#L72
export function requestMatcher(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used only in teleterm, so I don't think we should keep this function in shared - we can fix the one we have in DocumentAccessRequests/RequestList/RequestList.tsx.
I would also add TODO about replacing it with server-side filtering.

Copy link
Contributor Author

@kimlisa kimlisa Jun 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added TODO comment (keeping file in shared because of comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to rewrite it to make it more safe, but I ultimately couldn't find a good solution. It'd have been trivial if requestMatcher received the whole AccessRequest object rather than the value of propName:

export function requestMatcher(
  accessRequest: AccessRequest,
  searchValue: string,
  propName: keyof AccessRequest
) {
  if (propName === 'roles') {
    return accessRequest[propName].some(role =>
      role.toUpperCase().includes(searchValue)
    );
  }

  if (propName === 'resources') {
    return accessRequest[propName].some(r =>
      Object.values(r.id)
        .concat(Object.values(r.details.hostname || {}))
        .concat(Object.values(r.details.friendlyName || {}))
        .some(v => v.toUpperCase().includes(searchValue))
    );
  }
}

targetValue: any,
searchValue: string,
propName: keyof AccessRequest
) {
if (propName === 'roles') {
return targetValue.some((role: string) =>
role.toUpperCase().includes(searchValue)
);
}

if (propName === 'resources') {
return targetValue.some((r: Resource) =>
Object.values(r.id)
.concat(Object.values(r.details.hostname || {}))
.concat(Object.values(r.details.friendlyName || {}))
.some(v => v.toUpperCase().includes(searchValue))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
BlockedByStartTimeButton,
ButtonPromotedInfo,
} from 'shared/components/AccessRequests/Shared/Shared';
import { requestMatcher } from 'shared/components/AccessRequests/NewRequest/matcher';

export function RequestList({
attempt,
Expand Down Expand Up @@ -142,24 +143,6 @@ export function RequestList({
);
}

function requestMatcher(
targetValue: any,
searchValue: string,
propName: keyof AccessRequest & string
) {
if (propName === 'roles') {
return targetValue.some((role: string) =>
role.toUpperCase().includes(searchValue)
);
}

if (propName === 'resources') {
return targetValue.some((r: any) =>
Object.keys(r).some(k => r[k].toUpperCase().includes(searchValue))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was the issue

);
}
}

const renderActionCell = (
request: AccessRequest,
flags: RequestFlags,
Expand Down
Loading