HOME BLOG

How to be able to switch to another user without entering password

Posted on: September 17th, 2022 by Olu No Comments

Hi folks,

In this post I explain how to switch to another user in Linux without needing to enter a password.

You can do this with the PAM authentication module.

Edit /etc/pam.d/su

vim /etc/pam.d/su

 

Then enter the following lines

auth       [success=ignore default=1] pam_succeed_if.so user = the-username
auth       sufficient   pam_succeed_if.so use_uid user ingroup the-groupname

Save and exit the file.

This will allow you to switch to the-user if you belong to the-group without needing to enter the password for the user using a command like

 su - the-user

 

That’s all for now. Till next time, happy software engineering.

 

References

How to Switch (su) to Another User Account without Password. https://www.tecmint.com/switch-user-account-without-password/

How to use sudo command without password

Posted on: September 17th, 2022 by Olu No Comments

Hi folks,

in this post I discuss how you can use sudo command without password on a Linux system.

Back up your sudoers file

cp /etc/sudoers /etc/sudoers.old

Then, run visudo command

visudo

 

Then enter an entry like

your-user-name ALL=(ALL) NOPASSWD: ALL

 

Then, if visuo uses nano, press Ctrl X to save. Then press enter to finish the save.

Please note: it’s not good security pracrice to let a non-root user run sudo without passwords, so you may want to limit it to some specific commands.

That’s all for now. Till next time, happy software development.

 

References

How to Use sudo Commands Without Password in Linux. https://www.makeuseof.com/using-sudo-without-password/

Some interesting docker commands

Posted on: September 17th, 2022 by Olu No Comments

Hi folks,

Here I will talk about a few interesting commads  to know when working with Docker.

First, what is Docker? According to wikipedia, Docker is a set of platform as a service (PaaS) products that use OS-level virtualization to deliver software in packages called containers. The service has both free and premium tiers. The software that hosts the containers is called Docker Engine. It was first started in 2013 and is developed by Docker, Inc. Docker can package an application and its dependencies in a virtual container that can run on any Linux, Windows, or macOS computer. This enables the application to run in a variety of locations, such as on-premises, in public (see decentralized computing, distributed computing, and cloud computing) or private cloud. When running on Linux, Docker uses the resource isolation features of the Linux kernel (such as cgroups and kernel namespaces) and a union-capable file system (such as OverlayFS) to allow containers to run within a single Linux instance, avoiding the overhead of starting and maintaining virtual machines. Docker on macOS uses a Linux virtual machine to run the containers.

 

How to log in to a Docker artifacts repository

docker login -u someuser -p somepassword the-login-url

 

To build an image, you do

docker build -t name_of_container /path/to/directory-with-dockerfile/

To use a different docker file, you do

docker build -t name_of_container --file=name-of-docker-file /path/to/directory-with-dockerfile/

So, if you’re in the directory with the Dockerfile, just do

docker build -t name_of_container .

 

How to tag your image

docker tag your-image:latest the-repo-url/path/to/your/your-image:latest

 

How to push your image to a repository

docker push the-repo-url/path/to/your/image:latest

 

How to list Docker containers using command

docker ps

 

How to list Docker images using command

docker image ls

 

How to start a container for a particular image, making it run in the background and keeping it running indefinitely (in case it doesn’t already run that way)

docker run -d  tail -f /dev/null

How to log into a container and have a shell (good for debugging)

docker exec -it  /bin/sh

 

How to kill a container

docker kill <container-id>

 

That’s all for now. Till next time, happy software development.

References

Docker (software). < href=”https://en.wikipedia.org/wiki/Docker_(software)”>https://en.wikipedia.org/wiki/Docker_(software),

How to view the list of registered tasks in Celery

Posted on: August 27th, 2022 by Olu No Comments

Hi folks,

In this post I talk about a nice tip that could come in handy while working with Celery. It is how to get all registered Celery tasks. You can use code like:

from celery import current_app
all_task_names = current_app.tasks.keys()
all_tasks = current_app.tasks.values()
foo_task = current_app.tasks['tasks.foo']

all_task_classes = [type(task) for task in current_app.tasks.itervalues()]

The task registry is only populated as the modules containing tasks are imported. If you have not imported all modules you can do like the celery worker does, and import all configured task module sources:

current_app.loader.import_default_modules()

That’s all for now. Till next time, happy software development.

 

References

How to get all tasks and periodic tasks in Celery [duplicate]. https://stackoverflow.com/a/12652113

How to set environment variables on Docker containers via docker-compose file

Posted on: August 18th, 2022 by Olu No Comments

Hi folks,

In this post I briefly go over how to set environment variables in your docker container which is managed by a docker-compose.yaml file.

You can use the env_file flag. Suppose you set environment variables in a file .env, you can read environment variables into a container, service_name as follows:

services:
    service_name:
        container_name: service_name
        ...
        env_file:
          - .env

That’s all for now. Till next time, happy software development.

How to install Python 3.6 on MacOS 12 using pyenv

Posted on: August 17th, 2022 by Olu No Comments

Hi folks,

Here’s a quick note on how to install Python 3.6 on MacOS 12 using pyenv.

I needed to do this while workingon a legacy app. Note that Python 3.6 has reached its end-of-life so you should normally be using a more recent version of the langauge as a developer.

Here is the error I was getting:

