<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Felix Ayange on Medium]]></title>
        <description><![CDATA[Stories by Felix Ayange on Medium]]></description>
        <link>https://medium.com/@felixdecoder2020?source=rss-51150fafbc00------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*2-vvyZpO8H6aTWAFjnpiXA.jpeg</url>
            <title>Stories by Felix Ayange on Medium</title>
            <link>https://medium.com/@felixdecoder2020?source=rss-51150fafbc00------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 08:59:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@felixdecoder2020/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Automate AWS EC2 Instance with Python (DevOps challenge)]]></title>
            <link>https://medium.com/@felixdecoder2020/automate-aws-ec2-instance-with-python-devops-challenge-f04bcaec20b7?source=rss-51150fafbc00------2</link>
            <guid isPermaLink="false">https://medium.com/p/f04bcaec20b7</guid>
            <dc:creator><![CDATA[Felix Ayange]]></dc:creator>
            <pubDate>Fri, 05 May 2023 15:57:03 GMT</pubDate>
            <atom:updated>2023-05-05T15:57:03.251Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Automate AWS EC2 Instance with Python (DevOps challenge)</strong></p><p>You are in a company and your <strong>EC2 </strong>instances keeps been stopped, No one know what is causing it. Your team lead needed someone who could <strong>automate</strong> all of the stopped <strong>EC2</strong> instances and automatically <strong>restart</strong> them every hour or so.</p><p>In this article I will be walking you through a step by step solution on how you could use:</p><p><strong>AWS Lambda</strong> which is a compute service that lets you run code without provisioning or managing servers</p><p><strong>Boto3</strong> which provides a Python API for AWS infrastructure services<strong>. </strong>Using the SDK for Python, you can build applications on top of Amazon S3, Amazon EC2, Amazon DynamoDB, and more. <a href="https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html">Check out the documentation for more details</a></p><p><strong>Event bridge</strong> which is a serverless service that uses events to connect application components together, making it easier for you to build scalable event-driven applications.</p><p>Using the above tools we will automate our process to automatically restart EC2 instances that are in a <strong>stopped</strong> state. We will be using <strong>python</strong> to write our code.</p><p>So without wasting much time, let’s get started. first we will start with a rough architectural flow which will give us an <strong>overview/roadmap</strong> of what we will be implementing, Sounds great yeah!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*78895GIDRT7ttxJD9Vj3gQ.png" /></figure><p>The architectural diagram is simply telling us to create a <strong>Lambda</strong>, import <strong>Boto3</strong> which will talk to our <strong>EC2 </strong>instances if there is any stopped then start it and <strong>event bridge</strong> will trigger the lambda to automatically restart the EC2 instances every hour.</p><p>First, let’s create a <strong>Lambda function</strong>. Login to your AWS account and search for <strong>Lambda</strong>. Click on it and hit the create button. Add the function name and select python 3.10 as the run time environment and hit the save button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*stjoeVZqlXDnc91_c5dewg.png" /></figure><p>You should see this page if that is done properly</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NbUraFmvJj9rHTq2uJdjMA.png" /></figure><p>But before writing any code, we need to give permission to talk to our <strong>EC2</strong> instances. To do that click on configuration on the above page. You should see this page below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JcwnPpKB3JDT3VQIsT50Zw.png" /></figure><p>On the execution role, click on edit, you should see the page below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bc_KnetuX2P4Sn8fcUktyw.png" /></figure><p>From the above screen shot, scroll down and click on View the StartEc2Instances (That is the name of the lambda function you created earlier). Once that is done you should see the page below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iwkiEjwDpiRbILOrzV76kA.png" /></figure><p>Click on the policy name from the above screen shot</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qy8pBanE5kgzLGVBje7fIw.png" /></figure><p>From the above screen shot click on edit, you should see a screen shot below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6XyxVkpZ5s00AEb2fLsq4g.png" /></figure><p>Under edit statement, there are two section one for add action and the other for add resource.<br>for <strong>add action</strong>, search for <strong>EC2 </strong>instance and select on <strong>all actions</strong><br>for the <strong>add resource</strong> section, click on add resource and under the resource type select <strong>All resources</strong> and click add.</p><p>Click next and save changes then you should be able to see <strong>EC2</strong> full access as seen in the screenshot below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MZdyYreeEKp6ZC-skVhcfg.png" /></figure><p>That’s all. This will allow the L<strong>ambda </strong>to talk to <strong>EC2</strong> instances and do whatever it need to do, to query, to start or stop <strong>EC2</strong> instances.</p><p>Once your permissions is good to go, head back to Lambda function we created earlier, this is where the magic is gonna happen. We will be writing our python code under the code section to automate the process.</p><p>First, import the <strong>boto3</strong>, I already explained to you what <strong>botot3</strong> is</p><p>To use <strong>boto3</strong>, We will first import it and indicate which service or services it’s going to use: In our case we will be using <strong>EC2</strong> resources.</p><pre>import boto3<br><br># Let&#39;s use Amazon EC2<br>ec2 = boto3.resource(&#39;ec2&#39;)</pre><p>Now that we have an <strong>EC2</strong> resource, we can send requests to the service and loop through the instances and get the instances states, if the state equals <strong>stopped </strong>we need to start it, That’s basically what we will be doing, nothing fancy. The following code uses the <strong>EC2</strong> instances collection to print out all instances states:</p><pre># Print out instances states<br>for intance in ec2.intances.all():<br>    print(intance.state)</pre><p>To test, we will make sure will have some set of EC2 instances created, with states, <strong>running</strong>, <strong>stopped</strong>. Our focus here is on the <strong>stopped EC2 </strong>instances yeah.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6qmJZjVDgnJ9O8DnjE0M2g.png" /></figure><p>From the screenshoot above, we created four instances, two are <strong>running </strong>and two <strong>stopped. </strong>once this is done, head back to Lambda and click on deploy first, fill in the details and hit the test button you should see the output below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aBZu__onVpcHa5BByG7RDQ.png" /></figure><p>Boom! wow just look at that!!. As you can see we have four set of instances, two <strong>running</strong> and two <strong>stopped</strong>. Our interest is to restart the <strong>stopped</strong> instances right.</p><p>But before doing that I like us to print just the names of the instances and not the entire objects as shown. So let’s update our code to something like this:</p><pre>for intance in ec2.intances.all():<br>    print(intance.state[&#39;Name&#39;])</pre><p>Deploy and test again. This time you should only see the state names</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*76aAZrtJ4XqaROuCurmQ6g.png" /></figure><p>Wow, so far so good, our automation process is going smoothly.</p><p>Now, putting everything together, our code should look something like this:</p><pre>import boto3<br><br>def lambda_handler(event, context):<br>   <br>   #Create ec2 client<br>   ec2 = boto3.resource(&#39;ec2&#39;)<br>   <br>   #loop through the instances<br>   for instance in ec2.instances.all():<br>      <br>       state = instance.state[&#39;Name&#39;]<br>       <br>       #Check if state is stopped start the instance<br>       if state == &#39;stopped&#39;:<br>          try:<br>            instance.start()<br>          except:<br>             print(&quot;Something went wrong, please try again&quot;)</pre><p>Deploy and test again.</p><p>Go back to your <strong>EC2 </strong>instances, and boom look at that,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1NR4NibREMpibLR4uXSF4A.png" /></figure><p>our instances is in pending states after some seconds the stopped instances will start as you can see in the below screenshot</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YaKAXECPTc2ziJQZm9AwUA.png" /></figure><p>Wow..our automation is working as expected, all <strong>stopped</strong> instances are back and running.</p><p>One last thing, the goal is to <strong>restart</strong> the <strong>stopped</strong> instances automatically after an hour without having to click on test button yeah!. To do this, We need to add trigger to the <strong>Lambda </strong>such that<strong> </strong>for every hour, our <strong>stopped </strong>instance are back and <strong>running</strong>.</p><p>To do this, go back to <strong>lambda</strong> and click on <strong>add triggers</strong>.</p><p>1. Select the source and the source we are using here is event bridge <br> 2. Create a new role<br> 3. We need to add a schedule, that is a <strong>cron job </strong>that triggers the <strong>Lambda</strong> every hour. cron(0/60 * * * ? *). <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html">Check out documentation here</a></p><p>4. Click add</p><p>see screenshot below</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S6EJir4JmPi2ZE39-yJdnw.png" /></figure><p>Wow! wow!! wow!!..we have successfully executed our <strong>AWS EC2</strong> isntancs automation.</p><p>That was easy and cool yeah!</p><p>If you find this article useful, please give me a thumbs up to motivate me for more interesting articles like this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f04bcaec20b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Database Encryption In Laravel Using CipherSweet]]></title>
            <link>https://medium.com/@felixdecoder2020/database-encryption-in-laravel-using-ciphersweet-5cbdff84aa52?source=rss-51150fafbc00------2</link>
            <guid isPermaLink="false">https://medium.com/p/5cbdff84aa52</guid>
            <dc:creator><![CDATA[Felix Ayange]]></dc:creator>
            <pubDate>Thu, 05 Jan 2023 16:04:54 GMT</pubDate>
            <atom:updated>2023-01-05T16:04:54.918Z</atom:updated>
            <content:encoded><![CDATA[<p>Some times when you are working on a project, you might want to store sensitive personal data in your database. Should an unauthorised person get access to your DB, all sensitive information can be read which is obviously not a good practice.</p><p>To solve this problem, you can encrypt the personal data. This way, unauthorised persons cannot read it, but your application can still decrypt it when you need to display or work with the data.</p><p>In this article, I will work you through a step by step approach on how you could use CipherSweet to encrypt sensitive data in your laravel project without stress.</p><p>First, What is CipherSweet?</p><p>CipherSweet is a package developed by <a href="https://paragonie.com/">Paragon Initiative Enterprises </a>for implementing searchable field-level encryption. It can encrypt and decrypt values in a very secure way. It is also able to create blind indexes. These indexes can be used to perform searches on encrypted data. The indexes themselves are unreadable by humans.</p><p>Next, we will need to integrate CipherSweet in our Laravel project using a package from Spatie <a href="https://github.com/spatie/laravel-ciphersweet">Laravel CipherSweet</a> which is a wrapper over CipherSweet, which allows you to easily and conveniently use it with Laravel’s Eloquent models.</p><p>Next, let’s install CipherSweet package via Composer</p><pre>composer require spatie/laravel-ciphersweet</pre><p>After the package is successfully installed, You must publish and run the migrations with:</p><pre>php artisan vendor:publish — tag=”ciphersweet-migrations”<br>php artisan migrate</pre><p>This will create a new table blind_indexes . This table will be use to store a hashed index value along with morph column</p><p>Optionally, you can publish the config file with:</p><pre>php artisan vendor:publish --tag=&quot;ciphersweet-config&quot;</pre><p>This is the contents of the config file:</p><pre>return [<br>    /*<br>     * This controls which cryptographic backend will be used by CipherSweet.<br>     * Unless you have specific compliance requirements, you should choose<br>     * &quot;nacl&quot;.<br>     *<br>     * Supported: &quot;boring&quot;, &quot;fips&quot;, &quot;nacl&quot;<br>     */<br><br>    &#39;backend&#39; =&gt; env(&#39;CIPHERSWEET_BACKEND&#39;, &#39;nacl&#39;),<br><br>    /*<br>     * Select which key provider your application will use. The default option<br>     * is to read a string literal out of .env, but it&#39;s also possible to<br>     * provide the key in a file or use random keys for testing.<br>     *<br>     * Supported: &quot;file&quot;, &quot;random&quot;, &quot;string&quot;<br>     */<br><br>    &#39;provider&#39; =&gt; env(&#39;CIPHERSWEET_PROVIDER&#39;, &#39;string&#39;),<br><br>    /*<br>     * Set provider-specific options here. &quot;string&quot; will read the key directly<br>     * from your .env file. &quot;file&quot; will read the contents of the specified file<br>     * to use as your key. &quot;custom&quot; points to a factory class that returns a<br>     * provider from its `__invoke` method. Please see the docs for more details.<br>     */<br>    &#39;providers&#39; =&gt; [<br>        &#39;file&#39; =&gt; [<br>            &#39;path&#39; =&gt; env(&#39;CIPHERSWEET_FILE_PATH&#39;),<br>        ],<br>        &#39;string&#39; =&gt; [<br>            &#39;key&#39; =&gt; env(&#39;CIPHERSWEET_KEY&#39;),<br>        ],<br>    ],<br>];</pre><p>Next, generate an encryption key. This can be achieve using a command below:-</p><pre>php artisan ciphersweet:generate-key</pre><p>Which will generate the encryption key as show below:</p><pre>Here is your new encryption key<br><br>d35dc725c711e74e42451588b7d03eaf09d4d546fc53d2f8b7cc949eb343eb32<br><br>First, you should encrypt your model values using this command<br>ciphersweet:encrypt &lt;MODEL-CLASS&gt; d35dc725c711e74e42451588b7d03eaf09d4d546fc53d2f8b7cc949eb343eb32<br><br>Next, you should add this line to your .env file<br>CIPHERSWEET_KEY=d35dc725c711e74e42451588b7d03eaf09d4d546fc53d2f8b7cc949eb343eb32</pre><p>Next, add the key to your .env file. The key will be used by your application to read encrypted values..</p><pre>CIPHERSWEET_KEY=d35dc725c711e74e42451588b7d03eaf09d4d546fc53d2f8b7cc949eb343eb32</pre><p>Next step, prepare model and attributes. To achieve this, add interface CipherSweetEncrypted and UsesCipherSweet trait in your selected model. Let’s say we use PasswordReset model. You will need to implement the <strong>configureCipherSweet</strong> method to configure which field you want to encrypt using CipherSweet. See example below</p><pre>class PasswordReset extends Model implements CipherSweetEncrypted<br>{<br>    use HasFactory;<br>    use UsesCipherSweet;<br><br>    protected $table = &#39;password_resets&#39;;<br>    protected $primaryKey = &#39;email&#39;;<br>    protected $fillable = [&#39;token&#39;, &#39;email&#39;, &#39;created_at&#39;, &#39;token_verified_at&#39;];<br>    public $timestamps = false;<br><br>    public static function configureCipherSweet($encryptedRow): void<br>    {<br>        $encryptedRow<br>            -&gt;addField(&#39;token&#39;)<br>            -&gt;addBlindIndex(&#39;token&#39;, new BlindIndex(&#39;token_index&#39;));<br>    }<br>}</pre><p>The example above will encrypt the token field on the PasswordReset model. It will also add a blind index in the blind_indexes table which allows you to search on it.</p><h3>Searching on blind indexes</h3><p>Even though values are encrypted, you can still search them using a blind index. The blind indexes will have been built up when you ran the command to encrypt the model values.</p><p>This package provides a whereBlind and orWhereBlind scope to search on blind indexes.</p><p>The first parameter is the column, the second is the index name you set up when calling -&gt;addBlindIndex, the third is the raw value, the package will automatically apply any transformations and hash the value to search on the blind index. See example below</p><pre>$validateToken = PasswordReset::where(&#39;email&#39;, $request-&gt;email)<br>            -&gt;whereBlind(&#39;token&#39;, &#39;token_index&#39;, $request-&gt;token)-&gt;first();</pre><h3>Rotating keys</h3><p>Should you suspect that somebody got hold of your encrypting key, you can re-encrypt the values. Simply generate another encrypting key, and run the php artisan ciphersweet:encrypt command again.</p><pre>php artisan ciphersweet:encrypt &quot;App\User&quot; &lt;your-new-key&gt;</pre><p>This will update all the encrypted fields and blind indexes of the model. Once this is done, you can update your environment or config file to use the new key.</p><p>We have reached the end of the article. I hope you’ve learned something.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5cbdff84aa52" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>