Skip to content

Commit

Permalink
Trust bundle fetch
Browse files Browse the repository at this point in the history
Implemented the initial code for trust bundle fetching.

Signed-off-by: Kai Chen <[email protected]>
  • Loading branch information
EdgarAllan-Bro committed Dec 11, 2024
1 parent e2278dd commit 233f32f
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
2 changes: 2 additions & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import SelectServer from "./components/select-server";
import ClusterList from "./components/cluster-list";
import ClusterManagement from "./components/cluster-management";
import FederationList from "./components/federation-list";
import TrustBundleCreate from "components/trustbundle-create";
import AgentList from "./components/agent-list";
import CreateJoinToken from "./components/agent-create-join-token";
import EntryList from "./components/entry-list";
Expand Down Expand Up @@ -38,6 +39,7 @@ function App() {
<Route path="/" exact component={AgentList} />
<Route path="/clusters" exact component={ClusterList} />
<Route path="/federations" exact component={FederationList} />
<Route path="/trustbundle" exact component={TrustBundleCreate} />
<Route path="/agents" exact component={AgentList} />
<Route path="/entries" exact component={EntryList} />
<RenderOnAdminRole>
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,12 @@ class NavigationBar extends Component<NavigationBarProp, NavigationBarState> {
withAuth={Boolean(withAuth)}
subLinks={[
{ label: 'Federations List', to: '/federations' },
{ label: 'Obtain Trust Bundle', to: '/trustbundle', adminOnly: true},
{ label: 'Create Federation', to: '/federation/create', adminOnly: true},
]}
/>
</div>

<div className="dropdown">
<BareMetalServer className="icon-spacing"/>
<a href="/tornjak/serverinfo" className="dropbtn">Tornjak ServerInfo</a>
Expand Down
142 changes: 142 additions & 0 deletions frontend/src/components/trustbundle-create.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { Component } from 'react';
import axios from 'axios';
import { InlineNotification, TextArea, Button } from 'carbon-components-react';
import { ToastContainer } from 'react-toastify';
import GetApiServerUri from './helpers';
import TornjakApi from './tornjak-api-helpers';
import { connect } from 'react-redux';
import {
tornjakMessageFunc,
serverInfoUpdateFunc,
agentsListUpdateFunc,
clusterTypeInfoFunc,
tornjakServerInfoUpdateFunc,
selectorInfoFunc,
serverSelectedFunc
} from 'redux/actions';
import { RootState } from 'redux/reducers';
import { showResponseToast } from './error-api';
import './style.css';

type TrustBundleCreateProps = {
tornjakMessageFunc: (globalErrorMessage: string) => void,
serverInfoUpdateFunc: Function,
agentsListUpdateFunc: Function,
clusterTypeInfoFunc: Function,
tornjakServerInfoUpdateFunc: Function,
selectorInfoFunc: Function,
serverSelectedFunc: Function,
globalErrorMessage: string,
globalServerSelected: string,
};

type TrustBundleCreateState = {
trustBundle: string,
loading: boolean,
error: string
}

class TrustBundleCreate extends Component<TrustBundleCreateProps, TrustBundleCreateState> {
TornjakApi: TornjakApi;
constructor(props: TrustBundleCreateProps) {
super(props);
this.TornjakApi = new TornjakApi(props);
this.state = {
trustBundle: "",
loading: false,
error: ""
};
this.getTrustBundle = this.getTrustBundle.bind(this);
}

getTrustBundle() {
this.setState({loading: true, error: "", trustBundle: ""});
const endpoint = GetApiServerUri('/api/v1/spire/bundle');

console.log("Got: " + endpoint);

axios.get(endpoint)
.then(res => {
this.setState({
trustBundle: JSON.stringify(res.data, null, 2),
loading: false
});
})
.catch(err => {
console.error("Error fetching trust bundle:", err);
let errorMessage = "Failed to fetch trust bundle. Could not connect to backend";
if (err.response) {
errorMessage = `Failed to fetch trust bundle. Server returned status ${err.response.status}`;
} else if (err.request) {
errorMessage = "Failed to fetch trust bundle. No response received from backend.";
}

this.setState({loading: false, error: errorMessage});
showResponseToast(err);
});
}

render() {
const { trustBundle, loading, error } = this.state;
return (
<div className="trustbundle-create" data-test="trustbundle-create">
<h3>Obtain Trust Bundle</h3>
<div>
<Button onClick={this.getTrustBundle} kind="primary">Get Trust Bundle</Button>
</div>
{loading && (
<InlineNotification
kind="info"
title="Loading..."
subtitle="Fetching the trust bundle from the SPIRE server."
hideCloseButton
/>
)}
{error && (
<InlineNotification
kind="error"
title="Error"
subtitle={error}
hideCloseButton
/>
)}
{!loading && !error && trustBundle && (
<div className="trust-bundle-area">
<TextArea
cols={50}
labelText="Fetched Trust Bundle"
rows={10}
value={trustBundle}
readOnly
/>
</div>
)}
<ToastContainer
className="carbon-toast"
containerId="notifications"
draggable={false}
/>
</div>
);
}
}

const mapStateToProps = (state: RootState) => ({
globalServerSelected: state.servers.globalServerSelected,
globalErrorMessage: state.tornjak.globalErrorMessage,
});

export default connect(
mapStateToProps,
{
tornjakMessageFunc,
serverInfoUpdateFunc,
agentsListUpdateFunc,
clusterTypeInfoFunc,
tornjakServerInfoUpdateFunc,
selectorInfoFunc,
serverSelectedFunc,
}
)(TrustBundleCreate);

export { TrustBundleCreate };

0 comments on commit 233f32f

Please sign in to comment.