Add getAllPaths() to credentials operations.

Fixes #38
This commit is contained in:
Scott Frederick
2018-10-17 15:23:06 -05:00
parent c75b9d8eb2
commit 4ea4f2647d
8 changed files with 266 additions and 7 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.credhub.core;
import org.springframework.credhub.support.CredentialDetails;
import org.springframework.credhub.support.CredentialName;
import org.springframework.credhub.support.CredentialPath;
import org.springframework.credhub.support.CredentialRequest;
import org.springframework.credhub.support.CredentialSummary;
import org.springframework.credhub.support.ParametersRequest;
@@ -126,6 +127,13 @@ public interface CredHubCredentialsOperations {
*/
List<CredentialSummary> findByPath(final String path);
/**
* Retrieve a collection of all paths that contain credentials.
*
* @return a collection of paths
*/
List<CredentialPath> getAllPaths();
/**
* Delete a credential by its full name.
*

View File

@@ -20,6 +20,8 @@ import org.springframework.core.ParameterizedTypeReference;
import org.springframework.credhub.support.CredentialDetails;
import org.springframework.credhub.support.CredentialDetailsData;
import org.springframework.credhub.support.CredentialName;
import org.springframework.credhub.support.CredentialPath;
import org.springframework.credhub.support.CredentialPathData;
import org.springframework.credhub.support.CredentialRequest;
import org.springframework.credhub.support.CredentialSummary;
import org.springframework.credhub.support.CredentialSummaryData;
@@ -53,6 +55,7 @@ public class CredHubCredentialsTemplate implements CredHubCredentialsOperations
static final String NAME_URL_QUERY_VERSIONS = NAME_URL_QUERY + "&versions={versions}";
static final String NAME_LIKE_URL_QUERY = BASE_URL_PATH + "?name-like={name}";
static final String PATH_URL_QUERY = BASE_URL_PATH + "?path={path}";
static final String SHOW_ALL_URL_QUERY = BASE_URL_PATH + "?paths=true";
static final String REGENERATE_URL_PATH = "/api/v1/regenerate";
private CredHubOperations credHubOperations;
@@ -256,6 +259,22 @@ public class CredHubCredentialsTemplate implements CredHubCredentialsOperations
});
}
@Override
public List<CredentialPath> getAllPaths() {
return credHubOperations.doWithRest(new RestOperationsCallback<List<CredentialPath>>() {
@Override
public List<CredentialPath> doWithRestOperations(
RestOperations restOperations) {
ResponseEntity<CredentialPathData> response = restOperations
.getForEntity(SHOW_ALL_URL_QUERY, CredentialPathData.class);
throwExceptionOnError(response);
return response.getBody().getPaths();
}
});
}
@Override
public void deleteByName(final CredentialName name) {
Assert.notNull(name, "credential name must not be null");

View File

@@ -0,0 +1,77 @@
/*
* Copyright 2016-2017 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
*
* http://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 org.springframework.credhub.support;
/**
* The path to a credential that has been written to CredHub. Clients don't typically
* instantiate objects of this type, but will receive them in response to requests.
*
* @author Scott Frederick
*/
public class CredentialPath {
protected String path;
/**
* Create a {@link CredentialPath}. Intended for internal use.
*/
CredentialPath() {
}
/**
* Create a {@link CredentialPath} from the provided parameters. Intended for
* internal use. Clients will get {@link CredentialPath} objects populated from
* CredHub responses.
*
* @param path the name of the credential
*/
public CredentialPath(String path) {
this.path = path;
}
/**
* Get the path to the credential.
*
* @return the credential path
*/
public String getPath() {
return this.path;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof CredentialPath))
return false;
CredentialPath that = (CredentialPath) o;
return (path != null ? !path.equals(that.path) : that.path != null);
}
@Override
public int hashCode() {
return path != null ? path.hashCode() : 0;
}
@Override
public String toString() {
return "CredentialPath{"
+ "path=" + path
+ '}';
}
}

View File

@@ -0,0 +1,85 @@
/*
* Copyright 2016-2017 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
*
* http://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 org.springframework.credhub.support;
import java.util.Arrays;
import java.util.List;
/**
* A collection of {@link CredentialPath}s. Clients don't typically instantiate
* objects of this type, but will receive them in response to requests.
*
* @author Scott Frederick
*/
public class CredentialPathData {
private List<CredentialPath> paths;
/**
* Create a {@link CredentialPathData}.
*/
CredentialPathData() {
}
/**
* Create a {@link CredentialPathData} from the provided parameters. Intended for internal
* use. Clients will get {@link CredentialPathData} objects populated from
* CredHub responses.
*
* @param paths a collection of {@link CredentialPath}s
*/
public CredentialPathData(CredentialPath... paths) {
this.paths = Arrays.asList(paths);
}
/**
* Get the collection of {@link CredentialPath}s.
*
* @return the collection of {@link CredentialPath}s
*/
public List<CredentialPath> getPaths() {
return this.paths;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof CredentialPathData))
return false;
if (!super.equals(o))
return false;
CredentialPathData that = (CredentialPathData) o;
return paths != null ? paths.equals(that.paths)
: that.paths == null;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (paths != null ? paths.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "CredentialPathData{"
+ "paths=" + paths
+ '}';
}
}

