Skip to content

Commit

Permalink
✨ Adds support for secrets.yaml (#25)
Browse files Browse the repository at this point in the history
* ✨ Adds support for secrets.yaml

* ✨ Detect add-ons that don't support secrets.yaml
  • Loading branch information
frenck authored Aug 8, 2018
1 parent 2b41497 commit 8c1efbc
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 5 deletions.
5 changes: 4 additions & 1 deletion base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ ENV \
# Copy root filesystem
COPY rootfs /

# Copy yq
ARG BUILD_ARCH=amd64
COPY bin/yq_${BUILD_ARCH} /usr/bin/yq

# Set shell
SHELL ["/bin/ash", "-o", "pipefail", "-c"]

# Install base system
ARG BUILD_ARCH=amd64
RUN \
set -o pipefail \
\
Expand Down
Binary file added base/bin/yq_aarch64
Binary file not shown.
Binary file added base/bin/yq_amd64
Binary file not shown.
Binary file added base/bin/yq_armhf
Binary file not shown.
Binary file added base/bin/yq_i386
Binary file not shown.
4 changes: 2 additions & 2 deletions base/rootfs/usr/lib/hassio-addons/modules/api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ hass.api.call() {

hass.log.debug "Requested API resource: ${HASS_API_ENDPOINT}${resource}"
hass.log.debug "API HTTP Response code: ${status}"
hass.log.debug "API Response: ${response}"
hass.log.debug "API Response: ${response}"

if [[ "${status}" -eq 401 ]]; then
hass.log.error "Unable to authenticate with the API, permission denied"
return "${EX_NOK}"
Expand Down
74 changes: 72 additions & 2 deletions base/rootfs/usr/lib/hassio-addons/modules/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ hass.config.get() {
return "${EX_OK}"
fi

if hass.config.is_secret "${key}"; then
hass.config.get_secret "${key}"
return "${EX_OK}"
fi

if hass.jq.is_string "${ADDON_CONFIG_PATH}" ".${key}"; then
hass.jq "${ADDON_CONFIG_PATH}" ".${key} // empty"
return "${EX_OK}"
Expand All @@ -53,15 +58,55 @@ hass.config.get() {
fi
return "${EX_OK}"
fi

if hass.jq.is_number "${ADDON_CONFIG_PATH}" ".${key}"; then
hass.jq "${ADDON_CONFIG_PATH}" ".${key}"
return "${EX_OK}"
fi

return "${EX_NOK}"
}

# ------------------------------------------------------------------------------
# Gets a configuration option value by getting it from secrets.yaml
#
# Arguments:
# $1 Key of the config option
# Returns:
# Value of the key in the referenced to the secrets file
# ------------------------------------------------------------------------------
hass.config.get_secret() {
local key=${1}
local secret
local value

hass.log.trace "${FUNCNAME[0]}:" "$@"

if ! hass.directory_exists "/config"; then
hass.die "This add-on does not support secrets!"
fi

if ! hass.file_exists "/config/secrets.yaml"; then
hass.die "A secret was requested, but could not find a secrets.yaml"
fi

if ! hass.config.is_secret "${key}"; then
hass.die "The requested secret does not reference the secrets.yaml"
fi

secret=$(hass.jq "${ADDON_CONFIG_PATH}" ".${key} // empty")
secret="${secret#'!secret '}"

value=$(yq read "/config/secrets.yaml" "${secret}" )

if [[ "${value}" = "null" ]]; then
hass.die "Secret ${secret} not found in secrets.yaml file."
fi

echo "${value}"
return "${EX_OK}"
}

# ------------------------------------------------------------------------------
# Checks if a configuration option exists in the config file
#
Expand Down Expand Up @@ -145,3 +190,28 @@ hass.config.false() {

return "${EX_NOK}"
}

# ------------------------------------------------------------------------------
# Checks if a configuration option is refering to a secret
#
# Arguments:
# $1 Key of the config option
# Returns:
# None
# ------------------------------------------------------------------------------
hass.config.is_secret() {
local key=${1}
hass.log.trace "${FUNCNAME[0]}:" "$@"

if ! hass.jq.is_string "${ADDON_CONFIG_PATH}" ".${key}"; then
return "${EX_NOK}"
fi

if [[
"$(hass.jq "${ADDON_CONFIG_PATH}" ".${key} // empty")" != '!secret '*
]]; then
return "${EX_NOK}"
fi

return "${EX_OK}"
}

0 comments on commit 8c1efbc

Please sign in to comment.