Creating extension point for kubernetesAuthStrategy

Signed-off-by: Andres Mauricio Gomez P <andmagom@outlook.com>
This commit is contained in:
Andres Mauricio Gomez P
2023-11-23 10:44:44 -05:00
parent 6010564860
commit 49e6285275
9 changed files with 107 additions and 49 deletions
@@ -1,38 +0,0 @@
/*
* Copyright 2020 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { AuthMetadata, ClusterDetails } from '../types/types';
import { KubernetesRequestAuth } from '@backstage/plugin-kubernetes-common';
/**
* Authentication data used to make a request to Kubernetes
* @public
*/
export type KubernetesCredential =
| { type: 'bearer token'; token: string }
| { type: 'anonymous' };
/**
*
* @public
*/
export interface AuthenticationStrategy {
getCredential(
clusterDetails: ClusterDetails,
authConfig: KubernetesRequestAuth,
): Promise<KubernetesCredential>;
validateCluster(authMetadata: AuthMetadata): Error[];
}
+44 -3
View File
@@ -29,6 +29,9 @@ import {
KubernetesClusterSupplierExtensionPoint,
kubernetesClusterSupplierExtensionPoint,
KubernetesClustersSupplier,
KubernetesAuthStrategyExtensionPoint,
AuthenticationStrategy,
kubernetesAuthStrategyExtensionPoint,
} from '@backstage/plugin-kubernetes-node';
class ObjectsProvider implements KubernetesObjectsProviderExtensionPoint {
@@ -65,6 +68,35 @@ class ClusterSuplier implements KubernetesClusterSupplierExtensionPoint {
}
}
class AuthStrategy implements KubernetesAuthStrategyExtensionPoint {
private authStrategies: Array<{
key: string;
strategy: AuthenticationStrategy;
}>;
constructor() {
this.authStrategies = new Array<{
key: string;
strategy: AuthenticationStrategy;
}>();
}
static addAuthStrategiesFromArray(
authStrategies: Array<{ key: string; strategy: AuthenticationStrategy }>,
builder: KubernetesBuilder,
) {
authStrategies.forEach(st => builder.addAuthStrategy(st.key, st.strategy));
}
getAuthenticationStrategies() {
return this.authStrategies;
}
addAuthStrategy(key: string, authStrategy: AuthenticationStrategy) {
this.authStrategies.push({ key, strategy: authStrategy });
}
}
/**
* This is the backend plugin that provides the Kubernetes integration.
* @alpha
@@ -75,6 +107,7 @@ export const kubernetesPlugin = createBackendPlugin({
register(env) {
const extPointObjectsProvider = new ObjectsProvider();
const extPointClusterSuplier = new ClusterSuplier();
const extPointAuthStrategy = new AuthStrategy();
env.registerExtensionPoint(
kubernetesObjectsProviderExtensionPoint,
extPointObjectsProvider,
@@ -83,6 +116,10 @@ export const kubernetesPlugin = createBackendPlugin({
kubernetesClusterSupplierExtensionPoint,
extPointClusterSuplier,
);
env.registerExtensionPoint(
kubernetesAuthStrategyExtensionPoint,
extPointAuthStrategy,
);
env.registerInit({
deps: {
@@ -95,15 +132,19 @@ export const kubernetesPlugin = createBackendPlugin({
async init({ http, logger, config, catalogApi, permissions }) {
const winstonLogger = loggerToWinstonLogger(logger);
// TODO: expose all of the customization & extension points of the builder here
const { router } = await KubernetesBuilder.createBuilder({
const builder: KubernetesBuilder = KubernetesBuilder.createBuilder({
logger: winstonLogger,
config,
catalogApi,
permissions,
})
.setObjectsProvider(extPointObjectsProvider.getObjectsProvider())
.setClusterSupplier(extPointClusterSuplier.getClusterSupplier())
.build();
.setClusterSupplier(extPointClusterSuplier.getClusterSupplier());
AuthStrategy.addAuthStrategiesFromArray(
extPointAuthStrategy.getAuthenticationStrategies(),
builder,
);
const { router } = await builder.build();
http.use(router);
},
});
@@ -46,7 +46,6 @@ import { MultiTenantServiceLocator } from '../service-locator/MultiTenantService
import { SingleTenantServiceLocator } from '../service-locator/SingleTenantServiceLocator';
import {
CustomResource,
KubernetesClustersSupplier,
KubernetesFetcher,
KubernetesObjectsProviderOptions,
KubernetesObjectTypes,
@@ -54,7 +53,10 @@ import {
ObjectsByEntityRequest,
ServiceLocatorMethod,
} from '../types/types';
import { KubernetesObjectsProvider } from '@backstage/plugin-kubernetes-node';
import {
KubernetesClustersSupplier,
KubernetesObjectsProvider,
} from '@backstage/plugin-kubernetes-node';
import {
DEFAULT_OBJECTS,
KubernetesFanOutHandler,
@@ -17,7 +17,6 @@
import { Entity } from '@backstage/catalog-model';
import { Logger } from 'winston';
import {
ClusterDetails,
KubernetesFetcher,
KubernetesObjectsProviderOptions,
KubernetesServiceLocator,
@@ -26,7 +25,6 @@ import {
ObjectToFetch,
CustomResource,
} from '../types/types';
import { AuthenticationStrategy, KubernetesCredential } from '../auth/types';
import {
ClientContainerStatus,
ClientCurrentResourceUsage,
@@ -45,7 +43,10 @@ import {
PodStatus,
} from '@kubernetes/client-node';
import {
AuthenticationStrategy,
ClusterDetails,
CustomResourcesByEntity,
KubernetesCredential,
KubernetesObjectsByEntity,
} from '@backstage/plugin-kubernetes-node';
@@ -26,12 +26,10 @@ import {
import lodash, { Dictionary } from 'lodash';
import { Logger } from 'winston';
import {
ClusterDetails,
FetchResponseWrapper,
KubernetesFetcher,
ObjectFetchParams,
} from '../types/types';
import { KubernetesCredential } from '../auth/types';
import {
ANNOTATION_KUBERNETES_AUTH_PROVIDER,
FetchResponse,
@@ -43,6 +41,10 @@ import fetch, { RequestInit, Response } from 'node-fetch';
import * as https from 'https';
import fs from 'fs-extra';
import { JsonObject } from '@backstage/types';
import {
ClusterDetails,
KubernetesCredential,
} from '@backstage/plugin-kubernetes-node';
export interface KubernetesClientBasedFetcherOptions {
logger: Logger;
+10 -2
View File
@@ -23,8 +23,10 @@ import type {
KubernetesRequestBody,
} from '@backstage/plugin-kubernetes-common';
import { Config } from '@backstage/config';
import { KubernetesCredential } from '../auth/types';
import { ClusterDetails } from '@backstage/plugin-kubernetes-node';
import {
ClusterDetails,
KubernetesCredential,
} from '@backstage/plugin-kubernetes-node';
/**
*
@@ -150,3 +152,9 @@ export interface KubernetesObjectsProviderOptions {
* @public
*/
export type ObjectsByEntityRequest = KubernetesRequestBody;
export type {
AuthMetadata,
ClusterDetails,
KubernetesClustersSupplier,
} from '@backstage/plugin-kubernetes-node';
+20
View File
@@ -14,6 +14,7 @@
* limitations under the License.
*/
import { createExtensionPoint } from '@backstage/backend-plugin-api';
import { AuthenticationStrategy } from '@backstage/plugin-kubernetes-node';
import { KubernetesClustersSupplier } from '@backstage/plugin-kubernetes-node';
import { KubernetesObjectsProvider } from '@backstage/plugin-kubernetes-node';
@@ -54,3 +55,22 @@ export const kubernetesClusterSupplierExtensionPoint =
createExtensionPoint<KubernetesClusterSupplierExtensionPoint>({
id: 'kubernetes.cluster-supplier',
});
/**
* The interface for {@link kubernetesAuthStrategyExtensionPoint}.
*
* @public
*/
export interface KubernetesAuthStrategyExtensionPoint {
addAuthStrategy(key: string, strategy: AuthenticationStrategy): void;
}
/**
* An extension point the exposes the ability to add an Auth Strategy.
*
* @public
*/
export const kubernetesAuthStrategyExtensionPoint =
createExtensionPoint<KubernetesAuthStrategyExtensionPoint>({
id: 'kubernetes.auth-strategy',
});
+2
View File
@@ -34,6 +34,8 @@ export {
type KubernetesObjectsProviderExtensionPoint,
kubernetesClusterSupplierExtensionPoint,
type KubernetesClusterSupplierExtensionPoint,
kubernetesAuthStrategyExtensionPoint,
type KubernetesAuthStrategyExtensionPoint,
} from './extensions';
export * from './types';
@@ -130,3 +130,23 @@ export interface KubernetesClustersSupplier {
*/
getClusters(): Promise<ClusterDetails[]>;
}
/**
* Authentication data used to make a request to Kubernetes
* @public
*/
export type KubernetesCredential =
| { type: 'bearer token'; token: string }
| { type: 'anonymous' };
/**
*
* @public
*/
export interface AuthenticationStrategy {
getCredential(
clusterDetails: ClusterDetails,
authConfig: KubernetesRequestAuth,
): Promise<KubernetesCredential>;
validateCluster(authMetadata: AuthMetadata): Error[];
}