Skip to content

Commit

Permalink
Merge pull request #487 from mplsgrant/update-auth-command
Browse files Browse the repository at this point in the history
update auth command
  • Loading branch information
mplsgrant authored Aug 23, 2024
2 parents 6850b47 + a6fc4e1 commit ddc6f09
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
43 changes: 34 additions & 9 deletions src/warnet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,37 +136,62 @@ def init():
@click.argument("kube_config", type=str)
def auth(kube_config: str) -> None:
"""
Authorize access to a warnet cluster using a kube config file
Authenticate with a warnet cluster using a kube config file
"""
try:
current_kubeconfig = os.environ.get("KUBECONFIG", os.path.expanduser("~/.kube/config"))
combined_kubeconfig = (
f"{current_kubeconfig}:{kube_config}" if current_kubeconfig else kube_config
)
os.environ["KUBECONFIG"] = combined_kubeconfig
command = "kubectl config view --flatten"
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
with open(kube_config) as file:
content = yaml.safe_load(file)
for elem in content:
print(elem)
content["clusters"][0]
user = content["users"][0]
user_name = user["name"]
user_token = user["user"]["token"]
content["contexts"][0]
flatten_cmd = "kubectl config view --flatten"
result_flatten = subprocess.run(
flatten_cmd, shell=True, check=True, capture_output=True, text=True
)
except subprocess.CalledProcessError as e:
print("Error occurred while executing kubectl config view --flatten:")
print(e.stderr)
sys.exit(1)

if result.returncode == 0:
if result_flatten.returncode == 0:
with open(current_kubeconfig, "w") as file:
file.write(result.stdout)
file.write(result_flatten.stdout)
print(f"Authorization file written to: {current_kubeconfig}")
else:
print("Could not create authorization file")
print(result.stderr)
sys.exit(result.returncode)
print(result_flatten.stderr)
sys.exit(result_flatten.returncode)

try:
update_cmd = f"kubectl config set-credentials {user_name} --token {user_token}"
result_update = subprocess.run(
update_cmd, shell=True, check=True, capture_output=True, text=True
)
if result_update.returncode != 0:
print("Could not update authorization file")
print(result_flatten.stderr)
sys.exit(result_flatten.returncode)
except subprocess.CalledProcessError as e:
print("Error occurred while executing kubectl config view --flatten:")
print(e.stderr)
sys.exit(1)

with open(current_kubeconfig) as file:
contents = yaml.safe_load(file)
print("\nUse the following command to switch to a new user:")
print(" kubectl config use-context [user]\n")
print("Available users:")
for context in contents["contexts"]:
print(f" {context['name']}")
for c in contents["contexts"]:
print(f" {c['name']}")


if __name__ == "__main__":
Expand Down
9 changes: 8 additions & 1 deletion src/warnet/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,20 @@ def should_copy(item: Path) -> bool:

print(f"Finished copying files to {target_dir}")


def copy_network_defaults(directory: Path):
"""Create the project structure for a warnet project's network"""
copy_defaults(directory, WAR_NETWORK_DIR, WAR_NETWORK_FILES.joinpath(), [])


def copy_scenario_defaults(directory: Path):
"""Create the project structure for a warnet project's scenarios"""
copy_defaults(directory, WAR_SCENARIOS_DIR, WAR_SCENARIOS_FILES.joinpath(), ["__init__.py", "__pycache__", "commander.py"])
copy_defaults(
directory,
WAR_SCENARIOS_DIR,
WAR_SCENARIOS_FILES.joinpath(),
["__init__.py", "__pycache__", "commander.py"],
)


@network.command()
Expand Down

0 comments on commit ddc6f09

Please sign in to comment.