Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit log suspend #64

Merged
merged 13 commits into from
Dec 14, 2023
58 changes: 52 additions & 6 deletions plugins/backstage-plugin-flux/dev/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import { Content } from '@backstage/core-components';
import { createDevApp } from '@backstage/dev-utils';
import { EntityProvider } from '@backstage/plugin-catalog-react';
import { Entity } from '@backstage/catalog-model';
import { configApiRef } from '@backstage/core-plugin-api';
import {
BackstageUserIdentity,
IdentityApi,
ProfileInfo,
configApiRef,
identityApiRef,
} from '@backstage/core-plugin-api';
import { ConfigReader } from '@backstage/core-app-api';
import {
KubernetesApi,
Expand Down Expand Up @@ -46,6 +52,12 @@ import {
} from '../src/hooks/useGetDeployments';
import { Namespace } from '../src/objects';

const userId = 'user:default/guest';
const profile = {
displayName: 'Guest',
picture: 'https://avatars.githubusercontent.com/u/35202557?v=4',
};

const fakeEntity: Entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Component',
Expand Down Expand Up @@ -309,6 +321,36 @@ class StubKubernetesAuthProvidersApi implements KubernetesAuthProvidersApi {
}
}

class StubIdentityApi implements IdentityApi {
async getBackstageIdentity(): Promise<BackstageUserIdentity> {
return {
ownershipEntityRefs: [userId],
type: 'user',
userEntityRef: userId,
};
}

async getProfileInfo(): Promise<ProfileInfo> {
return profile;
}

getCredentials = jest.fn();
signOut = jest.fn();
AlinaGoaga marked this conversation as resolved.
Show resolved Hide resolved

async fromLegacy(): Promise<{
result: {
userId: string;
profile: ProfileInfo;
getIdToken?: () => Promise<string>;
signOut?: () => Promise<void>;
};
}> {
return {
result: { profile, userId },
};
}
}

createDevApp()
.addPage({
title: 'Helm Releases',
Expand All @@ -322,6 +364,7 @@ createDevApp()
gitops: { baseUrl: 'https://example.com/wego', readOnly: false },
}),
],
[identityApiRef, new StubIdentityApi()],
[
kubernetesApiRef,
new StubKubernetesClient([
Expand Down Expand Up @@ -382,6 +425,7 @@ createDevApp()
gitops: { baseUrl: 'https://example.com/wego', readOnly: false },
}),
],
[identityApiRef, new StubIdentityApi()],
[
kubernetesApiRef,
new StubKubernetesClient([
Expand Down Expand Up @@ -428,6 +472,7 @@ createDevApp()
gitops: { baseUrl: 'https://example.com/wego', readOnly: false },
}),
],
[identityApiRef, new StubIdentityApi()],
[
kubernetesApiRef,
new StubKubernetesClient([
Expand Down Expand Up @@ -462,7 +507,6 @@ createDevApp()
),
]),
],

[kubernetesAuthProvidersApiRef, new StubKubernetesAuthProvidersApi()],
]}
>
Expand All @@ -486,6 +530,7 @@ createDevApp()
gitops: { baseUrl: 'https://example.com/wego', readOnly: false },
}),
],
[identityApiRef, new StubIdentityApi()],
[
kubernetesApiRef,
new StubKubernetesClient([
Expand All @@ -503,7 +548,6 @@ createDevApp()
),
]),
],

[kubernetesAuthProvidersApiRef, new StubKubernetesAuthProvidersApi()],
]}
>
Expand All @@ -527,6 +571,7 @@ createDevApp()
gitops: { baseUrl: 'https://example.com/wego', readOnly: false },
}),
],
[identityApiRef, new StubIdentityApi()],
[
kubernetesApiRef,
new StubKubernetesClient([
Expand All @@ -538,7 +583,6 @@ createDevApp()
),
]),
],

