Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: docker parameters for seeds #317

Merged
merged 2 commits into from
Oct 5, 2023

Conversation

noaccOS
Copy link
Collaborator

@noaccOS noaccOS commented Jul 5, 2023

add environment variables to docker compose to be able to run the seeds with working astarte parameters

@coveralls
Copy link

coveralls commented Jul 5, 2023

Pull Request Test Coverage Report for Build 1b397325d64f5a1e95d8d8a7fef80c144069d8bb-PR-317

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 83.901%

Totals Coverage Status
Change from base Build af596578246434293a5ee65433169320715a5c96: 0.0%
Covered Lines: 1428
Relevant Lines: 1702

💛 - Coveralls

@rbino
Copy link
Collaborator

rbino commented Jul 11, 2023

@noaccOS I'd keep an opt-in approach here: your PR is useful if I actually want to use seeds to connect to a real Astarte instance, but I would keep the usecase of just wanting some random seeds to be able to bring up Edgehog.
So you should fall back to default hardcoded values instead of failing if the env variables are not present

Comment on lines 1 to 5
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEICx5W2odFd5CyMTv5VlLW96fgvWtcJ3bIJVVc3GWhMHBoAoGCCqGSM49
AwEHoUQDQgAEhV0KI4hByk0uDkCg4yZImMTiAtz2azmpbh0sLAKOESdlRYOFw90U
p4F9fRRV5Li6Pn5XZiMCZhVkS/PoUbIKpA==
-----END EC PRIVATE KEY-----
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this in a seeds subdirectory (i.e. priv/repo/seeds/realm_private.pem) so it's clear it's only for development purposes

@@ -0,0 +1 @@
notaprivatekey
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment for the subdirectory

Comment on lines 41 to 56
IPBASE_API_KEY: "${IPBASE_API_KEY}"
GOOGLE_GEOLOCATION_API_KEY: "${GOOGLE_GEOLOCATION_API_KEY}"
GOOGLE_GEOCODING_API_KEY: "${GOOGLE_GEOCODING_API_KEY}"
S3_ACCESS_KEY_ID: "${S3_ACCESS_KEY_ID}"
S3_SECRET_ACCESS_KEY: "${S3_SECRET_ACCESS_KEY}"
S3_REGION: "${S3_REGION}"
S3_SCHEME: "${S3_SCHEME}"
S3_HOST: "${S3_HOST}"
S3_PORT: "${S3_PORT}"
S3_BUCKET: "${S3_BUCKET}"
S3_ASSET_HOST: "${S3_ASSET_HOST}"
S3_GCP_CREDENTIALS: "${S3_GCP_CREDENTIALS}"
SEEDS_REALM: "${SEEDS_REALM}"
SEEDS_REALM_PRIVATE_KEY_FILE: /keys/realm_private.pem
SEEDS_TENANT_PRIVATE_KEY_FILE: /keys/tenant_private.pem
SEEDS_ASTARTE_BASE_API_URL: "${SEEDS_ASTARTE_BASE_API_URL}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you already have an .env file, I think you can just use

env_file:
  ./filename.env

In that case, make the filename not hidden to make it more evident (e.g. compose.env like we do in Astarte)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docker by default only reads the .env file for the compose variables, and it is not possible to change that (at least I haven't found a way in the docs).
To be able to use a custom.env file, the user would need to pass the option -f custom.env with each command.

This is in contrast with Astarte's compose file, where it is possible to use another file, because here we're reading the variables in the compose file directly https://github.com/edgehog-device-manager/edgehog/pull/317/files#diff-e45e45baeda1c1e73482975a664062aa56f20c03dd9d64a827aba57775bed0d3R62 instead of just redirecting the variables to the containers.

As for using the env_file option, I'm not against that but I'll need to rename the SEEDS_*_PRIVATE_KEY_FILE as the same name is used in the .env.
Not necessarily a bad thing as it might be ambiguous as it is now.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I get it, in that case let's leave everything as is (no need to split between env_file and .env since it just disperses things)

Logger.warning(
"""
The realm's private key is not a valid RSA/RC private key. \
This instante will not be able to connect to Astarte.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*instance

The realm's private key is not a valid RSA/RC private key. \
This instante will not be able to connect to Astarte.
"""
|> String.trim_trailing("\n")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to split warning messages on multiple lines you can do

Logger.warning("My very very long" <>
  "message"
)

(not sure about the formatting, but mix format will take care of that). Split it at < 100 columns.


read_file_from_env_var = fn env_var ->
System.get_env(env_var, "")
|> File.read()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File.read!() already fails with helpful messages and mentioning the filename, just use that

@@ -25,19 +25,40 @@ alias Edgehog.{
Tenants
}

require Logger

read_file_from_env_var = fn env_var ->
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a trailing ! since this can raise

require Logger

read_file_from_env_var = fn env_var ->
System.get_env(env_var, "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we already raise, use System.fetch_env!

Comment on lines 55 to 59
"""
Using default tenant private key. \
Please be sure to avoid using this for production.
"""
|> String.trim_trailing("\n")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment on multiline Logger.warning

Comment on lines 74 to 76
read_file_from_env_var.("SEEDS_REALM_PRIVATE_KEY_FILE")
|> then(fn pk ->
case X509.PrivateKey.from_pem(pk) do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can pipe into case, so you drop then:

read_file(...)
|> X509.PrivateKey.from_pem()
|> case do
  ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot do that as I need pk to return it in case of error

Unfortunately piping wouldn't allow me to do it

Keeps defaults in a single point

Signed-off-by: Francesco Noacco <francesco.noacco@secomind.com>
Signed-off-by: Francesco Noacco <francesco.noacco@secomind.com>
@rbino rbino merged commit ef663af into edgehog-device-manager:main Oct 5, 2023
4 checks passed
@noaccOS noaccOS deleted the dynamic-seeds branch October 22, 2024 12:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants