grafana/public/app/features/serviceaccounts/components/ServiceAccountProfile.tsx
linoman 408dab8c57
IAM: Protect managed service account frontend details page (#77839)
* Add `isManaged` property to frontend model

* Remove enabled and token buttons for managed SA

* Replace trash icon for lock icon for managed SA

* Block the role picker for managed SA

* Filter SA list usiong the managed filter

* Rename external for managed

* Add only managed filter

* Toggle the enable buttons for managed sa

* Disable add token and delete token buttons

* Remove the edit name button

* Disable the Role picker for managed sa

* Hide the permissions section

* Add managed by row

---------

Co-authored-by: Gabriel MABILLE <gamab@users.noreply.github.com>
Co-authored-by: Sofia Papagiannaki <1632407+papagian@users.noreply.github.com>
2023-11-14 17:52:48 +01:00

92 lines
3.0 KiB
TypeScript

import { css } from '@emotion/css';
import React from 'react';
import { dateTimeFormat, GrafanaTheme2, OrgRole, TimeZone } from '@grafana/data';
import { Label, TextLink, useStyles2 } from '@grafana/ui';
import { fetchRoleOptions } from 'app/core/components/RolePicker/api';
import { contextSrv } from 'app/core/core';
import { AccessControlAction, Role, ServiceAccountDTO } from 'app/types';
import { ServiceAccountProfileRow } from './ServiceAccountProfileRow';
import { ServiceAccountRoleRow } from './ServiceAccountRoleRow';
interface Props {
serviceAccount: ServiceAccountDTO;
timeZone: TimeZone;
onChange: (serviceAccount: ServiceAccountDTO) => void;
}
export function ServiceAccountProfile({ serviceAccount, timeZone, onChange }: Props): JSX.Element {
const styles = useStyles2(getStyles);
const ableToWrite = contextSrv.hasPermission(AccessControlAction.ServiceAccountsWrite);
const [roles, setRoleOptions] = React.useState<Role[]>([]);
const onRoleChange = (role: OrgRole) => {
onChange({ ...serviceAccount, role: role });
};
const onNameChange = (newValue: string) => {
onChange({ ...serviceAccount, name: newValue });
};
React.useEffect(() => {
async function fetchOptions() {
try {
if (contextSrv.hasPermission(AccessControlAction.ActionRolesList)) {
let options = await fetchRoleOptions(serviceAccount.orgId);
setRoleOptions(options);
}
} catch (e) {
console.error('Error loading options for service account');
}
}
if (contextSrv.licensedAccessControlEnabled()) {
fetchOptions();
}
}, [serviceAccount.orgId]);
return (
<div className={styles.section}>
<h3>Information</h3>
<table className="filter-table">
<tbody>
<ServiceAccountProfileRow
label="Name"
value={serviceAccount.name}
onChange={!serviceAccount.isExternal ? onNameChange : undefined}
disabled={!ableToWrite || serviceAccount.isDisabled}
/>
<ServiceAccountProfileRow label="ID" value={serviceAccount.login} disabled={serviceAccount.isDisabled} />
<ServiceAccountRoleRow
label="Roles"
serviceAccount={serviceAccount}
onRoleChange={onRoleChange}
roleOptions={roles}
/>
<ServiceAccountProfileRow
label="Creation date"
value={dateTimeFormat(serviceAccount.createdAt, { timeZone })}
disabled={serviceAccount.isDisabled}
/>
{serviceAccount.isExternal && serviceAccount.requiredBy && (
<tr>
<td>
<Label>Used by</Label>
</td>
<td>
<TextLink href={`/plugins/${serviceAccount.requiredBy}`}>{serviceAccount.requiredBy}</TextLink>
</td>
</tr>
)}
</tbody>
</table>
</div>
);
}
export const getStyles = (theme: GrafanaTheme2) => ({
section: css`
margin-bottom: ${theme.spacing(4)};
`,
});