[kubernetesAuthProvidersApiRef, new StubKubernetesAuthProvidersApi()],
]}
>
Expand All @@ -562,6 +606,7 @@ createDevApp()
gitops: { baseUrl: 'https://example.com/wego', readOnly: false },
}),
],
[identityApiRef, new StubIdentityApi()],
[
kubernetesApiRef,
new StubKubernetesClient([
Expand All @@ -580,7 +625,6 @@ createDevApp()
),
]),
],

[kubernetesAuthProvidersApiRef, new StubKubernetesAuthProvidersApi()],
]}
>
Expand All @@ -604,6 +648,7 @@ createDevApp()
gitops: { baseUrl: 'https://example.com/wego', readOnly: false },
}),
],
[identityApiRef, new StubIdentityApi()],
[
kubernetesApiRef,
new StubKubernetesClient([
Expand Down Expand Up @@ -662,7 +707,6 @@ createDevApp()
),
]),
],

[kubernetesAuthProvidersApiRef, new StubKubernetesAuthProvidersApi()],
]}
>
Expand Down Expand Up @@ -704,6 +748,7 @@ createDevApp()
]),
],
[kubernetesAuthProvidersApiRef, new StubKubernetesAuthProvidersApi()],
[identityApiRef, new StubIdentityApi()],
]}
>
<EntityProvider entity={fakeEntity}>
Expand All @@ -726,6 +771,7 @@ createDevApp()
gitops: { baseUrl: 'https://example.com/wego', readOnly: false },
}),
],
[identityApiRef, new StubIdentityApi()],
[
kubernetesApiRef,
new StubKubernetesClient([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import React from 'react';
import { Entity } from '@backstage/catalog-model';
import { EntityProvider } from '@backstage/plugin-catalog-react';
import { renderInTestApp, TestApiProvider } from '@backstage/test-utils';
import { configApiRef } from '@backstage/core-plugin-api';
import {
BackstageUserIdentity,
configApiRef,
IdentityApi,
identityApiRef,
ProfileInfo,
} from '@backstage/core-plugin-api';
import { ConfigReader } from '@backstage/core-app-api';
import {
KubernetesApi,
Expand All @@ -18,6 +24,12 @@ import {
import * as helmRepository from '../../__fixtures__/helm_repository.json';
import { EntityFluxHelmRepositoriesCard } from './EntityFluxHelmRepositoriesCard';

const userId = 'user:default/guest';
const profile = {
displayName: 'Guest',
picture: 'https://avatars.githubusercontent.com/u/35202557?v=4',
};

const makeTestHelmRepository = (name: string, url: string) => {
const repo = JSON.parse(JSON.stringify(helmRepository));

Expand Down Expand Up @@ -86,6 +98,36 @@ class StubKubernetesAuthProvidersApi implements KubernetesAuthProvidersApi {
}
}

class StubIdentityApi implements IdentityApi {
async getBackstageIdentity(): Promise<BackstageUserIdentity> {
return {
ownershipEntityRefs: [userId],
type: 'user',
userEntityRef: userId,
};
}

async getProfileInfo(): Promise<ProfileInfo> {
return profile;
}

getCredentials = jest.fn();
signOut = jest.fn();

async fromLegacy(): Promise<{
result: {
userId: string;
profile: ProfileInfo;
getIdToken?: () => Promise<string>;
signOut?: () => Promise<void>;
};
}> {
return {
result: { profile, userId },
};
}
}

const entity: Entity = {
apiVersion: 'v1',
kind: 'Component',
Expand Down Expand Up @@ -121,6 +163,7 @@ describe('<EntityFluxHelmRepositoriesCard />', () => {
kubernetesAuthProvidersApiRef,
new StubKubernetesAuthProvidersApi(),
],
[identityApiRef, new StubIdentityApi()],
]}
>
<EntityProvider entity={entity}>
Expand Down
6 changes: 5 additions & 1 deletion plugins/backstage-plugin-flux/src/components/helpers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { helm, kubernetes, oci, git, flux } from '../images/icons';
import { useToggleSuspendResource } from '../hooks/useToggleSuspendResource';
import { configApiRef, useApi } from '@backstage/core-plugin-api';
import { useGetLatestFluxRelease } from '../hooks/useGetFluxRelease';
import { useGetUserInfo } from '../hooks/useGetUser';

export type Source = GitRepository | OCIRepository | HelmRepository;
export type Deployment = HelmRelease | Kustomization;
Expand Down Expand Up @@ -187,12 +188,15 @@ export function GroupAction({
resource: Source | Deployment | ImagePolicy;
}) {
const { sync, isSyncing } = useSyncResource(resource);
const { data } = useGetUserInfo();
const user = data?.result?.profile.email || data?.result?.userId;
AlinaGoaga marked this conversation as resolved.
Show resolved Hide resolved
const { loading: isSuspending, toggleSuspend } = useToggleSuspendResource(
resource as Source | Deployment,
true,
user,
);
const { loading: isResuming, toggleSuspend: toogleResume } =
useToggleSuspendResource(resource as Source | Deployment, false);
useToggleSuspendResource(resource as Source | Deployment, false, user);
const isLoading = isSyncing || isSuspending || isResuming;
const config = useApi(configApiRef);
const readOnly = config.getOptionalBoolean('gitops.readOnly');
Expand Down
51 changes: 51 additions & 0 deletions plugins/backstage-plugin-flux/src/hooks/useGetUser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { identityApiRef } from '@backstage/core-plugin-api';
import { getUserInfo } from './useGetUser';

function makeMockIdentityApi() {
return {
getObjectsByEntity: jest.fn(),
getProfileInfo: jest.fn(),
getBackstageIdentity: jest.fn(),
getCredentials: jest.fn(),
signOut: jest.fn(),
} as jest.Mocked<typeof identityApiRef.T>;
}

function makeMockUserIdentityApi() {
return {
fromLegacy: jest.fn(),
};
}

describe('getUserInfo', () => {
it('should get the user details', async () => {
const userId = 'user:default/guest';
const profile = {
displayName: 'Guest',
picture: 'https://avatars.githubusercontent.com/u/35202557?v=4',
};

const identityApi = makeMockIdentityApi();
const userIdentityApi = makeMockUserIdentityApi();

identityApi.getBackstageIdentity.mockImplementation(async () => {
return {
ownershipEntityRefs: [userId],
type: 'user',
userEntityRef: userId,
};
});

identityApi.getProfileInfo.mockImplementation(async () => profile);

userIdentityApi.fromLegacy.mockImplementation(async () => {
return {
result: { profile, userId },
};
});

const userDetails = await getUserInfo(identityApi);

expect(userDetails).toEqual({ result: { profile, userId } });
});
});
32 changes: 32 additions & 0 deletions plugins/backstage-plugin-flux/src/hooks/useGetUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
IdentityApi,
identityApiRef,
useApi,
} from '@backstage/core-plugin-api';
import { UserIdentity } from '@backstage/core-components';
import { useQuery } from '@tanstack/react-query';

export async function getUserInfo(identityApi: IdentityApi) {
const backstageIdentity = await identityApi.getBackstageIdentity();
const profile = await identityApi.getProfileInfo();

return await UserIdentity.fromLegacy({
AlinaGoaga marked this conversation as resolved.
Show resolved Hide resolved
userId: backstageIdentity.userEntityRef,
profile: profile,
});
}

/**
*
* @public
*/
export function useGetUserInfo() {
const identityApi = useApi(identityApiRef);
AlinaGoaga marked this conversation as resolved.
Show resolved Hide resolved

const { isLoading, data, error } = useQuery<any, Error>({
AlinaGoaga marked this conversation as resolved.
Show resolved Hide resolved
queryKey: ['user_info'],
queryFn: () => getUserInfo(identityApi),
});

return { isLoading, data, error };
}
Loading