Using the following python code, I added an SSH key to bitbucket (version 8.9.5):
#!/usr/bin/python
import os
import sys
import json
import base64
import logging
import time
import datetime
import requests
from requests.auth import HTTPBasicAuth
bitbucketBaseUrl = "bitbucket.company.com"
bitbucketUserName = "admin"
logging.basicConfig(filename='post-migration-updates.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s', level=logging.INFO)
public_key = "ssh-rsa key-text your_email@example.com"
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
def addAccessKey(projectKey, bitbucketPassword):
logging.info("Update project with key: {}".format(projectKey))
data = {
"text": public_key,
"permission": "PROJECT",
"projects": projectKey,
}
url = "https://{}/rest/ssh/1.0/keys/".format(bitbucketBaseUrl)
logging.info("REST URL {}".format(url))
response = requests.post(
url,
auth=HTTPBasicAuth(bitbucketUserName,bitbucketPassword),
json=data,
headers=headers
)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))
def validateScriptParameters():
if len(sys.argv) != 3:
sys.exit("Usage: {} [admin password] [Project Key e.g. FW]".format(os.path.basename(sys.argv[0])))
validateScriptParameters()
logging.info("Connecting to bitbucket")
bitbucketPassword = sys.argv[1].strip()
projectKey = sys.argv[2].strip()
print projectKey
addAccessKey(projectKey, bitbucketPassword)
I get the following response:
{
"algorithmType": "RSA",
"bitLength": 3072,
"createdDate": 1704983323891,
"id": 7185,
"label": "your_email@example.com",
"text": "ssh-rsa key-text= your_email@example.com"
}
When I browse to https://bitbucket.company.com/plugins/servlet/ssh/projects/<project key>/keys, the key is not there. I can't see it even as the system administrator.
(Key contents have been redacted)
Any thoughts?
Hi, Can you please use the below endpoint to add the project SSH key
`http://{baseurl}/rest/keys/latest/projects/{projectKey}/ssh`
Thanks
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.