{"id":552,"date":"2014-02-03T00:47:43","date_gmt":"2014-02-02T22:47:43","guid":{"rendered":"http:\/\/www.ludovicocaldara.net\/dba\/?p=552"},"modified":"2020-08-18T16:45:21","modified_gmt":"2020-08-18T14:45:21","slug":"wallets-and-proxy-users","status":"publish","type":"post","link":"https:\/\/www.ludovicocaldara.net\/dba\/wallets-and-proxy-users\/","title":{"rendered":"Removing passwords from Oracle scripts: Wallets and Proxy Users"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft\" style=\"color: #333333; font-style: normal; line-height: 24.375px; margin-top: 0.4em; white-space: normal; background-color: #ffffff;\" alt=\"\" src=\"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/f\/f0\/Secret_Service_agents_stand_guard.jpg\" width=\"406\" height=\"269\" \/><br \/>\nVery often I encounter customers that include Oracle account passwords in their scripts in order to connect to the database.<br \/>\nFor DBAs, sometimes things are<br \/>\neasier when they run scripts locally using the oracle account, since the &#8220;connect \/ as sysdba&#8221; usually do the job, even with some security concerns. But what if we need other users or we need to connect remotely?<br \/>\nSince longtime Oracle supplies secure wallets and the proxy authentication. Let&#8217;s see what they are and how to use them.<\/p>\n<p><strong>Secure Wallet<\/strong><br \/>\nSecure wallets are managed through the tool <strong>mkstore<\/strong>. They allow to store a username and password in a secure wallet accessible only by its owner. The wallet is then accessed by the Oracle Client to connect to a remote database, meaning that you DON&#8217;T HAVE to specify any username and password!<\/p>\n<p>Let&#8217;s see how to implement this the quick way:<\/p>\n<p>Create a directory that will contain your wallet:<\/p>\n<pre class=\"lang:sh decode:true\">$ mkdir .wlt<\/pre>\n<p>Create the wallet, use an arbitrary complex password to protect it:<\/p>\n<pre class=\"lang:sh decode:true\">$ mkstore -wrl \/home\/ludovico\/.wlt -create\r\nOracle Secret Store Tool : Version 12.1.0.1\r\nCopyright (c) 2004, 2012, Oracle and\/or its affiliates. All rights reserved.\r\n\r\nEnter password:\r\n\r\nEnter password again:\r\n\r\n$ ls -lia .wlt\r\n\r\ntotal 16\r\n1973369 drwxrwxr-x 2 ludovico ludovico 4096 Feb 2 22:33 .\r\n1973368 drwx------ 3 ludovico ludovico 4096 Feb 2 22:33 ..\r\n1973377 -rw------- 1 ludovico ludovico 2901 Feb 2 22:33 cwallet.sso\r\n1973376 -rw------- 1 ludovico ludovico 0 Feb 2 22:33 cwallet.sso.lck\r\n1973375 -rw------- 1 ludovico ludovico 2856 Feb 2 22:33 ewallet.p12\r\n1973373 -rw------- 1 ludovico ludovico 0 Feb 2 22:33 ewallet.p12.lck<\/pre>\n<p>Immagine that you&#8217;ve a user created with a very complex password:<\/p>\n<pre class=\"lang:plsql decode:true\">SQL &gt; create user batch identified by sDGVA_NNZBNsdRiW2h9mSyzbqBYJo;\r\n\r\nUser created.<\/pre>\n<p>Then you need to insert these credentials, including the connect string, into the wallet.<\/p>\n<pre class=\"lang:sh decode:true\">$ mkstore -wrl \/home\/ludovico\/.wlt -createCredential PROD batch<\/pre>\n<pre class=\"lang:sh decode:true\">Oracle Secret Store Tool : Version 12.1.0.1\r\nCopyright (c) 2004, 2012, Oracle and\/or its affiliates. All rights reserved.\r\n\r\nYour secret\/Password is missing in the command line\r\nEnter your secret\/Password: \r\n\r\nRe-enter your secret\/Password: \r\n\r\nEnter wallet password:\r\n\r\nCreate credential oracle.security.client.connect_string1<\/pre>\n<p>Keep in mind that you can have multiple credentials in the wallet for different remote descriptors (connect strings), but if you want many credentials for the very same connect string you need to create different wallets in different directories.<\/p>\n<p>Now you need to tell your Oracle Client to use that wallet by using the wallet_location parameter in your sqlnet.ora, so you need to have a private TNS_ADMIN:<\/p>\n<pre class=\"lang:sh decode:true\">$ ls -l $TNS_ADMIN\r\ntotal 8\r\n-rw-r--r-- 1 ludovico ludovico 169 Feb 2 22:47 sqlnet.ora\r\n-rw-r----- 1 ludovico ludovico 1751 Feb 2 22:45 tnsnames.ora\r\n\r\n$ cat \u00a0$TNS_ADMIN\/sqlnet.ora\r\nWALLET_LOCATION= (SOURCE= (METHOD=file) (METHOD_DATA= (DIRECTORY=\/home\/ludovico\/.wlt)))\r\nSQLNET.WALLET_OVERRIDE = TRUE\r\nSSL_CLIENT_AUTHENTICATION = FALSE\r\nSSL_VERSION = 0<\/pre>\n<p>If everything&#8217;s alright, you should be able to connect to the database PROD as the batch user, without specifying any username or password.<\/p>\n<pre class=\"lang:plsql decode:true\">$ sqlplus \/@PROD\r\n\r\nSQL*Plus: Release 12.1.0.1.0 Production on Sun Feb 2 22:50:49 2014\r\n\r\nCopyright (c) 1982, 2013, Oracle. All rights reserved.\r\n\r\nConnected to:\r\nOracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production\r\nWith the Partitioning, OLAP, Advanced Analytics and Real Application Testing options\r\n\r\nSQL&gt; show user\r\nUSER is \"BATCH\"<\/pre>\n<p><strong>Attention<\/strong>: when mkstore modifies the wallet, only the clients with the same or above versions will be able to access the wallet, so if you access your wallet with a 11g client you shouldn&#8217;t modify the wallet with a 12c version of mkstore. This is not documented by Oracle, but you can infer it from different &#8220;not a bug&#8221; entries on MOS \ud83d\ude42<\/p>\n<p><strong>Proxy Users<br \/>\n<\/strong>You can push things a little farther, and hook your wallet with a proxy user, in order to connect to arbitrary users. That&#8217;s it, a proxy user is entitled to connect to the database on behalf of other users. In this example, we&#8217;ll see how, through the batch account, we can connect as OE, SH or HR:<\/p>\n<pre class=\"lang:plsql decode:true\">SYSTEM@PROD &gt; alter user OE grant connect through BATCH;\r\n\r\nUser altered.\r\n\r\nSYSTEM@PROD &gt; alter user HR grant connect through BATCH;\r\n\r\nUser altered.\r\n\r\nSYSTEM@PROD &gt; alter user SH grant connect through BATCH;\r\n\r\nUser altered.\r\n\r\nSYSTEM@PROD &gt; select proxy, client from dba_proxies;\r\n\r\nPROXY CLIENT\r\n---------- --------------------\r\nBATCH HR\r\nBATCH SH\r\nBATCH OE<\/pre>\n<p>Now I can specify with which user I want to work on the DB, connect to it through the batch account, without specifying the password thanks to the wallet:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:plsql decode:true\">$ sqlplus [SH]@PROD\r\n\r\nConnected to:\r\nOracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production\r\nWith the Partitioning, OLAP, Advanced Analytics and Real Application Testing options\r\n\r\nSQL &gt; show user\r\n\r\nUSER is \"SH\"\r\nSQL &gt; connect [HR]@PROD\r\nConnected.\r\n\r\nSQL &gt; show user\r\nUSER is \"HR\"\r\nSQL&gt; connect [OE]\/@PROD\r\nConnected.\r\n\r\nSQL&gt; show user\r\n\r\nUSER is \"OE\"\r\nOE@PROD &gt; select sys_context('USERENV','PROXY_USER') from dual;\r\n\r\nSYS_CONTEXT('USERENV','PROXY_USER')\r\n----------------------------------------------------------------\r\nBATCH<\/pre>\n<p>See how it&#8217;s easy? But don&#8217;t forget to keep your wallet secure using unix\/windows permissions!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Very often I encounter customers that include Oracle account passwords in their scripts in order to connect to the database. For DBAs, sometimes things are easier when they run scripts locally using the oracle account, since the &#8220;connect \/ as &hellip; <a href=\"https:\/\/www.ludovicocaldara.net\/dba\/wallets-and-proxy-users\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[336,326,3,330,132],"tags":[145,144,147,22,146,143],"class_list":["post-552","post","type-post","status-publish","format-standard","hentry","category-devops","category-oracle","category-oracledb","category-oracle-inst-upg","category-triblog","tag-authentication","tag-external-store","tag-hardening","tag-oracle-database","tag-security","tag-wallet"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/comments?post=552"}],"version-history":[{"count":4,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/552\/revisions"}],"predecessor-version":[{"id":556,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/posts\/552\/revisions\/556"}],"wp:attachment":[{"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/media?parent=552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/categories?post=552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ludovicocaldara.net\/dba\/wp-json\/wp\/v2\/tags?post=552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}