Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add secret for java sdk #42

Merged
merged 13 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions examples-secret/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>runtime-sdk-parent</artifactId>
<groupId>io.mosn.layotto</groupId>
<version>1.3.0-SNAPSHOT</version>
</parent>

<groupId>io.mosn.layotto</groupId>
<artifactId>examples-secret</artifactId>
fft0518 marked this conversation as resolved.
Show resolved Hide resolved
<version>1.3.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>io.mosn.layotto</groupId>
<artifactId>runtime-sdk</artifactId>
<version>1.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>io.mosn.layotto</groupId>
<artifactId>runtime-sdk</artifactId>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/java</sourceDirectory>
<finalName>examples-secret</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
<mainClass>io.mosn.layotto.examples.secret.Secret</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>assemble-all</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.mosn.layotto.examples.secret;

import io.mosn.layotto.v1.RuntimeClientBuilder;
import io.mosn.layotto.v1.config.RuntimeProperties;
import spec.sdk.runtime.v1.client.RuntimeClient;
import spec.sdk.runtime.v1.domain.secret.GetBulkSecretRequest;
import spec.sdk.runtime.v1.domain.secret.GetBulkSecretResponse;
import spec.sdk.runtime.v1.domain.secret.GetSecretRequest;
import spec.sdk.runtime.v1.domain.secret.GetSecretResponse;

import java.util.HashMap;
import java.util.Map;

public class Secret {

static String storeName = "secret_demo";
static String key = "db-user-pass:password";
static String value = "S!S*d$zDsb=";
static String []keys = new String[]{"redisPassword", "db-user-pass:password", "db-user-pass:username"};
static Map<String, String> values = new HashMap<String, String>(){{put("redisPassword", "redis123");put("db-user-pass:password", "S!S*d$zDsb=");put("db-user-pass:username", "devuser");}};

public static void main(String args[]){
RuntimeClient client = new RuntimeClientBuilder()
.withPort(RuntimeProperties.DEFAULT_PORT)
.build();

/*GetSecret*/
Map<String, String> meta = new HashMap<>();
GetSecretRequest secretreq = new GetSecretRequest();
secretreq.setStoreName(storeName);
secretreq.setKey(key);
secretreq.setMetaData(meta);
GetSecretResponse response = client.getSecret(secretreq);
System.out.println(response.getData());
assertMap(response.getData(),key,value);

/*GetBulkSecret*/
Map<String, String> meta2 = new HashMap<>();
fft0518 marked this conversation as resolved.
Show resolved Hide resolved
GetBulkSecretRequest bulksecretreq = new GetBulkSecretRequest();
bulksecretreq.setStoreName(storeName);
bulksecretreq.setMetaData(meta2);
GetBulkSecretResponse bulkrespone = client.getBulkSecret(bulksecretreq);
System.out.println(bulkrespone.getData());
assertBulkMap(bulkrespone.getData(), keys,values);
}
private static void assertMap(Map<String, String> secret, String key, String value) {
if (secret.get(key).equals(value)) {
return;
}
throw new AssertionError();
}

private static void assertBulkMap(Map<String, Map<String, String>> bulksecret, String keys[], Map<String, String> values ) {
Map<String, String> temp = new HashMap<>();
for (String key : keys) {
temp = bulksecret.get(key);
if(!(temp.get(key).equals(values.get(key))))
throw new AssertionError();
}
return;
}


}
18 changes: 18 additions & 0 deletions examples-secret/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1} - %m%n</Property>
</Properties>

<Appenders>
<Console name="console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
</Appenders>

<Loggers>
<Root level="info">
<AppenderRef ref="console"/>
</Root>
</Loggers>
</Configuration>
55 changes: 55 additions & 0 deletions sdk/src/main/java/io/mosn/layotto/v1/RuntimeClientGrpc.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@
import spec.sdk.runtime.v1.domain.lock.TryLockResponse;
import spec.sdk.runtime.v1.domain.lock.UnlockRequest;
import spec.sdk.runtime.v1.domain.lock.UnlockResponse;
import spec.sdk.runtime.v1.domain.secret.GetSecretRequest;
import spec.sdk.runtime.v1.domain.secret.GetSecretResponse;
import spec.sdk.runtime.v1.domain.secret.GetBulkSecretRequest;
import spec.sdk.runtime.v1.domain.secret.GetBulkSecretResponse;
import spec.sdk.runtime.v1.domain.sequencer.GetNextIdRequest;
import spec.sdk.runtime.v1.domain.sequencer.GetNextIdResponse;
import spec.sdk.runtime.v1.domain.state.DeleteStateRequest;
Expand Down Expand Up @@ -1101,4 +1105,55 @@ public UnlockResponse unlock(UnlockRequest request) {
}

}

