Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Key added via REST API not visibile through web interface

John Lawlor
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 11, 2024

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?

1 answer

0 votes
Navpreet Ahuja
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
January 15, 2024

Hi, Can you please use the below endpoint to add the project SSH key

`http://{baseurl}/rest/keys/latest/projects/{projectKey}/ssh`

Reference: https://developer.atlassian.com/server/bitbucket/rest/v809/api-group-authentication/#api-keys-latest-projects-projectkey-ssh-post

 

Thanks

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events