Friday, 7 October 2016

Web request with HTTP authentication from Powershell

Web requests can be made from Powershell with HTTP authentication in the following 2 ways:

1. Using credential objects
$username = "foo"
$password = "bar" | ConvertTo-SecureString -asPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username,$password)
$res = Invoke-WebRequest http://localhost:3000 -Credential $cred

2. Adding appropriate header
$acctname = "foo"
$password = "bar"
$params = @{uri = 'http://localhost:3000';
                   Method = 'Get'; #(or POST, or whatever)
                   Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($acctname):$($password)"));
           } #end headers hash table
   } #end $params hash table
$var = curl @params

Curl above is same as Invoke-WebRequest because on Windows, by default curl is just an alias to Invoke-WebRequest.

A JSON request with a body can be made as follows:

$res = Invoke-WebRequest http://localhost:3000/stores -Credential $cred -Method POST -Headers @{"Content-Type"="application/json"} -Body (ConvertTo-Json @{"Key"="Value"})

Saturday, 13 August 2016

Frustrating experience with AWS Beanstalk

I have used Azure's Paas services before. For a friend's website, I was trying out AWS's Paas offering today. The experience is nothing less than just frustrating. I have a fully functioning rails application. It is working fine locally. However, when I tried to upload the application using the beanstalk command line, I am seeing the following error.

ERROR: [Instance: i-002808929ac3e534b] Command failed on instance. Return code: 1 Output: (TRUNCATED)...SyntaxError: Invalid CSS after "": expected selector or at-rule, was "{"
/opt/rubies/ruby-2.3.1/bin/bundle:23:in `load'
/opt/rubies/ruby-2.3.1/bin/bundle:23:in `
Tasks: TOP => assets:precompile
(See full trace by running task with --trace).
Hook /opt/elasticbeanstalk/hooks/appdeploy/pre/ failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.

Now the complete log is not available here. You need to log into the instance and check it. However, as the lines show, it is a problem with asset pre-compilation. I tried running asset compilation locally; but I do not see this error. Interestingly, the instance created by the CLI did not allow permission for SSH. It's acceptable source was set to some other IP. It seems to me they launched their product in a hurry.

Wednesday, 22 June 2016


Robocopy is remarkably different from other copy commands. It is often used in msbuild projects but there are a bunch of caveats which would appear non-intuitive to a first time user. The reason people consider using it is because it allows retries and after parameterised wait intervals.

The default syntax of robocopy takes folders as parameters and copies source folder to destination. To copy a file, the syntax is the following:

robocopy C:\source C:\destination filename.ext

Notice that filename is not part of the source here. Also, because of this limitation a file can not be copied to another location with a different name. Due to this, in msbuild projects, you need to copy the file using robocopy (assuming you use robocopy) and then rename it.

Monday, 20 June 2016

Detailed changes of a stash

I have been using git for a few years now. Stashing is one of the very frequent operations I do on git. So, on a number of occasions, I find myself with the need of looking into what the stash contains (Perhaps, I should branch more and stash less). The following command, which shows the detailed changes in a particular stash, does come in handy in those situations.

 git stash show -p stash@{1}

Tuesday, 14 June 2016

Bypassing SSL in WebRequests

Using C#, there are two prevalent ways of making HTTP requests:
- using HttpWebRequest
- using HttpClient

When you have a cancellation token that you want to use, only the later provides the facility for that. So, there are scenarios for each method to be adopted is what I am getting at.

While accessing HTTPS sites whose SSL certificates are not trusted, we manually allow access in case of the browser. The same can b achieved via code using the following methods.

1. Setting ServerCertificateValidationCallback for the request.
This method only for the first method of making HTTP requests.

// provide a custom callback
request.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(SSLValidationDelegate);

