mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
ServiceAccounts: Fix token creation with expiry (#45855)
* ServiceAccounts: Fix token creation with expiry * ServiceAccounts: remove extraneous import
This commit is contained in:
parent
33b6f996e1
commit
1cc5b25a05
@ -15,6 +15,7 @@ import {
|
|||||||
RadioButtonGroup,
|
RadioButtonGroup,
|
||||||
useStyles2,
|
useStyles2,
|
||||||
} from '@grafana/ui';
|
} from '@grafana/ui';
|
||||||
|
import { ApiKey, OrgRole } from 'app/types';
|
||||||
|
|
||||||
const EXPIRATION_OPTIONS = [
|
const EXPIRATION_OPTIONS = [
|
||||||
{ label: 'No expiration', value: false },
|
{ label: 'No expiration', value: false },
|
||||||
@ -24,7 +25,7 @@ const EXPIRATION_OPTIONS = [
|
|||||||
interface CreateTokenModalProps {
|
interface CreateTokenModalProps {
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
token: string;
|
token: string;
|
||||||
onCreateToken: (name: string) => void;
|
onCreateToken: (token: ApiKey) => void;
|
||||||
onClose: () => void;
|
onClose: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,7 +88,16 @@ export const CreateTokenModal = ({ isOpen, token, onCreateToken, onClose }: Crea
|
|||||||
</Field>
|
</Field>
|
||||||
)}
|
)}
|
||||||
</FieldSet>
|
</FieldSet>
|
||||||
<Button onClick={() => onCreateToken(newTokenName)} disabled={isWithExpirationDate && !isExpirationDateValid}>
|
<Button
|
||||||
|
onClick={() =>
|
||||||
|
onCreateToken({
|
||||||
|
name: newTokenName,
|
||||||
|
role: OrgRole.Viewer,
|
||||||
|
secondsToLive: getSecondsToLive(newTokenExpirationDate),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
disabled={isWithExpirationDate && !isExpirationDateValid}
|
||||||
|
>
|
||||||
Generate token
|
Generate token
|
||||||
</Button>
|
</Button>
|
||||||
</>
|
</>
|
||||||
@ -128,6 +138,13 @@ export const CreateTokenModal = ({ isOpen, token, onCreateToken, onClose }: Crea
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getSecondsToLive = (date: Date | string) => {
|
||||||
|
const dateAsDate = new Date(date);
|
||||||
|
const now = new Date();
|
||||||
|
|
||||||
|
return Math.ceil((dateAsDate.getTime() - now.getTime()) / 1000);
|
||||||
|
};
|
||||||
|
|
||||||
const getStyles = (theme: GrafanaTheme2) => {
|
const getStyles = (theme: GrafanaTheme2) => {
|
||||||
return {
|
return {
|
||||||
modal: css`
|
modal: css`
|
||||||
|
@ -12,7 +12,7 @@ import {
|
|||||||
createServiceAccountToken,
|
createServiceAccountToken,
|
||||||
} from './state/actions';
|
} from './state/actions';
|
||||||
import { ServiceAccountTokensTable } from './ServiceAccountTokensTable';
|
import { ServiceAccountTokensTable } from './ServiceAccountTokensTable';
|
||||||
import { getTimeZone, NavModel, OrgRole } from '@grafana/data';
|
import { getTimeZone, NavModel } from '@grafana/data';
|
||||||
import { Button, VerticalGroup } from '@grafana/ui';
|
import { Button, VerticalGroup } from '@grafana/ui';
|
||||||
import { CreateTokenModal } from './CreateTokenModal';
|
import { CreateTokenModal } from './CreateTokenModal';
|
||||||
|
|
||||||
@ -67,15 +67,8 @@ const ServiceAccountPageUnconnected = ({
|
|||||||
deleteServiceAccountToken(parseInt(match.params.id, 10), key.id!);
|
deleteServiceAccountToken(parseInt(match.params.id, 10), key.id!);
|
||||||
};
|
};
|
||||||
|
|
||||||
const onCreateToken = (name: string) => {
|
const onCreateToken = (token: ApiKey) => {
|
||||||
createServiceAccountToken(
|
createServiceAccountToken(serviceAccount.id, token, setNewToken);
|
||||||
serviceAccount.id,
|
|
||||||
{
|
|
||||||
name,
|
|
||||||
role: OrgRole.Viewer,
|
|
||||||
},
|
|
||||||
setNewToken
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const onModalClose = () => {
|
const onModalClose = () => {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { ServiceAccountDTO, ThunkResult } from '../../../types';
|
import { ApiKey, ServiceAccountDTO, ThunkResult } from '../../../types';
|
||||||
import { getBackendSrv } from '@grafana/runtime';
|
import { getBackendSrv } from '@grafana/runtime';
|
||||||
import {
|
import {
|
||||||
acOptionsLoaded,
|
acOptionsLoaded,
|
||||||
@ -49,11 +49,11 @@ export function loadServiceAccount(saID: number): ThunkResult<void> {
|
|||||||
|
|
||||||
export function createServiceAccountToken(
|
export function createServiceAccountToken(
|
||||||
saID: number,
|
saID: number,
|
||||||
data: any,
|
token: ApiKey,
|
||||||
onTokenCreated: (key: string) => void
|
onTokenCreated: (key: string) => void
|
||||||
): ThunkResult<void> {
|
): ThunkResult<void> {
|
||||||
return async (dispatch) => {
|
return async (dispatch) => {
|
||||||
const result = await getBackendSrv().post(`${BASE_URL}/${saID}/tokens`, data);
|
const result = await getBackendSrv().post(`${BASE_URL}/${saID}/tokens`, token);
|
||||||
onTokenCreated(result.key);
|
onTokenCreated(result.key);
|
||||||
dispatch(loadServiceAccountTokens(saID));
|
dispatch(loadServiceAccountTokens(saID));
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user