Skip to content

Commit

Permalink
feat: add emqx-init
Browse files Browse the repository at this point in the history
  • Loading branch information
prehor committed Jun 21, 2024
1 parent e568df9 commit 4e92eda
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 0 deletions.
29 changes: 29 additions & 0 deletions apps/emqx-init/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
ARG VERSION
FROM docker.io/library/alpine:3.20

ARG TARGETPLATFORM
ARG VERSION
ARG CHANNEL

ENV CHARSET="UTF-8" \
LANG="en_US.UTF-8" \
UMASK="0002" \
TZ="Etc/UTC"

USER root

RUN \
apk add --no-cache \
bash \
ca-certificates \
catatonit \
curl

COPY ./apps/usbip/entrypoint.sh /entrypoint.sh

WORKDIR /config

ENTRYPOINT ["/usr/bin/catatonit", "--"]
CMD ["/entrypoint.sh"]

LABEL org.opencontainers.image.source="https://github.com/torvalds/linux/blob/master/tools/usb/usbip/README"
8 changes: 8 additions & 0 deletions apps/emqx-init/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# EMQX Init

EMQX Authentication:
* https://docs.emqx.com/en/emqx/latest/access-control/authn/authn.html
* https://github.com/emqx/emqx/discussions/9142#discussioncomment-3862923

EMQX Authorization:
* https://docs.emqx.com/en/emqx/latest/access-control/authz/authz.html
9 changes: 9 additions & 0 deletions apps/emqx-init/ci/goss.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
# yaml-language-server: $schema=https://raw.githubusercontent.com/goss-org/goss/master/docs/schema.yaml
file:
/entrypoint.sh:
exists: true
mode: "0755"
/usr/bbin/curl:
exists: true
mode: "0755"
3 changes: 3 additions & 0 deletions apps/emqx-init/ci/latest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
version="1.0.0"
printf "%s" "${version}"
59 changes: 59 additions & 0 deletions apps/emqx-init/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash

: "${INIT_EMQX_HOST:=localhost}"
: "${INIT_EMQX_PORT:=18083}"
: "${INIT_EMQX_SUPER_USER}"
: "${INIT_EMQX_SUPER_PASS}"
: "${INIT_EMQX_ACCESS_KEY}"
: "${INIT_EMQX_SECRET_KEY}"
: "${INIT_EMQX_IS_SUPERUSER:=false}"
: "${INIT_EMQX_TOPIC:=${INIT_EMQX_ACCESS_KEY}/*}"

INIT_EMQX_BASE_API_URL="http://${INIT_EMQX_SUPER_USER}:${INIT_EMQX_SUPER_PASS}@${INIT_EMQX_HOST}:${INIT_EMQX_PORT}/api/v5"
INIT_EMQX_AUTHN_API_URL="${INIT_EMQX_BASE_API_URL}/authentication/password_based:built_in_database/users"
INIT_EMQX_AUTHZ_API_URL="${INIT_EMQX_BASE_API_URL}/authorization/sources/built_in_database/username"

while ! curl -s "${INIT_EMQX_AUTHN_API_URL}"; do
echo 'Waiting for EMQX to start...'
sleep 5
done

echo 'EMQX started, ready to initialize...';

# Authentication
curl -s "${INIT_EMQX_AUTHN_API_URL}" \
-H 'Content-Type: application/json' \
-d "$(cat <<EOF
{
"user_id": "${INIT_EMQX_ACCESS_KEY}",
"password": "${INIT_EMQX_SECRET_KEY}",
"is_superuser": "${INIT_EMQX_IS_SUPERUSER}"
}
EOF
)"
echo 'EMQX access key created!'

# Superuser does not need authorization
if [ "${INIT_EMQX_IS_SUPERUSER}" -eq "true" ]; then
return
fi

# Authorization
curl -s "${INIT_EMQX_AUTHZ_API_URL}" \
-H 'Content-Type: application/json' \
-d "$(cat <<EOF
[
{
"username": "${INIT_EMQX_ACCESS_KEY}",
"rules": [
{
"action": "all",
"permission": "allow",
"topic": "${INIT_EMQX_TOPIC}"
}
]
}
]
EOF
)"
echo 'EMQX authorization created!'
10 changes: 10 additions & 0 deletions apps/emqx-init/metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
app: emqx-init
semver: true
channels:
- name: stable
platforms: ["linux/amd64"]
stable: true
tests:
enabled: true
type: cli

0 comments on commit 4e92eda

Please sign in to comment.