MagicMirror² v2.5.0 is available! For more information about this release, check out this topic.

secrets.js



  • Hi guys,

    Yesterday I lost all my configuration bc my editor broke the file. Thats a pitty. It cost me a day to get it back working bc I use a lot of complicated modules e.g. like the openhab floorplan.

    That got me thinking.

    Config.js is typically not committed to git because of all the secret stuff like apiKeys for google, darksky, your lat/lon of your house which you might not want to share via github.

    How you guys are doing this? Having version control, but not publicly visible. Ok, I could pay github, or have a own server set up for my privat git. But sharing back with the community your own enhancements of the core system gets difficult.

    I thought about a simpler solution.

    What about splitting the configuration into two files:

    config.js

    var config = {
    ...
       { ...
    // instead of 
          apiKey1 = „my secret key“,
    // we do
          apiKey = this.secrets.apiKey1,
       }
    

    And we sum up all the secrets like lat/lon/apikeys in a dedicated file

    secrets.js

    var secrets = {
       apiKey1 = „MySuperSecretApiKey1“,
       Latitude = YouMustNotKnowWhereILive,
       ...
    }
    

    The secrets.js file must be read somehow by the core system and the this.secrets must be populated.

    And it would be downward compatible. If you dont have a secrets.js you are good to go as well.

    config.js could go in a public version control system and only secrets.js would stay in .gitignore.

    Is there an easy way to do this on my own? Some sorr of an „include secrets.js“ statement in javascript. Kind of bash like „source anotherBashScript“. I am new to JavaScript. Sorry if this is a silly question.

    Looking forward to your feedback.

    Regards
    Ralf



  • @rak No offense but this is way more work then necessary. The current setup works great …the suggested thing to do is make a backup of your config.js before changing anything that way you never lose your work 😉



  • True. There is always an easy solution. Disadvantage is here you dont have versioning. GIT is a great tool. Why not using the advantage of it?



  • @rak Well you’d have to take that up with the developers… so far this is the system that they implemented and it’s worked well so far 🙂 You could always modify the config.js yourself to do what you want.



  • @rak You may wish to use dotenv. You can either do this on a module level, or for your local install.

    https://www.npmjs.com/package/dotenv

    Here you would just make an .env file in your project root with something like:

    API_KEY=MySuperSecretApiKey1
    

    Then you should be able to update the config file with:

    var localConfig = require('dotenv').config();
    
    var config = {
    ...
       { ...
          apiKey = process.env.API_KEY,
       }
    

    Just make sure to add the .env file to .gitignore so it won’t get committed via git.



  • @mrmidi Thank you very much. This goes into the desired direction.

    I have installed dotenv and added

    var localConfig = require('dotenv').config();
    

    If I do so starting the MM it shows missing config file.

    Any ideas?



  • @rak This is most interesting 🙂

    I am actually working on testing this now. There is no real reason this should not have worked, but you are 100% right, it seems to tank the config.

    What’s really interesting is that I am tinkering around in the ./js/app.js file, and it’s actually not breaking when it’s being imported. The config file that comes in gets merged just fine and consoling it out, shows the correct dontenv getting replaced.

    This means somewhere else along the line, this code is breaking.

    Hopefully I can follow the config through the app and detect where that’s happening and post back ASAP.



  • @rak OK, so after digging into this more, it looks like this won’t work without an update to the way the config.js file is loaded.

    Currently, it is being loaded two ways.

    1. As an exportable module that can be used via require() statements
    2. As vanilla javascript

    So the dotenv would have worked fine ( and DOES work fine ) when used in a module context. However, the vanilla JS cannot reference module data this way. The solution would be to dig more into the ./js/app.js and ./js/main.js files and look at how they are handling the loadConfig functions and see if there is not a way to tweak it for your purposes.

    Sadly, there is not much more I can think of to help with that without spending several hours tinkering around.



  • @mrmidi I’m pretty sure the problem is, that the config file gets loaded in the browser. The browser doesn’t have require, it’s a nodejs feature.

    https://github.com/MichMich/MagicMirror/search?q=CONFIG_FILE&unscoped_q=CONFIG_FILE



  • Hi!

    I understand this issue. I use a gitlab private repository. I clone it in “magicmirror_config” in /home/pi/
    And then I created a symlink im /home/pi/MagicMirror/config/ for my versioned config.js

    And all my personalized files are in symlink where I need it.

    That’s really easy and like that I can test on local device (my Mac) before put it on the raspberry Pi. 🙂