Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SSH Key field when making git datasources #15394

Open
Mailstorm-ctrl opened this issue Mar 11, 2024 · 6 comments
Open

Add SSH Key field when making git datasources #15394

Mailstorm-ctrl opened this issue Mar 11, 2024 · 6 comments
Labels
complexity: medium Requires a substantial but not unusual amount of effort to implement needs milestone Awaiting prioritization for inclusion with a future NetBox release status: backlog Awaiting selection for work type: feature Introduction of new functionality to the application

Comments

@Mailstorm-ctrl
Copy link

Mailstorm-ctrl commented Mar 11, 2024

NetBox version

3.6.3

Feature type

New functionality

Proposed functionality

This is in regards to #14941

Add a field that allows a user to supply a path to an SSH key file to use to authenticate to GitHub.

Use case

GitHub enterprise tenants with Managed Users require the use of an external IdP to authenticate to GitHub. Username and password authentication will not work to the API or to login. Allowing the use of SSH keys allows people using an EMU instance (or just preference) to use git over SSH.

Database changes

Most likely need a new field that will allow a user to supply a SSH key path. I can see a use case for requiring multiple different keys

External dependencies

No response

@Mailstorm-ctrl Mailstorm-ctrl added the type: feature Introduction of new functionality to the application label Mar 11, 2024
@ross-cello
Copy link

Would love to see this implemented

@cpmills1975
Copy link
Contributor

cpmills1975 commented Mar 20, 2024

Please do implement this. My local git repo requires SSH keys to authenticate. Given the complexity of maintaining SSH keys, IMHO it would be worth splitting out the authentication methods from the data sources at this point and creating a 'Credential' model to store the keys in the DB. The Credential model would be one of either username/password combination or SSH key and optional password. The datasource would have a foreign key relationship to the credential making the credential re-usable across several datasources if desired. If Dulwich needs the key in a file (I'll admit to only cursory glances at the docs), then the key would need to be written to a temporary file when any remote file sync activity takes place.

@jeremystretch jeremystretch added the status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation label Mar 20, 2024
@jeremystretch
Copy link
Member

This is open for volunteers.

@markkuleinio
Copy link
Contributor

markkuleinio commented Mar 22, 2024

Does someone know how to even provide the keyfile to porcelain.clone()? I mean, I've tried to provide key_filename="path_to_keyfile" in the call, but it doesn't seem to do anything.

SyncError('Fetching remote data failed (HangupException): git@github.com: Permission denied (publickey).\r')

I don't seem to find any example anywhere.

(aaand update: key_filename works just fine, provided that I also restart netbox-rq, not just netbox... to be continued)

@markkuleinio
Copy link
Contributor

Workaround to use SSH key:

Add this in /etc/systemd/system/netbox-rq.service, in the [Service] section:

Environment="GIT_SSH_COMMAND=ssh -i /path/to/ssh_private_key"

and restart the service:

sudo systemctl daemon-reload
sudo systemctl restart netbox-rq

Limitation: You can only select one keyfile --> If you have more than one data source, you need to authorize the same private key to access all those data sources.

@jeremystretch jeremystretch added complexity: medium Requires a substantial but not unusual amount of effort to implement needs milestone Awaiting prioritization for inclusion with a future NetBox release status: backlog Awaiting selection for work and removed status: needs owner This issue is tentatively accepted pending a volunteer committed to its implementation labels May 21, 2024
@Mailstorm-ctrl
Copy link
Author

Mailstorm-ctrl commented May 22, 2024

Well this is exciting. I got this to work with no code changes...though this is dirty and I'm almost sure it goes against some kind of practice. I have successfully gotten this to work with multiple repos with multiple keys. Here's how I did it:

We are going to be enabling login on the local netbox user on the host server! If you don't want to do this, then sorry you can't do this

  1. Enable local login on the netbox user (or whatever user you used for the system account when initially installing netbox.
    sudo usermod -s /bin/bash netbox
  2. Switch to the user to generate your SSH keys
    sudo su - netbox
  3. Generate your ssh keys and name them whatever you want.
  4. Run eval "$(ssh-agent)" (See here for why)
  5. Run the following for however many keys you made:
    ssh-add <path to sshkey>
  6. In GitHub, create your deploy keys
  7. Back on the netbox host, create or modify the ~/.ssh/config file while still acting as the netbox user. Modify the following to your liking:
Host repo1.github.com
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/key1
Host repo2.github.com
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile ~/.ssh/key2
  1. In netbox, go ahead and add a git datasource. However, for the url you will do something similar to this: git@repo1.github.com:orgname/repo1. Leave the username and password blank. I can't verify if the branch works or not.
  2. Enjoy your SSH connected repos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complexity: medium Requires a substantial but not unusual amount of effort to implement needs milestone Awaiting prioritization for inclusion with a future NetBox release status: backlog Awaiting selection for work type: feature Introduction of new functionality to the application
Projects
None yet
Development

No branches or pull requests

5 participants