<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>keyring on R Views</title>
    <link>https://rviews.rstudio.com/tags/keyring/</link>
    <description>Recent content in keyring on R Views</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 21 Mar 2019 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://rviews.rstudio.com/tags/keyring/" rel="self" type="application/rss+xml" />
    
    
    
    
    <item>
      <title>How to Avoid Publishing Credentials in Your Code</title>
      <link>https://rviews.rstudio.com/2019/03/21/how-to-avoid-publishing-credentials-in-your-code/</link>
      <pubDate>Thu, 21 Mar 2019 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2019/03/21/how-to-avoid-publishing-credentials-in-your-code/</guid>
      <description>
        

&lt;p&gt;&lt;em&gt;Roland Stevenson is a data scientist and consultant who may be reached on &lt;a href=&#34;https://www.linkedin.com/in/roland-stevenson/&#34;&gt;Linkedin&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When accessing an API or database in R, it is often necessary to provide credentials such as a login name and password. You may find yourself being prompted with something like this:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;/post/2019-03-15-roland_files/roland.png&#34; alt=&#34;Figure: Providing credentials via an interactive prompt&#34; /&gt;&lt;/p&gt;

&lt;p&gt;When writing an R script that requires a user to provide credentials, you will want a way to have the script prompt the user or, better yet, programatically provided the credentials in the R script. Either way, be careful! You don&amp;rsquo;t want to put your credentials out there in the clear for all the world to see. Best practices&lt;sup class=&#34;footnote-ref&#34; id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; emphatically state:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;As with every programming language, it is important to &lt;strong&gt;avoid publishing code with your credentials in plain text&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, how can we provide credentials without putting them in the script itself?  There are a variety of options described in RStudio&amp;rsquo;s &lt;a href=&#34;https://db.rstudio.com/best-practices/managing-credentials/&#34;&gt;&amp;ldquo;Databases using R&amp;rdquo;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I will focus on two cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;simply prompting for credentials via &lt;a href=&#34;https://cran.r-project.org/package=rstudioapi&#34;&gt;&lt;code&gt;rstudioapi&lt;/code&gt;&lt;/a&gt;

&lt;ul&gt;
&lt;li&gt;suitable for simple credential management&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;storing sets of encrypted credentials in a local file via the R &lt;a href=&#34;https://cran.r-project.org/package=keyring&#34;&gt;&lt;code&gt;keyring&lt;/code&gt;&lt;/a&gt; package

&lt;ul&gt;
&lt;li&gt;suitable for more complicated credential management&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&#34;prompting-for-a-username-and-password&#34;&gt;Prompting for a username and password&lt;/h3&gt;

&lt;p&gt;If an R Script requires only one set of credentials and those credentials are easy to remember, it may be easiest to prompt the user for them using &lt;code&gt;rstudioapi&lt;/code&gt;. A typical example would be prompting users for their username and password to access a corporate database:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;username &amp;lt;- rstudioapi::askForPassword(&amp;quot;Database username&amp;quot;)
password &amp;lt;- rstudioapi::askForPassword(&amp;quot;Database password&amp;quot;)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This method may also be convenient if the user&amp;rsquo;s credentials tend to change over time.&lt;/p&gt;

&lt;h3 id=&#34;the-r-keyring-package&#34;&gt;The R Keyring package&lt;/h3&gt;

&lt;p&gt;A more sophisticated option is to use the R &lt;code&gt;keyring&lt;/code&gt; package to store and access encrypted credentials locally. This might be more suitable if multiple credentials exist to access a variety of services (think multiple access tokens).  With &lt;code&gt;keyring&lt;/code&gt;, one password unlocks the keyring which then provides access to all the credentials.&lt;/p&gt;

&lt;p&gt;To use the &lt;code&gt;keyring&lt;/code&gt; package, a user only needs to to install and load the package&lt;sup class=&#34;footnote-ref&#34; id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34;&gt;2&lt;/a&gt;&lt;/sup&gt; and define three strings: the keyring name, a keyring service, and the username that we want to associate our secret credentials with.&lt;/p&gt;

