Skip to content
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

Add remote files #54

Merged
merged 1 commit into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
uses: luizm/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SHELLCHECK_OPTS: -x
with:
sh_checker_comment: true
sh_checker_exclude: "tests"
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ Allow override sops version on installation

## [Unreleased]

From this version, the installation on Helm 2 requires additional steps.
Check [README.md](README.md#installation-on-helm-2)

### Added
- Implement alternate syntax (https://github.com/jkroepke/helm-secrets/pull/52)
- Remote values support (supporting http:// and helm downloader plugins) (https://github.com/jkroepke/helm-secrets/pull/54)

## [3.3.5] - 2020-10-16

Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,27 @@ curl -LsSf https://github.com/jkroepke/helm-secrets/releases/download/v3.3.4/hel
curl -LsSf https://github.com/jkroepke/helm-secrets/releases/download/v3.3.4/helm-secrets.tar.gz | tar -C "$HOME/.local/share/helm/plugins" -xzf-
```

### Installation on Helm 2
Helm 2 doesn't support downloader plugins. Since unknown keys in `plugin.yaml` are fatal, then plugin installation need special handling.

Error on Helm 2 installation:
```
# helm plugin install https://github.com/jkroepke/helm-secrets
Error: yaml: unmarshal errors:
line 12: field platformCommand not found in type plugin.Metadata
```

Workaround:

1. Install helm-secrets via [manual installation](README.md#manual-installation)
2. Strip `platformCommand` from `plugin.yaml`:
```
sed -i '/platformCommand:/,+2 d' "${HELM_HOME:-"${HOME}/.helm"}/plugins/helm-secrets*/plugin.yaml"
```
3. Done

Client [here](https://github.com/adorsys-containers/ci-helm/blob/f9a8a5bf8953ab876266ca39ccbdb49228e9f117/images/2.17/Dockerfile#L91) for an example!

## Change secret driver

It's possible to use another secret driver then sops, e.g. Hasicorp Vault.
Expand Down
23 changes: 9 additions & 14 deletions scripts/commands/dec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ EOF
}

decrypt_helper() {
file="${1}"
encrypted_file="${1}"

if [ ! -f "$file" ]; then
printf 'File does not exist: %s\n' "${file}"
if ! encrypted_file_path=$(_file_get "${encrypted_file}"); then
printf '[helm-secrets] File does not exist: %s\n' "${encrypted_file}"
exit 1
fi

if ! driver_is_file_encrypted "${file}"; then
if ! driver_is_file_encrypted "${encrypted_file_path}"; then
return 1
fi

file_dec="$(file_dec_name "${file}")"
encrypted_file_dec="$(_file_dec_name "${encrypted_file_path}")"

if ! driver_decrypt_file "yaml" "${file}" "${file_dec}"; then
printf 'Error while decrypting file: %s\n' "${file}"
if ! driver_decrypt_file "yaml" "${encrypted_file_path}" "${encrypted_file_dec}"; then
printf '[helm-secrets] Error while decrypting file: %s\n' "${file}"
exit 1
fi

Expand All @@ -50,11 +50,6 @@ dec() {

file="$1"

if [ ! -f "${file}" ]; then
printf 'File does not exist: %s\n' "${file}"
exit 1
else
printf 'Decrypting %s\n' "${file}"
decrypt_helper "${file}"
fi
printf '[helm-secrets] Decrypting %s\n' "${file}"
decrypt_helper "${file}"
}
2 changes: 1 addition & 1 deletion scripts/commands/enc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ encrypt_helper() {
printf 'File does not exist: %s\n' "${dir}/${file}"
exit 1
fi
file_dec="$(file_dec_name "${file}")"
file_dec="$(_file_dec_name "${file}")"

if [ ! -f "${file_dec}" ]; then
file_dec="${file}"
Expand Down
22 changes: 12 additions & 10 deletions scripts/commands/helm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ Typical usage:
EOF
}

helm_wrapper_cleanup() {
decrypted_files=$(mktemp)

_trap_hook() {
if [ -s "${decrypted_files}" ]; then
if [ "${QUIET}" = "false" ]; then
echo >&2
Expand All @@ -33,20 +35,15 @@ helm_wrapper_cleanup() {
else
xargs -0 rm >&2 <"${decrypted_files}"
fi
fi

rm "${decrypted_files}"
rm "${decrypted_files}"
fi
}

helm_wrapper() {
decrypted_files=$(mktemp)

argc=$#
j=0

#cleanup on-the-fly decrypted files
trap helm_wrapper_cleanup EXIT

while [ $j -lt $argc ]; do
case "$1" in
--)
Expand All @@ -71,15 +68,20 @@ helm_wrapper() {
;;
esac

file_dec="$(file_dec_name "${file}")"
if ! real_file=$(_file_get "${file}"); then
printf '[helm-secrets] File does not exist: %s\n' "${file}"
exit 1
fi

file_dec="$(_file_dec_name "${real_file}")"
if [ -f "${file_dec}" ]; then
set -- "$@" "$file_dec"

if [ "${QUIET}" = "false" ]; then
printf '[helm-secrets] Decrypt skipped: %s' "${file}" >&2
fi
else
if decrypt_helper "${file}"; then
if decrypt_helper "${real_file}"; then
set -- "$@" "$file_dec"
printf '%s\0' "${file_dec}" >>"${decrypted_files}"

Expand Down
6 changes: 4 additions & 2 deletions scripts/commands/view.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ EOF
view_helper() {
file="$1"

if [ ! -f "${file}" ]; then
if ! _file_exists "$file"; then
printf 'File does not exist: %s\n' "${file}"
exit 1
fi

driver_decrypt_file "yaml" "${file}"
real_file=$(_file_get "${file}")

driver_decrypt_file "yaml" "${real_file}"
}

view() {
Expand Down
16 changes: 6 additions & 10 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

set -eu

# Path to current directory
SCRIPT_DIR="$(dirname "$0")"

# shellcheck source=scripts/lib/http.sh
. "${SCRIPT_DIR}/lib/http.sh"

SOPS_DEFAULT_VERSION="v3.6.1"
SOPS_VERSION="${SOPS_VERSION:-$SOPS_DEFAULT_VERSION}"
SOPS_LINUX_URL="${SOPS_LINUX_URL:-"https://github.com/mozilla/sops/releases/download/${SOPS_VERSION}/sops-${SOPS_VERSION}.linux"}"
Expand All @@ -13,16 +19,6 @@ RED='\033[0;31m'
#YELLOW='\033[1;33m'
NOC='\033[0m'

download() {
if command -v curl >/dev/null; then
curl -sSfL "$1"
elif command -v wget >/dev/null; then
wget -q -O- "$1"
else
return 1
fi
}

get_sha_256() {
if command -v sha256sum >/dev/null; then
res=$(sha256sum "$1")
Expand Down
50 changes: 50 additions & 0 deletions scripts/lib/file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env sh

# shellcheck source=scripts/lib/file/local.sh
. "${SCRIPT_DIR}/lib/file/local.sh"

# shellcheck source=scripts/lib/file/http.sh
. "${SCRIPT_DIR}/lib/file/http.sh"

# shellcheck source=scripts/lib/file/custom.sh
. "${SCRIPT_DIR}/lib/file/custom.sh"

_file_get_protocol() {
case "$1" in
http*)
echo "http"
;;
*://*)
echo "custom"
;;
*)
echo "local"
;;
esac
}

_file_exists() {
file_type=$(_file_get_protocol "${1}")

_file_"${file_type}"_exists "$@"
}

_file_get() {
file_type=$(_file_get_protocol "${1}")

_file_"${file_type}"_get "$@"
}

_file_put() {
file_type=$(_file_get_protocol "${1}")

_file_"${file_type}"_put "$@"
}

_file_dec_name() {
if [ "${DEC_DIR}" != "" ]; then
printf '%s' "${DEC_DIR}/$(basename "${1}" ".yaml")${DEC_SUFFIX}"
else
printf '%s' "$(dirname "${1}")/$(basename "${1}" ".yaml")${DEC_SUFFIX}"
fi
}
16 changes: 16 additions & 0 deletions scripts/lib/file/custom.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env sh

_file_custom_exists() {
_file_custom_get "$@" >/dev/null
}

_file_custom_get() {
_tmp_file=$(mktemp)
helm template "${SCRIPT_DIR}/lib/file/helm-values-getter" -f "${1}" >"${_tmp_file}"
printf '%s' "${_tmp_file}"
}

_file_custom_put() {
echo "Can't write to remote files!"
exit 1
}
3 changes: 3 additions & 0 deletions scripts/lib/file/helm-values-getter/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apiVersion: v2
name: helm-values-getter
version: 1.0.0
1 change: 1 addition & 0 deletions scripts/lib/file/helm-values-getter/templates/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{- .Values | toYaml -}}
Empty file.
16 changes: 16 additions & 0 deletions scripts/lib/file/http.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env sh

_file_http_exists() {
_file_http_get "$@" >/dev/null
}

_file_http_get() {
_tmp_file=$(mktemp)
download "${1}" >"${_tmp_file}"
printf '%s' "${_tmp_file}"
}

_file_http_put() {
echo "Can't write to remote files!"
exit 1
}
15 changes: 15 additions & 0 deletions scripts/lib/file/local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env sh

set -eu

_file_local_exists() {
test -f "${1}"
}

_file_local_get() {
_file_local_exists "$@" && printf '%s' "${1}"
}

_file_local_put() {
cat - >"${1}"
}
11 changes: 11 additions & 0 deletions scripts/lib/http.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env sh

download() {
if command -v curl >/dev/null; then
curl -sSfL "$1"
elif command -v wget >/dev/null; then
wget -q -O- "$1"
else
return 1
fi
}
Loading