View File

@@ -79,7 +79,7 @@ public class CredentialSummaryData {
@Override
public String toString() {
return "CredentialSummaryResponse{"
return "CredentialSummaryData{"
+ "credentials=" + credentials
+ '}';
}

View File

@@ -20,12 +20,32 @@ package org.springframework.credhub.core;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.credhub.support.CredentialPath;
import org.springframework.credhub.support.CredentialPathData;
import org.springframework.http.ResponseEntity;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.credhub.core.CredHubCredentialsTemplate.NAME_URL_QUERY;
import static org.springframework.credhub.core.CredHubCredentialsTemplate.SHOW_ALL_URL_QUERY;
import static org.springframework.http.HttpStatus.OK;
@RunWith(MockitoJUnitRunner.class)
public class CredHubCredentialsTemplateUnitTests extends CredHubCredentialsTemplateUnitTestsBase {
@Test
public void getAllPaths() {
when(restTemplate.getForEntity(SHOW_ALL_URL_QUERY, CredentialPathData.class))
.thenReturn(new ResponseEntity<>(new CredentialPathData(new CredentialPath("/path1"), new CredentialPath("/path2")), OK));
List<CredentialPath> paths = credHubTemplate.getAllPaths();
assertThat(paths.size()).isEqualTo(2);
assertThat(paths).extracting("path").contains("/path1", "/path2");
}
@Test
public void deleteByName() {
credHubTemplate.deleteByName(NAME);

View File

@@ -0,0 +1,53 @@
package org.springframework.credhub.support;
import org.junit.Test;
import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
public class CredentialPathDataTests extends JsonParsingUnitTestsBase {
@Test
public void deserializeWithPaths() throws IOException {
String json = "{\n" +
" \"paths\": [\n" +
" {\n" +
" \"path\": \"/\"\n" +
" },\n" +
" {\n" +
" \"path\": \"/director-name/\"\n" +
" },\n" +
" {\n" +
" \"path\": \"/director-name/deploy1/\"\n" +
" },\n" +
" {\n" +
" \"path\": \"/director-name/deploy2/\"\n" +
" },\n" +
" {\n" +
" \"path\": \"/director2/\"\n" +
" }\n" +
" ]\n" +
"}";
CredentialPathData paths = parseResponse(json);
assertThat(paths.getPaths().size()).isEqualTo(5);
assertThat(paths.getPaths()).extracting("path")
.contains("/", "/director-name/", "/director-name/deploy1/", "/director-name/deploy2/", "/director2/");
}
@Test
public void deserializeWithNoPaths() throws IOException {
String json = "{\n" +
" \"paths\": []" +
"}";
CredentialPathData paths = parseResponse(json);
assertThat(paths.getPaths().size()).isEqualTo(0);
}
private CredentialPathData parseResponse(String json) throws java.io.IOException {
return objectMapper.readValue(json, CredentialPathData.class);
}
}

View File

@@ -20,8 +20,6 @@ import java.util.List;
import org.junit.Test;
import org.springframework.credhub.support.json.JsonCredential;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
@@ -46,7 +44,7 @@ public class CredentialSummaryDataUnitTests extends JsonParsingUnitTestsBase {
" ]\n" +
"}";
CredentialSummaryData response = parseResponse(json, JsonCredential.class);
CredentialSummaryData response = parseResponse(json);
assertThat(response.getCredentials().size(), equalTo(3));
@@ -71,14 +69,13 @@ public class CredentialSummaryDataUnitTests extends JsonParsingUnitTestsBase {
" ]\n" +
"}";
CredentialSummaryData response = parseResponse(json, JsonCredential.class);
CredentialSummaryData response = parseResponse(json);
assertThat(response.getCredentials(), notNullValue());
assertThat(response.getCredentials().size(), equalTo(0));
}
private CredentialSummaryData parseResponse(String json, Class<JsonCredential> jsonCredentialClass)
throws java.io.IOException {
private CredentialSummaryData parseResponse(String json) throws java.io.IOException {
return objectMapper.readValue(json, CredentialSummaryData.class);
}
}