-
Notifications
You must be signed in to change notification settings - Fork 89
feat: add feature to prevent nested kubeconfig contexts #6555
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e2bf95b
feat: add feature to prevent nested kubeconfig contexts
agoose77 ec77474
refactor: split out deployer command
agoose77 6021af8
feat: also test for .kube/config
agoose77 1dc7575
feat: allow KUBECONFIG to be set but empty (more permissive)
agoose77 e01f81a
Merge branch 'main' into agoose77/feat-prohibit-nested-kubeconfig
agoose77 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,71 @@ | ||
| """ | ||
| Actions available provisioning credentials to deploy against Kubernetes clusters | ||
| """ | ||
|
|
||
| import os | ||
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| import typer | ||
| from ruamel.yaml import YAML | ||
|
|
||
| from deployer.cli_app import app | ||
| from deployer.commands.validate.config import cluster_config as validate_cluster_config | ||
| from deployer.infra_components.cluster import Cluster | ||
|
|
||
| # Without `pure=True`, I get an exception about str / byte issues | ||
| yaml = YAML(typ="safe", pure=True) | ||
|
|
||
|
|
||
| def ensure_single_kubeconfig_context(): | ||
| kubeconfig_path = Path.home() / ".kube" / "config" | ||
|
|
||
| if ( | ||
| # Do not allow non-empty KUBECONFIG | ||
| os.environ.get("KUBECONFIG") | ||
| or ( | ||
| # Do not allow non-empty kubectl config file | ||
| kubeconfig_path.exists() | ||
| and kubeconfig_path.stat().st_size > 0 | ||
| ) | ||
| ): | ||
| raise RuntimeError( | ||
| "Attempting to create a nested KUBECONFIG context, which has been explicitly forbidden by the presence of the DEPLOYER_NO_NESTED_KUBECONFIG environment variable." | ||
| ) | ||
|
|
||
|
|
||
| @app.command() | ||
| def use_cluster_credentials( | ||
| cluster_name: str = typer.Argument(..., help="Name of cluster to operate on"), | ||
| commandline: str = typer.Argument( | ||
| "", | ||
| help="Optional shell command line to run after authenticating to this cluster", | ||
| ), | ||
| ): | ||
| """ | ||
| Pop a new shell or execute a command after authenticating to the given cluster using the deployer's credentials | ||
| """ | ||
| if "DEPLOYER_NO_NESTED_KUBECONFIG" in os.environ: | ||
| ensure_single_kubeconfig_context() | ||
|
|
||
| # This function is to be used with the `use-cluster-credentials` CLI | ||
| # command only - it is not used by the rest of the deployer codebase. | ||
| validate_cluster_config(cluster_name) | ||
|
|
||
| cluster = Cluster.from_name(cluster_name) | ||
|
|
||
| # Cluster.auth() method has the context manager decorator so cannot call | ||
| # it like a normal function | ||
| with cluster.auth(): | ||
| # This command will spawn a new shell with all the env vars (including | ||
| # KUBECONFIG) inherited, and once you quit that shell the python program | ||
| # will resume as usual. | ||
| # TODO: Figure out how to change the PS1 env var of the spawned shell | ||
| # to change the prompt to f"cluster-{cluster.spec['name']}". This will | ||
| # make it visually clear that the user is now operating in a different | ||
| # shell. | ||
| args = [os.environ["SHELL"], "-l"] | ||
| # If a command to execute is specified, just execute that and exit. | ||
| if commandline: | ||
| args += ["-c", commandline] | ||
| subprocess.check_call(args) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.