Skip to content
This repository has been archived by the owner on Oct 18, 2021. It is now read-only.

Commit

Permalink
Merge pull request #23 from Nike-Inc/update-client-7.1.1
Browse files Browse the repository at this point in the history
feat: Update to work with java client 7.1.1
  • Loading branch information
shawn-sher authored May 18, 2020
2 parents f70061f + fae87f2 commit 93de8d6
Show file tree
Hide file tree
Showing 9 changed files with 565 additions and 28 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,28 @@ The code snippet shown below is an example of how this can be done.

Cerberus Archaius client is a small project. It only has a few classes and they are all fully documented. For further details please see the source code, including javadocs and unit tests.

## Development

### Run Integration Tests

First, make sure the following environment variables are set before running the Cerberus Archaius Client integration tests:

``` bash
export CERBERUS_ADDR=https://example.cerberus.com
export TEST_REGION=us-west-2
export CERBERUS_ROOT_SDB_PATH=example/integration-tests-sdb/
```

Then, make sure AWS credentials have been obtained. One method is by running [gimme-aws-creds](https://github.com/Nike-Inc/gimme-aws-creds):

```bash
gimme-aws-creds
```

Next, in the project directory run:
```gradle
./gradlew integration
```
<a name="license"></a>
## License

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (c) 2017 Nike, Inc.
# Copyright (c) 2020 Nike, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
# limitations under the License.
#

version=6.0.1
version=7.0.0
groupId=com.nike
artifactId=cerberus-archaius-client

Expand Down
4 changes: 2 additions & 2 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Nike, Inc.
* Copyright (c) 2020 Nike, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,7 @@ repositories {

dependencies {
compile 'com.netflix.archaius:archaius-aws:0.6.5'
compile 'com.nike:cerberus-client:5.1.0'
compile 'com.nike:cerberus-client:7.1.1'

compile "org.apache.commons:commons-lang3:3.5"
compile "com.squareup.okhttp3:okhttp:3.9.0"
Expand Down
5 changes: 3 additions & 2 deletions gradle/integration.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Nike, Inc.
* Copyright (c) 2020 Nike, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -31,4 +31,5 @@ dependencies {
integrationCompile configurations.testCompile
integrationCompile sourceSets.test.output
integrationRuntime configurations.testRuntime
}
integrationCompile 'com.fieldju:commons:1.1.0'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright (c) 2020 Nike, Inc.
*
* 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 com.nike.cerberus.archaius.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import com.fieldju.commons.EnvUtils;
import com.nike.cerberus.client.CerberusClient;
import com.nike.cerberus.client.CerberusServerApiException;
import com.nike.cerberus.client.CerberusServerException;
import com.nike.cerberus.client.model.CerberusListFilesResponse;
import com.nike.cerberus.client.model.CerberusListResponse;
import com.nike.cerberus.client.model.CerberusResponse;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.BeforeClass;
import org.junit.Test;

public class ArchaiusClientIntegrationTests {

private static final String CERBERUS_ADDR_ENV_PROPERTY = "CERBERUS_ADDR";
private static final String CERBERUS_REGION_ENV_PROPERTY = "CERBERUS_REGION";
private static final String CERBERUS_ROOT_SDB_PATH_PROPERTY = "CERBERUS_ROOT_SDB_PATH";

private static String cerberusUrl;
private static String region;
private static String rootSdbPath;

private static String secretPath;
private static String sdbFullSecretPath;
private static Map<String, String> secretData;

private static CerberusClient cerberusClient;

@BeforeClass
public static void setUp() {

rootSdbPath = EnvUtils.getRequiredEnv(CERBERUS_ROOT_SDB_PATH_PROPERTY);

secretPath = UUID.randomUUID().toString();
sdbFullSecretPath = rootSdbPath + secretPath;

String key = RandomStringUtils.randomAlphabetic(15);
String value = RandomStringUtils.randomAlphabetic(25);
secretData = new HashMap<>();
secretData.put(key, value);
}

@Test
public void test_properties_configured() {
cerberusUrl = EnvUtils.getRequiredEnv(CERBERUS_ADDR_ENV_PROPERTY);
region = EnvUtils.getRequiredEnv(CERBERUS_REGION_ENV_PROPERTY);
}

@Test
public void test_create_cerberus_client() {
cerberusClient = ArchaiusCerberusClientFactory.getClient();
}

@Test
public void test_client_secret_functionality() {

// create client
cerberusClient = ArchaiusCerberusClientFactory.getClient();

// create secret
cerberusClient.write(sdbFullSecretPath, secretData);

// read secret
CerberusResponse cerberusReadResponse = cerberusClient.read(sdbFullSecretPath);
assertEquals(secretData, cerberusReadResponse.getData());

// list secrets
CerberusListResponse cerberusListResponse = cerberusClient.list(rootSdbPath);
assertTrue(cerberusListResponse.getKeys().contains(secretPath));

// update secret
Map<String, String> newSecretData = generateNewSecretData();
cerberusClient.write(sdbFullSecretPath, newSecretData);
secretData = newSecretData;

// confirm updated secret data
CerberusResponse cerberusReadResponseUpdated = cerberusClient.read(sdbFullSecretPath);
assertEquals(newSecretData, cerberusReadResponseUpdated.getData());

// delete secret
cerberusClient.delete(sdbFullSecretPath);

// confirm secret is deleted
try {
cerberusClient.read(sdbFullSecretPath);
} catch (CerberusServerException cse) {
assertEquals(404, cse.getCode());
}
}

@Test
public void test_client_crud_file_functionality() {

// create client
cerberusClient = ArchaiusCerberusClientFactory.getClient();

String fileContentStr = "file content string!";
byte[] fileContentArr = fileContentStr.getBytes(StandardCharsets.UTF_8);

// create file
cerberusClient.writeFile(sdbFullSecretPath, fileContentArr);

// read file
byte[] file = cerberusClient.readFileAsBytes(sdbFullSecretPath);
String resultContentStr = new String(file, StandardCharsets.UTF_8);
assertEquals(fileContentStr, resultContentStr);

// list files
CerberusListFilesResponse response = cerberusClient.listFiles(rootSdbPath);
assertEquals(
StringUtils.substringAfter(sdbFullSecretPath, "/"),
response.getSecureFileSummaries().get(0).getPath());

// update file
String newFileContentStr = "new file content string*";
byte[] newFileContentArr = newFileContentStr.getBytes(StandardCharsets.UTF_8);
cerberusClient.writeFile(sdbFullSecretPath, newFileContentArr);

// confirm updated file data
byte[] updatedFileResult = cerberusClient.readFileAsBytes(sdbFullSecretPath);
String updatedFileStr = new String(updatedFileResult, StandardCharsets.UTF_8);
assertEquals(newFileContentStr, updatedFileStr);

// delete file
cerberusClient.deleteFile(sdbFullSecretPath);

// confirm file is deleted
try {
cerberusClient.readFileAsBytes(sdbFullSecretPath);
} catch (CerberusServerApiException cse) {
assertEquals(404, cse.getCode());
}
}

private Map<String, String> generateNewSecretData() {
String key = RandomStringUtils.randomAlphabetic(20);
String value = RandomStringUtils.randomAlphabetic(30);
Map<String, String> newSecretData = new HashMap<>();
newSecretData.put(key, value);

return newSecretData;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Nike, Inc.
* Copyright (c) 2020 Nike, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,11 +16,14 @@

package com.nike.cerberus.archaius.client;

import com.netflix.config.ConfigurationManager;
import com.nike.cerberus.client.CerberusClient;
import com.nike.cerberus.client.CerberusClientException;
import com.nike.cerberus.client.CerberusClientFactory;
import com.nike.cerberus.client.auth.DefaultCerberusCredentialsProviderChain;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.configuration.AbstractConfiguration;

/**
* Client factory for creating a Cerberus client with URL resolved using the
Expand All @@ -35,19 +38,42 @@ public class ArchaiusCerberusClientFactory {
* @return Cerberus client
*/
public static CerberusClient getClient() {
final ArchaiusCerberusUrlResolver archaiusUrlResolver = new ArchaiusCerberusUrlResolver();
return getClient(null, null);
}

/**
* Resolves the Cerberus/Cerberus URL via the {@link ArchaiusCerberusUrlResolver} and creates a
* new {@link CerberusClient} with the {@link DefaultCerberusCredentialsProviderChain}.
*
* @param aur optional ArchaiusCerbersUrlResolver
* @param configuration optional AbstractConfiguration
* @return Cerberus client
*/
public static CerberusClient getClient(
ArchaiusCerberusUrlResolver aur, AbstractConfiguration configuration) {
if (aur == null) {
aur = new ArchaiusCerberusUrlResolver();
}

if (configuration == null) {
configuration = ConfigurationManager.getConfigInstance();
}

final Map<String, String> defaultHeaders = new HashMap<>();
final String xCerberusClientHeaderValue = ClientVersion.getClientHeaderValue();
defaultHeaders.put(ClientVersion.CERBERUS_CLIENT_HEADER, xCerberusClientHeaderValue);

return CerberusClientFactory.getClient(
archaiusUrlResolver,
// pass the client HTTP header value to be used in authenticate calls to Cerberus
final String url = aur.resolveUrl(configuration);
final String region = aur.resolveRegion(configuration);

if (url == null || region == null) {
throw new CerberusClientException("Missing url or region");
}

final DefaultCerberusCredentialsProviderChain dccpc =
new DefaultCerberusCredentialsProviderChain(
archaiusUrlResolver, xCerberusClientHeaderValue),
// pass the client header to be used in all other calls to Cerberus (e.g. read,
// write, etc.)
defaultHeaders);
url, region, xCerberusClientHeaderValue);

return CerberusClientFactory.getClient(url, dccpc, defaultHeaders);
}
}
Loading

0 comments on commit 93de8d6

Please sign in to comment.