Skip to content

Commit 5743352

Browse files
jason-foxdcalvoalonso
authored andcommitted
feat(docker): add docker secrets
* Fix #58 - Add Docker Secrets * Remove colon
1 parent 02b4383 commit 5743352

5 files changed

Lines changed: 210 additions & 9 deletions

File tree

config.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,68 @@ config.iota = {
6464
port: 4061
6565
},
6666

67+
/**
68+
* Configuration for secured access to instances of the Context Broker secured with a PEP Proxy.
69+
* For the authentication mechanism to work, the authentication attribute in the configuration has to be fully
70+
* configured, and the authentication.enabled subattribute should have the value `true`.
71+
*
72+
* The Username and password should be considered as sensitive data and should not be stored in plaintext.
73+
* Either encrypt the config and decrypt when initializing the instance or use environment variables secured by
74+
* docker secrets.
75+
*/
76+
//authentication: {
77+
//enabled: false,
78+
/**
79+
* Type of the Identity Manager which is used when authenticating the IoT Agent.
80+
* Either 'oauth2' or 'keystone'
81+
*/
82+
//type: 'keystone',
83+
/**
84+
* Name of the additional header passed to retrieve the identity of the IoT Agent
85+
*/
86+
//header: 'Authorization',
87+
/**
88+
* Hostname of the Identity Manager.
89+
*/
90+
//host: 'localhost',
91+
/**
92+
* Port of the Identity Manager.
93+
*/
94+
//port: '5000',
95+
/**
96+
* URL of the Identity Manager - a combination of the above
97+
*/
98+
//url: 'localhost:5000',
99+
/**
100+
* KEYSTONE ONLY: Username for the IoT Agent
101+
* - Note this should not be stored in plaintext.
102+
*/
103+
//user: 'IOTA_AUTH_USER',
104+
/**
105+
* KEYSTONE ONLY: Password for the IoT Agent
106+
* - Note this should not be stored in plaintext.
107+
*/
108+
//password: 'IOTA_AUTH_PASSWORD',
109+
/**
110+
* OAUTH2 ONLY: URL path for retrieving the token
111+
*/
112+
//tokenPath: '/oauth2/token',
113+
/**
114+
* OAUTH2 ONLY: Flag to indicate whether or not the token needs to be periodically refreshed.
115+
*/
116+
//permanentToken: true,
117+
/**
118+
* OAUTH2 ONLY: ClientId for the IoT Agent
119+
* - Note this should not be stored in plaintext.
120+
*/
121+
//clientId: 'IOTA_AUTH_CLIENT_ID',
122+
/**
123+
* OAUTH2 ONLY: ClientSecret for the IoT Agent
124+
* - Note this should not be stored in plaintext.
125+
*/
126+
//clientSecret: 'IOTA_AUTH_CLIENT_SECRET'
127+
//},
128+
67129
/**
68130
* Default resource of the IoT Agent. This value must be different for every IoT Agent connecting to the IoT
69131
* Manager.

docker/Dockerfile

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,28 @@
1+
#
2+
# Copyright 2019 Atos Spain S.A
3+
#
4+
# This file is part of iotagent-lora
5+
#
6+
# iotagent-lora is free software: you can redistribute it and/or
7+
# modify it under the terms of the GNU Affero General Public License as
8+
# published by the Free Software Foundation, either version 3 of the License,
9+
# or (at your option) any later version.
10+
#
11+
# iotagent-lora is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
# See the GNU Affero General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Affero General Public
17+
# License along with iotagent-lora. If not, see http://www.gnu.org/licenses/.
18+
#
19+
120
ARG NODE_VERSION=8.15.0-slim
221
FROM node:${NODE_VERSION}
322
ARG GITHUB_ACCOUNT=Atos-Research-and-Innovation
423
ARG GITHUB_REPOSITORY=IoTagent-LoRaWAN
524
ARG DOWNLOAD=latest
25+
ARG SOURCE_BRANCH=master
626

727
# Copying Build time arguments to environment variables so they are persisted at run time and can be
828
# inspected within a running container.
@@ -14,6 +34,12 @@ ENV DOWNLOAD=${DOWNLOAD}
1434

1535
MAINTAINER FIWARE IoTAgent Team. Atos Spain S.A
1636

37+
# IMPORTANT: For production environments use Docker Secrets to protect values of the sensitive ENV
38+
# variables defined below, by adding _FILE to the name of the relevant variable.
39+
#
40+
# - IOTA_AUTH_USER, IOTA_AUTH_PASSWORD - when using Keystone Security
41+
# - IOTA_AUTH_CLIENT_ID, IOTA_AUTH_CLIENT_SECRET - when using OAuth2 Security
42+
1743
#
1844
# The following RUN command retrieves the source code from GitHub.
1945
#
@@ -31,8 +57,8 @@ MAINTAINER FIWARE IoTAgent Team. Atos Spain S.A
3157
#
3258
RUN if [ "${DOWNLOAD}" = "latest" ] ; \
3359
then \
34-
RELEASE="master"; \
35-
echo "INFO: Building Latest Development"; \
60+
RELEASE="${SOURCE_BRANCH}"; \
61+
echo "INFO: Building Latest Development from ${SOURCE_BRANCH} branch."; \
3662
elif [ "${DOWNLOAD}" = "stable" ]; \
3763
then \
3864
RELEASE=$(curl -s https://api.github.com/repos/"${GITHUB_ACCOUNT}"/"${GITHUB_REPOSITORY}"/releases/latest | grep 'tag_name' | cut -d\" -f4); \
@@ -41,13 +67,14 @@ RUN if [ "${DOWNLOAD}" = "latest" ] ; \
4167
RELEASE="${DOWNLOAD}"; \
4268
echo "INFO: Building Release: ${RELEASE}"; \
4369
fi && \
70+
RELEASE_CONCAT=$(echo "${RELEASE}" | tr / -); \
4471
# Ensure that unzip is installed, and download the sources
4572
apt-get update && \
4673
apt-get install -y --no-install-recommends unzip && \
4774
wget --no-check-certificate -O source.zip https://github.com/"${GITHUB_ACCOUNT}"/"${GITHUB_REPOSITORY}"/archive/"${RELEASE}".zip && \
4875
unzip source.zip && \
4976
rm source.zip && \
50-
mv "${GITHUB_REPOSITORY}-${RELEASE}" /opt/iotagent-lora && \
77+
mv "${GITHUB_REPOSITORY}-${RELEASE_CONCAT}" /opt/iotagent-lora && \
5178
# Remove unzip and clean apt cache
5279
apt-get clean && \
5380
apt-get remove -y unzip && \
@@ -65,13 +92,14 @@ RUN \
6592
# Remove Git and clean apt cache
6693
apt-get clean && \
6794
apt-get remove -y git && \
68-
apt-get -y autoremove
95+
apt-get -y autoremove && \
96+
chmod +x docker/entrypoint.sh
6997

7098
USER node
7199
ENV NODE_ENV=production
72100

73101
# Expose 4041 for NORTH PORT
74102
EXPOSE ${IOTA_NORTH_PORT:-4041}
75103

76-
ENTRYPOINT ["pm2-runtime", "bin/iotagent-lora"]
104+
ENTRYPOINT ["docker/entrypoint.sh"]
77105
CMD ["-- ", "config.js"]

docker/README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Further settings for the IoT Agent for the LoRaWaN Protocol itself - such as spe
9999
Protocol - can be found in the IoT Agent for the LoRaWaN Protocol
100100
[Users Guide](https://fiware-lorawan.readthedocs.io/en/latest/users_manual/index.html#configuration-provisioning).
101101

102-
## How to build your own image
102+
## How to build an image
103103

104104
The [Dockerfile](https://github.com/Atos-Research-and-Innovation/IoTagent-LoRaWAN/blob/master/docker/Dockerfile)
105105
associated with this image can be used to build an image in several ways:
@@ -124,13 +124,20 @@ docker build -t iot-agent . --build-arg DOWNLOAD=stable
124124
docker build -t iot-agent . --build-arg DOWNLOAD=1.7.0
125125
```
126126

127-
- To download code from your own fork of the GitHub repository add the `GITHUB_ACCOUNT` and `GITHUB_REPOSITORY`
128-
arguments to the `docker build` command.
127+
## Building from your own fork
128+
129+
To download code from your own fork of the GitHub repository add the `GITHUB_ACCOUNT`, `GITHUB_REPOSITORY` and
130+
`SOURCE_BRANCH` arguments (default `master`) to the `docker build` command.
129131

130132
```console
131-
docker build -t iot-agent . --build-arg GITHUB_ACCOUNT=<your account> --build-arg GITHUB_REPOSITORY=<your repo>
133+
docker build -t iot-agent . \
134+
--build-arg GITHUB_ACCOUNT=<your account> \
135+
--build-arg GITHUB_REPOSITORY=<your repo> \
136+
--build-arg SOURCE_BRANCH=<your branch>
132137
```
133138

139+
## Building from your own source files
140+
134141
Alternatively, if you want to build directly from your own sources, please copy the existing `Dockerfile` into file the
135142
root of the repository and amend it to copy over your local source using :
136143

@@ -139,3 +146,21 @@ COPY . /opt/iotagent-lora/
139146
```
140147

141148
Full instructions can be found within the `Dockerfile` itself.
149+
150+
### Docker Secrets
151+
152+
As an alternative to passing sensitive information via environment variables, `_FILE` may be appended to some sensitive
153+
environment variables, causing the initialization script to load the values for those variables from files present in
154+
the container. In particular, this can be used to load passwords from Docker secrets stored in
155+
`/run/secrets/<secret_name>` files. For example:
156+
157+
```console
158+
docker run --name iotagent -e IOTA_AUTH_PASSWORD_FILE=/run/secrets/password -d fiware/iotagent-lorawan
159+
```
160+
161+
Currently, this `_FILE` suffix is supported for:
162+
163+
- `IOTA_AUTH_USER`
164+
- `IOTA_AUTH_PASSWORD`
165+
- `IOTA_AUTH_CLIENT_ID`
166+
- `IOTA_AUTH_CLIENT_SECRET`

docker/entrypoint.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2019 Atos Spain S.A
4+
#
5+
# This file is part of iotagent-lora
6+
#
7+
# iotagent-lora is free software: you can redistribute it and/or
8+
# modify it under the terms of the GNU Affero General Public License as
9+
# published by the Free Software Foundation, either version 3 of the License,
10+
# or (at your option) any later version.
11+
#
12+
# iotagent-lora is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15+
# See the GNU Affero General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Affero General Public
18+
# License along with iotagent-lora. If not, see http://www.gnu.org/licenses/.
19+
#
20+
21+
# usage: file_env VAR [DEFAULT]
22+
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
23+
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
24+
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
25+
file_env() {
26+
local var="$1"
27+
local fileVar="${var}_FILE"
28+
local def="${2:-}"
29+
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
30+
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
31+
exit 1
32+
fi
33+
local val="$def"
34+
if [ "${!var:-}" ]; then
35+
val="${!var}"
36+
elif [ "${!fileVar:-}" ]; then
37+
val="$(< "${!fileVar}")"
38+
fi
39+
export "$var"="$val"
40+
unset "$fileVar"
41+
}
42+
43+
file_env 'IOTA_AUTH_USER'
44+
file_env 'IOTA_AUTH_PASSWORD'
45+
file_env 'IOTA_AUTH_CLIENT_ID'
46+
file_env 'IOTA_AUTH_CLIENT_SECRET'
47+
48+
49+
if [[ -z "$IOTA_AUTH_ENABLED" ]]; then
50+
echo "***********************************************"
51+
echo "WARNING: It is recommended to enable authentication for secure connection"
52+
echo "***********************************************"
53+
else
54+
if [[ -z "$IOTA_AUTH_USER" ]] || [ -z "$IOTA_AUTH_PASSWORD" ]]; then
55+
echo "***********************************************"
56+
echo "WARNING: Default IoT Agent Auth credentials have not been overridden"
57+
echo "***********************************************"
58+
else
59+
echo "***********************************************"
60+
echo "INFO: IoT Agent Auth credentials have been overridden"
61+
echo "***********************************************"
62+
fi
63+
fi
64+
65+
pm2-runtime /opt/iotagent-lora/bin/iotagent-lora

docker/hooks/build

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2019 Atos Spain S.A
4+
#
5+
# This file is part of iotagent-lora
6+
#
7+
# iotagent-lora is free software: you can redistribute it and/or
8+
# modify it under the terms of the GNU Affero General Public License as
9+
# published by the Free Software Foundation, either version 3 of the License,
10+
# or (at your option) any later version.
11+
#
12+
# iotagent-lora is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15+
# See the GNU Affero General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Affero General Public
18+
# License along with iotagent-lora. If not, see http://www.gnu.org/licenses/.
19+
#
20+
21+
docker build --build-arg SOURCE_BRANCH=$SOURCE_BRANCH -t $IMAGE_NAME .

0 commit comments

Comments
 (0)