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 support for membership policy creation #22

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions deployment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ includes multiple components working together for real-time messaging and data t
- Communicates with the UI application.
- **Setup**: A separate backend application is deployed for each EDC instance.

**NOTE:**

- When the backend application starts, it will create the Asset with id `edc-chat-app`, Policy, and Contract Definition. Please refer `EDCService.initializePreEdcProcess()` for more details
- However, if the assetId as an `edc-chat-app` is already in the edc database, it’ll skip this process.
- Policy is created to check `Membership` credential of participant. Each participate needs to present `Membership` credential to start data negotiation.


### **UI Application**

- **Purpose**: Provides a WebSocket-enabled interface for real-time chat.
Expand Down
7 changes: 5 additions & 2 deletions edc-chat-app-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ successful chat flow:
- a Postgres database to store other Business Partners' information and chat history.
- a simple UI to send messages to and receive messages from other Business Partners.

**NOTE:**
- When the backend application starts, it will create the Asset with id `edc-chat-app`, Policy, and Contract Definition. Please refer `EDCService.initializePreEdcProcess()` for more details
- However, if the assetId as an `edc-chat-app` is already in the edc database, it’ll skip this process.
- Policy is created to check `Membership` credential of participant. Each participate needs to present `Membership` credential to start data negotiation.

## Packages Overview

The backend application consists of the below packages:
Expand Down Expand Up @@ -69,8 +74,6 @@ server. The [WebSocketConfig.java](src/main/java/com/smartsense/chat/config/WebS
| `AGREEMENT_RETRY_LIMIT` | Max retry check for contract to be finalized |
| `APP_HOST_URL` | Application host url to add in asset |



- Run the application by executing the following command:
```bash
./gradlew bootRun
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
import com.smartsense.chat.edc.client.EDCConnectorClient;
import com.smartsense.chat.edc.settings.AppConfig;
import com.smartsense.chat.service.ChatMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
Expand Down Expand Up @@ -58,7 +57,7 @@ private Map<String, Object> prepareNegotiationRequest(String receiverDspUrl, Str
negotiationRequest.put("edc:counterPartyAddress", receiverDspUrl);
negotiationRequest.put("edc:protocol", "dataspace-protocol-http");
negotiationRequest.put("edc:counterPartyId", receiverBpnL);
negotiationRequest.put("edc:policy", prepareNegotiationPolicy(receiverBpnL, offerId));
negotiationRequest.put("edc:policy", prepareNegotiationMembershipPolicy(receiverBpnL, offerId));
log.info("Negotiation request looks like: {}", negotiationRequest);
return negotiationRequest;
}
Expand All @@ -72,4 +71,15 @@ private Map<String, Object> prepareNegotiationPolicy(String receiverBpnL, String
negotiationPolicy.put("assigner", receiverBpnL);
return negotiationPolicy;
}

private Map<String, Object> prepareNegotiationMembershipPolicy(String receiverBpnL, String offerId) {
Map<String, Object> negotiationPolicy = new HashMap<>();
negotiationPolicy.put("@id", offerId);
negotiationPolicy.put("@type", "Offer");
negotiationPolicy.put("permission", List.of(Map.of("action", "use",
"constraint", Map.of("and", List.of(Map.of("leftOperand", "Membership", "operator", "eq", "rightOperand", "active"))))));
negotiationPolicy.put("target", config.edc().assetId());
negotiationPolicy.put("assigner", receiverBpnL);
return negotiationPolicy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

import com.smartsense.chat.edc.client.EDCConnectorClient;
import com.smartsense.chat.edc.settings.AppConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
Expand All @@ -25,7 +24,7 @@ public class PolicyCreationService {

public void createPolicy() {
log.info("Policy creation process is started...");
edc.createPolicy(config.edc().edcUri(), cretePolicyRequest(), config.edc().authCode());
edc.createPolicy(config.edc().edcUri(), createMemberShipPolicyRequest(), config.edc().authCode());
log.info("Policy creation process is completed...");
}

Expand All @@ -38,4 +37,15 @@ private Map<String, Object> cretePolicyRequest() {
policyCreation.put("policy", Map.of("@type", "Set", "permission", List.of(Map.of("action", "use"))));
return policyCreation;
}

private Map<String, Object> createMemberShipPolicyRequest() {
Map<String, Object> policyCreation = new HashMap<>();
policyCreation.put("@context", List.of("https://w3id.org/tractusx/policy/v1.0.0", "http://www.w3.org/ns/odrl.jsonld",
Map.of("@vocab", "https://w3id.org/edc/v0.0.1/ns/", "dct", "https://purl.org/dc/terms/")));
policyCreation.put("@type", "PolicyDefinitionRequestDto");
policyCreation.put("@id", config.edc().policyId());
policyCreation.put("policy", Map.of("@type", "Set", "permission", List.of(Map.of("action", "use",
"constraint", Map.of("and", List.of(Map.of("leftOperand", "Membership", "operator", "eq", "rightOperand", "active")))))));
return policyCreation;
}
}
Loading