{"id":23261,"date":"2015-10-18T18:21:52","date_gmt":"2015-10-18T12:51:52","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=23261"},"modified":"2015-10-19T10:54:11","modified_gmt":"2015-10-19T05:24:11","slug":"user-mangement-using-chef-and-aws-opsworks","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/user-mangement-using-chef-and-aws-opsworks\/","title":{"rendered":"User Mangement using CHEF and AWS OpsWorks"},"content":{"rendered":"<p><strong>CHEF<\/strong> is the most popular configuration management tool in the market these days as CHEF turns infrastructure into code and you can do almost anything using it. Recipes are the heart of CHEF. <strong>OpsWorks<\/strong> has been gaining a lot of momentum for last few months, the major factor being its support for CHEF. So,\u00a0in this blog we will be discussing how we can can manage users on multiple machines and their permissions as well. You will see how easy it is to do so and manage all the configurations as well. The recipes I have used can be shared can be <strong>used independent of OpsWorks<\/strong> with minimal changes.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28516\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/download-1.jpg\" alt=\"download (1)\" width=\"1251\" height=\"353\" \/><\/p>\n<h3>USE-CASE<\/h3>\n<p>I had multiple users on my machines. Whenever there was a new user who joined the firm to give him access on those machines I had to manually go and add the user to that machine. Also, adding the user to a group was necessary so that permissions could be managed at the level of groups. So, I had to think\u00a0of a way out and to manage all this through one click using CHEF and OpsWorks. We will be using a CHEF recipe\u00a0and passing values to those using Opsworks data-bags and deploying the configs using one click deployment in OpsWorks. What OpsWorks does is that it creates a copy of your GitHub repo locally to itself and then executes the recipes when we specify which one to execute. Since, we are using\u00a0data bags in our recipes, we will give input values to them as JSON through OpsWorks.<\/p>\n<h3>GATHERING THE CHEF RECIPES<\/h3>\n<h4><strong>Recipe 1<\/strong>: This is the user creation recipe:<\/h4>\n<p>[js]<\/p>\n<p>users = data_bag(&quot;user&quot;)<br \/>\nusers.each do |user|<br \/>\n    user_data = data_bag_item(&quot;user&quot;, user)<br \/>\n        user user_data[&quot;id&quot;] do<br \/>\n                comment user_data[&quot;comments&quot;]<br \/>\n                #uid user_data[&quot;uid&quot;]<br \/>\n                #gid user_data[&quot;gid&quot;]<br \/>\n                home user_data[&quot;home&quot;]<br \/>\n                shell user_data[&quot;shell&quot;]<br \/>\n                password user_data[&quot;password&quot;]<br \/>\n                supports :manage_home =&gt; true<br \/>\n                action :create<br \/>\n        end<\/p>\n<p>        directory &quot;#{user_data[&#8216;home&#8217;]}\/.ssh&quot; do<br \/>\n \t        mode 0755<br \/>\n \t        owner user_data[&quot;id&quot;]<br \/>\n \t        action :create<br \/>\n        end<\/p>\n<p>        file &quot;#{user_data[&#8216;home&#8217;]}\/.ssh\/authorized_keys&quot; do<br \/>\n \t        content user_data[&quot;ssh_keys&quot;].join(&quot;\\n&quot;)<br \/>\n \t        mode 0600<br \/>\n \t        owner user_data[&quot;id&quot;]<br \/>\n \t        action :create<br \/>\n        end<br \/>\n\tif user_data[&quot;sudo&quot;] == &quot;true&quot;<br \/>\n\t\ttemplate &quot;\/etc\/sudoers.d\/#{user_data[&quot;id&quot;]}&quot; do<br \/>\n       \t\tsource &quot;sudoers.erb&quot;<br \/>\n       \t\tmode &quot;0644&quot;<br \/>\n       \t\tvariables({<br \/>\n     \t\t:sudoers_users =&gt; user_data[&quot;id&quot;]<br \/>\n  \t\t})<br \/>\n\t\tend<br \/>\n\tend<br \/>\nend<\/p>\n<p>[\/js]<\/p>\n<p>The above script will create users based on the JSON values we pass in OpsWorks. It will create a .ssh folder inside the home directory of the user as well and append the ssh-keys inside the authorized_keys file.<\/p>\n<h4><strong>Recipe 2<\/strong>: This will be used to create groups. We will need to pass values to it using OpsWorks:<\/h4>\n<p>[js]<br \/>\ngroups = data_bag(&quot;group&quot;)<br \/>\ngroups.each do |group|<br \/>\n        group_data = data_bag_item(&#8216;group&#8217;, group)<br \/>\n        group group_data[&#8216;id&#8217;] do<br \/>\n                gid group_data[&quot;gid&quot;]<br \/>\n                members group_data[&quot;members&quot;]<br \/>\n        end<br \/>\nend<br \/>\n[\/js]<\/p>\n<h4><strong>Recipe 3<\/strong>: Next we would need a script which will help us modify our sudoers file inside sudoers.d:<\/h4>\n<p>I have just entered some random permissions for the groups:<\/p>\n<p>[js]<br \/>\nline = &quot;%sap    ALL= NOPASSWD: \/usr\/sbin\/apache2*<br \/>\n%sap    ALL= NOPASSWD: \/usr\/local\/bin\/apache2*<br \/>\n#%sap    ALL= NOPASSWD: \/usr\/local\/bin\/restart-sauce-connect<br \/>\n%sap    ALL= NOPASSWD: \/usr\/local\/bin\/restart-sauce-connect<br \/>\njenkins ALL= NOPASSWD: \/usr\/sbin\/apache2*<br \/>\njenkins ALL= NOPASSWD: \/usr\/local\/bin\/restart-sauce-connect<br \/>\n&quot;<br \/>\nfile = Chef::Util::FileEdit.new(&#8216;\/etc\/sudoers.d\/anyname&#8217;)<br \/>\nfile.insert_line_if_no_match(\/#{line}\/, line)<br \/>\nfile.write_file<br \/>\n[\/js]<\/p>\n<p>Now, just push the recipe to a git repository inside the cookbook user (or any other name of your choice).The structure should be like this:<br \/>\nrepo-&gt;master-branch-&gt;cookbook-&gt;recipes-&gt;recipe1.rb, recipe2.rb, recipe3.rb<\/p>\n<h3>EXECUTING RECIPES<\/h3>\n<h4>1. Creating a user on servers according to Recipe 1 and giving input to the recipe using OpsWorks.<\/h4>\n<p>Go to the AWS OpsWorks console. I am assuming that you already have created a stack and added instances to the stack. Remember it supports not all the OS versions so you might want to check the compatibility. From the Dashboard select the stack:<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28519\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/Edit-Post-\u2039-TO-THE-NEW-Blog-\u2014-WordPress.png\" alt=\"Edit Post \u2039 TO THE NEW Blog \u2014 WordPress\" width=\"624\" height=\"377\" \/><\/p>\n<p>Next, is adding the GitHub repo to your stack settings in order for you to use the recipe we created:<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28326\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/Stack-AAP-Stack-\u2013-AWS-OpsWorks-2015-10-15-16-19-48.png\" alt=\"Stack - AAP-Stack \u2013 AWS OpsWorks 2015-10-15 16-19-48\" width=\"1238\" height=\"467\" \/><\/p>\n<p>Click on edit inside <strong>Stack Settings<\/strong>:<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28327\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/Stack-AAP-Stack-\u2013-AWS-OpsWorks-2015-10-15-16-21-53.png\" alt=\"Stack - AAP-Stack \u2013 AWS OpsWorks 2015-10-15 16-21-53\" width=\"1024\" height=\"137\" \/><\/p>\n<p>Now, just enter the GitHub repo. details:<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28328\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/Edit-Stack-AAP-Stack-\u2013-AWS-OpsWorks-2015-10-15-16-24-14.png\" alt=\"Edit Stack - AAP-Stack \u2013 AWS OpsWorks 2015-10-15 16-24-14\" width=\"1240\" height=\"586\" \/><\/p>\n<p>Now, go to <strong>Deployments<\/strong>:<br \/>\n.<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-medium wp-image-28329\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/Stack-AAP-Stack-\u2013-AWS-OpsWorks-2015-10-15-16-26-51.jpeg\" alt=\"Stack - AAP-Stack \u2013 AWS OpsWorks 2015-10-15 16-26-51\" width=\"147\" height=\"300\" \/><\/p>\n<p>You will be taken to the following page:<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28330\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/Deployments-AAP-Stack-\u2013-AWS-OpsWorks-2015-10-15-16-31-29.jpeg\" alt=\"Deployments - AAP-Stack \u2013 AWS OpsWorks 2015-10-15 16-31-29\" width=\"1025\" height=\"411\" \/><\/p>\n<p>Just go ahead and click on Run Command and it should take you to the below page:<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28332\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/Repeat-deployment-2015-09-28T113322-0000-AAP-Stack-\u2013-AWS-OpsWorks-2015-10-15-16-35-43.jpeg\" alt=\"Repeat deployment 2015-09-28T11:33:22+00:00 - AAP-Stack \u2013 AWS OpsWorks 2015-10-15 16-35-43\" width=\"1023\" height=\"555\" \/><\/p>\n<p>Select Execute Recipes from the drop-down and enter the cookbook name and recipe name as shown in the image.We are using cookbook named &#8220;final&#8221; and recipe named &#8220;createuserwithsudo&#8221;. \u00a0When entering them in the Recipes to execute box use &#8220;<strong>::<\/strong>&#8221; (two colons) to separate them.<br \/>\nAlso, \u00a0now, click on Advanced which should open a box to input a JSON:<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28333\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/Repeat-deployment-2015-09-28T113322-0000-AAP-Stack-\u2013-AWS-OpsWorks-2015-10-15-16-42-45.jpeg\" alt=\"Repeat deployment 2015-09-28T11:33:22+00:00 - AAP-Stack \u2013 AWS OpsWorks 2015-10-15 16-42-45\" width=\"992\" height=\"542\" \/><\/p>\n<p>Sample JSON input:<\/p>\n<p>[js]<br \/>\n{<br \/>\n  &quot;opsworks&quot;: {<br \/>\n    &quot;data_bags&quot;: {<br \/>\n      &quot;user&quot;: {<br \/>\n        &quot;user1&quot; : {<br \/>\n          &quot;id&quot; : &quot;user1&quot;,<br \/>\n          &quot;comments&quot; : &quot;some comment&quot;,<br \/>\n          &quot;home&quot; : &quot;\/home\/user1&quot;,<br \/>\n          &quot;shell&quot; : &quot;\/bin\/bash&quot;,<br \/>\n          &quot;sudo&quot; : &quot;false&quot;,<br \/>\n          &quot;password&quot; : &quot;$1$d01YpgzW$Yt64wYX\/uWstYf2lGiZuR0&quot;,<br \/>\n          &quot;ssh_keys&quot; : [&quot;ssh-key-1&quot;,&quot;ssh-key-2&quot;]<br \/>\n         },<br \/>\n        &quot;user2&quot; : {<br \/>\n          &quot;id&quot; : &quot;user2&quot;,<br \/>\n          &quot;comments&quot; : &quot;some comment&quot;,<br \/>\n          &quot;home&quot; : &quot;\/home\/user2&quot;,<br \/>\n          &quot;shell&quot; : &quot;\/bin\/bash&quot;,<br \/>\n          &quot;sudo&quot; : &quot;true&quot;,<br \/>\n          &quot;password&quot; : &quot;$1$d01YpgzW$Yt64wYX\/uWstYf2lGiZuR0&quot;,<br \/>\n          &quot;ssh_keys&quot; : [&quot;ssh-key-1&quot;,&quot;ssh-key-2&quot;]<br \/>\n         },<br \/>\n           &quot;user3&quot; : {<br \/>\n          &quot;id&quot; : &quot;user3&quot;,<br \/>\n          &quot;comments&quot; : &quot;some comment&quot;,<br \/>\n          &quot;home&quot; : &quot;\/home\/user3&quot;,<br \/>\n          &quot;shell&quot; : &quot;\/bin\/bash&quot;,<br \/>\n          &quot;sudo&quot; : &quot;true&quot;,<br \/>\n          &quot;password&quot; : &quot;$1$d01YpgzW$Yt64wYX\/uWstYf2lGiZuR0&quot;,<br \/>\n          &quot;ssh_keys&quot; : [&quot;ssh-key-1&quot;,&quot;ssh-key-2&quot;]<br \/>\n       }<br \/>\n      }<br \/>\n    }<br \/>\n  }<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>You need to replace the user1, 2 and 3 with user-name you want on the server. Also, replace the ssh-keys with the ones associated with the user. The password for the user needs to be generated via open-ssl. CHEF recipe won&#8217;t take password in normal text format. Also,<br \/>\n<strong>sudo<\/strong>==<strong>true<\/strong> means user will have root privileges. Next, click on <b>Execute Recipes<\/b>\u00a0after choosing the instances of the stack on which you want the recipe to be executed:<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28337\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/Repeat-deployment-2015-09-28T113322-0000-AAP-Stack-\u2013-AWS-OpsWorks-2015-10-15-16-57-11.jpeg\" alt=\"Repeat deployment 2015-09-28T11:33:22+00:00 - AAP-Stack \u2013 AWS OpsWorks 2015-10-15 16-57-11\" width=\"1028\" height=\"393\" \/><\/p>\n<p>So, you are done!\u00a0After the recipe is executed, it will show you the results then and there. Also, if you wish to see logs of a previously executed recipe click on the date &amp; time and it should take you to the particular command&#8217;s page where you can see logs as well:<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28340\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/Deployments-AAP-Stack-\u2013-AWS-OpsWorks-2015-10-15-17-05-03.jpeg\" alt=\"Deployments - AAP-Stack \u2013 AWS OpsWorks 2015-10-15 17-05-03\" width=\"1015\" height=\"388\" \/><\/p>\n<p>If you make any changes to your recipes in your GitHub, after pushing the changes you need to go to &#8220;<strong>Run Command&#8221;<\/strong> and select <strong>Update Cookbooks<\/strong>:<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28348\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/Run-command-AAP-Stack-\u2013-AWS-OpsWorks-2015-10-15-17-52-32.jpeg\" alt=\"Run command - AAP-Stack \u2013 AWS OpsWorks 2015-10-15 17-52-32\" width=\"1005\" height=\"578\" \/>The user will be created on the specified servers<strong>.<\/strong><\/p>\n<h4>2. Now, if you wish to add the newly created user to a group<\/h4>\n<p>Simply execute the <strong>Recipe 2<\/strong>. It will create a group if it does not exist already and then add the users to the group. Execute the recipe as we did before. Enter the cookbook name and recipe name:<br \/>\n<img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-28342\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/10\/2015-09-30T081510-0000-Deployments-AAP-Stack-\u2013-AWS-OpsWorks-2015-10-15-17-31-05.jpeg\" alt=\"2015-09-30T08:15:10+00:00 - Deployments - AAP-Stack \u2013 AWS OpsWorks 2015-10-15 17-31-05\" width=\"986\" height=\"499\" \/><\/p>\n<p>Sample JSON:<\/p>\n<p>[js]<br \/>\n{<br \/>\n  &quot;opsworks&quot;: {<br \/>\n    &quot;data_bags&quot;: {<br \/>\n      &quot;group&quot;: {<br \/>\n        &quot;admin&quot;: {<br \/>\n             &quot;gid&quot;: &quot;3308&quot;,<br \/>\n             &quot;members&quot;: [&quot;user1&quot;,&quot;user2&quot;,&quot;user3&quot;,&quot;user4&quot;,&quot;user5&quot;]<br \/>\n               }<\/p>\n<p>               }<\/p>\n<p>                 }<\/p>\n<p>               }<br \/>\n}<br \/>\n[\/js]<\/p>\n<h4>3. Handling the file inside sudoers.d to give different permissions.<\/h4>\n<p>Use<strong>\u00a0Recipe 3<\/strong> for this. Just go ahead and run the recipe. I have written some basics commands which can be allowed for a group. You can use this however you like. Remember to update cookbooks in OpsWorks console so that the changes in GitHub repo are reflected here. Just simply execute the recipe as we have done earlier, choose the server and it will append to the file inside sudoers.d<\/p>\n<p>For example: \/etc\/sudoers.d\/serveralpha<\/p>\n<p>You need to make changes to the recipe according to your use case and names of files.\u00a0In case you make changes to the recipe make sure you run the <strong>Update Cookbooks<\/strong> command as I have shown in step 1.<\/p>\n<p>This is the basic way of handling users on your servers using CHEF. In my next blog, I shall talk about more use-cases of CHEF.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CHEF is the most popular configuration management tool in the market these days as CHEF turns infrastructure into code and you can do almost anything using it. Recipes are the heart of CHEF. OpsWorks has been gaining a lot of momentum for last few months, the major factor being its support for CHEF. So,\u00a0in this [&hellip;]<\/p>\n","protected":false},"author":174,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":18},"categories":[1174,2348,1],"tags":[248,1935,1910,2606,2597,2599,2601,49,260,2598,1343,2607,1779,2600,2605,2603,2602,2604,2596,2595,1170],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23261"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/174"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=23261"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/23261\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=23261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=23261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=23261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}