Showing posts with label Golang. Show all posts
Showing posts with label Golang. Show all posts

Monday, February 3, 2020

How to enable ASP.NET error pages using Azure Serial Console

It's possible to enable ASP.NET error pages on Azure by using the new Azure Serial Console. Let's see how.
By default, ASP.NET web applications running on a remote server set the customErrors property to "RemoteOnly". That means that, unless you're running on the local server, you won't be able to view the original error and the stack trace related it. And that's a good thing! A lot of successful hacks derive from understanding the exception messages and working around them.

But what if you're testing a new server, a new deployment process or just released a new feature and need to enable the error pages very quickly? Well, if you're using Azure, you can use Azure Serial Console to do the job. No SSHing, no RDPing or uploading of configurations to the remote environment. Let's see how.

Azure Serial Console

Today we will use Azure Serial Console. According to Microsoft:
The Serial Console in the Azure portal provides access to a text-based console for virtual machines (VMs) and virtual machine scale set instances running either Linux or Windows. This serial connection connects to the ttyS0 or COM1 serial port of the VM or virtual machine scale set instance, providing access independent of the network or operating system state. The serial console can only be accessed by using the Azure portal and is allowed only for those users who have an access role of Contributor or higher to the VM or virtual machine scale set.
In other words, Azure Serial Console is a nice, simple and accessible tool that can be run from the Azure portal allowing us to interact with our cloud resources including our Azure App Services.

Accessing the console

To access the console for your web application, first we find our Azure App Service in the Portal by clicking on App Services:
Selecting the web site we want to open:
And click on Console on the Development Tools section. You should then see a shell similar to:

Using the Console

Now the fun part. We are ready to interact with our App Service directly from that shell. For starters, let's get some help:
The above screenshot shows some of the administrative commands available on the system. Most of them are standard DOS command prompt utilities that you probably used on your Windows box but never cared to learn. So what can we do?

Linux Tools on Azure Serial Console

Turns out that Redmond is bending to the accessibility, ubiquity and to the power of POSIX / open source tools used and loved by system administrators such as ls, diff, cat, ps, more, less, echo, grep, sed and others. So before jumping to the solution, let's review what we can do with some of these tools.
Example 1: a better dir with ls
Example 2: Creatting and appending content to files using echo, pipes and cat
Example 3: getting disk information with df
Example 4: viewing mounted partitions with mount
Example 5: Displaying differences between files using diff
Example 6: Getting kernel information using uname
Example 7: Even curl and scp is available!

Disabling Custom Errors

Okay, back to our problem. If you know some ASP.NET, you know that the trick is to modify the customErrors Element (ASP.NET Settings Schema) and set the property to   Off   . So let's see how we can change that configuration using a command line tool.

Backing up

Obviously we want to backup our web.config. I hope that's obvious with:
cp web.config web.config.orig

Using sed to replace configuration

