Skip to content

Commit

Permalink
chore: minor improvements (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
shini4i authored Feb 17, 2023
1 parent beb8f5f commit 805907b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Vadim Gedz
Copyright (c) 2022-2023 Vadim Gedz

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
48 changes: 38 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,57 @@
# kd
Kubernetes secrets Decoder

![Version](https://img.shields.io/github/v/tag/shini4i/kd?style=plastic)
![license](https://img.shields.io/github/license/shini4i/kd?style=plastic)
![Shell](https://img.shields.io/badge/shell-bash-green)
![Version](https://img.shields.io/github/v/tag/shini4i/kd)
![license](https://img.shields.io/github/license/shini4i/kd)

<img src="https://raw.githubusercontent.com/shini4i/assets/main/src/kd/demo.png" alt="Showcase" height="441" width="620">
</div>

<b>kd</b> is yet another attempt to create a super simple and easy to use cli tool
that will help with kubernetes secrets decoding.
`kd` is a bash script that decodes Kubernetes secrets. It makes it easy to view the contents of secrets stored in a Kubernetes cluster.

There are alternatives to this script, but they are either not maintained or require way too many dependencies.

## Requirements
- kubectl
- yq
## Prerequisites

Before you can use `kd`, you need to have the following installed:

- `kubectl`: the Kubernetes command-line tool
- `yq`: a YAML parser and processor

## Installation
The script can be installed using brew:

### Option 1: Install with Homebrew (macOS and Linux)

If you're using macOS or Linux, you can install `kd` using Homebrew. To do so, run the following command:

```bash
brew install shini4i/tap/kd
```

### Option 2: Install manually

To install `kd` manually, download the `scr/kd.sh` script and add it to your PATH. You can do this by running the following commands:

```bash
curl https://raw.githubusercontent.com/shini4i/kd/main/src/kd.sh -o kd
chmod +x kd
sudo mv kd /usr/local/bin/
````

## Usage
Here are some examples of how to use kd:

```bash
kd <secret-name> <namespace>
# Decode a secret in the current namespace
kd my-secret
# Decode a secret in a specific namespace
kd my-secret my-namespace
```
If the namespace is omitted, the currently selected namespace will be used.

## Contributing

Contributions to `kd` are welcome! Please open an issue or a pull request if you find a bug or want to submit a patch.

New feature requests are not expected, but suggestions for improving the existing functionality or fixing bugs are appreciated.
38 changes: 21 additions & 17 deletions src/kd.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
#!/usr/bin/env bash

print_help() {
echo "Usage: $(basename "$0") <secret_name> <namespace>"
echo " <secret_name> Name of the secret to decode"
echo " <namespace> Namespace of the secret (optional)"
cat << EOF
Usage: $(basename "$0") <secret_name> <namespace>
<secret_name> Name of the secret to decode
<namespace> Namespace of the secret (optional)
Examples:
$(basename "$0") my-secret # Decode the 'my-secret' secret in the current namespace
$(basename "$0") my-secret my-ns # Decode the 'my-secret' secret in the 'my-ns' namespace
EOF
}

parse_args() {
secret_name=$1

if [ $# -eq 2 ]; then
namespace=$2
set_namespace() {
if [ $# -eq 1 ]; then
namespace=$1
else
namespace=$(kubectl config view --minify -o jsonpath='{..namespace}')
namespace=$(kubectl config view --minify -o jsonpath='{..namespace}') || { echo "Error: Unable to get current namespace"; exit 1; }
echo "No namespace specified, using currently selected namespace: $namespace"
fi
}

main() {
local secret
local secret_name=$1
local secret_content

if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ $# -lt 1 ]; then
print_help
exit 0
fi
case $secret_name in
-h|--help|"") print_help; exit 0;;
esac

parse_args "$@"
set_namespace "$2"

secret=$(kubectl get secret "$secret_name" -n "$namespace" -o yaml)
echo "$secret" | yq e '.data | to_entries | .[] | .key + ": " + (.value | @base64d)'
secret_content=$(kubectl get secret "$secret_name" -n "$namespace" -o yaml) || { echo "Error: Unable to get secret $secret_name in namespace $namespace"; exit 1; }
echo "$secret_content" | yq e '.data | to_entries | .[] | .key + ": " + (.value | @base64d)'
}

main "$@"

0 comments on commit 805907b

Please sign in to comment.