Atlassian Connect Spring Boot - Unable to make api request to bitbucket rest api

bitbucket-cloud
ac-connect-spring-boot
rest-api

#1

I have created a new addon and have installed the add-on successfully. An atlassianHost is created with base url https://bitbucket.org.

Now within incoming request, I need to make a request to bitbucket rest api whose base url is https://api.bitbucket.org. When making the request with AtlassianHostRestClients, the out going request does not get signed with the JWT tokens as there is no AtlassianHost with the base url.

Because of this I get a 403 access forbidden response.

Is there an alternate way to make this call?

Regards
Tejas


#2

Hi @tejas.shah.ext,

Can you share you descriptor and snippet of your code on how you invoke your request/s?

Cheers,
Anne Calantog


#3

Hi Anne

Below is my descriptor

{
  "key": "Atlassian-CI-Integration",
  "baseUrl": "https://07aadc56.ngrok.io",
  "name": "Atlassian Connect Integration",
  "description": "Connect Addon",
  "authentication": {
    "type": "jwt"
  },
  "modules": {
  "oauthConsumer":{
  	"clientId": "NPC3FLxUpUYKaKgdas"
  },
  
  "repoPage": [
            {
                "url": "/squad-pull-requests?repoPath={repo_path}",
                "name": {
                    "value": "Pull requests - Squads"
                },
                "location": "org.bitbucket.repository.navigation",
                "key": "squad-pull-requests",
                "params": {
                    "auiIcon": "aui-iconfont-devtools-pull-request"
                },
                "weight": 1
            }
        ]
  },
  "lifecycle": {
    "installed": "/installed",
    "uninstalled": "/uninstalled"
  },
  "scopes": ["account", "repository"],
  "contexts": ["personal"]
  
}

I am able to successfully get the incoming calls authenticated and the AtlassianHost entry is created fine. Its just that when I make a call to api.bitbucket.org that I get a 401.

Regards
Tejas


#4

Hi @tshah,

Can you please share a code snippet on how you call http://api.bitbucket.org/ ? Kindly replace any credentials that you use. I’ll try to replicate on my end.

Cheers,
Anne Calantog


#5

Hi @acalantog

It is a pretty straight forward call using AtlassianHostRestClients

package com.vuitton.devops.atlassian.bitbucket;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.atlassian.connect.spring.AtlassianHostRestClients;
import com.vuitton.devops.atlassian.bitbucket.model.RepositoryPullRequestBean;

@Service
public class BitBucketRepositoryManager {
	
	@Autowired
	private AtlassianHostRestClients bitbucketRequestClient;

	public RepositoryPullRequestBean fetchPullRequestsForAllSquads(){
		
		ResponseEntity<RepositoryPullRequestBean> requestBean=bitbucketRequestClient.authenticatedAsAddon().getForEntity("https://api.bitbucket.org/2.0/repositories/tejas_shah_lv/Atlassian-Integration/pullrequests?destination.branch.name=Pre-Release", RepositoryPullRequestBean.class);
		
		return requestBean.getBody();
		
	}
}

Cheers
Tejas


#6

Hi @acalantog

I did a little more digging around. The problem seems to be in the method getHostFromRequestUrl. Apparently the code only allows requests to the base url when using AtlassianHostRestClients. I tried to use a separate rest client to directly make the call by creating a jwt token. But even this requires the base url to be used for creating the token.

Cheers
Tejas


#7

Hi @acalantog

Any ideas about this? The only option I see is to use the low level api to create the jwt token from the atlassianHost.

Cheers
Tejas


#8

Hi @tshah,

Apologies, I’m not particularly familiar developing Bitbucket cloud apps with Spring. AFAIK, the scaffold Spring Connect Integration project is specifically for Jira and Confluence. For Bitbucket, as they have different set of format, you could use the Connect framework and there it automatically generates the token for you.

But if you want to use Spring and Java in this case, then my best bet is for you is to use Atlassian’s JWT library for Java: Atlassian JWT Libriaries

Related articles:

Cheers,
Anne Calantog