Skip to content

Commit

Permalink
Add --no-reason argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Tof1973 committed May 6, 2024
1 parent b5b1116 commit 9b8b250
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ ocm backplane session --delete <session-name>
If you need to run some oc command(s) with elevation using backplane-cluster-admin user, you can use the elevate command for this.
Backplane elevate takes as first positional argument the reason for this elevation. If the first argument is an empty string, then it will be considered as an empty reason, but you cannot just skip the reason argument if you provide also other positional argument(s).
If you want to not provide an empty string as reason, you can use the -n/--no-reason option and oc command will start at first positional argument.
The elevate command requires a none empty reason for the elevation. When a reason is provided it will be used for future usage, in order you do not have to provide a reason for each elevation commands. The reasons are stored in the kubeconfig context, so it is valid only for the cluster for which it has been provided. When a reason is created/used, the last used reason timestamp is updated in the context, and the reason will be kept for 20min after its last usage, in order to avoid bad usage.
Expand All @@ -290,6 +291,11 @@ If you run the elevate command with an empty reason for the first time (or after
$ ocm-backplane elevate '' -- get secret xxx
Please enter a reason for elevation, it will be stored in current context for 20 minutes : <here you can enter your reason>
```
or
```
$ ocm-backplane elevate -n -- get secret xxx
Please enter a reason for elevation, it will be stored in current context for 20 minutes : <here you can enter your reason>
```
If then you rerun an elevate command, for the same cluster, before the expiration delay, no prompt will be done and previous reason will be used for elevation.
### Run elevate without command
Expand All @@ -307,16 +313,16 @@ Please enter a reason for elevation, it will be stored in current context for 20
If a prompt is required but that stdin and/or stderr are redirected to file or output, then an error will be generated.
```
$ cat patch.json | ocm-backplane elevate '' -- patch -f -
$ cat patch.json | ocm-backplane elevate -n -- patch -f -
ERRO[0000] please enter a reason for elevation
$ ocm-backplane elevate '' -- get secret xxx 2> error.txt
$ ocm-backplane elevate -n -- get secret xxx 2> error.txt
ERRO[0000] please enter a reason for elevation
```
In order to avoid those errors, you can either run the the elevate without command before or provide a none empty reason.
No issue if only stdout is redirected.
```
$ ocm-backplane elevate '' -- get secret xxx | grep xxx
$ ocm-backplane elevate -n -- get secret xxx | grep xxx
Please enter a reason for elevation, it will be stored in current context for 20 minutes : <here you can enter your reason>
```
Expand Down
14 changes: 14 additions & 0 deletions cmd/ocm-backplane/elevate/elevate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/spf13/cobra"
)

var noReason bool
var ElevateCmd = &cobra.Command{
Use: "elevate [<REASON> [<COMMAND>]]",
Short: "Give a justification for elevating privileges to backplane-cluster-admin and attach it to your user object",
Expand All @@ -20,6 +21,19 @@ If no COMMAND (and eventualy also REASON) is/are provided then the command will
SilenceUsage: true,
}

func init() {
ElevateCmd.Flags().BoolVarP(
&noReason,
"no-reason",
"n",
false,
"Do not take reason as first argument, and prompt for it if needed and possible.",
)
}

func runElevate(cmd *cobra.Command, argv []string) error {
if noReason {
argv = append([]string{""}, argv...)
}
return elevate.RunElevate(argv)
}

0 comments on commit 9b8b250

Please sign in to comment.