<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/startup-with-secrets-a-poor-mans-approach/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.jottr.org/2018/03/30/startup-secrets/"> JottR on R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>New release: <strong><a rel="nofollow" href="https://cran.r-project.org/package=startup" target="_blank">startup</a></strong> 0.10.0 is now on CRAN.</p>
<p>If your R startup files (<code>.Renviron</code> and <code>.Rprofile</code>) get long and windy, or if you want to make parts of them public and other parts private, then you can use the <strong><a rel="nofollow" href="https://cran.r-project.org/package=startup" target="_blank">startup</a></strong> package to split them up in separate files and directories under <code>.Renviron.d/</code> and <code>.Rprofile.d/</code>. For instance, the <code>.Rprofile.d/repos.R</code> file can be solely dedicated to setting in the <code>repos</code> option, which specifies from which web servers R packages are installed from. This makes it easy to find and easy to share with others (e.g. on GitHub). To make use of <strong>startup</strong>, install the package and then call <code>startup::install()</code> once. For an introduction, see <a rel="nofollow" href="https://www.jottr.org/2016/12/22/startup/" target="_blank">Start Me Up</a>.</p>
<p><img alt="ZX Spectrum animation" src="https://i0.wp.com/www.jottr.org/post/startup_0.10.0-zxspectrum.gif?w=456&ssl=1" data-recalc-dims="1"><br>
<em>startup::startup() is cross platform.</em></p>
<p>Several R packages provide APIs for easier access to online services such as GitHub, GitLab, Twitter, Amazon AWS, Google GCE, etc. These packages often rely on R options or environment variables to hold your secret credentials or tokens in order to provide more or less automatic, batch-friendly access to those services. For convenience, it is common to set these secret options in <code>~/.Rprofile</code> or secret environment variables in <code>~/.Renviron</code> – or if you use the <strong><a rel="nofollow" href="https://cran.r-project.org/package=startup" target="_blank">startup</a></strong> package, in separate files. For instance, by adding a file <code>~/.Renviron.d/private/github</code> containing:</p>
<pre><code>## GitHub token used by devtools
GITHUB_PAT=db80a925a60ee5b57f323c7b3719bbaaf9f96b26
</code></pre>
<p>then, when you start R, environment variable <code>GITHUB_PAT</code> will be accessible from within R as:</p>
<pre><code>> Sys.getenv(&quot;GITHUB_PAT&quot;)
[1] &quot;db80a925a60ee5b57f323c7b3719bbaaf9f96b26&quot;
</code></pre>
<p>which means that also <strong>devtools</strong> can make use of it.</p>
<p><strong>IMPORTANT</strong>: If you’re on a shared file system or a computer with multiple users, you want to make sure no one else can access your files holding “secrets”. If you’re on Linux or macOS, this can be done by:</p>
<pre><code>$ chmod -R go-rwx ~/.Renviron.d/private/
</code></pre>
<p>Also, <em>keeping “secrets” in options or environment variables is <strong>not</strong> super secure</em>. For instance, <em>if your script or a third-party package dumps <code>Sys.getenv()</code> to a log file, that log file will contain your “secrets” too</em>. Depending on your default settings on the machine / file system, that log file might be readable by others in your group or even by anyone on the file system. And if you’re not careful, you might even end up sharing that file with the public, e.g. on GitHub.</p>
<p>Having said this, with the above setup we at least know that the secret token is only loaded when we run R and only when we run R as ourselves. <strong>Starting with startup 0.10.0</strong> (*), we can customize the startup further such that secrets are only loaded conditionally on a certain environment variable. For instance, if we instead of putting our secret files in a folder named:</p>
<pre><code>~/.Renviron.d/private/SECRET=develop/
</code></pre>
<p>because then (i) that folder will not be visible to anyone else because we already restricted access to <code>~/.Renviron.d/private/</code> and (ii) the secrets defined by files of that folder will <em>only be loaded</em> during the R startup <em>if and only if</em> environment variable <code>SECRET</code> has value <code>develop</code>. For example,</p>
<pre><code>$ SECRET=develop Rscript -e &quot;Sys.getenv('GITHUB_PAT')&quot;
[1] &quot;db80a925a60ee5b57f323c7b3719bbaaf9f96b26&quot;
</code></pre>
<p>will load the secrets, but none of:</p>
<pre><code>$ Rscript -e &quot;Sys.getenv('GITHUB_PAT')&quot;
[1] &quot;&quot;

$ SECRET=runtime Rscript -e &quot;Sys.getenv('GITHUB_PAT')&quot;
[1] &quot;&quot;
</code></pre>
<p>In other words, with the above approach, you can avoid loading secrets by default and only load them when you really need them. This lowers the risk of exposing them by mistake in log files or to R code you’re not in control of. Furthermore, if you only need <code>GITHUB_PAT</code> in <em>interactive</em> devtools sessions, name the folder:</p>
<pre><code>~/.Renviron.d/private/interactive=TRUE,SECRET=develop/
</code></pre>
<p>and it will only be loaded in an interactive session, e.g.</p>
<pre><code>$ SECRET=develop Rscript -e &quot;Sys.getenv('GITHUB_PAT')&quot;
[1] &quot;&quot;
</code></pre>
<p>and</p>
<pre><code>$ SECRET=develop R --quiet

> Sys.getenv('GITHUB_PAT')
[1] &quot;db80a925a60ee5b57f323c7b3719bbaaf9f96b26&quot;
</code></pre>
<p>To repeat what already been said above, <em>storing secrets in environment variables or R variables provides only very limited security</em>. The above approach is meant to provide you with a bit more control if you are already storing credentials in <code>~/.Renviron</code> or <code>~/.Rprofile</code>. For a more secure approach to store secrets, see the <strong><a rel="nofollow" href="https://cran.r-project.org/package=keyring" target="_blank">keyring</a></strong> package, which makes it easy to “access the system credential store from R” in a cross-platform fashion, provides a better alternative.</p>
<h2>What’s new in startup 0.10.0?</h2>
<ul>
<li>
<p>Renviron and Rprofile startup files that use <code></code></ul>