python-build: use readline from homebrew
Installing Python-3.6.0...
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
python-build: use zlib from xcode sdk

BUILD FAILED (OS X 12.0.1 using python-build 20180424)

Inspect or clean up the working tree at /var/folders/q9/s5s1hzrd6m1_0by3tx5j6sh40000gn/T/python-build.20220815235540.47954
Results logged to /var/folders/q9/s5s1hzrd6m1_0by3tx5j6sh40000gn/T/python-build.20220815235540.47954.log

Last 10 log lines:
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include   -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include   -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers   -I. -I./Include -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/olu/.pyenv/versions/3.6.0/openssl/include -I/Users/olu/.pyenv/versions/3.6.0/include  -I/usr/local/opt/readline/include -I/usr/local/opt/readline/include -I/Users/olu/.pyenv/versions/3.6.0/openssl/include -I/Users/olu/.pyenv/versions/3.6.0/include   -DPy_BUILD_CORE  -c ./Modules/pwdmodule.c -o Modules/pwdmodule.o
./Modules/posixmodule.c:8146:15: error: implicit declaration of function 'sendfile' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        ret = sendfile(in, out, offset, &sbytes, &sf, flags);
              ^
./Modules/posixmodule.c:10340:5: warning: code will never be executed [-Wunreachable-code]
    Py_FatalError("abort() called from Python code didn't abort!");
    ^~~~~~~~~~~~~
1 warning and 1 error generated.
make: *** [Modules/posixmodule.o] Error 1
make: *** Waiting for unfinished jobs....

After a lot of research, a solution was found no StackOverflow as follows:

CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" 

LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib"

pyenv install --patch 3.6.13 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)

Hopefully this helps others. Till next time, happy software development.

 

References

1. Problems installing python 3.6 with pyenv on Mac OS Big Sur. https://stackoverflow.com/a/68227540.

SonarQube – a tool for monitoring code quality

Posted on: August 6th, 2022 by Olu No Comments

Hi folks,

In this post I will talk about an interesting tool you can use to monitor the quality of your code base when doing software development. The tool is called SonarQube. SonarQube is an open-source platform created by SonarSource and helps with continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs, code smells and vulnerabilities. It supports 17 programming languages.

I really like SonarQube because it can help you write more secure and robust code. It’s web user interface is also quite intuitive to use. It highlights issues and gives suggestions on how to fix them, which is pretty helpful.

You can integrate SonarQube checks into your continuous integration pipeline to do code quality checks automatically when code is merged into certain branches or on pushing code into certain branches of your repository. You can set thresholds for various parameters like bugs, vulnerabilities that can be present in a build before failing the build.

There is a community edition of the software, which is free. There are also other paid versions e.g. Developer, Enterprise and Data Center editions.

Thus, if your team really cares about monitoring code quality, I highly recommend SonarQube. That’s all for now. Till next time, happy software development.

References

SonarQube. Wikipedia. https://en.wikipedia.org/wiki/SonarQube.

Dowloads | SonarQube. https://www.sonarqube.org/downloads/.

pretty-quick: A Great Code Formatting library

Posted on: July 22nd, 2022 by Olu No Comments

Hi folks,

In this post I will talk about a nice code-formatting tool for your web applciations, useful if you use JavaScript, HTML, etc. It’s called pretty-quick.

pretty-quick runs Prettier on your changed files. That is, if you use a version control system like Git and you change some files, pretty-quick will run Prettier on the changed files so that they have a nice format. Information about this library can be found here.

What excites me the most about this library is when using it as a pre-commit hook. This can be done easily using Husky. A guide for this can be found in the link above.

So, if you want nice consistent code formatting for your front-end web application source code, I recommend pretty-quick.

That’s all for now. Till next time, happy software development.

How to mock Python functions with Pytest

Posted on: June 8th, 2022 by Olu No Comments

Hi folks,

In this post I talk about a nice way to mock functions when developing Python applications and using Pytest.

There are many ways of doing mocking in Python. But in this post I will cover how to do it using a nice library called pytest-mock.

To use pytest-mock, you need to install it using a command

 

pip install pytest-mock

 

Say you want to mock a function called is_eligible inside a module called application. You just need to use the mocker fixture which is provided by pytest-mock.

So your function can look as follows.

 

def test_some_function(mocker):
    mocker.patch('application.is_eligible', return_value=True)
    assert some_func_using_is_eligible() == True

 

That’s all for now. Till next time, happy software development.

 

References

Mocking functions in Python with Pytest Part I. https://www.freblogg.com/pytest-functions-mocking-1

Handy SQL query for backups

Posted on: April 13th, 2022 by Olu No Comments

Hi folks,

In this post I share a quick and handy technique for backing up tables within a database. It’s the CREATE TABLE AS statement.

You can use it to create a table by copying data from an existing table e.g.

CREATE TABLE new_table
  AS (SELECT * FROM old_table);

 

You can even back up specific columns or rows that meet certain conditions e.g.

CREATE TABLE suppliers
  AS (SELECT companies.id, companies.address, categories.cat_type
      FROM companies, categories
      WHERE companies.id = categories.id
      AND companies.id > 1000);

 

Note: this is a light-weight backup and shouldn’t be used a substitute for a full backup of the entire database.

That’s all for now. Till next time, happy software development.

 

Reference

SQL: CREATE TABLE AS Statement. https://www.techonthenet.com/sql/tables/create_table2.php