Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from davidzhao98/upgrade_java_client
Browse files Browse the repository at this point in the history
Upgrade java client
  • Loading branch information
fieldju authored Jun 7, 2019
2 parents a4fd93a + 5e958fb commit e43f774
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 26 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ targetCompatibility = 1.8

apply from: file('gradle/dependencies.gradle')
apply from: file('gradle/check.gradle')
apply from: file('gradle/integration.gradle')
apply from: file('gradle/bintray.gradle')

group = groupId
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
version=2.0.0
version=3.0.0
groupId=com.nike
artifactId=cerberus-spring-boot-client
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) 2019 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 @@ -25,7 +25,7 @@ dependencies {
compile "org.springframework.boot:spring-boot-autoconfigure:1.5.2.RELEASE"

// Cerberus dependencies
compile "com.nike:cerberus-client:6.1.0"
compile "com.nike:cerberus-client:7.0.1"

// Test dependencies
testRuntime 'org.slf4j:slf4j-simple:1.7.25'
Expand Down
42 changes: 42 additions & 0 deletions gradle/integration.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2019 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.
*/

sourceSets {
integration {
java.srcDir file('src/integration/java')
resources.srcDir file('src/integration/resources')
}
}

task integration(type: Test, description: 'Runs integration tests') {
testClassesDir = sourceSets.integration.output.classesDir
classpath = sourceSets.integration.runtimeClasspath
}

dependencies {
integrationCompile sourceSets.main.output
integrationCompile configurations.testCompile
integrationCompile sourceSets.test.output
integrationCompile 'com.fieldju:commons:1.1.0'
integrationCompile group: 'org.projectlombok', name: 'lombok', version: '1.18.8'
integrationRuntime configurations.testRuntime
}

integration {
testLogging {
showStandardStreams = true
}
}
20 changes: 2 additions & 18 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,22 +1,6 @@
#
# Copyright (c) 2017 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.
#

#Mon Aug 29 15:51:55 PDT 2016
#Thu Jun 06 14:24:12 PDT 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2019 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.springboot;

import com.fieldju.commons.EnvUtils;
import com.nike.cerberus.client.CerberusClient;
import com.nike.cerberus.client.CerberusServerException;
import com.nike.cerberus.client.model.CerberusListResponse;
import com.nike.cerberus.client.model.CerberusResponse;
import com.nike.cerberus.springboot.testapp.IntegrationTestSpringApp;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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

public class CerberusSpringBootClientIntegrationTests {

private IntegrationTestSpringApp app;

private String cerberusRootSdbPath;
private String secretPath;
private String sdbFullSecretPath;
private Map<String, String> secretData;
private ExecutorService executorService = Executors.newSingleThreadExecutor();

@Before
public void before() {
EnvUtils.getRequiredEnv("CERBERUS_URL");
EnvUtils.getRequiredEnv("CERBERUS_REGION");
cerberusRootSdbPath = EnvUtils.getRequiredEnv("SDB_ROOT_PATH");

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

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

app = new IntegrationTestSpringApp();

executorService.submit(() -> app.run());
}

@After
public void after() {
app.shutdown();
}

@Test
public void test_that_the_cerberus_springboot_client_can_be_used_in_a_spring_app() {
CerberusClient cerberusClient = app.getApplicationContext().getBean(CerberusClient.class);

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

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

// list secrets
CerberusListResponse cerberusListResponse = cerberusClient.list(cerberusRootSdbPath);
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());
}
}

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
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2019 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.springboot.testapp;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

@Slf4j
public class IntegrationTestSpringApp {

private final ApplicationContext applicationContext;
private boolean isRunning = true;

public ApplicationContext getApplicationContext() {
return applicationContext;
}

public void shutdown() {
isRunning = false;
}

public void run() {
while (isRunning) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException("Sleep interrupted");
}
}
}

public IntegrationTestSpringApp() {
applicationContext = new AnnotationConfigApplicationContext("com.nike.cerberus.springboot.testapp");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2019 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.springboot.testapp;

import com.nike.cerberus.springboot.CerberusClientSpringBootConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Slf4j
@Configuration
@Import(CerberusClientSpringBootConfiguration.class)
public class IntegrationTestSpringAppConfig {
public IntegrationTestSpringAppConfig() {
log.info("IntegrationTestSpringAppConfig Loaded");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Nike, Inc.
* Copyright (c) 2019 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 @@ -27,9 +27,10 @@
* Spring configuration class that provides a CerberusClient for interacting with Cerberus.
* <p>
* This class is configured via {@link CerberusClientSpringBootProperties} which has
* one required setting for accessing Cerberus, e.g. in your {@code application.properties}:
* two required settings for accessing Cerberus, e.g. in your {@code application.properties}:
* <pre>
* cerberus.url=https://test.cerberus.example.com
* cerberus.region=us-west-2
* </pre>
*
* @see <a href="http://engineering.nike.com/cerberus/docs/user-guide/quick-start">Cerberus Quick Start Guide</a>
Expand All @@ -53,10 +54,14 @@ public CerberusClientSpringBootConfiguration(CerberusClientSpringBootProperties
@Bean
public CerberusClient cerberusClient() {
String url = cerberusClientProperties.getUrl();
String region = cerberusClientProperties.getRegion();
if (url == null || url.isEmpty()) {
throw new IllegalArgumentException("cerberus.url setting is required! " +
"E.g. Your application needs a configuration property like 'cerberus.url=https://test.cerberus.example.com'");
} else if (region == null || region.isEmpty()) {
throw new IllegalArgumentException("cerberus.region setting is required! " +
"E.g. Your application needs a configuration property like 'cerberus.region=us-west-2'");
}
return DefaultCerberusClientFactory.getClient(url);
return DefaultCerberusClientFactory.getClient(url, region);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017 Nike, Inc.
* Copyright (c) 2019 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 @@ -22,9 +22,10 @@
/**
* A {@link ConfigurationProperties} companion for {@link CerberusClientSpringBootProperties}.
* <p>
* This class has one required setting for accessing Cerberus, e.g. in your {@code application.properties}:
* This class has two required settings for accessing Cerberus, e.g. in your {@code application.properties}:
* <pre>
* cerberus.url=https://test.cerberus.example.com
* cerberus.region=us-west-2
* </pre>
*
* @see <a href="http://engineering.nike.com/cerberus/docs/user-guide/quick-start">Cerberus Quick Start Guide</a>
Expand All @@ -33,6 +34,7 @@
public class CerberusClientSpringBootProperties {

private String url;
private String region;

/**
* Cerberus URL, e.g. https://test.cerberus.example.com
Expand All @@ -47,4 +49,18 @@ public String getUrl() {
public void setUrl(String url) {
this.url = url;
}

/**
* Cerberus region for sts auth, e.g. us-west-2
*/
public String getRegion() {
return region;
}

/**
* Cerberus region for sts auth, e.g. us-west-2
*/
public void setRegion(String region) {
this.region = region;
}
}

0 comments on commit e43f774

Please sign in to comment.