// define the callback to allow always
private static bool SSLValidationDelegate(Object o, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors errors){
    return true;

2. Setting ServerCertificateValidationCallback for the ServicePointManager class.
This method essentially does the same but instead of doing it at the request level it does that for all.

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
HttpClient client = new HttpClient();
CancellationTokenSource cts = new CancellationTokenSource();
string url = "https://localhost/some/path";
HttpResponseMessage response = client.GetAsync(url, cts.Token).Result;

So when a cancellation needs to be used,we have to take the second route.

Thursday, 19 May 2016

Last Shutdown time on Windows

Checking the last shut down time on Linux is as simple as reading through logs. On Windows, it is not as straight forward though. Recently, I needed to find the last shutdown time of my Windows desktop. I knew System events is what I should be looking at but searching through the logs is not as easy as a regular expression. After some reading, I found that I can filter the log with the following parameters to search for the shutdown events.

Event source: USER32
Event Id: 1074

Thursday, 28 April 2016

Pull from a shared Windows machine

As a distributed version control system, one of the benefits of using git is that you can pull from anyone's repo just as you would do from a server. With Git's support for SSH, it is very easy to do it on Linux. Two Windows machines don't talk SSH to each other though. So, I was not sure  how to pull from my repo on one Windows machine to another. It turned out equally easy though (may be easier).

git pull file:////<machine_name>E$/path/to/repo/root

Drives like E: and C: get replaced by E$ and C$ respectively. As both of my machines were on the same domain, it was easy. Sharing might come into picture for machines on different domains.

Sunday, 3 April 2016

Adhaar card woes

I have had my Adhaar card for a few years now; much before the government started incentivising it. However, then the card did not have a phone number information with it. Now, when I want to use it, it seems the new portal needs phone authentication. I guess whoever implemented that feature never thought of backward compatibility. Now, even though I have an adhaar card, I can't use it.

I guess I am part of collateral damage in government's march for progress. I talked to adhaar customer care and they told me to go to the Adhaar office at Banjara hills, Hyderabad and meet the manager of the institution, who should be able to update my phone number. When I went there, the manager was a difficult person to find as the place was jam-packed. He told me that he can't do it in the afternoon and asked me to come during the morning some other day.

All this while I wasn't aware that there is an adhaar office at Madhapur. The customer care associate (read moron) had asked me for my PIN code and she could not find which office is nearer to me. So, this time I went to the nearer office. The manager of the first office had told me that the office will be open on weekends too. However, when I went to the second office, it was closed on weekends.

I guess this is the bottom line of "digital India". You still need to make multiple trips to government agencies to get the simplest of things done.

Thursday, 10 March 2016

Prefer getters over directly accessing private variables

While doing some regular debugging, I found a rather interesting property of how variables are evaluated during load time in C#. Consider a scenario as follows.

Let us say we have a class that is connecting an application to another environment. So, instantiating this class can fail in the absence of an environment. We shall simulate that by just throwing an exception for now.

    public class Class1
        public static Class1 GetInstance(){
            throw new Exception();

        public void Functionality()
            // some (hopefully) usable functionality

        private static Class1 singletonInstance = new Class1();

Let us consider the following class using  the above class.

    public class UserClass
        private static Class1 variable = Class1.GetInstance();

        public static void SomeFunctionality()

        public static void SomeOtherFunctionality()


Let us write a test for the above class.

    public class UnitTest1
        public void TestMethod1()

Now this test fails even though the code path being tested does not involve the exception being thrown. That is because when UserClass  is loaded, its private member's instantiation fails due to the exception.

If we modify UserClass as follows, the test passes fine.

     public class UserClass
        public static void SomeFunctionality()

        public static void SomeOtherFunctionality()


The reason is simple: the static call to GetInstance() within the static method is not evaluated because it is not in the code path being executed. In retrospect this is not very un-intuitive.

However it gives me a reason for preferring getters even within the class when lazy evaluation is desired.

        public static void SomeFunctionality()

        private static Class1 getClass1()
            return Class1.GetInstance();

        public static void SomeOtherFunctionality()


Tuesday, 16 February 2016

Steam failure due to SDDM breeze theme

I installed KDE on Arch linux and as suggested I used SDDM with breeze theme. However, it seems breeze is causing issues and it is better to switch back to the default theme.

I had noticed something was off when I first logged in via SDDM. After login, the login screen did not go away; it just stayed as an app with a title bar and I could minimise it. I did not think much about it at that time.

I installed steam and it started fine. The next day when I started steam, it failed. I tried to start it from command line and I got the following error.

/home/dknight/.local/share/Steam/ line 161: VERSION_ID: 
unbound variable
/home/dknight/.local/share/Steam/ line 161: VERSION_ID: 
unbound variable
Running Steam on arch  64-bit
/home/dknight/.local/share/Steam/ line 161: VERSION_ID: 
unbound variable
STEAM_RUNTIME is enabled automatically
Installing breakpad exception handler for appid(steam)/
libGL error: failed to open drm device: Permission denied
libGL error: failed to load driver: i965
libGL error: unable to load driver:
libGL error: failed to load driver: swrast

I was not sure how to fix it so I jumped on to #archlinux channel on Freenode. A user by the handle Namarrgon took interest in my issue. During the course of discussion, I found out that there were a lot of errors with the X session. Upon his suggestion, I ran the following command.

loginctl show-session $XDG_SESSION_ID -p Active -p Remote -p Active

The output is as follows:


This rang a bell as it meant the session wasn't considered active and as a result the right acl weren't set which in turn didn't allow access to /dev/dri/card0. Namarrgon suggested to log out and try using startx to start the X session. When I got a hint that loading X was might be the issue, I remembered that SDDM loads it and it was having issues. So,it might be because of SDDM. Looking at dmesg output I found the following line.

[   61.033982] sddm-greeter[360]: segfault at 30 ip 00007f67673d89bf 
sp 00007ffdb3a81180 error 4 in

Upon seeing this Namarrgon, pointed out to a bug which attributed the SDDM issue to breeze theme.

Wednesday, 13 January 2016

Poor quality of service on order via TinyOwl

The lure: TinyOwl pushed a notification telling me that I had INR 50 wallet balance that would lapse soon.

The need: My cook took a day off. So, maintaining my lazy behaviour, I decided to order food.

Sequence of events:
I ordered from Pita Shop at 9pm. The delivery was supposed to be at 10pm. After the restaurant confirmed the order, I was waiting for the order to arrive. At 9:53pm I got a call from the delivery boy and I explained to him the route to my place. From the restaurant to my place should not take more than 10 minutes. 5 minutes past 10, TinyOwl app asked me if the order had arrived and in response to my negative answer, it triggered a call to a customer service representative who dug up some info and told me that the delivery boy should reach any minute. I thought that was fine and that I could wait a few minutes more. Around 10:15pm, I started ringing the delivery boy to see if he was having any issue with the route but every time I called his phone is either busy or he let my call ring and disconnect. Five minutes later, I started a conversation with TinyOwl support on the app to ask them to look into the matter. They engaged the shop owner who called me to ask about the delivery. He asked me the number of the delivery boy. He told me that he did not employ the delivery boy so he didn't have his number. I was a little surprised but I gave him the number. Finally at 10:40pm the order was delivered. I told the delivery boy to dial the number of the owner of the restaurant. He replied that he didn't have that number. When I told him that the owner should have called him some time back, as the owner had taken his number from me, he told me that he did not get any such call. When I enquired about the reason for the delay, he told me that he had to deliver another order at a place around 4 kms away from my place so he went there first resulting in the delay.

1. The delivery boy wasn't picking my calls but was busy on phone twice. So, he was deliberately avoiding my calls.
2. The shop owner did not employ the delivery boy so his orders will not be on priority.
3. The fact that the shop owner never called the delivery boy only shows his disregard towards customers.

Further observation:
There was no printed bill in the package that was delivered to me.