{"id":33,"date":"2022-05-20T02:18:24","date_gmt":"2022-05-20T02:18:24","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/joycesplayground\/?p=33"},"modified":"2022-05-20T02:18:24","modified_gmt":"2022-05-20T02:18:24","slug":"dont-share-secrets","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/joycesplayground\/2022\/05\/20\/dont-share-secrets\/","title":{"rendered":"Don&#8217;t share secrets!"},"content":{"rendered":"\n<p>My childhood friend always said that retelling others&#8217; secrets would give you a terrible sickness. While you can&#8217;t make your application <em>sick<\/em>, you can accidentally give away API keys, login credentials, endpoint information, etc. if you do not keep them stored safely. This article is part <em>don&#8217;t do what we did<\/em> and part <em>here&#8217;s what you should do instead<\/em>.<\/p>\n\n\n\n<p>Originally, since my team needed to store credentials for a Mapbox API key, we all made an unchecked dart file with the same name and kept the key there. However, this turned out to be an issue later on when <a href=\"https:\/\/blogs.oregonstate.edu\/joycesplayground\/2022\/05\/06\/automating-with-github-actions\/\" data-type=\"URL\" data-id=\"https:\/\/blogs.oregonstate.edu\/joycesplayground\/2022\/05\/06\/automating-with-github-actions\/\">setting up our test suite<\/a>. I thought I had everything down with our fancy new github action that would call <code>flutter test<\/code> on every pull request. However, the tests promptly failed because of a line that looked like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import 'package:bla\/.credentials.dart';<\/code><\/pre>\n\n\n\n<p>In our local development environments <code>flutter test<\/code> was fine. However, in the github environment, the credentials file didn&#8217;t exist! I really struggled with this for a few days and tried all kinds of things.<\/p>\n\n\n\n<p>I tried creating the file on the fly from within a <code>setUp()<\/code> call in the test suite. I probably didn&#8217;t get the incantation correct because this didn&#8217;t work.<\/p>\n\n\n\n<p>I then tried to generate the credentials file from within the <code>tests.yaml<\/code> file which is used for the github action workflow. Again, something was possibly off about how I did it because I got a new error that the import statement was unused. <\/p>\n\n\n\n<p>Finally, I thought <em>at work we use .env for secrets but would that work here?<\/em> Spoiler alert: yes it does and it&#8217;s actually an ideal way to keep environment variables secret based on the environment (e.g. &#8216;development&#8217;, &#8216;staging&#8217;, &#8216;production&#8217;). So after some googling I stumbled upon the <a href=\"https:\/\/pub.dev\/packages\/flutter_dotenv\" data-type=\"URL\" data-id=\"https:\/\/pub.dev\/packages\/flutter_dotenv\">flutter_dotenv<\/a> package on <a href=\"https:\/\/pub.dev\/\" data-type=\"URL\" data-id=\"https:\/\/pub.dev\/\">pub.dev<\/a>. <\/p>\n\n\n\n<p>The instructions were actually quite simple. All it consisted of is:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>adding the package to our application<\/li><li>adding a .env file with secrets<\/li><li>making sure the .env file is not checked into the repo (i.e. add .env to .gitignore)<\/li><li>finally, generating the .env file on the fly in the <code>tests.yaml<\/code> file<\/li><\/ol>\n\n\n\n<p>That last bit felt extremely similar to what I had going before so you may be wondering how this is any different than what I tried in my second attempt at getting this to work. This approach differs because now I could remove that import statement and instead just import the dot_env package. Then in the code, instead of grabbing those constants from the dart file, I could use the dot_env package to access them from a global <code>env<\/code> object.<\/p>\n\n\n\n<p>It kinda looks like:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dotenv.env&#091;'MY_API_KEY'];<\/code><\/pre>\n\n\n\n<p>This means that now in the github environment, as long as we generate the .env file and add some dummy values in it (empty strings work fine), we don&#8217;t get any errors when running <code>flutter test<\/code>.<\/p>\n\n\n\n<p>I think as folks who clearly haven&#8217;t had much experience with this kind of <em>secrecy<\/em>, we took a na\u00efve approach and it worked ok at first but it was bound to have issues later. I hope this wisdom can help others out there running into a similar issue with keeping your secrets SECRET!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My childhood friend always said that retelling others&#8217; secrets would give you a terrible sickness. While you can&#8217;t make your application sick, you can accidentally give away API keys, login credentials, endpoint information, etc. if you do not keep them stored safely. This article is part don&#8217;t do what we did and part here&#8217;s what [&hellip;]<\/p>\n","protected":false},"author":12334,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-33","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/joycesplayground\/wp-json\/wp\/v2\/posts\/33","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/joycesplayground\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/joycesplayground\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/joycesplayground\/wp-json\/wp\/v2\/users\/12334"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/joycesplayground\/wp-json\/wp\/v2\/comments?post=33"}],"version-history":[{"count":2,"href":"https:\/\/blogs.oregonstate.edu\/joycesplayground\/wp-json\/wp\/v2\/posts\/33\/revisions"}],"predecessor-version":[{"id":35,"href":"https:\/\/blogs.oregonstate.edu\/joycesplayground\/wp-json\/wp\/v2\/posts\/33\/revisions\/35"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/joycesplayground\/wp-json\/wp\/v2\/media?parent=33"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/joycesplayground\/wp-json\/wp\/v2\/categories?post=33"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/joycesplayground\/wp-json\/wp\/v2\/tags?post=33"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}