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

Commit

Permalink
Add file support to Java Client (#35)
Browse files Browse the repository at this point in the history
Add file support
  • Loading branch information
sdford authored Apr 30, 2018
1 parent 95646f4 commit 3337504
Show file tree
Hide file tree
Showing 9 changed files with 617 additions and 3 deletions.
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=5.0.0
version=5.1.0
groupId=com.nike
artifactId=cerberus-client
4 changes: 2 additions & 2 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ dependencies {
shadow "org.apache.commons:commons-lang3:3.4"
shadow "org.slf4j:slf4j-api:1.7.25"
shadow "com.google.code.gson:gson:2.5"
shadow "com.google.code.findbugs:jsr305:3.0.1"

compile "com.squareup.okhttp3:okhttp:3.9.0"
compile "org.apache.commons:commons-lang3:3.4"
compile "com.google.code.gson:gson:2.5"
compile "com.google.code.findbugs:jsr305:3.0.1"
compile "org.slf4j:slf4j-api:1.7.25"
compileOnly "com.google.code.findbugs:jsr305:3.0.1"
compileOnly 'com.google.code.findbugs:annotations:3.0.1'

compile "com.amazonaws:aws-java-sdk-core:${AWS_SDK_VERSION}"
compile "com.amazonaws:aws-java-sdk-kms:${AWS_SDK_VERSION}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@

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.DefaultCerberusUrlResolver;
import com.nike.cerberus.client.model.CerberusListFilesResponse;
import com.nike.cerberus.client.model.CerberusListResponse;
import com.nike.cerberus.client.model.CerberusResponse;
import okhttp3.OkHttpClient;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.BeforeClass;
import org.junit.Test;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -171,4 +175,54 @@ public void test_secret_is_deleted_after_auth_with_iam_principal_name() {
}
}

@Test
public void test_crud_for_files() {

staticIamRoleCerberusCredentialsProvider = new StaticIamRoleCerberusCredentialsProvider(
new DefaultCerberusUrlResolver(),
iam_principal_arn,
region);

cerberusClient = new CerberusClient(new DefaultCerberusUrlResolver(),
staticIamRoleCerberusCredentialsProvider, new OkHttpClient());

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(ROOT_SDB_PATH);
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());
}
}

}
38 changes: 38 additions & 0 deletions src/main/java/com/nike/cerberus/client/CerberusApiError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2018 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.client;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class CerberusApiError {
private int code;
private String message;

public int getCode() {
return code;
}

@SuppressFBWarnings("UWF_UNWRITTEN_FIELD")
public String getMessage() {
return message;
}

@Override
public String toString() {
return String.format("ErrorCode: %s, Message: %s", code, message);
}
}
Loading

0 comments on commit 3337504

Please sign in to comment.