&lt;p&gt;The following example shows how to create a keyring name &lt;code&gt;my_keyring&lt;/code&gt;, with credentials to access &lt;code&gt;my_database&lt;/code&gt; as &lt;code&gt;my_username&lt;/code&gt;.  We first create a &lt;code&gt;backend_file&lt;/code&gt; type of keyring which will store the encrypted credentials in the user&amp;rsquo;s home directory ( &lt;code&gt;~/.config/r-keyring&lt;/code&gt;).  With &lt;code&gt;keyring_create&lt;/code&gt;, we prompt for the password that will unlock the keyring.  Finally, we store a credential in the keyring with &lt;code&gt;set&lt;/code&gt; before locking it with &lt;code&gt;keyring_lock&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;library(keyring)

# Set variables to be used in keyring.
kr_name &amp;lt;- &amp;quot;my_keyring&amp;quot;
kr_service &amp;lt;- &amp;quot;my_database&amp;quot;
kr_username &amp;lt;- &amp;quot;my_username&amp;quot;

# Create a keyring and add an entry using the variables above
kb &amp;lt;- keyring::backend_file$new()
# Prompt for the keyring password, used to unlock keyring
kb$keyring_create(kr_name)
# Prompt for the credential to be stored in the keyring
kb$set(kr_service, username=kr_username, keyring=kr_name)
# Lock the keyring
kb$keyring_lock(kr_name)

# The encrypted keyring file is now stored at ~/.config/r-keyring/ and can be
# accessed by any R program that provides the keyring password
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can store credentials for multiple usernames per service, and multiple services per keyring.  This is ideal in the case of an application that must access a variety of services via access tokens.  The encrypted credentials file can either be published with the code, or perhaps for extra security, distributed via a separate channel.&lt;/p&gt;

&lt;h2 id=&#34;retrieving-credentials&#34;&gt;Retrieving credentials&lt;/h2&gt;

&lt;p&gt;To retrieve credentials, set the same three variables and use the &lt;code&gt;keyring&lt;/code&gt; &lt;code&gt;get()&lt;/code&gt; function, which will prompt us for the keyring password that we set when we called &lt;code&gt;create&lt;/code&gt;.  A retrieval script might look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;library(keyring)
library(DBI)

# Set variables to be used in keyring.
kr_name &amp;lt;- &amp;quot;my_keyring&amp;quot;
kr_service &amp;lt;- &amp;quot;my_database&amp;quot;
kr_username &amp;lt;- &amp;quot;my_username&amp;quot;

# Output the stored password: normally you would not want to do this
keyring::backend_file$new()$get(service = kr_service,
                                user = kr_username,
                                keyring = kr_name)


# Establish connection to Teradata retrieving the password from the keyring.
dbConnect(drv = odbc::odbc(),
                dsn = &amp;quot;my_dsn&amp;quot;, # set DSN options in ~/.odbc.ini
                pwd = keyring::backend_file$new()$get(service = kr_service,
                                                      user = kr_username,
                                                      keyring = kr_name))

&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With this, we are able to retrieve arbitrary credentials for a particular username and service, allowing us to manage much more complicated sets of credentials with a single password.&lt;/p&gt;

&lt;p&gt;So, which is the best way to ensure that plain text credentials are not published with code? If your code relies on a limited number of credentials, an interactive prompt may be the more suitable choice:  code users know what their username and password are and can easily enter them interactively.&lt;/p&gt;

&lt;p&gt;If the code requires multiple, hard-to-remember, or cumbersome to provide credentials, you might want to consider using keyrings.  Users will only need to provide one password, which will unlock the keyring and provide access to all credentials.&lt;/p&gt;
&lt;div class=&#34;footnotes&#34;&gt;

&lt;hr /&gt;

&lt;ol&gt;
&lt;li id=&#34;fn:3&#34;&gt;&lt;a href=&#34;https://db.rstudio.com/best-practices/managing-credentials/&#34;&gt;&amp;ldquo;Databases using R&amp;rdquo;&lt;/a&gt; from RStudio
 &lt;a class=&#34;footnote-return&#34; href=&#34;#fnref:3&#34;&gt;↩&lt;/a&gt;&lt;/li&gt;
&lt;li id=&#34;fn:2&#34;&gt;The r-keyring package is automatically installed and available in &lt;a href=&#34;https://github.com/ras44/rstudio-project&#34;&gt;rstudio-project&lt;/a&gt;.
 &lt;a class=&#34;footnote-return&#34; href=&#34;#fnref:2&#34;&gt;↩&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2019/03/21/how-to-avoid-publishing-credentials-in-your-code/&#39;;&lt;/script&gt;
      </description>
    </item>
    
  </channel>
</rss>
