Skip to content

Commit

Permalink
Push analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Sep 23, 2024
1 parent 47114a2 commit 18572d6
Show file tree
Hide file tree
Showing 10 changed files with 385 additions and 101 deletions.
4 changes: 4 additions & 0 deletions manage-gui/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ export function getMigratedPdPPolicies() {
return fetchJson("pdp/migrated_policies");
}

export function getPolicyPushAnalysis() {
return fetchJson("/pdp/push_analysis");
}

export function importPdPPolicies() {
return postPutJson("pdp/import_policies", {}, "PUT")
}
Expand Down
6 changes: 4 additions & 2 deletions manage-gui/src/components/PolicyMissingEnforcements.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ export default function PolicyMissingEnforcements({}) {
<section className="policy-response">
<table>
<thead>
<tr>
{headers.map((header, index) => <th className={header} key={index}>
{I18n.t(`policies.${header}`)}
</th>)}
</tr>
</thead>
<tbody>
{policies.map((policy, index) => <tr>
{policies.map((policy, index) => <tr key={index}>
<td><a href={`/metadata/policy/${policy.id}`} target="_blank">
{policy.data.name}
</a></td>
Expand All @@ -47,7 +49,7 @@ export default function PolicyMissingEnforcements({}) {
<td>
<div className="providers">
{policy.data.policyEnforcementDecisionAbsent.map((provider, index) =>
<a href={`/metadata/${provider.type}/${provider.id}`} target="_blank">
<a key={index} href={`/metadata/${provider.type}/${provider.id}`} target="_blank">
{`${getNameForLanguage(provider.data.metaDataFields)}${organisationName(provider)}`}
</a>
)}
Expand Down
140 changes: 68 additions & 72 deletions manage-gui/src/pages/Policies.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React from "react";
import I18n from "i18n-js";
import {isEmpty, stop} from "../utils/Utils";
import ConfirmationDialog from "../components/ConfirmationDialog";
import "./Policies.scss";
import {getMigratedPdPPolicies, getPdPPolicies, importPdPPolicies} from "../api";
import ReactDiffViewer, {DiffMethod} from 'react-diff-viewer-continued';
import {getPolicyPushAnalysis, importPdPPolicies} from "../api";
import PolicyPlayground from "../components/PolicyPlaygound";
import withRouterHooks from "../utils/RouterBackwardCompatability";
import PolicyMissingEnforcements from "../components/PolicyMissingEnforcements";
Expand All @@ -19,24 +17,27 @@ class Policies extends React.PureComponent {
tabs: tabs,
selectedTab: tab,
importResults: {},
pdpPolicies: [],
pdpMigratedPolicies: [],
showMoreImported: false,
policyPushAnalysis: {differences:[], missing_policies:[]},
loading: false,
copiedToClipboardClassName: "",
confirmationDialogOpen: false,
confirmationQuestion: "",
confirmationDialogAction: () => this,
cancelDialogAction: () => this.setState({confirmationDialogOpen: false})
};
}

componentDidMount() {
const {selectedTab} = this.state;
if (selectedTab === "push") {
this.initialState();
}
}


initialState = e => {
stop(e);
this.setState({loading: true});
Promise.all([getMigratedPdPPolicies(), getPdPPolicies()])
getPolicyPushAnalysis()
.then(res => this.setState({
pdpMigratedPolicies: res[0],
pdpPolicies: res[1],
policyPushAnalysis: res,
loading: false
}));
}
Expand All @@ -55,16 +56,20 @@ class Policies extends React.PureComponent {
}
if (tab === "push") {
this.setState({loading: true});
Promise.all([getMigratedPdPPolicies(), getPdPPolicies()])
getPolicyPushAnalysis()
.then(res => this.setState({
pdpMigratedPolicies: res[0],
pdpPolicies: res[1],
policyPushAnalysis: res,
loading: false
}));
}
this.props.navigate(`/policies/${tab}`);
};

toggleShowMore = e => {
stop(e);
this.setState({showMoreImported: !this.state.showMoreImported})
}

renderTab = (tab, selectedTab) =>
<span key={tab}
className={tab === selectedTab ? "active" : ""}
Expand All @@ -79,77 +84,75 @@ class Policies extends React.PureComponent {
}

renderImport = () => {
const {importResults, loading} = this.state;
const {importResults, showMoreImported, loading} = this.state;
return (
<section className="import">
<p>Import the current PdP policies into Manage. Once imported they can be pushed.</p>
<p>For now PdP does not overwrite the current policies in the push-endpoint, but stores them in a policy
migrations table</p>
<a className={`button ${loading ? "grey disabled" : "green"}`}
onClick={this.runImport}>
{I18n.t("policies.runImport")}
</a>
{!isEmpty(importResults) &&
<section className="results">
<h2>Imported policies</h2>
<ul className="policies">
{importResults.imported.map((metaData,index ) => <li key={index}>
<span>{metaData.data.name}</span>
<span>{metaData.data.description}</span>
</li>)}
</ul>
<h2>Not imported policies</h2>
<ul className="policies">
{importResults.errors.map((data, index) => <li key={index}>
<span>{data.name}</span>
<span>{data.error}</span>
</li>)}
{importResults.errors.map((data, index) =>
<li key={index}>
<span>{data.name}</span>
<span>{data.error}</span>
</li>)}
</ul>
<h2>Imported policies</h2>
<a href={"/#show"}
onClick={this.toggleShowMore}>
{!showMoreImported ? "Show all" : "Hide"}
</a>
{showMoreImported && <ul className="policies">
{importResults.imported.map((metaData, index) =>
<li key={index}>
<span>{metaData.data.name}</span>
<span>{metaData.data.description}</span>
</li>)}
</ul>}
</section>}
</section>
);
};

renderPush = () => {
const {pdpMigratedPolicies, pdpPolicies, loading} = this.state;
const missingPolicies = pdpPolicies
.filter(p => !pdpMigratedPolicies.some(mp => mp.name === p.name));
const forgotToPush = pdpMigratedPolicies.some(policy => !pdpPolicies.find(p => p.name === policy.name))
const {policyPushAnalysis, loading} = this.state;
return (
<section className="import">
<p>After importing the current PdP policies into Manage and subsequently pushing those Manage policies
to PdP,
we now can compare the original PdP policies with the pushed ones.</p>
to PdP, we now compare the original PdP policies with the pushed ones.</p>
<a className={`button ${loading ? "grey disabled" : "green"}`}
onClick={e => this.initialState(e)}>
{I18n.t("policies.reload")}
</a>
{(!isEmpty(pdpPolicies)) &&
<section className="results">
{!isEmpty(missingPolicies) && <div>
<h2>Not imported policies</h2>
<ul className="policies">
{missingPolicies.map((policy, index) => <li key={index}>
<span>{policy.name}</span>
<span>{policy.description}</span>
</li>)}
</ul>
</div>}
<h2>Policies compared</h2>
{!forgotToPush && <ul className="policies">
{pdpMigratedPolicies
.map((policy, index) => <li key={index}>
<span>{policy.name}</span>
<span>{policy.description}</span>
<ReactDiffViewer oldValue={pdpPolicies.find(p => p.name === policy.name).xml}
newValue={policy.xml}
compareMethod={DiffMethod.TRIMMED_LINES}
splitView={true}/>
</li>)}
</ul>}
{forgotToPush &&
<p>You did not push the latest policies to PdP. Can't compare before you do.</p>}
</section>}
<section className="results">
<h2># Total PDP policies </h2>
<p>{policyPushAnalysis.policy_count}</p>
<h2># Total active PDP policies </h2>
<p>{policyPushAnalysis.active_policy_count}</p>
<h2># Pushed policies</h2>
<p>{policyPushAnalysis.migrated_policy_count}</p>
<h2>Missing policies</h2>
{policyPushAnalysis.missing_policies.length === 0 && <p>None missing</p>}
<ul className="policies">
{policyPushAnalysis.missing_policies.map((policy, index) => <li key={index}>
<span>{policy.name}</span>
<span>{policy.description}</span>
</li>)}
</ul>
<h2>Diffs between policies</h2>
{policyPushAnalysis.differences.length === 0 && <p>No diffs</p>}
<ul className="policies">
{policyPushAnalysis.differences.map((diff, index) => <li key={index}>
<span>{Object.keys(diff)[0]}</span>
<span>{Object.values(diff)[0]}</span>
</li>)}
</ul>
</section>
</section>
);
};
Expand All @@ -165,14 +168,15 @@ class Policies extends React.PureComponent {
<PolicyMissingEnforcements/>
);
};

renderCurrentTab = selectedTab => {
switch (selectedTab) {
case "import" :
return this.renderImport();
case "playground" :
return this.renderPlayground();
case "push" :
return this.renderPush();
case "playground" :
return this.renderPlayground();
case "missing_enforcements" :
return this.renderMissingEnforcements();
default :
Expand All @@ -183,18 +187,10 @@ class Policies extends React.PureComponent {
render() {
const {
tabs,
selectedTab,
confirmationDialogOpen,
confirmationQuestion,
confirmationDialogAction,
cancelDialogAction
selectedTab
} = this.state;
return (
<div className="mod-policies">
<ConfirmationDialog isOpen={confirmationDialogOpen}
cancel={cancelDialogAction}
confirm={confirmationDialogAction}
question={confirmationQuestion}/>
<section className="tabs">
{tabs.map(tab => this.renderTab(tab, selectedTab))}
</section>
Expand Down
11 changes: 8 additions & 3 deletions manage-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.xmlunit/xmlunit-core -->
<dependency>
<groupId>org.xmlunit</groupId>
<artifactId>xmlunit-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
Expand All @@ -182,8 +188,8 @@
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.27.2</version>
<artifactId>wiremock-standalone</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -192,7 +198,6 @@
<version>5.8.2</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down
Loading

0 comments on commit 18572d6

Please sign in to comment.