@Override
public GetSecretResponse getSecret(GetSecretRequest req){
try {
RuntimeProto.GetSecretRequest request = RuntimeProto.GetSecretRequest.newBuilder()
.setStoreName(req.getStoreName())
.setKey(req.getKey())
.putAllMetadata(req.getMetaData())
fft0518 marked this conversation as resolved.
Show resolved Hide resolved
.build();

RuntimeProto.GetSecretResponse response = runtimeStubManager.getBlockingStub()
.getSecret(request);
GetSecretResponse getSecretResponse = new GetSecretResponse();
getSecretResponse.setData(response.getDataMap());
return getSecretResponse;
} catch (Exception e) {
logger.error("getSecret error ", e);
throw new RuntimeClientException(e);
}

}
@Override
public GetBulkSecretResponse getBulkSecret(GetBulkSecretRequest req){
try {
RuntimeProto.GetBulkSecretRequest request = RuntimeProto.GetBulkSecretRequest.newBuilder()
.setStoreName(req.getStoreName())
.putAllMetadata(req.getMetaData())
fft0518 marked this conversation as resolved.
Show resolved Hide resolved
.build();

RuntimeProto.GetBulkSecretResponse response = runtimeStubManager.getBlockingStub()
.getBulkSecret(request);
GetBulkSecretResponse getBulkSecretResponse = new GetBulkSecretResponse();
Map<String, Map<String, String>> tempMap = new HashMap<String,Map<String,String>>();
if(!(response.getDataMap().isEmpty())){
for(Map.Entry<String,RuntimeProto.SecretResponse> entry : response.getDataMap().entrySet()){
String key = entry.getKey();
RuntimeProto.SecretResponse value = entry.getValue();
Map<String, String> secretMap = value.getSecretsMap();
tempMap.put(key, secretMap);

}
}
getBulkSecretResponse.setData(tempMap);
return getBulkSecretResponse;
} catch (Exception e) {
logger.error("getSecret error ", e);
throw new RuntimeClientException(e);
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public interface RuntimeClient extends
StateRuntime,
LockRuntime,
SequencerRuntime,
FileRuntime, OssRuntime {
FileRuntime, OssRuntime, SecretRuntime {

void shutdown();
}
13 changes: 13 additions & 0 deletions sdk/src/main/java/spec/sdk/runtime/v1/domain/SecretRuntime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package spec.sdk.runtime.v1.domain;

import spec.sdk.runtime.v1.domain.secret.GetBulkSecretRequest;
import spec.sdk.runtime.v1.domain.secret.GetBulkSecretResponse;
import spec.sdk.runtime.v1.domain.secret.GetSecretRequest;
import spec.sdk.runtime.v1.domain.secret.GetSecretResponse;

public interface SecretRuntime {
ZLBer marked this conversation as resolved.
Show resolved Hide resolved

GetSecretResponse getSecret(GetSecretRequest req);

GetBulkSecretResponse getBulkSecret(GetBulkSecretRequest req);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package spec.sdk.runtime.v1.domain.secret;

import java.util.HashMap;
import java.util.Map;

public class GetBulkSecretRequest {
private String storeName;

private Map<String, String> metaData;

public String getStoreName() {
return storeName;
}

public void setStoreName(String storeName) {
this.storeName = storeName;
}

public Map<String, String> getMetaData() {

return metaData;
}

public void setMetaData(Map<String, String> metaData) {

this.metaData = metaData;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package spec.sdk.runtime.v1.domain.secret;


import java.util.HashMap;
import java.util.Map;

public class GetBulkSecretResponse {

private Map<String, Map<String, String>> data;

public Map<String, Map<String, String>> getData() {
return data;
}

public void setData(Map<String, Map<String, String>> data) {
this.data = data;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package spec.sdk.runtime.v1.domain.secret;

import java.util.HashMap;
import java.util.Map;

public class GetSecretRequest {
private String storeName;

private String key;

private Map<String, String> metaData;

public void setStoreName(String storeName) {
this.storeName = storeName;
}

public void setKey(String key) {
this.key = key;
}

public void setMetaData(Map<String, String> metaData) {

this.metaData = metaData;
}

public String getStoreName() {
return storeName;
}

public String getKey() {
return key;
}

public Map<String, String> getMetaData() {
return metaData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package spec.sdk.runtime.v1.domain.secret;

import java.util.Map;

public class GetSecretResponse {

private Map<String, String> data;

public Map<String, String> getData() {
return data;
}

public void setData(Map<String, String> data) {
this.data = data;
}

}