Skip to content

Commit 114f944

Browse files
committed
Polishing.
Refactor AuthenticationSteps creation into static factory method. Extend tests, add documentation. See gh-821 Original pull request: gh-853
1 parent fdcab61 commit 114f944

File tree

4 files changed

+91
-11
lines changed

4 files changed

+91
-11
lines changed

spring-vault-core/src/main/java/org/springframework/vault/authentication/GitHubAuthentication.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
package org.springframework.vault.authentication;
1717

1818
import java.util.Map;
19+
1920
import org.apache.commons.logging.Log;
2021
import org.apache.commons.logging.LogFactory;
22+
2123
import org.springframework.util.Assert;
2224
import org.springframework.vault.VaultException;
2325
import org.springframework.vault.support.VaultResponse;
@@ -30,6 +32,7 @@
3032
* personal access token.
3133
*
3234
* @author Nanne Baars
35+
* @author Mark Paluch
3336
* @since 3.2
3437
* @see GitHubAuthentication
3538
* @see RestOperations
@@ -58,11 +61,24 @@ public GitHubAuthentication(GitHubAuthenticationOptions options, RestOperations
5861
this.restOperations = restOperations;
5962
}
6063

64+
/**
65+
* Creates a {@link AuthenticationSteps} for GitHub authentication given
66+
* {@link GitHubAuthenticationOptions}.
67+
* @param options must not be {@literal null}.
68+
* @return {@link AuthenticationSteps} for github authentication.
69+
*/
70+
public static AuthenticationSteps createAuthenticationSteps(GitHubAuthenticationOptions options) {
71+
72+
Assert.notNull(options, "GitHubAuthentication must not be null");
73+
74+
return AuthenticationSteps.fromSupplier(options.getTokenSupplier())
75+
.map(GitHubAuthentication::getGitHubLogin)
76+
.login(AuthenticationUtil.getLoginPath(options.getPath()));
77+
}
78+
6179
@Override
6280
public AuthenticationSteps getAuthenticationSteps() {
63-
return AuthenticationSteps.fromSupplier(options.getTokenSupplier())
64-
.map(token -> getGitHubLogin(token))
65-
.login(AuthenticationUtil.getLoginPath(this.options.getPath()));
81+
return createAuthenticationSteps(options);
6682
}
6783

6884
@Override

spring-vault-core/src/main/java/org/springframework/vault/authentication/GitHubAuthenticationOptions.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
* Instances of this class are immutable once constructed.
2828
*
2929
* @author Nanne Baars
30-
* @author Mark Paluch
3130
* @since 3.2
3231
* @see GitHubAuthentication
3332
* @see #builder()

spring-vault-core/src/test/java/org/springframework/vault/authentication/GitHubAuthenticationIntegrationTest.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ void before() throws Exception {
5353
prepare().mountAuth("github");
5454
}
5555

56-
prepare().getVaultOperations()
57-
.doWithSession(
58-
restOperations -> restOperations.postForEntity("auth/github/config", Map.of("organization_id", 1,
59-
"base_url", "http://localhost:%d".formatted(gitHubMockServer.getPort())), Map.class));
56+
gitHubMockServer.start();
57+
58+
prepare().getVaultOperations().doWithSession(restOperations -> {
59+
Map<String, String> config = Map.of("organization", "foo", "organization_id", "" + organizationId,
60+
"base_url", "http://localhost:%d".formatted(gitHubMockServer.getPort()));
61+
return restOperations.postForEntity("auth/github/config", config, Map.class);
62+
});
6063
}
6164

6265
@AfterEach
@@ -66,21 +69,45 @@ void after() throws IOException {
6669

6770
@Test
6871
void shouldLoginSuccessfully() {
72+
73+
GitHubAuthenticationOptions options = GitHubAuthenticationOptions.builder()
74+
.tokenSupplier(() -> "TOKEN")
75+
.build();
6976
RestTemplate restTemplate = TestRestTemplateFactory.create(Settings.createSslConfiguration());
7077
setupGithubMockServer(gitHubUserResponse(), gitHubOrganizationResponse(organizationId),
7178
gitHubTeamResponse(organizationId));
7279

73-
GitHubAuthentication authentication = new GitHubAuthentication(
74-
GitHubAuthenticationOptions.builder().tokenSupplier(() -> "TOKEN").build(), restTemplate);
80+
GitHubAuthentication authentication = new GitHubAuthentication(options, restTemplate);
7581
VaultToken loginToken = authentication.login();
7682

7783
assertThat(loginToken.getToken()).isNotNull();
7884
}
7985

86+
@Test
87+
void shouldLoginUsingAuthenticationSteps() {
88+
89+
GitHubAuthenticationOptions options = GitHubAuthenticationOptions.builder()
90+
.tokenSupplier(() -> "TOKEN")
91+
.build();
92+
RestTemplate restTemplate = TestRestTemplateFactory.create(Settings.createSslConfiguration());
93+
setupGithubMockServer(gitHubUserResponse(), gitHubOrganizationResponse(organizationId),
94+
gitHubTeamResponse(organizationId));
95+
96+
AuthenticationSteps steps = GitHubAuthentication.createAuthenticationSteps(options);
97+
98+
AuthenticationStepsExecutor executor = new AuthenticationStepsExecutor(steps, restTemplate);
99+
VaultToken loginToken = executor.login();
100+
101+
assertThat(loginToken.getToken()).isNotNull();
102+
}
103+
80104
@Test
81105
void shouldFailIfOrganizationIsNotTheSame() {
106+
82107
RestTemplate restTemplate = TestRestTemplateFactory.create(Settings.createSslConfiguration());
83-
var wrongOrganizationId = organizationId + 1;
108+
109+
int wrongOrganizationId = organizationId + 1;
110+
84111
setupGithubMockServer(gitHubUserResponse(), gitHubOrganizationResponse(wrongOrganizationId),
85112
gitHubTeamResponse(wrongOrganizationId));
86113

src/main/antora/modules/ROOT/pages/vault/authentication.adoc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,44 @@ See also:
537537
* https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/signJwt
538538
* https://cloud.google.com/iam/docs/reference/rest/v1/projects.serviceAccounts/signJwt (deprecated)
539539

540+
[[vault.authentication.github]]
541+
== GitHub Authentication
542+
543+
The https://www.vaultproject.io/docs/auth/github.html[github]
544+
auth backend provides an authentication mechanism based on GitHub tokens.
545+
Vault does not support an OAuth workflow to generate GitHub tokens,
546+
so it does not act as a GitHub application.
547+
548+
The authentication mechanism requires a GitHub token (or a supplier)
549+
to pass on the token to Vault which then authenticates against your GitHub
550+
account.
551+
552+
====
553+
[source,java]
554+
----
555+
@Configuration
556+
class AppConfig extends AbstractVaultConfiguration {
557+
558+
// …
559+
560+
@Override
561+
public ClientAuthentication clientAuthentication() {
562+
563+
GitHubAuthentication options = GitHubAuthentication.builder()
564+
.token(…).build();
565+
566+
return new GitHubAuthentication(options, restOperations());
567+
}
568+
569+
// …
570+
}
571+
----
572+
====
573+
574+
See also:
575+
576+
* https://www.vaultproject.io/api-docs/auth/github[Vault Documentation: GitHub auth method]
577+
540578
[[vault.authentication.pcf]]
541579
== PCF authentication
542580

0 commit comments

Comments
 (0)