Skip to content

Commit b0c1ea6

Browse files
Add personal access token support (#495)
This authentication method is only available for Bitbucket Server but not for Bitbucket Cloud. It has been introduced in Bitbucket Server 5.5: - https://confluence.atlassian.com/bitbucketserver/bitbucket-server-5-5-release-notes-938037662.html Documentation can be found here: - https://confluence.atlassian.com/bitbucketserver0716/personal-access-tokens-1086402495.html Co-authored-by: Günter Grodotzki <gunter@grodotzki.com>
1 parent 13f54c8 commit b0c1ea6

File tree

8 files changed

+120
-14
lines changed

8 files changed

+120
-14
lines changed

docs/USER_GUIDE.adoc

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,27 +99,40 @@ properly configured in _Manage Jenkins_ » _Configure System_
9999
[id=bitbucket-creds-config]
100100
== Credentials configuration
101101

102-
The configuration of Bitbucket plugin (for both _Bitbucket Multibranch_ projects and _Bitbucket Team/Project_) has
102+
The configuration of the plugin (for both _Bitbucket Multibranch_ projects and _Bitbucket Team/Project_) has
103103
two credentials to configure:
104104

105-
. *Scan Credentials*: credentials used to access Bitbucket API in order to discover repositories, branches and pull requests.
106-
If not set then anonymous access is used, so only public repositories, branches and pull requests are discovered and managed. Note that the
107-
Webhooks auto-register feature requires scan credentials to be set. Only HTTP or OAuth credentials are accepted in this field.
108-
. *Checkout Credentials*: credentials used to check out sources once the repository, branch or pull request is discovered. HTTP, SSH and OAuth credentials
109-
are allowed. If not set then _Scan Credentials_ are used.
105+
. *Scan Credentials*: Credentials used to access Bitbucket API in order to discover repositories, branches and pull requests.
106+
If not set then anonymous access is used, so only public repositories, branches and pull requests are discovered and managed.
107+
Note that the Webhooks auto-register feature requires scan credentials to be set.
108+
HTTP Basic Authentication, Access Token and OAuth credentials are supported.
109+
. *Checkout Credentials*: Credentials used to check out sources once the repository, branch or pull request is discovered.
110+
HTTP Basic Authentication, SSH and OAuth credentials are supported.
111+
If not set then _Scan Credentials_ are used.
110112

111113
image::images/screenshot-3.png[scaledwidth=90%]
112114

115+
=== Access Token
116+
117+
The plugin can make use of an access token (Bitbucket Server only) instead of the standard username/password.
118+
119+
First create a new _personal access token_ in Bitbucket as instructed in the https://confluence.atlassian.com/bitbucketserver0716/personal-access-tokens-1086402495.html[Bitbucket Personal Access Tokens Documentation].
120+
At least allow _read_ access for repositories. If you want the plugin to install the webhooks, allow _admin_ access for repositories.
121+
122+
Then create new _Secret text credentials_ in Jenkins and enter the Bitbucket personal access token value in the _Secret_ field.
123+
124+
When configuring a multi-branch project, add the _Checkout over SSH_ behavior to a branch source, as the token can only be used for the Bitbucket API.
125+
113126
=== OAuth credentials
114127

115-
Bitbucket plugin can make use of OAuth credentials instead of the standard username/password.
128+
The plugin can make use of OAuth credentials (Bitbucket Cloud only) instead of the standard username/password.
116129

117-
First create a new OAuth consumer as instructed in https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html[Bitbucket OAuth Documentation].
118-
Don't forget to check _This is a private consumer_ and at least allow read access to the repositories and Pull requests. If you want the Bitbucket to install the Webhooks also allow the read and write access of the Webhooks
130+
First create a new _OAuth consumer_ in Bitbucket as instructed in the https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html[Bitbucket OAuth Documentation].
131+
Don't forget to check _This is a private consumer_ and at least allow _read_ access for repositories and pull requests. If you want the plugin to install the webhooks, also allow _read_ and _write_ access for webhooks.
119132

120133
image::images/screenshot-10.png[scaledwidth=90%]
121134

122-
Then create new _Username with password credentials_, enter the Bitbucket OAuth consumer key in _Username_ field and the Bitbucket OAuth consumer secret in _Password_ field
135+
Then create new _Username with password credentials_ in Jenkins, enter the Bitbucket OAuth consumer key in the _Username_ field and the Bitbucket OAuth consumer secret in the _Password_ field.
123136

124137
image::images/screenshot-11.png[scaledwidth=90%]
125138

@@ -128,7 +141,7 @@ image::images/screenshot-12.png[scaledwidth=90%]
128141
[id=bitbucket-misc-config]
129142
== Miscellaneous configuration
130143

131-
In case of slow network, you can increase socket timeout using the link:https://jenkins.io/doc/book/managing/script-console/[Script Console] :
144+
In case of slow network, you can increase socket timeout using the link:https://jenkins.io/doc/book/managing/script-console/[Script Console]:
132145

133146
[source,groovy]
134147
----

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@
148148
<artifactId>authentication-tokens</artifactId>
149149
<version>1.3</version>
150150
</dependency>
151+
<dependency>
152+
<groupId>org.jenkins-ci.plugins</groupId>
153+
<artifactId>plain-credentials</artifactId>
154+
</dependency>
151155
<dependency>
152156
<groupId>org.scribe</groupId>
153157
<artifactId>scribe</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.cloudbees.jenkins.plugins.bitbucket.api.credentials;
2+
3+
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
4+
import hudson.util.Secret;
5+
import org.apache.http.HttpHeaders;
6+
import org.apache.http.HttpRequest;
7+
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
8+
9+
/**
10+
* Authenticator that uses an access token.
11+
*/
12+
public class BitbucketAccessTokenAuthenticator extends BitbucketAuthenticator {
13+
14+
private final Secret token;
15+
16+
/**
17+
* Constructor.
18+
*
19+
* @param credentials the access token that will be used
20+
*/
21+
public BitbucketAccessTokenAuthenticator(StringCredentials credentials) {
22+
super(credentials);
23+
token = credentials.getSecret();
24+
}
25+
26+
/**
27+
* Provides the access token as header.
28+
*
29+
* @param request the request
30+
*/
31+
public void configureRequest(HttpRequest request) {
32+
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token.getPlainText());
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.cloudbees.jenkins.plugins.bitbucket.api.credentials;
2+
3+
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
4+
import edu.umd.cs.findbugs.annotations.NonNull;
5+
import hudson.Extension;
6+
import jenkins.authentication.tokens.api.AuthenticationTokenContext;
7+
import jenkins.authentication.tokens.api.AuthenticationTokenSource;
8+
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
9+
10+
/**
11+
* Source for access token authenticators.
12+
*/
13+
@Extension
14+
public class BitbucketAccessTokenAuthenticatorSource extends AuthenticationTokenSource<BitbucketAccessTokenAuthenticator, StringCredentials> {
15+
16+
/**
17+
* Constructor.
18+
*/
19+
public BitbucketAccessTokenAuthenticatorSource() {
20+
super(BitbucketAccessTokenAuthenticator.class, StringCredentials.class);
21+
}
22+
23+
/**
24+
* Converts string credentials to an authenticator.
25+
*
26+
* @param credentials the access token
27+
* @return an authenticator that will use the access token
28+
*/
29+
@NonNull
30+
@Override
31+
public BitbucketAccessTokenAuthenticator convert(@NonNull StringCredentials credentials) {
32+
return new BitbucketAccessTokenAuthenticator(credentials);
33+
}
34+
35+
/**
36+
* Whether this source works in the given context.
37+
*
38+
* @param ctx the context
39+
* @return whether this can authenticate given the context
40+
*/
41+
@Override
42+
public boolean isFit(AuthenticationTokenContext ctx) {
43+
return ctx.mustHave(BitbucketAuthenticator.SCHEME, "https")
44+
&& ctx.mustHave(BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE, BitbucketAuthenticator.BITBUCKET_INSTANCE_TYPE_SERVER);
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
<div>
2-
Credentials used to scan branches (also the default credentials to use when checking out sources)
2+
Credentials used to scan branches (also the default credentials to use when checking out sources).
3+
<p>
4+
For security reasons most credentials are only available when HTTPS is used.
5+
</p>
36
</div>
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
<div>
2-
Credentials used to scan branches (also the default credentials to use when checking out sources)
2+
Credentials used to scan branches (also the default credentials to use when checking out sources).
3+
<p>
4+
For security reasons most credentials are only available when HTTPS is used.
5+
</p>
36
</div>

src/main/resources/com/cloudbees/jenkins/plugins/bitbucket/SSHCheckoutTrait/help.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div>
2-
By default the discovered branches / pull requests will all use the same username / password credentials
2+
By default the discovered branches / pull requests will all use the same credentials
33
that were used for discovery when checking out sources. This means that the checkout will be using the
44
<code>https://</code> protocol for the Git repository.
55
<p>
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<div>
22
Select the credentials to use for managing hooks. Both GLOBAL and SYSTEM scoped credentials are eligible as the
33
management of hooks is run in the context of Jenkins itself and not in the context of the individual items.
4+
<p>
5+
For security reasons most credentials are only available when HTTPS is used.
6+
</p>
47
</div>

0 commit comments

Comments
 (0)