Service accounts: Fix service account role picker on reload of profile page (#65911)

* working but unsure if best strategy

* removal of roleoptions to pass into the profile component

* remove the dispatch of ac

* added comment

* added one more comment tro trigger pipeline
This commit is contained in:
Eric Leijonmarck 2023-04-04 16:27:40 +01:00 committed by GitHub
parent 819c2f4ad8
commit e9d356010d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 14 deletions

View File

@ -30,7 +30,6 @@ const setup = (propOverrides: Partial<Props>) => {
serviceAccount: {} as ServiceAccountDTO, serviceAccount: {} as ServiceAccountDTO,
tokens: [], tokens: [],
isLoading: false, isLoading: false,
roleOptions: [],
match: { match: {
params: { id: '1' }, params: { id: '1' },
isExact: true, isExact: true,

View File

@ -6,7 +6,7 @@ import { Button, ConfirmModal, HorizontalGroup } from '@grafana/ui';
import { Page } from 'app/core/components/Page/Page'; import { Page } from 'app/core/components/Page/Page';
import { contextSrv } from 'app/core/core'; import { contextSrv } from 'app/core/core';
import { GrafanaRouteComponentProps } from 'app/core/navigation/types'; import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
import { AccessControlAction, ApiKey, Role, ServiceAccountDTO, StoreState } from 'app/types'; import { AccessControlAction, ApiKey, ServiceAccountDTO, StoreState } from 'app/types';
import { ServiceAccountPermissions } from './ServiceAccountPermissions'; import { ServiceAccountPermissions } from './ServiceAccountPermissions';
import { CreateTokenModal, ServiceAccountToken } from './components/CreateTokenModal'; import { CreateTokenModal, ServiceAccountToken } from './components/CreateTokenModal';
@ -26,7 +26,6 @@ interface OwnProps extends GrafanaRouteComponentProps<{ id: string }> {
serviceAccount?: ServiceAccountDTO; serviceAccount?: ServiceAccountDTO;
tokens: ApiKey[]; tokens: ApiKey[];
isLoading: boolean; isLoading: boolean;
roleOptions: Role[];
} }
function mapStateToProps(state: StoreState) { function mapStateToProps(state: StoreState) {
@ -34,7 +33,6 @@ function mapStateToProps(state: StoreState) {
serviceAccount: state.serviceAccountProfile.serviceAccount, serviceAccount: state.serviceAccountProfile.serviceAccount,
tokens: state.serviceAccountProfile.tokens, tokens: state.serviceAccountProfile.tokens,
isLoading: state.serviceAccountProfile.isLoading, isLoading: state.serviceAccountProfile.isLoading,
roleOptions: state.serviceAccounts.roleOptions,
timezone: getTimeZone(state.user), timezone: getTimeZone(state.user),
}; };
} }
@ -58,7 +56,6 @@ export const ServiceAccountPageUnconnected = ({
tokens, tokens,
timezone, timezone,
isLoading, isLoading,
roleOptions,
createServiceAccountToken, createServiceAccountToken,
deleteServiceAccount, deleteServiceAccount,
deleteServiceAccountToken, deleteServiceAccountToken,
@ -171,12 +168,7 @@ export const ServiceAccountPageUnconnected = ({
</HorizontalGroup> </HorizontalGroup>
)} )}
{serviceAccount && ( {serviceAccount && (
<ServiceAccountProfile <ServiceAccountProfile serviceAccount={serviceAccount} timeZone={timezone} onChange={onProfileChange} />
serviceAccount={serviceAccount}
timeZone={timezone}
roleOptions={roleOptions}
onChange={onProfileChange}
/>
)} )}
<HorizontalGroup justify="space-between" height="auto"> <HorizontalGroup justify="space-between" height="auto">
<h3>Tokens</h3> <h3>Tokens</h3>

View File

@ -3,6 +3,7 @@ import React from 'react';
import { dateTimeFormat, GrafanaTheme2, OrgRole, TimeZone } from '@grafana/data'; import { dateTimeFormat, GrafanaTheme2, OrgRole, TimeZone } from '@grafana/data';
import { useStyles2 } from '@grafana/ui'; import { useStyles2 } from '@grafana/ui';
import { fetchRoleOptions } from 'app/core/components/RolePicker/api';
import { contextSrv } from 'app/core/core'; import { contextSrv } from 'app/core/core';
import { AccessControlAction, Role, ServiceAccountDTO } from 'app/types'; import { AccessControlAction, Role, ServiceAccountDTO } from 'app/types';
@ -12,13 +13,13 @@ import { ServiceAccountRoleRow } from './ServiceAccountRoleRow';
interface Props { interface Props {
serviceAccount: ServiceAccountDTO; serviceAccount: ServiceAccountDTO;
timeZone: TimeZone; timeZone: TimeZone;
roleOptions: Role[];
onChange: (serviceAccount: ServiceAccountDTO) => void; onChange: (serviceAccount: ServiceAccountDTO) => void;
} }
export function ServiceAccountProfile({ serviceAccount, timeZone, roleOptions, onChange }: Props): JSX.Element { export function ServiceAccountProfile({ serviceAccount, timeZone, onChange }: Props): JSX.Element {
const styles = useStyles2(getStyles); const styles = useStyles2(getStyles);
const ableToWrite = contextSrv.hasPermission(AccessControlAction.ServiceAccountsWrite); const ableToWrite = contextSrv.hasPermission(AccessControlAction.ServiceAccountsWrite);
const [roles, setRoles] = React.useState<Role[]>([]);
const onRoleChange = (role: OrgRole) => { const onRoleChange = (role: OrgRole) => {
onChange({ ...serviceAccount, role: role }); onChange({ ...serviceAccount, role: role });
@ -27,6 +28,22 @@ export function ServiceAccountProfile({ serviceAccount, timeZone, roleOptions, o
const onNameChange = (newValue: string) => { const onNameChange = (newValue: string) => {
onChange({ ...serviceAccount, name: newValue }); onChange({ ...serviceAccount, name: newValue });
}; };
// TODO: this is a temporary solution to fetch roles for service accounts
// until we make use of the state from the serviceaccountspage
// and pass it down to the serviceaccountprofile
React.useEffect(() => {
if (contextSrv.licensedAccessControlEnabled()) {
if (contextSrv.hasPermission(AccessControlAction.ActionRolesList)) {
fetchRoleOptions(serviceAccount.orgId)
.then((roles) => {
setRoles(roles);
})
.catch((err) => {
console.log('fetchRoleOptions error: ', err);
});
}
}
}, [serviceAccount.orgId]);
return ( return (
<div className={styles.section}> <div className={styles.section}>
@ -44,7 +61,7 @@ export function ServiceAccountProfile({ serviceAccount, timeZone, roleOptions, o
label="Roles" label="Roles"
serviceAccount={serviceAccount} serviceAccount={serviceAccount}
onRoleChange={onRoleChange} onRoleChange={onRoleChange}
roleOptions={roleOptions} roleOptions={roles}
/> />
<ServiceAccountProfileRow <ServiceAccountProfileRow
label="Creation date" label="Creation date"