3. Introduction to CredHubOperations
The interface org.springframework.credhub.core.CredHubOperations
and the implementation org.springframework.credhub.core.CredHubTemplate
are the central classes in Spring CredHub.
CredHubOperations
provides access to additional operations interfaces that model the full CredHub API:
/**
* Get the operations for saving, retrieving, and deleting credentials.
*/
CredHubCredentialOperations credentials();
/**
* Get the operations for adding, retrieving, and deleting credential permissions.
*/
CredHubPermissionOperations permissions();
/**
* Get the operations for adding, retrieving, and deleting credential permissions.
*/
CredHubPermissionV2Operations permissionsV2();
/**
* Get the operations for retrieving, regenerating, and updating certificates.
*/
CredHubCertificateOperations certificates();
/**
* Get the operations for interpolating service binding credentials.
*/
CredHubInterpolationOperations interpolation();
/**
* Get the operations for retrieving CredHub server information.
*/
CredHubInfoOperations info();
3.1. Mapping to CredHub API
Each method of the Operations
interfaces maps directly to one endpoint of the CredHub HTTP API.
The following table shows the mapping between the CredHub API and the appropriate Spring CredHub Operations
interface.
3.2. CredHubOperations Auto-configuration
A CredHubOperations
Spring bean is created using Spring Boot auto-configuration when application properties are properly configured.
Application classes can autowire an instance of this bean to interact with a CredHub server.
/*
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.credhub;
import org.springframework.credhub.core.CredHubOperations;
import org.springframework.credhub.support.CredentialDetails;
import org.springframework.credhub.support.SimpleCredentialName;
import org.springframework.credhub.support.password.PasswordCredential;
import org.springframework.credhub.support.password.PasswordParameters;
import org.springframework.credhub.support.password.PasswordParametersRequest;
import org.springframework.stereotype.Component;
@Component
public class CredHubService {
private final CredHubOperations credHubOperations;
private final SimpleCredentialName credentialName;
public CredHubService(CredHubOperations credHubOperations) {
this.credHubOperations = credHubOperations;
this.credentialName = new SimpleCredentialName("example", "password");
}
public String generatePassword() {
PasswordParameters parameters = PasswordParameters.builder().length(12).excludeLower(false).excludeUpper(false)
.excludeNumber(false).includeSpecial(true).build();
CredentialDetails<PasswordCredential> password = this.credHubOperations.credentials()
.generate(PasswordParametersRequest.builder().name(this.credentialName).parameters(parameters).build());
return password.getValue().getPassword();
}
public String getPassword() {
CredentialDetails<PasswordCredential> password = this.credHubOperations.credentials()
.getByName(this.credentialName, PasswordCredential.class);
return password.getValue().getPassword();
}
}