-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add context locks to prevent certain kubectl commands
- Loading branch information
Showing
13 changed files
with
378 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package contextlock | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
func NewCommand() *cobra.Command { | ||
cmd := &cobra.Command{} | ||
|
||
cmd = &cobra.Command{ | ||
Use: "context-lock", | ||
Short: "Lock and unlock contexts", | ||
Long: `Lock and unlock contexts. | ||
This will allow you to lock and unlock contexts for the "kubert kubectl" command. This can be useful if you want to prevent accidentally running certain kubectl commands to a cluster. | ||
Keep in mind, this will only work when using kubectl through the "kubert kubectl" command. Direct commands using just "kubectl" will not be blocked. (If you use this feature, you could set an alias for "kubectl" or "k" to "kubert kubectl".) | ||
Both "lock" and "unlock" will set an explicit setting for the given context. That means if either of those has been set, kubert will ignore the default setting. If you want to use the default setting again, use "kubert context-lock delete <context>". | ||
What kubectl commands should be blocked can be configured in the kubert configuration file.`, | ||
} | ||
|
||
cmd.AddCommand(NewLockCommand()) | ||
cmd.AddCommand(NewUnlockCommand()) | ||
cmd.AddCommand(NewDeleteCommand()) | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package contextlock | ||
|
||
import ( | ||
"github.com/idebeijer/kubert/internal/kubert" | ||
"github.com/idebeijer/kubert/internal/state" | ||
"github.com/idebeijer/kubert/internal/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewDeleteCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "delete", | ||
Short: "Delete lock setting for the current context", | ||
Long: `Delete lock setting for the current context. | ||
This will delete the explicit lock/unlock setting for the current context. So if either "lock" or "unlock" was set, it will be removed and the default will be used.`, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return kubert.ShellPreFlightCheck() | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
sm, err := state.NewManager() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clientConfig, err := util.KubeClientConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := sm.DeleteContextLock(clientConfig.CurrentContext); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package contextlock | ||
|
||
import ( | ||
"github.com/idebeijer/kubert/internal/kubert" | ||
"github.com/idebeijer/kubert/internal/state" | ||
"github.com/idebeijer/kubert/internal/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewLockCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "lock", | ||
Short: "Lock current context", | ||
Long: `Lock current context. | ||
This will set an explicit "lock" for the current context. That means it wil override the default setting. If the current context should use the default again, use "kubert context-lock delete".`, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return kubert.ShellPreFlightCheck() | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
sm, err := state.NewManager() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clientConfig, err := util.KubeClientConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := sm.SetContextLock(clientConfig.CurrentContext, true); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package contextlock | ||
|
||
import ( | ||
"github.com/idebeijer/kubert/internal/kubert" | ||
"github.com/idebeijer/kubert/internal/state" | ||
"github.com/idebeijer/kubert/internal/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewUnlockCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "unlock", | ||
Short: "Unlock current context", | ||
Long: `Unlock current context. | ||
This will set an explicit "unlock" for the current context. That means it wil override the default setting. If the current context should use the default again, use "kubert context-lock delete".`, | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return kubert.ShellPreFlightCheck() | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
sm, err := state.NewManager() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
clientConfig, err := util.KubeClientConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := sm.SetContextLock(clientConfig.CurrentContext, false); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.