Skip to content

Commit

Permalink
remove unnecessary privileges (Redis)
Browse files Browse the repository at this point in the history
* pin Redis to v7.2 (BSD license)
* enable release asset functionality
  • Loading branch information
ab77 committed May 23, 2024
1 parent 007e4c9 commit 0734e1a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ restart: ## Restart all services
.PHONY: update
update: # Pull and deploy latest changes from git
@git pull
@(MAKE) up
@$(MAKE) up

.PHONY: destroy ## Stop and remove any existing containers and volumes
destroy:
Expand Down
10 changes: 5 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ services:
*with-default-privileges,
*with-default-volumes,
]
image: balena/open-balena-api:v22.0.0
image: balena/open-balena-api:v22.2.0
depends_on:
- db
- redis
Expand Down Expand Up @@ -110,6 +110,8 @@ services:
TRUST_PROXY: 172.16.0.0/12
VPN_PORT: 443
WEBRESOURCES_S3_BUCKET: web-resources
WEBRESOURCES_S3_REGION: "us-east-1" # this is required for minio
HIDE_UNVERSIONED_ENDPOINT: "false"

# https://github.com/balena-io/open-balena-registry
registry:
Expand Down Expand Up @@ -187,10 +189,8 @@ services:
# https://hub.docker.com/_/redis
redis:
<<: *base-service
image: redis:alpine
cap_add:
- SYS_RESOURCE
- SYS_ADMIN
# https://redis.io/blog/what-redis-license-change-means-for-our-managed-service-providers/
image: redis:7.2-alpine
volumes:
- redis-data:/data
healthcheck:
Expand Down
52 changes: 39 additions & 13 deletions src/balena-tests/balena.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function update_ca_certificates() {
if [[ -e "${CERTS}/ca-bundle.pem" ]]; then
if [[ "$(readlink -f "${CERTS}/${TLD}-chain.pem")" =~ \/private\/ ]]; then
mkdir -p /usr/local/share/ca-certificates
cat < "${CERTS}/ca-bundle.pem" > /usr/local/share/ca-certificates/balenaRootCA.crt
cat <"${CERTS}/ca-bundle.pem" > /usr/local/share/ca-certificates/balenaRootCA.crt
# shellcheck disable=SC2034
CURL_CA_BUNDLE=${CURL_CA_BUNDLE:-${CERTS}/ca-bundle.pem}
NODE_EXTRA_CA_CERTS=${NODE_EXTRA_CA_CERTS:-${CURL_CA_BUNDLE}}
Expand All @@ -62,7 +62,7 @@ function update_ca_certificates() {
# ... or
# https://thomas-leister.de/en/how-to-import-ca-root-certificate/
# https://github.com/puppeteer/puppeteer/issues/2377
ROOT_CA=${ROOT_CA:-$(cat < "${NODE_EXTRA_CA_CERTS}" | openssl base64 -A)}
ROOT_CA=${ROOT_CA:-$(cat <"${NODE_EXTRA_CA_CERTS}" | openssl base64 -A)}
else
rm -f /usr/local/share/ca-certificates/balenaRootCA.crt
unset NODE_EXTRA_CA_CERTS CURL_CA_BUNDLE ROOT_CA
Expand Down Expand Up @@ -140,7 +140,7 @@ function configure_virtual_device() {

function check_device_status() {
if [[ -e /balena/config.json ]]; then
balena_device_uuid="$(cat < /balena/config.json | jq -r .uuid)"
balena_device_uuid="$(cat </balena/config.json | jq -r .uuid)"

if [[ -n $balena_device_uuid ]]; then
is_online="$(balena devices --json --fleet "${TEST_FLEET}" \
Expand Down Expand Up @@ -185,21 +185,33 @@ function deploy_release() {
popd
}

function get_last_release() {
with_backoff balena releases "${TEST_FLEET}" \
| head -n 2 \
| tail -n 1 \
| grep -E '^.*\s+success\s+.*\s+true$' \
| awk '{print $2}'
function get_releases() {
with_backoff balena releases --json "${TEST_FLEET}"
}

function get_release_commit() {
echo "$(get_releases)" | jq -re \
'select((.[].status=="success")
and (.[].is_invalidated==false)
and (.[].is_final==true)
and (.[].release_type=="final"))[0].commit'
}

function get_release_id() {
echo "$(get_releases)" | jq -re \
'select((.[].status=="success")
and (.[].is_invalidated==false)
and (.[].is_final==true)
and (.[].release_type=="final"))[0].id'
}

function supervisor_update_target_state() {
balena_device_uuid="$(cat < /balena/config.json | jq -r .uuid)"
balena_device_uuid="$(cat </balena/config.json | jq -r .uuid)"

if [[ -n $balena_device_uuid ]]; then
while ! curl -X POST --silent --fail \
--header "Content-Type: application/json" \
--header "Authorization: Bearer $(cat ~/.balena/token)" \
--header "Authorization: Bearer $(cat <~/.balena/token)" \
--data "{\"uuid\": \"${balena_device_uuid}\", \"data\": {\"force\": true}}" \
"https://api.${DNS_TLD}/supervisor/v1/update"; do

Expand All @@ -209,7 +221,7 @@ function supervisor_update_target_state() {
}

function check_running_release() {
balena_device_uuid="$(cat < /balena/config.json | jq -r .uuid)"
balena_device_uuid="$(cat </balena/config.json | jq -r .uuid)"

if [[ -n $balena_device_uuid ]] && [[ -n $1 ]]; then
while ! [[ $(balena device "${balena_device_uuid}" | grep -E ^COMMIT | awk '{print $2}') =~ ${should_be_running_release_id} ]]; do
Expand All @@ -236,6 +248,17 @@ function get_os_version() {
echo "${os_version}"
}

function upload_release_asset() {
local release_id
release_id=${1:-}

curl --silent --retry ${{ env.RETRY }} --fail "https://api.$DNS_TLD/resin/release_asset" \
--header "Authorization: Bearer $(cat <~/.balena/token)" \
--form '[email protected]' \
--form "release=${release_id}" \
--form "asset_key=logo-$((RANDOM)).png" | jq -re .asset.href \
| xargs curl --retry ${{ env.RETRY }} -sfivo /tmp/logo-$((RANDOM)).png
}

# --- main
if [[ "$PRODUCTION_MODE" =~ true ]]; then
Expand Down Expand Up @@ -283,5 +306,8 @@ remove_update_lock
wait_for_device # spin here until test-device comes online

# .. and ensure the device is running our release
should_be_running_release_id="$(get_last_release)"
should_be_running_release_id="$(get_release_commit)"
check_running_release "${should_be_running_release_id}"

# finally see if we can upload an additional asset to a release
upload_release_asset "$(get_release_id)"

0 comments on commit 0734e1a

Please sign in to comment.