Implementing (Psuedo) Profiles in Git (Part 2!)
Tags: authentication, git, ssh
Published: 2024-07-09 19:45:32

---
title: Implementing (Psuedo) Profiles in Git (Part 2!)
published: true
date: 2024-07-03 18:58:00 UTC
tags: authentication,git,ssh
canonical_url: https://thjones2.blogspot.com/2024/07/implementing-psuedo-profiles-in-git.html
---

 As noted in my first [_Implementing (Psuedo) Profiles in Git_](https://www.blogger.com/blog/post/edit/3054063691986932274/8861509966346951193) post:

> I'm an automation consultant for an IT contracting company. Using git is a daily part of my work-life. … Then things started shifting, a bit. Some customers wanted me to use my corporate email address as my ID. Annoying, but not an especially big deal, by itself. Then, some wanted me to use their privately-hosted repositories and wanted me to use identities issued by them.

This led me down a path of setting up multiple git "profiles" that I captured into my first article on this topic. To better support such per-project identities, it's also a good habit to use per-project authentication methods. I generally prefer to do git-over-SSH – rather than git-over-http(s) – when interfacing with remote Git repositories. Because I don't like having to keep re-entering my password, I use an SSH-agent to manage my keys. When one only has one or two projects they regularly interface with, this means a key-agent that is only needing to store a couple of authentication-keys.

Unfortunately, if you have more than one key in your SSH agent, when you attempt to connect to a remote SSH service, the agent will iteratively-present keys until the remote accepts one of them. If you've got three or more keys in your agent, the agent could present 3+ keys to the remote SSH server. By itself, this isn't a problem: the remote logs the earlier-presented keys as an authentication failure, but otherwise let you go about your business. However, if the remote SSH server is hardened, it very likely will be configured to lock your account after the third authentication-failure. As such, if you've got four or more keys in your agent and the remote requires a key that your agent doesn't present in the first three autentication-attempts, you'll find your account for that remote SSH service getting locked out.

What to do? Launch multiple ssh-agent instantiations.

Unfortunately, without modifying the default behavior, when you invoke the ssh-agent service, it will create a (semi) randomly-named UNIX domain-socket to listen for requests on. If you've only got a single ssh-agent instance running, this is a non-problem. If you've got multiple, particularly if you're using a tool like [direnv](https://github.com/direnv/direnv), setting up your `SSH_AUTH_SOCKET` in your `.envrc` files is problematic if you don't have predictably-named socket-paths.

How to solve this conundrum? Well, I finally got tired of, every time I rebooted my dev-console, having to run `eval $( ssh-agent )` in per-project Xterms. So, I started googling and ultimately just dug through the man page for ssh-agent. In doing the latter, I found:

```
DESCRIPTION
ssh-agent is a program to hold private keys used for public key authentication. Through use of environment variables the
agent can be located and automatically used for authentication when logging in to other machines using ssh(1).

The options are as follows:

-a bind_address
Bind the agent to the UNIX-domain socket bind_address. The default is $TMPDIR/ssh-XXXXXXXXXX/agent.<ppid>.
```

So, now I can add appropriate command-aliases to my bash profile(s) (which I've already moved to `~/.bash_profile.d/<PROJECT>`)  that can be referenced based on where in my dev-console's filesystem hierachy I am and can set up my `.envrcs`, too. Result: if I'm in `<CUSTOMER_1>/<PROJECT>/<TREE>`, I get attached to an ssh-agent set up for _that_ customer's project(s); if I'm in `<CUSTOMER_2>/<PROJECT>/<TREE>`, I get attached to an ssh-agent set up for _that_ customer's project(s); etc..