Now, we will use sed (a tool available on the GNU operating system that Linux hackers can't live without) to change the setting directly from the console. I'm a sed geek and use it extensively in a Hugo project I've been working on (thousands of markdown files). Together with Go, the i3 window manager, Vim, ranger and grep, my Fedora workstation becomes an ideal development environment. Now, back to .NET...

Testing the Patch

We can safely test if our changes will work by typing:
sed 's/RemoteOnly/Off' web.config

Applying the Patch

Let's jump right to how to replace our customErrors element from   RemoteOnly   to   Off   ? The solution is this simple one-liner script:
sed -i 's/RemoteOnly/Off/' web.config

Switching Back

Now, obviously we may want to switch back. That's why it was important to backup your web.config before. We can switch back by replacing the changed web.config with the original:
rm web.config
mv web.config.orig web.config
Or by running sed again, this time with the parameters inverted:
sed -i 's/Off/RemoteOnly/' web.config

Security Considerations

I hope I don't need to repeat that it's unsafe to leave error pages off on your cloud services. Even if they are simply a playground, there are risks of malicious users pivoting to different services (like your database) and accessing confidential data. Please disable them as soon as possible.

What about Kudu?

Yes, Azure Kudu allows editing files on a remote Azure App Service by using a WISIWYG editor. However, we can't count on that always, everywhere. Remember, with the transition to a microservice-based architecture, more and more our apps will run on serverless and containerized environments meaning tools like that wouldn't be available. So the tip presented on this post will definitely stand the test of time! 😉

Final Thoughts

Wow, that seems a long post for such a small hack but I felt the need to stress certain things here:
  1. Developers shouldn't be afraid to use the terminal - I see this pattern especially with Microsoft developers assuming that there should always be a button to do something. The more you use the terminal, the more confident you'll be with the tools you're using regardless of where you are. 
  2. Microsoft is moving towards Linux and you should too - The GNU tools prove an unimaginable asset to know. Once you know how to use them better, you'll realize that your toolset grows and you get more creative getting things faster. Plus, the ability to pipe output between them yields unlimited possibilities. Don't know where to start? WSL is the best way to learn the Linux on Windows 10.
  3. Be creative, use the best tool for the job - choose wise the tool you use. Very frequently the command line is the fastest (and quickest) way to accomplish most of the automatic workflow. And it can be automated!

Conclusion

The Azure Serial Console can be a powerful tool to help you manage, inspect, debug and run quick commands against your Azure App Service and your Virtual Machines. And combined with the Linux tools it becomes even more powerful!

And you, what's your favorite hack?

References

See Also

Monday, November 12, 2018

Windows Subsystem for Linux, the best way to learn Linux on Windows

Want to learn Linux but don't know how/where to start? WSL may be a good option.
In 2018, Microsoft released the Windows Subsystem for Linux (WSL). WSL lets developers run the GNU/Linux shell on a Windows 10 PC, a very convenient way to access the beloved tools, utilities and services Linux offers without the overhead of a VM.
WSL is also the best way to learn Linux on Windows!

About WSL

Currently WSL supports Ubuntu, Debian, Suse and Kali distributions and can:
  • run bash shell scripts 
  • run GNU/Linux command-line applications including: vim, emacs, tmux
  • run programming languages like JavaScript, Node.js, Ruby, Python, Golang, Rust, C/C++, C# & F#, etc.
  • run background services like ssh shells, MySQL, Apache, lighttpd;
  • install additional software using own GNU/Linux distribution package manager.
  • invoke Windows applications.
  • access your Windows filesystem

Installing WSL on Windows 10

Installing WSL is covered by Microsoft on this article and is as easy is two steps.

Step 1 - Run a Powershell Command

On your Windows PC, you will need to run this PowerShell script as Administrator (shift + right-click):
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows -Subsystem-Linux
After the installation ends, restart your PC.

Step 2 - Install WSL from the Windows Store

After the reboot, WSL can be installed through the Windows Store. To open the Windows Store on your Windows 10, click:
Start -> Type Store -> Click on the Windows Store:
Then type "Linux" on the search box and you should get something similar results to this:

Click on the icon, accept the terms and Windows will download and install WSL for you.

Running WSL

After installation started, you will be prompted to enter your username and password. After done, you'll get a cool Linux terminal to start playing with. You can even have multiple Distros installed on your Windows 10 machine. On mine, I installed Debian and Ubuntu.

Using the Terminal

Okay, so now that we have access to our Linux shell, what to do next? Let's go through these use cases:
  • Accessing my Windows files
  • Access internet resources
  • I install software

Accessing Windows Files

WSL mounts your Windows files on the /mnt/c mount point. To verify on yours type mount on the command prompt and look for C: on it. Your windows files should be there.
In case you don't know Linux, listing files is done with   ls  . This is the content of my C drive as as seen from WSL:

Accessing the Internet

Your WSL instance should have access to the internet. Testing the internet is as simple as doing a ping to Google:
You can also verify your network info with ifconfig:
 

Installing Software

Installing software on Ubuntu/Debian is done by the apt command. For example, this is how we search packages:
To install packages, use apt-get install. For example, to install Ruby on the Ubuntu WSL, run the command below:
sudo apt-get install ruby-full

Using git

We can leverage apt and install git with:
sudo apt-get install git
... # apt installs git
git --help # to get help
And, I'd recommend learn to use it on the terminal. Atlassian has an excellent tutorial to learn git.

Getting Help

Need help? The man tool is there to help you. For example, we could run the commands below to get help on git for example:
man git

Additional tip: try the new Windows Terminal

And, if you want to invest more time on your WSL, I'd suggest that you install the new Windows Terminal. Download the last release from GitHub and install it on your box. It's very customizeable and contains shells for WSL, PowerShell, Azure CLI and the traditional Windows terminal.

What's next?

Now that you know how to locate your files, have access to the internet and installed some software, I'd recommend that you:

Conclusion

Congratulations! You have the WSL installed on your machine and now you have a Linux terminal to starting playing with. Now what? The first thing I'd recommend is to get comfortable with basic system commands, understand the filesystem, learn to add/remove software and run administrative tasks on the terminal. WSL is perfect for users who want to learn Linux and to those who spent a lot of time on Windows but need access to a Linux terminal.

If you want to know more about my setup, here's why I use Fedora Linux with the fantastic i3 window manager on the desktop and CentOS on servers. Happy hacking!

References

See Also

    Tuesday, November 6, 2018

    Why use Fedora

    Ubuntu may be the most popular Linux distribution however Fedora may be a better alternative for your desktop. Read to understand.
    Photo by Clem Onojeghuo on Unsplash

    Fedora Linux is a fantastic operating system. On this post I'd like to explore the reasons why I use and recommend Fedora as my daily driver, and present you with some of the reasons why I think it could be your next (and last) Linux distribution.

    It's 2023 and there are endless praises to Fedora in YouTube. TL;DR: Since this post was written (7 years ago), Fedora became the most recommended Linux distro for users that want the best Linux experience. Hope you enjoy the article!

    My personal journey

    Everyone has their own Linux journey. In the beginning it's common for people to try out many distributions until they settle down to one they're comfortable with. In the end, it's all about trade offs. So let let me tell you mine and how I got to choosing Fedora as my main Linux Distribution.

    I always used Linux... in dual-boot mode. My journey with it started two decades ago when I was introduced to Slackware Linux. What I loved in Slackware was the level of granularity in which I could tweak my system. But soon I realized that Slackware required a lot of hands on and quickly moved to Red Hat Linux (before RHEL) which provided a more streamlined experience with its package-based system based on yum/RPMs.

    Since then I've tried (in that order): Mandriva, Fedora Core, OpenSuse and more recently Ubuntu, Mint, elementary OS, Xubuntu, Lubuntu, Arch, Manjaro and SOLUS. Yes, call me a distrohopper. Then I decided to retry Fedora 24 and check how it had progressed. Eight years later, Fedora remains my primary desktop.

    With all that said, let me describe some of the reasons I think you should consider using Fedora on your desktop.

    Why I chose Fedora

    So let's explore some honest reasons to use Fedora.

    Simplicity

    Simplicity definitely comes first. Simplicity is one of the characteristics that I enjoy the most in Fedora and is the reason I often recommend it to new users. From the installation to the default UI, everything seems integrated, accessible, fast and intuitive to use. Plus, the default GNOME desktop is pretty solid, its animations are smooth and the performance is really good.

    A Fedora workstation running Gnome 3.30 / Source

    Stability

    Fedora is very stable. And differently from what people think, Fedora is not a beta-testing environment for RHEL. Packages are tested on its development channel, validated and once stable with all dependencies met, released to millions of users. Be sure that for each update that you get, hundreds of hours of tests were performed.

    Security

    Fedora shines on the security aspect. We know that online security is way more than keeping the system up to date is as important as having a good anti-virus software, a working firewall and safe web browsing habits. Fedora counts with a robust SELinux integration and given its First Principle, Fedora users frequent more up to date software and kernel updates than all other non-rolling release distros.
    For those who don't know, SELinux was developed by the NSA and is the standard access control tool for critical services blocking suspicious behavior by default.

    Privacy

    Linux users shouldn't be concerned about privacy. Or should we?  Fedora discloses its Privacy Policy publicly and adheres to it as to its core principles: Freedom, Friends, Features and First. Differently from other distributions, Fedora never sent your desktop search results to e-commerce sites, or contains intrusive telemetry that cannot be turned off.

    Development-Friendly

    Developers are pretty much covered with everything on the development site. You will find  Go, Rust Docker, Swift, Python, NodeJs, Ruby, Java,  among others can be found in the repos. Anything else? You will find it there.

    Performance

    Fedora performs. My 5 yr-old laptop boots in less than 10 seconds. The performance is mainly due to more up to date software and to keeping the OS running only with the necessary resources. Yes there are faster options and yes, Gnome is not as light as other window managers (but got really better as of Gnome 3.36) but it's still the best choice for the average user.

    Freedom

    With Fedora, free alternatives are chosen to proprietary code and content and limit the effects of proprietary or patent encumbered code on the Project. Releases that are predictable and include only 100% free software.

    Community-Driven

    Differently from other distros which are subject to private interests, Fedora is driven by a community of dedicated and passionate enthusiasts.  All communication is open to the public and everyone is invited to collaborate! The Fedora communities on the internet are pretty receptive and you'll always find someone willing to help you.

    Reliability

    The Fedora project serves as the base for RHEL and CentOS. So it needs to be mature, it needs to be stable. It's a very serious Linux desktop operating system used daily by millions of users and servers around the world. It needs to be reliable. Personally, I had zero problems in my last 5 years doing distribution upgrades.

    Cutting-Edge Software

    Fedora is frequently mentioned as the distribution that explores the outer limits of what Linux can do. But that's no news to Fedora users since is the First foundation adopted by the project. Fedora repos usually land features way earlier than Ubuntu and derivatives (Elementary, Mint and Pop!_OS) which IMHO is as best as it can be without being a rolling release. And remember, you can always add different repos or install software trough universal Linux packages like Flatpak.

    Frequent Updates

    Fedora updates are frequent. Sometimes, multiple times a week I get software upgrades covering security issues, performance, stability and even Kernel updates. It's also common to get multiple kernel updates per release. That usually means running a more mature, stable and secure kernel. It also means better performance and supported hardware too.

    Impressive Software Selection

    The default package repository contains all the software you will ever need. Plus, in Fedora the software can be managed by the powerful DNF package manager. For example, this is the number of packages (software) available from the repository ran from my XFCE instance:
    DNF also handles distribution updates which happen twice a year.

    Custom Software Repositories

    The amount of software available on the official Fedora repo is incredible. Probably everything that you need, you can find there. But if you're not covered for the package selection on the free/default repo, you still could make use of RPM Fusion to install software that doesn't adhere to Fedora's requirements.

    Not to mention Copr (Cool Other Package Repo), a Fedora project to help  building and managing third party package repositories easy. Copr is hosted under the Fedora Infrastructure and allows developers to create repos so they can be shared with users.

    Excellent Hardware Support

    Fedora has excellent support for hardware. I don't have cutting-edge hardware neither am a gamer so I cannot comment on that but for most people, both the Fedora installer and the kernel are very good at recognizing and activating the hardware. That's probably a good reason why most people stop distrohopping when they get to Fedora. Most of the issues (including lack of hardware support) disappear.

    Incubator for new features

    The Fedora community creates many of the technical features that have made Linux powerful, flexible, and usable for a wide spectrum of millions of users, administrators, and developers worldwide. In fact, this is the Missions and Foundations of the project:
    The Fedora community prefers approaches that benefit the progress of free software in the future over those that emphasize short term ease of use.
    Some of the features developed with Fedora include:
    • The Linux Kernel - there are hundreds of kernel hackers worldwide using Fedora. The most famous of them is Linus Torwalds, the creator of Linux ("It just works").
    • Wayland - a new display system replacing the venerable X.org.
    • XWayland - extensions to the X server so it can be run as a Wayland protocol.
    • systemd - Linux's default init system and used on 95% of the modern and most popular distros.
    • GNOME - a free and open-source desktop environment for Unix-like operating systems. 
    • The GTK toolkit - a cross-platform widget toolkit for creating graphical user interfaces.
    • PipeWire - a new audio and video subsystem
    • Flatpak - application packaging
    • The cockpit project - Manager your server in a web browser and perform system tasks with a mouse.
    • Anaconda installer - Anaconda is a free and open-source system installer for Linux distributions used in Fedora, RHEL, CentOS and other Linux distributions. 
    • Podman - a tool to create and maintain containers.
      Buildah - a tool to create and manage container images
    • Silverblue - a next-gen OS for the desktop 
    • Fedora CoreOS - a next-gen cloud appliance. 
    • Modularity - Modularity is a mechanism of making multiple versions of software available to your system.

    Two upgrades per year

    Because Fedora releases happen twice a year, you'll get big system updates (including Gnome, GCC and base libs) twice a year. It's the best way to have up to date software without the complexities and issues rolling release distros have. Upgrading is as simple as 2 clicks on the Software app or running the below on your terminal:
    sudo dnf upgrade --refresh
    sudo dnf install dnf-plugin-system-upgrade
    sudo dnf system-upgrade download --releasever=XXX
    sudo dnf system-upgrade reboot

    Universal Linux Packages

    You know you can install software using the package repos. Another option is using Snap or FlatPak. And installation is as simple as opening Software -> Searching and clicking install.

    RHEL

    Many people ignore this but another strong reason to use Fedora is because it's RHEL's upstream. You'll be using with Yum/DNF, SystemD, SELinux, and testing what will be available on the next RHEL, the leading Linux-based OS on servers. Invaluable knowledge to have.

    Variants 

    Don't like GNOME? Fedora also ships different spins including KDE, Xfce, LXQt, MATE, Cinnamon, LXDE and others. All available in both 64-bit and 32-bit versions. It has even support for ARM and IoT.

    Fedora in some of its variants
    You can also find on Labs, variants that are target to designers, astronomers, scientists and musicians.

    A multitude of options for your desktop

    Don't like the default? You can always change. For example, check this post to learn more about a multitude of desktops and window managers available on Fedora 31. Among my favorites:
    • i3 (and its Wayland alternative Sway) - my personal choice ❤!
    • XFCE - also use on my cloud instances, including on my Ubuntu instance on azure
    • KDE - an excellent desktop for your Linux PC
    • GNOME - I also have it installed at home as a fallback in case my i3 breaks (never happened 😊)
    • And another multitude of options - counting at 38 alternatives to customize your desktop
    Still on tiling window managers, for a couple of years now I'm using the i3 window manager (shown on the screenshot below) and I love it! For more information check my post on why i3 is awesome, how to install it and why it will change the way you use your Linux desktop.
    Source: DevianArt

    So, Fedora is perfect?

    Of course not! No software is perfect as no software will ever be bug-free. However, it's important to consider that open-source operating systems are extremely complex software bundles composed of thousands of packages developed by volunteers worldwide.

    Fedora provides the best balance in what matters most: open-source software, up-to-date software and a strong focus on stability, security and privacy. Plus, you can find on Fedora and its derivatives everything you're looking for. Need a more stable environment for your server or container? Go with Fedora Server or CentOS. Want to go even more cutting edge on the desktop? Try Silverblue. Need a lightweight alternative for that old netbook? Go with Fedora XFCE. Using ARM on a Pi or on a Pinebook Pro? Try Fedora ARM!

    Fedora also shines on its community aspect. The community is very open for onboarding newcomers and volunteers. And as previously mentioned, everyone is invited to collaborate, even if it's just to write articles for the Fedora Magazine. All communication is shared online and the teams are structured to help newcomers trough mentoring programs.

    Conclusion

    Fedora is a polished, modern, stable, secure and privacy-focused GNU/Linux operating system. With all its variants, I'm pretty sure there will be something for you too! If you have never used it, I would suggest that you try it, regardless of your technical background. If you use another distro, I would kindly recommend that you to download Fedora, install it on a VM and give it a try. And don't hesitate to send a big thank you to the @Fedora community on Twitter.

    References

    See Also

    About the Author

    Bruno Hildenbrand      
    Principal Architect, HildenCo Solutions.