Skip to content

Commit

Permalink
🐛 fix(permissions): Change user permissions to map current user (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatelot authored May 16, 2023
1 parent 2fe6c30 commit eed1a23
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
log/mosquitto.log
config/mosquitto.conf
config/password.txt
config/password.txt
docker-compose.override.yml
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM eclipse-mosquitto:2

ARG UID
ARG GID
ARG USER

RUN if [ ${USER} != "root" ]; then \
apk add shadow && \
groupadd -f -g ${GID} ${USER} && \
useradd -m -g ${USER} -u ${UID} ${USER}; \
fi

RUN chown -R ${USER}:${USER} /mosquitto

USER ${USER}
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Executables: local only
DOCKER_COMP = docker compose
DOCKER_COMP_EXEC = $(DOCKER_COMP) exec mosquitto

# Misc
.DEFAULT_GOAL = help

help: ## Outputs this help screen
@grep -E '(^[a-zA-Z0-9_-]+:.*?##.*$$)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}{printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m##/[33m/'

alias: ## Displays all alias of the package manager
$(DOCKER_COMP_EXEC) npm run

## —— Docker 🐳 ————————————————————————————————————————————————————————————————
build: ## Builds the Node image
$(DOCKER_COMP) build

up: ## Starts the docker hub
$(DOCKER_COMP) up -d

down: ## Stops the docker hub
$(DOCKER_COMP) down --remove-orphans

restart: ## Restarts the docker hub
$(DOCKER_COMP) restart

sh: ## Connects to the application container
$(DOCKER_COMP_EXEC) sh

logs: ## Displays the logs of the application container
$(DOCKER_COMP) logs -f mosquitto

## —— Project 🐝 ———————————————————————————————————————————————————————————————
setup-project: ## Initializes configuration
bin/setup
make build
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ This is a simple [Mosquitto](https://mosquitto.org) broker to quickly initialize

## How to use

To start the container, just :
At first startup, you need to setup the project:

```bash
UID=$UID GID=$GID docker compose up -d
make setup-project
```

Then, and for other startup, you just have to run:

```bash
make up
```

> Find other `make` helper command in the [Makefile](./Makefile)
The Mosquitto broker is now available on localhost. You can test it easily (require Mosquitto client):

| In one shell:
Expand Down Expand Up @@ -48,13 +56,13 @@ In the config file, set the value `allow_anonymous` to `false`, then uncomment t
### Change user password / create a new user

```bash
docker-compose exec mosquitto mosquitto_passwd -b /mosquitto/config/password.txt user password
docker-compose restart
docker compose exec mosquitto mosquitto_passwd -b /mosquitto/config/password.txt user password
make restart
```

### Delete user

```bash
docker-compose exec mosquitto mosquitto_passwd -D /mosquitto/config/password.txt user
docker-compose restart
docker compose exec mosquitto mosquitto_passwd -D /mosquitto/config/password.txt user
make restart
```
20 changes: 20 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

projectRootPath=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )/..

if [ ! -f ${projectRootPath}/docker-compose.override.yml ]; then
cp ${projectRootPath}/docker-compose.override.yml.dist ${projectRootPath}/docker-compose.override.yml

sed -i'' -e "s/\[GID\]/$(id -g)/g" ${projectRootPath}/docker-compose.override.yml;
sed -i'' -e "s/\[UID\]/$(id -u)/g" ${projectRootPath}/docker-compose.override.yml;

username=$(id -un)

if [ "$username" != "root" ]; then
username="unicorn"
fi

sed -i'' -e "s/\[USER\]/${username}/g" ${projectRootPath}/docker-compose.override.yml;

rm -f docker-compose.override.yml-e # Hack for MAC OS
fi
8 changes: 8 additions & 0 deletions docker-compose.override.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
mosquitto:
build:
args:
GID: [GID]
UID: [UID]
USER: [USER]
user: [UID]:[GID]
7 changes: 2 additions & 5 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
---
version: '3.7'

services:
mosquitto:
image: eclipse-mosquitto:2
user: mosquitto
build:
context: .
volumes:
- type: bind
source: ./config/
Expand Down

0 comments on commit eed1a23

Please sign in to comment.