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

✨ Add interactive cmd prompt for permission claims #2309

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

varshaprasad96
Copy link
Contributor

@varshaprasad96 varshaprasad96 commented Nov 4, 2022

Summary

This PR introduces an interactive command prompt to edit the status of open claims for apibindings. The user can provide - Yes/No/Skip to accept/reject/skip any action on a permission claim respectively.

Use cases:

  1. Edit permission claims related to a specific APIBinding:
➜  kcp git:(claims/edit) ./bin/kubectl-kcp claims edit apibinding cert-manager
Running interactive prompt. Enter (Yes/No/Skip)
Accept permission claim for Resource: secrets (APIBinding: cert-manager) > yes
  1. Edit permission claims related to all apibindings in a workspace:
➜  kcp git:(claims/edit) ✗ ./bin/kubectl-kcp claims edit apibinding
Running interactive prompt. Enter (Yes/No/Skip)
No open claims found for apibinding: "apiresource.kcp.dev-1wked"
Accept permission claim for Resource: secrets (APIBinding: cert-manager) > Yes
No open claims found for apibinding: "tenancy.kcp.dev-59phz"
  1. Use the edit command, but there are no open permission claims:
➜  kcp git:(claims/edit) ✗ ./bin/kubectl-kcp claims edit apibinding
Running interactive prompt. Enter (Yes/No/Skip)
No open claims found for apibinding: "apiresource.kcp.dev-1wked"
No open claims found for apibinding: "cert-manager"

What happens after editing?

  • The APIBinding's spec.AcceptablePermssionClaim gets updated based on the user input. For example:
➜  kcp git:(claims/edit) ✗ k get apibinding cert-manager -o yaml
apiVersion: apis.kcp.dev/v1alpha1
kind: APIBinding
metadata:
  ...
spec:
  permissionClaims:
  - all: true
    resource: secrets
    state: Accepted
  reference:
    workspace:
      exportName: cert-manager-stable
      path: root:cert-manager-service
status:
  appliedPermissionClaims:
  - all: true
    resource: secrets
  boundResources:
  - group: cert-manager.io
    resource: certificaterequests
    schema:
...

PS: With the updates added to the permission claims spec (like sub-resources, verbs), the prompt's UI will need to change accordingly.

Design doc: https://docs.google.com/document/d/1J31wXY1-2aCyyGFjUlusKoHDzV-zktAmRdIMjMf4OR0/edit

Related issue(s)

Fixes #

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Nov 5, 2022

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign ncdc for approval by writing /assign @ncdc in a comment. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@varshaprasad96 varshaprasad96 requested review from sttts and removed request for davidfestal and stevekuznetsov November 5, 2022 00:00
@varshaprasad96 varshaprasad96 force-pushed the claims/edit branch 3 times, most recently from 552cc9d to 25c8ff6 Compare November 5, 2022 00:10
pkg/cliplugins/claims/cmd/cmd.go Outdated Show resolved Hide resolved
%[1]s claims get apibinding cert-manager

# Edit the permission claims' status for all APIBindings in current workspace with an interactive prompt.
%[1]s claims get apibinding
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
%[1]s claims get apibinding
%[1]s claims edit apibinding

Copy link
Contributor

Choose a reason for hiding this comment

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

This reads weird - maybe require --all or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The idea was to just to list all the open claims for all bindings in case a specific binding wasn't given. It follows a similar format to the get command. If we add --all flag then we should probably do so for get too.

pkg/cliplugins/claims/cmd/cmd.go Outdated Show resolved Hide resolved
pkg/cliplugins/claims/plugin/edit.go Outdated Show resolved Hide resolved
pkg/cliplugins/claims/plugin/edit.go Outdated Show resolved Hide resolved
pkg/cliplugins/claims/plugin/edit.go Outdated Show resolved Hide resolved
pkg/cliplugins/claims/plugin/util.go Outdated Show resolved Hide resolved
pkg/cliplugins/claims/plugin/util.go Outdated Show resolved Hide resolved
}
}

func readInput(reader *bufio.Reader) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this different from readLine()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could make both the methods into one. The reason for having it different was in future we can extend this to parse an array or process string with a different delimiter. Say,

func readArray(reader *bufio.Reader) []string {
	arr := make([]string, 0)
	text := readLine(reader)

	for _, words := range strings.Split(text, ",") {
		arr = append(arr, strings.TrimSpace(words))
	}
	return arr
}

This is just to make post processing of string separate for future use cases.

return strings.ToLower(strings.Trim(strings.TrimSpace(text), "`'\""))
}

func inferText(input string, wr io.Writer) (ClaimAction, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any library to do this for us? I imagine there are other permutations as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried digging into some available options like go-prompt and prompt-ui. These are shell libraries which have more advanced features like auto-complete/suggestions or selections . And for creating simple prompts as we have done, they follow a similar approach as here. And then also looked into some upstream repos, like kubectl (https://github.com/kubernetes/kubectl/blob/master/pkg/cmd/util/editor/editoptions.go#L889) and kubebuilder (https://pkg.go.dev/sigs.k8s.io/kubebuilder/v3/pkg/plugin/util#YesNo), which seemed to follow a similar approach. For (Y)es|(N)o|(S)kip case, the available possibilities are less (precisely 6 - given that we are converting all inputs into a single case for inferring it).

@nrb
Copy link
Contributor

nrb commented Nov 7, 2022

A question I don't see addressed in the design doc - will there be a way for users to programmatically accept permission claims? I know there's danger there and we might not want to make it too easy, but I'm anticipating someone asking.

@varshaprasad96
Copy link
Contributor Author

A question I don't see addressed in the design doc - will there be a way for users to programmatically accept permission claims? I know there's danger there and we might not want to make it too easy, but I'm anticipating someone asking.

Agreed, for now my understanding was anyone who can access the workspace where APIBinding was created can change the state of the claim. Or maybe the right wording is, any user who can edit the apibinding can accept/reject respective claim. Are we looking to add another layer of rbac, wherein a user can view the binding, edit the export reference but not permission claim? (more like claims can be modified only be admins?)

@nrb
Copy link
Contributor

nrb commented Nov 8, 2022

Agreed, for now my understanding was anyone who can access the workspace where APIBinding was created can change the state of the claim. Or maybe the right wording is, any user who can edit the apibinding can accept/reject respective claim.

That's probably sufficient; if someone wants to automate things, they can use Go or wrappers around kubectl to do it. This command can be purely interactive. 👍

@openshift-merge-robot openshift-merge-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 22, 2022
@openshift-merge-robot
Copy link
Contributor

@varshaprasad96: PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@ncdc
Copy link
Member

ncdc commented Mar 6, 2023

@varshaprasad96 so sorry for losing sight of this PR! Are you still available to work on it, or would you be interested in trying to find someone to take it over for you?

@varshaprasad96
Copy link
Contributor Author

@ncdc I can work on this sometime this week and rebase with the latest changes.

@kcp-ci-bot
Copy link
Contributor

Issues go stale after 90d of inactivity.
After a furter 30 days, they will turn rotten.
Mark the issue as fresh with /remove-lifecycle stale.

If this issue is safe to close now please do so with /close.

/lifecycle stale

@kcp-ci-bot kcp-ci-bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 16, 2024
@sttts
Copy link
Member

sttts commented Apr 17, 2024

I still think we want this / something like this.

/remove-lifecycle stale

@kcp-ci-bot kcp-ci-bot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and has become stale. label Apr 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/apiexports needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants