org.springframework.credhub.support.CredentialDetails Java Examples

The following examples show how to use org.springframework.credhub.support.CredentialDetails. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CredHubCredentialTemplate.java    From spring-credhub with Apache License 2.0 6 votes vote down vote up
@Override
public <T> CredentialDetails<T> write(final CredentialRequest<T> credentialRequest) {
	Assert.notNull(credentialRequest, "credentialRequest must not be null");

	final ParameterizedTypeReference<CredentialDetails<T>> ref = new ParameterizedTypeReference<CredentialDetails<T>>() {
	};

	return this.credHubOperations.doWithRest((restOperations) -> {
		ResponseEntity<CredentialDetails<T>> response = restOperations.exchange(BASE_URL_PATH, HttpMethod.PUT,
				new HttpEntity<>(credentialRequest), ref);

		ExceptionUtils.throwExceptionOnError(response);

		return response.getBody();
	});
}
 
Example #2
Source File: CertificateIntegrationTests.java    From spring-credhub with Apache License 2.0 6 votes vote down vote up
@Test
public void bulkRegenerateCertificates() {
	CredentialDetails<CertificateCredential> rootCertificate = this.credentials
			.generate(
					CertificateParametersRequest
							.builder().name(ROOT_CERT_NAME).parameters(CertificateParameters.builder()
									.commonName("example.com").certificateAuthority(true).selfSign(true).build())
							.build());

	CredentialDetails<CertificateCredential> signedCertificate = this.credentials
			.generate(CertificateParametersRequest
					.builder().name(TEST_CERT_NAME).parameters(CertificateParameters.builder()
							.commonName("example.com").certificateAuthorityCredential(ROOT_CERT_NAME).build())
					.build());
	assertThat(signedCertificate.getValue().getCertificateAuthority())
			.isEqualTo(rootCertificate.getValue().getCertificate());

	List<CredentialDetails<CertificateCredential>> allVersions = this.credentials
			.getByNameWithHistory(TEST_CERT_NAME, CertificateCredential.class);
	assertThat(allVersions).hasSize(1);
	assertThat(allVersions.get(0).getId()).isEqualTo(signedCertificate.getId());

	List<CredentialName> regeneratedNames = this.certificates.regenerate(ROOT_CERT_NAME);
	assertThat(regeneratedNames).hasSize(1);
	assertThat(regeneratedNames).contains(TEST_CERT_NAME);
}
 
Example #3
Source File: CredHubTemplateDetailUnitTestsBase.java    From spring-credhub with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
void verifyGetById(ResponseEntity<CredentialDetails<T>> expectedResponse) {
	given(this.restTemplate.exchange(eq(CredHubCredentialTemplate.ID_URL_PATH), eq(HttpMethod.GET),
			isNull(HttpEntity.class), isA(ParameterizedTypeReference.class), eq(CREDENTIAL_ID)))
					.willReturn(expectedResponse);

	if (!expectedResponse.getStatusCode().equals(HttpStatus.OK)) {
		try {
			this.credHubTemplate.getById(CREDENTIAL_ID, String.class);
			fail("Exception should have been thrown");
		}
		catch (CredHubException ex) {
			assertThat(ex.getMessage()).contains(expectedResponse.getStatusCode().toString());
		}
	}
	else {
		CredentialDetails<T> response = this.credHubTemplate.getById(CREDENTIAL_ID, getType());

		assertDetailsResponseContainsExpectedCredential(expectedResponse, response);
	}
}
 
Example #4
Source File: CertificateIntegrationTests.java    From spring-credhub with Apache License 2.0 6 votes vote down vote up
@Test
public void generateCertificate() {
	assumeTrue(serverApiIsV2());

	CredentialDetails<CertificateCredential> certificate = this.credentials.generate(CertificateParametersRequest
			.builder().name(TEST_CERT_NAME)
			.parameters(CertificateParameters.builder().commonName("example.com").selfSign(true).build()).build());
	assertThat(certificate.getName().getName()).isEqualTo(TEST_CERT_NAME.getName());
	assertThat(certificate.getCredentialType()).isEqualTo(CredentialType.CERTIFICATE);
	assertThat(certificate.getId()).isNotNull();
	assertThat(certificate.getValue().getCertificate()).isNotNull();
	assertThat(certificate.getValue().getCertificateAuthority()).isNotNull();
	assertThat(certificate.getValue().getPrivateKey()).isNotNull();

	CertificateSummary byName = this.certificates.getByName(TEST_CERT_NAME);
	assertThat(byName.getName()).isEqualTo(TEST_CERT_NAME.getName());
	assertThat(byName.getId()).isNotNull();

	List<CertificateSummary> allCertificates = this.certificates.getAll();
	assertThat(allCertificates.size()).isGreaterThan(0);
	assertThat(allCertificates).extracting("name").contains(TEST_CERT_NAME.getName());
}
 
Example #5
Source File: CredentialIntegrationTests.java    From spring-credhub with Apache License 2.0 6 votes vote down vote up
@Test
public void generateNoOverwriteCredential() {
	CredentialDetails<UserCredential> generated = this.credentials.generate(UserParametersRequest.builder()
			.name(CREDENTIAL_NAME).username("test-user").parameters(this.passwordParameters.build()).build());
	assertThat(generated.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
	assertThat(generated.getValue().getUsername()).isEqualTo("test-user");
	assertThat(generated.getValue().getPassword()).matches("^[a-zA-Z0-9\\p{Punct}]{12}$");
	assertThat(generated.getValue().getPasswordHash()).isNotNull();

	CredentialDetails<UserCredential> noOverwrite = this.credentials
			.generate(UserParametersRequest.builder().name(CREDENTIAL_NAME).mode(WriteMode.NO_OVERWRITE)
					.username("test-user").parameters(this.passwordParameters.build()).build());
	assertThat(noOverwrite.getValue().getUsername()).isEqualTo("test-user");
	assertThat(noOverwrite.getValue().getPassword()).isEqualTo(generated.getValue().getPassword());
	assertThat(noOverwrite.getValue().getPasswordHash()).isEqualTo(generated.getValue().getPasswordHash());
}
 
Example #6
Source File: CredentialIntegrationTests.java    From spring-credhub with Apache License 2.0 6 votes vote down vote up
@Test
public void generateCredential() {
	CredentialDetails<UserCredential> generated = this.credentials.generate(UserParametersRequest.builder()
			.name(CREDENTIAL_NAME).username("test-user").parameters(this.passwordParameters.build()).build());
	assertThat(generated.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
	assertThat(generated.getCredentialType()).isEqualTo(CredentialType.USER);
	assertThat(generated.getValue().getUsername()).isEqualTo("test-user");
	assertThat(generated.getValue().getPassword()).matches("^[a-zA-Z0-9\\p{Punct}]{12}$");
	assertThat(generated.getValue().getPasswordHash()).isNotNull();

	CredentialDetails<UserCredential> retrieved = this.credentials.getById(generated.getId(), UserCredential.class);
	assertThat(retrieved.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());

	CredentialDetails<UserCredential> regenerated = this.credentials.regenerate(CREDENTIAL_NAME,
			UserCredential.class);
	assertThat(regenerated.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
	assertThat(regenerated.getValue().getUsername()).isEqualTo("test-user");
	assertThat(regenerated.getValue().getPassword()).matches("^[a-zA-Z0-9\\p{Punct}]{12}$");
	assertThat(regenerated.getValue().getPassword()).isNotEqualTo(generated.getValue().getPassword());
	assertThat(regenerated.getValue().getPasswordHash()).isNotEqualTo(generated.getValue().getPasswordHash());
}
 
Example #7
Source File: CredentialIntegrationTests.java    From spring-credhub with Apache License 2.0 6 votes vote down vote up
private void verifyHistory() {
	List<CredentialDetails<ValueCredential>> history = this.credentials.getByNameWithHistory(CREDENTIAL_NAME,
			ValueCredential.class);
	assertThat(history).hasSize(2);
	assertThat(history.get(0).getName()).isEqualTo(CREDENTIAL_NAME);
	assertThat(history.get(0).getValue().getValue()).isEqualTo("new-value");
	assertThat(history.get(1).getName()).isEqualTo(CREDENTIAL_NAME);
	assertThat(history.get(1).getValue().getValue()).isEqualTo(CREDENTIAL_VALUE);

	history = this.credentials.getByNameWithHistory(CREDENTIAL_NAME, 2, ValueCredential.class);
	assertThat(history).hasSize(2);
	assertThat(history.get(0).getName()).isEqualTo(CREDENTIAL_NAME);
	assertThat(history.get(0).getValue().getValue()).isEqualTo("new-value");
	assertThat(history.get(1).getName()).isEqualTo(CREDENTIAL_NAME);
	assertThat(history.get(1).getValue().getValue()).isEqualTo(CREDENTIAL_VALUE);
}
 
Example #8
Source File: CredhubIntegrationTest.java    From spring-cloud-config with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
	CredHubCredentialOperations credhubCredentialOperations = Mockito
			.mock(CredHubCredentialOperations.class);

	String expectedPath = "/myapp/master/default";
	SimpleCredentialName togglesCredentialName = new SimpleCredentialName(
			expectedPath + "/toggles");
	when(credhubCredentialOperations.findByPath(expectedPath))
			.thenReturn(singletonList(new CredentialSummary(togglesCredentialName)));
	JsonCredential credentials = new JsonCredential();
	credentials.put("key", "value");
	when(credhubCredentialOperations.getByName(
			new SimpleCredentialName(expectedPath + "/toggles"),
			JsonCredential.class))
					.thenReturn(new CredentialDetails<>("id1", togglesCredentialName,
							CredentialType.JSON, credentials));

	when(this.credHubOperations.credentials())
			.thenReturn(credhubCredentialOperations);
}
 
Example #9
Source File: CredHubCredentialTemplate.java    From spring-credhub with Apache License 2.0 6 votes vote down vote up
@Override
public <T, P> CredentialDetails<T> generate(final ParametersRequest<P> parametersRequest) {
	Assert.notNull(parametersRequest, "parametersRequest must not be null");

	final ParameterizedTypeReference<CredentialDetails<T>> ref = new ParameterizedTypeReference<CredentialDetails<T>>() {
	};

	return this.credHubOperations.doWithRest((restOperations) -> {
		ResponseEntity<CredentialDetails<T>> response = restOperations.exchange(BASE_URL_PATH, HttpMethod.POST,
				new HttpEntity<>(parametersRequest), ref);

		ExceptionUtils.throwExceptionOnError(response);

		return response.getBody();
	});
}
 
Example #10
Source File: CredHubCredentialsGeneratorTest.java    From spring-cloud-app-broker with Apache License 2.0 6 votes vote down vote up
@Test
void generateString() {
	given(this.credHubOperations.credentials())
		.willReturn(credHubCredentialOperations);

	CredentialDetails<PasswordCredential> creds = new CredentialDetails<>("id",
		new SimpleCredentialName("app-service"), CredentialType.PASSWORD,
		new PasswordCredential("password"));

	given(this.credHubCredentialOperations.generate(any(), eq(PasswordCredential.class)))
		.willReturn(Mono.just(creds));

	StepVerifier.create(generator.generateString("foo", "bar", "hello", 12, false, false, false, false))
		.assertNext(password -> assertThat(password).isEqualTo("password"))
		.verifyComplete();
}
 
Example #11
Source File: CredHubCredentialsGeneratorTest.java    From spring-cloud-app-broker with Apache License 2.0 6 votes vote down vote up
@Test
void generateUser() {
	given(this.credHubOperations.credentials())
		.willReturn(credHubCredentialOperations);

	CredentialDetails<UserCredential> creds = new CredentialDetails<>("id",
		new SimpleCredentialName("app-service"), CredentialType.PASSWORD,
		new UserCredential("username", "password"));

	given(this.credHubCredentialOperations.generate(any(), eq(UserCredential.class)))
		.willReturn(Mono.just(creds));

	StepVerifier.create(generator.generateUser("foo", "bar", "hello", 12, false, false, false, false))
		.assertNext(tuple2 -> {
			assertThat(tuple2.getT1()).isEqualTo("username");
			assertThat(tuple2.getT2()).isEqualTo("password");
		})
		.verifyComplete();
}
 
Example #12
Source File: CredHubCredentialTemplate.java    From spring-credhub with Apache License 2.0 6 votes vote down vote up
@Override
public <T> CredentialDetails<T> getById(final String id, final Class<T> credentialType) {
	Assert.notNull(id, "credential id must not be null");
	Assert.notNull(credentialType, "credential type must not be null");

	final ParameterizedTypeReference<CredentialDetails<T>> ref = new ParameterizedTypeReference<CredentialDetails<T>>() {
	};

	return this.credHubOperations.doWithRest((restOperations) -> {
		ResponseEntity<CredentialDetails<T>> response = restOperations.exchange(ID_URL_PATH, HttpMethod.GET, null,
				ref, id);

		ExceptionUtils.throwExceptionOnError(response);

		return response.getBody();
	});
}
 
Example #13
Source File: ReactiveCredentialIntegrationTests.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Test
public void generateOverwriteCredential() {
	AtomicReference<CredentialDetails<UserCredential>> generated = new AtomicReference<>();

	StepVerifier.create(this.credentials.generate(UserParametersRequest.builder().name(CREDENTIAL_NAME)
			.username("test-user").parameters(this.passwordParameters.build()).build(), UserCredential.class))
			.assertNext((response) -> {
				assertThat(response.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
				assertThat(response.getValue().getUsername()).isEqualTo("test-user");
				assertThat(response.getValue().getPassword()).matches("^[a-zA-Z0-9\\p{Punct}]{12}$");
				assertThat(response.getValue().getPasswordHash()).isNotNull();

				generated.set(response);
			}).verifyComplete();

	StepVerifier
			.create(this.credentials.generate(
					UserParametersRequest.builder().name(CREDENTIAL_NAME).mode(WriteMode.OVERWRITE)
							.username("test-user").parameters(this.passwordParameters.build()).build(),
					UserCredential.class))
			.assertNext((response) -> {
				assertThat(response.getValue().getUsername()).isEqualTo("test-user");
				assertThat(response.getValue().getPassword())
						.isNotEqualTo(generated.get().getValue().getPassword());
				assertThat(response.getValue().getPasswordHash())
						.isNotEqualTo(generated.get().getValue().getPasswordHash());
			}).verifyComplete();
}
 
Example #14
Source File: ReactiveCredentialIntegrationTests.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Test
public void generateNoOverwriteCredential() {
	AtomicReference<CredentialDetails<UserCredential>> generated = new AtomicReference<>();

	StepVerifier.create(this.credentials.generate(UserParametersRequest.builder().name(CREDENTIAL_NAME)
			.username("test-user").parameters(this.passwordParameters.build()).build(), UserCredential.class))
			.assertNext((response) -> {
				assertThat(response.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
				assertThat(response.getValue().getUsername()).isEqualTo("test-user");
				assertThat(response.getValue().getPassword()).matches("^[a-zA-Z0-9\\p{Punct}]{12}$");
				assertThat(response.getValue().getPasswordHash()).isNotNull();

				generated.set(response);
			}).verifyComplete();

	StepVerifier
			.create(this.credentials.generate(
					UserParametersRequest.builder().name(CREDENTIAL_NAME).mode(WriteMode.NO_OVERWRITE)
							.username("test-user").parameters(this.passwordParameters.build()).build(),
					UserCredential.class))
			.assertNext((response) -> {
				assertThat(response.getValue().getUsername()).isEqualTo("test-user");
				assertThat(response.getValue().getPassword()).isEqualTo(generated.get().getValue().getPassword());
				assertThat(response.getValue().getPasswordHash())
						.isEqualTo(generated.get().getValue().getPasswordHash());
			}).verifyComplete();
}
 
Example #15
Source File: ReactiveCredentialIntegrationTests.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Test
public void writeCredential() {
	AtomicReference<CredentialDetails<ValueCredential>> written = new AtomicReference<>();

	StepVerifier
			.create(this.credentials
					.write(ValueCredentialRequest.builder().name(CREDENTIAL_NAME).value(CREDENTIAL_VALUE).build()))
			.assertNext((response) -> {
				assertThat(response.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
				assertThat(response.getValue().getValue()).isEqualTo(CREDENTIAL_VALUE);
				assertThat(response.getCredentialType()).isEqualTo(CredentialType.VALUE);
				assertThat(response.getId()).isNotNull();

				written.set(response);
			}).verifyComplete();

	StepVerifier.create(this.credentials.getById(written.get().getId(), ValueCredential.class))
			.assertNext((response) -> {
				assertThat(response.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
				assertThat(response.getValue().getValue()).isEqualTo(CREDENTIAL_VALUE);
				assertThat(response.getCredentialType()).isEqualTo(CredentialType.VALUE);
			}).verifyComplete();

	StepVerifier.create(this.credentials.getByName(CREDENTIAL_NAME, ValueCredential.class))
			.assertNext((response) -> {
				assertThat(response.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
				assertThat(response.getValue().getValue()).isEqualTo(CREDENTIAL_VALUE);
				assertThat(response.getCredentialType()).isEqualTo(CredentialType.VALUE);
			}).verifyComplete();

	StepVerifier.create(this.credentials.findByName(new SimpleCredentialName("/test")))
			.assertNext((response) -> assertThat(response).extracting("name").extracting("name")
					.containsExactly(CREDENTIAL_NAME.getName()))
			.verifyComplete();

	StepVerifier.create(this.credentials.findByPath("/spring-credhub/integration-test"))
			.assertNext((response) -> assertThat(response).extracting("name").extracting("name")
					.containsExactly(CREDENTIAL_NAME.getName()))
			.verifyComplete();
}
 
Example #16
Source File: ReactiveCredHubCredentialTemplate.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Override
public <T, P> Mono<CredentialDetails<T>> generate(final ParametersRequest<P> parametersRequest,
		Class<T> credentialType) {
	Assert.notNull(parametersRequest, "parametersRequest must not be null");

	final ParameterizedTypeReference<CredentialDetails<T>> ref = new ParameterizedTypeReference<CredentialDetails<T>>() {
	};

	return this.credHubOperations
			.doWithWebClient((webClient) -> webClient.post().uri(BASE_URL_PATH).bodyValue(parametersRequest)
					.retrieve().onStatus(HttpStatus::isError, ExceptionUtils::buildError).bodyToMono(ref));
}
 
Example #17
Source File: RsaCredentialDetailsUnitTests.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
private void assertDetails(CredentialDetails<RsaCredential> data, String publicKey, String privateKey) {
	assertCommonDetails(data);

	assertThat(data.getCredentialType()).isEqualTo(CredentialType.RSA);
	assertThat(data.getValue().getPublicKey()).isEqualTo(publicKey);
	assertThat(data.getValue().getPrivateKey()).isEqualTo(privateKey);
}
 
Example #18
Source File: CredentialIntegrationTests.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Test
public void generateConvergeCredential() {
	CredentialDetails<UserCredential> generated = this.credentials.generate(UserParametersRequest.builder()
			.name(CREDENTIAL_NAME).username("test-user").parameters(this.passwordParameters.build()).build());
	assertThat(generated.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
	assertThat(generated.getValue().getUsername()).isEqualTo("test-user");
	assertThat(generated.getValue().getPassword()).matches("^[a-zA-Z0-9\\p{Punct}]{12}$");
	assertThat(generated.getValue().getPasswordHash()).isNotNull();

	CredentialDetails<UserCredential> convergeWithoutChanges = this.credentials
			.generate(UserParametersRequest.builder().name(CREDENTIAL_NAME).mode(WriteMode.CONVERGE)
					.username("test-user").parameters(this.passwordParameters.build()).build());
	assertThat(convergeWithoutChanges.getValue().getUsername()).isEqualTo("test-user");
	assertThat(convergeWithoutChanges.getValue().getPassword()).isEqualTo(generated.getValue().getPassword());
	assertThat(convergeWithoutChanges.getValue().getPasswordHash())
			.isEqualTo(generated.getValue().getPasswordHash());

	this.passwordParameters.includeSpecial(false);

	CredentialDetails<UserCredential> convergeWithChanges = this.credentials
			.generate(UserParametersRequest.builder().name(CREDENTIAL_NAME).mode(WriteMode.CONVERGE)
					.username("test-user").parameters(this.passwordParameters.build()).build());
	assertThat(convergeWithChanges.getValue().getUsername()).isEqualTo("test-user");
	assertThat(convergeWithChanges.getValue().getPassword()).isNotEqualTo(generated.getValue().getPassword());
	assertThat(convergeWithChanges.getValue().getPasswordHash())
			.isNotEqualTo(generated.getValue().getPasswordHash());
}
 
Example #19
Source File: ReactiveCredHubCredentialTemplate.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Mono<CredentialDetails<T>> regenerate(final CredentialName name, Class<T> credentialType) {
	Assert.notNull(name, "credential name must not be null");
	Assert.notNull(credentialType, "credential type must not be null");

	final ParameterizedTypeReference<CredentialDetails<T>> ref = new ParameterizedTypeReference<CredentialDetails<T>>() {
	};

	Map<String, Object> request = new HashMap<>(1);
	request.put(NAME_REQUEST_FIELD, name.getName());

	return this.credHubOperations
			.doWithWebClient((webClient) -> webClient.post().uri(REGENERATE_URL_PATH).bodyValue(request).retrieve()
					.onStatus(HttpStatus::isError, ExceptionUtils::buildError).bodyToMono(ref));
}
 
Example #20
Source File: ReactiveCredHubCredentialTemplate.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Mono<CredentialDetails<T>> getById(final String id, final Class<T> credentialType) {
	Assert.notNull(id, "credential id must not be null");
	Assert.notNull(credentialType, "credential type must not be null");

	final ParameterizedTypeReference<CredentialDetails<T>> ref = new ParameterizedTypeReference<CredentialDetails<T>>() {
	};

	return this.credHubOperations.doWithWebClient((webClient) -> webClient.get().uri(ID_URL_PATH, id).retrieve()
			.onStatus(HttpStatus::isError, ExceptionUtils::buildError).bodyToMono(ref));
}
 
Example #21
Source File: CredentialIntegrationTests.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Test
public void writeCredential() {
	CredentialDetails<ValueCredential> written = this.credentials
			.write(ValueCredentialRequest.builder().name(CREDENTIAL_NAME).value(CREDENTIAL_VALUE).build());
	assertThat(written.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
	assertThat(written.getValue().getValue()).isEqualTo(CREDENTIAL_VALUE);
	assertThat(written.getCredentialType()).isEqualTo(CredentialType.VALUE);
	assertThat(written.getId()).isNotNull();

	CredentialDetails<ValueCredential> byId = this.credentials.getById(written.getId(), ValueCredential.class);
	assertThat(byId.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
	assertThat(byId.getValue().getValue()).isEqualTo(CREDENTIAL_VALUE);
	assertThat(byId.getCredentialType()).isEqualTo(CredentialType.VALUE);

	CredentialDetails<ValueCredential> byName = this.credentials.getByName(CREDENTIAL_NAME, ValueCredential.class);
	assertThat(byName.getName().getName()).isEqualTo(CREDENTIAL_NAME.getName());
	assertThat(byName.getValue().getValue()).isEqualTo(CREDENTIAL_VALUE);
	assertThat(byName.getCredentialType()).isEqualTo(CredentialType.VALUE);

	List<CredentialSummary> foundByName = this.credentials.findByName(new SimpleCredentialName("/test"));
	assertThat(foundByName).hasSize(1);
	assertThat(foundByName).extracting("name").extracting("name").containsExactly(CREDENTIAL_NAME.getName());

	List<CredentialSummary> foundByPath = this.credentials.findByPath("/spring-credhub/integration-test");
	assertThat(foundByPath).hasSize(1);
	assertThat(foundByPath).extracting("name").extracting("name").containsExactly(CREDENTIAL_NAME.getName());
}
 
Example #22
Source File: CredHubCredentialsGenerator.java    From spring-cloud-app-broker with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<Tuple2<String, String>> generateUser(String applicationId, String serviceInstanceId, String descriptor,
	int length, boolean includeUppercaseAlpha, boolean includeLowercaseAlpha, boolean includeNumeric,
	boolean includeSpecial) {
	return credHubOperations.credentials().generate(UserParametersRequest.builder()
		.name(new SimpleCredentialName(applicationId, serviceInstanceId, descriptor))
		.parameters(passwordParameters(length, includeUppercaseAlpha, includeLowercaseAlpha, includeNumeric,
			includeSpecial))
		.build(), UserCredential.class)
		.map(CredentialDetails::getValue)
		.map(userCredential -> Tuples.of(userCredential.getUsername(), userCredential.getPassword()));
}
 
Example #23
Source File: RsaCredentialDetailsUnitTests.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Test
public void deserializeDetailsWithPrivateKey() {
	// @formatter:off
	final String credentials =
			"  \"type\": \"rsa\"," +
			"  \"value\": {" +
				"  \"private_key\": \"private-key\"" +
			"  }";
	// @formatter:on
	CredentialDetails<RsaCredential> data = parseDetails(credentials);

	assertDetails(data, null, "private-key");
}
 
Example #24
Source File: ReactiveCredHubCredentialTemplate.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Mono<CredentialDetails<T>> write(final CredentialRequest<T> credentialRequest) {
	Assert.notNull(credentialRequest, "credentialRequest must not be null");

	final ParameterizedTypeReference<CredentialDetails<T>> ref = new ParameterizedTypeReference<CredentialDetails<T>>() {
	};

	return this.credHubOperations
			.doWithWebClient((webClient) -> webClient.put().uri(BASE_URL_PATH).bodyValue(credentialRequest)
					.retrieve().onStatus(HttpStatus::isError, ExceptionUtils::buildError).bodyToMono(ref));
}
 
Example #25
Source File: CredHubCredentialsGenerator.java    From spring-cloud-app-broker with Apache License 2.0 5 votes vote down vote up
@Override
public Mono<String> generateString(String applicationId, String serviceInstanceId, String descriptor, int length,
	boolean includeUppercaseAlpha, boolean includeLowercaseAlpha, boolean includeNumeric,
	boolean includeSpecial) {
	return credHubOperations.credentials()
		.generate(PasswordParametersRequest.builder()
			.name(new SimpleCredentialName(applicationId, serviceInstanceId, descriptor))
			.parameters(passwordParameters(length, includeUppercaseAlpha, includeLowercaseAlpha, includeNumeric,
				includeSpecial))
			.build(), PasswordCredential.class)
		.map(CredentialDetails::getValue)
		.map(PasswordCredential::getPassword);
}
 
Example #26
Source File: JsonCredentialDetailsUnitTests.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
private void assertDetails(CredentialDetails<JsonCredential> data) {
	assertCommonDetails(data);

	assertThat(data.getCredentialType()).isEqualTo(CredentialType.JSON);

	JsonCredential valueMap = data.getValue();
	assertThat(valueMap).containsEntry("client_id", "test-id").containsEntry("client_secret", "test-secret")
			.containsEntry("uri", "https://example.com");
}
 
Example #27
Source File: SshCredentialDetailsUnitTests.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
private void assertDetails(CredentialDetails<SshCredential> data, String publicKey, String privateKey,
		String publicKeyFingerprint) {
	assertCommonDetails(data);

	assertThat(data.getCredentialType()).isEqualTo(CredentialType.SSH);
	assertThat(data.getValue().getPublicKey()).isEqualTo(publicKey);
	assertThat(data.getValue().getPrivateKey()).isEqualTo(privateKey);
	assertThat(data.getValue().getPublicKeyFingerprint()).isEqualTo(publicKeyFingerprint);
}
 
Example #28
Source File: CredHubDemoController.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
private void getCredentialsById(String id, Results results) {
	try {
		CredentialDetails<JsonCredential> retrievedDetails =
				credentialOperations.getById(id, JsonCredential.class);
		saveResults(results, "Successfully retrieved credentials by ID: ", retrievedDetails);
	} catch (Exception e) {
		saveResults(results, "Error retrieving credentials by ID: ", e.getMessage());
	}
}
 
Example #29
Source File: CredHubDemoController.java    From spring-credhub with Apache License 2.0 5 votes vote down vote up
private void getCredentialsByName(CredentialName name, Results results) {
	try {
		CredentialDetails<JsonCredential> retrievedDetails =
				credentialOperations.getByName(name, JsonCredential.class);
		saveResults(results, "Successfully retrieved credentials by name: ", retrievedDetails);
	} catch (Exception e) {
		saveResults(results, "Error retrieving credentials by name: ", e.getMessage());
	}
}
 
Example #30
Source File: CredHubTemplateDetailSshUnitTests.java    From spring-credhub with Apache License 2.0 4 votes vote down vote up
@Theory
public void getById(
		@FromDataPoints("detail-responses") ResponseEntity<CredentialDetails<SshCredential>> expectedResponse) {
	verifyGetById(expectedResponse);
}