Skip to content

Commit

Permalink
Feature to listen and report PDC events (#3)
Browse files Browse the repository at this point in the history
* Allow block listener to specify the peer listen options

1. Enable private data capability as part of peer listen
request on channel service.
2. Refactoring

Signed-off-by: S m, Aruna <[email protected]>

* Private Data Collections in published event structure

The commit also refactors the configuration files and allows
for bean creation instead of explicit singleton class definitions.

Signed-off-by: weihong-ou <[email protected]>
Signed-off-by: S m, Aruna <[email protected]>

* Refactor and fix test cases

1. Move configuration to the Config package.
2. Refactor within components and services to move them
to appropriate packages.

Signed-off-by: S m, Aruna <[email protected]>

* Update the fabric-sdk-java version

Introduce a version with private data events in the SDK.

Signed-off-by: S m, Aruna <[email protected]>

Co-authored-by: weihong-ou <[email protected]>
  • Loading branch information
arsulegai and weihong-ou authored Feb 23, 2022
1 parent 7d32d41 commit 845b096
Show file tree
Hide file tree
Showing 33 changed files with 532 additions and 537 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,6 @@ target/
.env
/config/application.yml
/config/connection-*
docker-compose-*
docker-compose-*
secrets
stores
3 changes: 0 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ RUN addgroup -S -g 10001 appGrp \
&& chown -R 10000:10001 /usr/local/tomcat && chown -R 10000:10001 /usr/local/config

USER 10000

COPY ./config/index.html /usr/local/tomcat/webapps/ROOT/

COPY ./config/setenv.sh /usr/local/tomcat/bin/

COPY ./target/hlf-connector.war /usr/local/tomcat/webapps/hlf-connector.war
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ services:
networks:
default:
external:
name: net_test
name: net_test
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<properties>
<java.version>8</java.version>
<jacoco.version>0.8.5</jacoco.version>
<protobuf.version>3.19.2</protobuf.version>
<spring-cloud.version>Hoxton.SR6</spring-cloud.version>
</properties>

Expand Down Expand Up @@ -104,10 +105,15 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
<dependency>
<groupId>org.hyperledger.fabric-sdk-java</groupId>
<artifactId>fabric-sdk-java</artifactId>
<version>2.2.0</version>
<version>2.2.12</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/hlf/java/rest/client/FabricClientBootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

/** Bootstrap for the Fabric-Client application */
@Slf4j
// @EnableRetry
// @EnableAsync
@SpringBootApplication
@EnableConfigurationProperties
public class FabricClientBootstrap extends SpringBootServletInitializer {
Expand Down

This file was deleted.

This file was deleted.

80 changes: 80 additions & 0 deletions src/main/java/hlf/java/rest/client/config/FabricProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package hlf.java.rest.client.config;

import javax.annotation.PostConstruct;
import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
* FabricProperties reads and binds with the application.yml and provide all the configuration as a
* bean configuration To use any configuration, just autowire and call the associated "get" method
*/
@Data
@Configuration
@ConfigurationProperties(prefix = "fabric")
public class FabricProperties {

private static final String SYSTEM_PROP_FABRIC_SERVICE_DISCOVERY_LOCALHOST =
"org.hyperledger.fabric.sdk.service_discovery.as_localhost";

private boolean localhostReportAddress;
private WalletConfig wallet;
private OrgConnectionConfig orgConnectionConfig;
private Client client;
private Events events;

@Data
public static class WalletConfig {
private String path;
private AdminUser adminUser;
private ClientUser clientUser;

@Data
public static class AdminUser {
private String name;
}

@Data
public static class ClientUser {
private String name;
}
}

@Data
public static class OrgConnectionConfig {
private String path;
private String filename;
}

@Data
public static class Client {
private Rest rest;

@Data
public static class Rest {
private String apikey;
}
}

@Data
@ConditionalOnProperty(prefix = "fabric.events", name = "enable", havingValue = "true")
public static class Events {
private boolean enable;
private String chaincode;
private String block;
}

/**
* Forces Hyperledger Service Discovery to report the localhost address for all nodes (peers &
* ordering service) when client is running on local machine. This property allows client to node
* connectivity when nodes cannot be accessed directly by the client on their public network
* address as in the case above.
*/
@PostConstruct
private void systemVariableSetup() {
System.setProperty(
SYSTEM_PROP_FABRIC_SERVICE_DISCOVERY_LOCALHOST,
String.valueOf(this.isLocalhostReportAddress()));
}
}
57 changes: 57 additions & 0 deletions src/main/java/hlf/java/rest/client/config/GatewayConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package hlf.java.rest.client.config;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.gateway.Wallet;
import org.hyperledger.fabric.gateway.Wallets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/** Configure Gateway connection for the Fabric network. */
@Slf4j
@Configuration
public class GatewayConfig {

@Autowired FabricProperties fabricProperties;

/**
* Create the gateway connection for connecting to the peer.
*
* @return gateway Gateway object to connect to Fabric network
* @throws IOException
*/
@Bean
public Gateway gateway(Wallet wallet) throws IOException {
// Load the Network Connection Configuration path
Path networkConfigPath =
Paths.get(
fabricProperties.getOrgConnectionConfig().getPath(),
fabricProperties.getOrgConnectionConfig().getFilename());
// Create the gateway builder based on the path to the org configuration file
// using the specified user, then connect
Gateway.Builder builder = Gateway.createBuilder();
builder
.identity(wallet, fabricProperties.getWallet().getClientUser().getName())
.networkConfig(networkConfigPath)
.discovery(true);
return builder.connect();
}

/**
* Obtain the Wallet details containing the user id information.
*
* @return wallet Wallet pull credentials from wallet
* @throws IOException
*/
@Bean
public Wallet wallet() throws IOException {
log.info("Obtain the Wallet containing Admin and Client user information");
// Load a file system based wallet for managing identities.
Path walletPath = Paths.get(fabricProperties.getWallet().getPath());
return Wallets.newFileSystemWallet(walletPath);
}
}
15 changes: 5 additions & 10 deletions src/main/java/hlf/java/rest/client/config/KafkaProperties.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package hlf.java.rest.client.config;

import lombok.Getter;
import lombok.Setter;
import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
Expand All @@ -10,17 +9,15 @@
* The type Kafka properties is added for fetching Kafka properties as configuration and can be used
* in Consumer and Producer using @Autowired
*/
@Getter
@Setter
@Data
@Configuration
@ConfigurationProperties(prefix = "kafka")
public class KafkaProperties {

private Consumer integration;
private Producer eventListener;

@Getter
@Setter
@Data
@ConditionalOnProperty("kafka.integration.brokerHost")
@Configuration
@ConfigurationProperties(prefix = "kafka.integration")
Expand All @@ -31,8 +28,7 @@ public static class Consumer extends SSLProperties {
private String saslJaasConfig;
}

@Getter
@Setter
@Data
@ConditionalOnProperty("kafka.event-listener.brokerHost")
@Configuration
@ConfigurationProperties(prefix = "kafka.event-listener")
Expand All @@ -43,8 +39,7 @@ public static class Producer extends SSLProperties {
}

/** The type Ssl properties is added for configuring SSL configuration for Kafka Cluster. */
@Getter
@Setter
@Data
public static class SSLProperties {

protected boolean sslEnabled;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hlf/java/rest/client/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired private HandlerExceptionResolver handlerExceptionResolver;

@Autowired private ApplicationProperties applicationProperties;
@Autowired private FabricProperties fabricProperties;

/**
* The configure method sets the permission limits to the Http request. Also, filter has been
Expand All @@ -34,7 +34,7 @@ protected void configure(HttpSecurity http) throws Exception {
.anyRequest()
.and()
.addFilterBefore(
new HeaderAuthenticationFilter(applicationProperties, handlerExceptionResolver),
new HeaderAuthenticationFilter(fabricProperties, handlerExceptionResolver),
LogoutFilter.class)
.csrf()
.disable(); // NOSONAR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
@Slf4j
@Configuration
public class SwaggerConfiguration {
public class SwaggerConfig {

/**
* This bean is responsible for adding api key header for all the APIs exposed. Open api global
Expand All @@ -41,17 +41,6 @@ public OpenApiCustomiser openApiGlobalHeaderCustomizer() {
});
}

/*
* @Bean public OpenAPI customOpenAPI(@Value("${springdoc.version}") String
* appVersion) { return new OpenAPI() .components(new Components()
* .addParameters("myHeader1", new Parameter().in("header").schema(new
* StringSchema()).name("myHeader1")).addHeaders("myHeader2", new
* Header().description("myHeader2 header").schema(new StringSchema())))
* .info(new Info() .title("Hyperledger Fabric REST Client")
* .version(appVersion)
* .description("Hyperledger Fabric REST Client for invoking and query fabric smart-contract"
* )); }
*/
@Bean
SpringDocConfiguration springDocConfiguration() {
return new SpringDocConfiguration();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/hlf/java/rest/client/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public enum ErrorCode {

HYPERLEDGER_FABRIC_TRANSACTION_ERROR(6000, "Hyperledger Fabric transaction related error"),

HYPERLEDGER_FABRIC_NOT_SUPPORTED(8000, "In Hyperledger Fabric this feature is not supported"),

AUTH_INVALID_API_KEY(9000, "Invalid api key"),

AUTH_EMPTY_USER_ID(9001, "Empty Or null User Id"),
Expand Down
Loading

0 comments on commit 845b096

Please sign in to comment.