This repository was archived by the owner on Jun 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsshme.py
More file actions
38 lines (36 loc) · 1.4 KB
/
sshme.py
File metadata and controls
38 lines (36 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import getpass
import json
import pathlib
import socket
import subprocess
import urllib.request
GITLAB_BASE_URL = "https://gitlab.YOURCOMPANY.com"
user_name = getpass.getuser()
user_password = getpass.getpass()
ssh_key_path = pathlib.Path.home().joinpath(".ssh", "id_ed25519")
if ssh_key_path.is_file():
print("Using existing SSH key:", ssh_key_path)
else:
print("Creating new SSH key...", ssh_key_path)
ssh_key_path.parent.mkdir(exist_ok=True)
subprocess.check_call(["ssh-keygen", "-q", "-t", "ed25519", "-f", str(ssh_key_path), "-N", ""])
pub_key_file = ssh_key_path.with_suffix(".pub")
with pub_key_file.open("r") as file:
ssh_key = file.read()
print("Creating temporary Gitlab access token...")
request = urllib.request.Request(
url=GITLAB_BASE_URL + "/oauth/token",
headers={"Content-Type": "application/json; charset=utf-8"},
data=json.dumps({"grant_type": "password", "username": user_name, "password": user_password}).encode()
)
with urllib.request.urlopen(request) as response:
access_token = json.loads(response.read())["access_token"]
print("Publishing SSH key to Gitlab....")
urllib.request.urlopen(urllib.request.Request(
url=GITLAB_BASE_URL + "/api/v4/user/keys",
headers={
"Content-Type": "application/json; charset=utf-8",
"Authorization": f"Bearer {access_token}",
},
data=json.dumps({"title": socket.gethostname(), "key": ssh_key}).encode()
))