@backstage/plugin-kubernetes-backend 0.18.7-next.0 → 0.18.7-next.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/alpha/package.json +1 -1
- package/dist/alpha.cjs.js +3 -150
- package/dist/alpha.cjs.js.map +1 -1
- package/dist/auth/AksStrategy.cjs.js +17 -0
- package/dist/auth/AksStrategy.cjs.js.map +1 -0
- package/dist/auth/AnonymousStrategy.cjs.js +16 -0
- package/dist/auth/AnonymousStrategy.cjs.js.map +1 -0
- package/dist/auth/AwsIamStrategy.cjs.js +80 -0
- package/dist/auth/AwsIamStrategy.cjs.js.map +1 -0
- package/dist/auth/AzureIdentityStrategy.cjs.js +58 -0
- package/dist/auth/AzureIdentityStrategy.cjs.js.map +1 -0
- package/dist/auth/DispatchStrategy.cjs.js +37 -0
- package/dist/auth/DispatchStrategy.cjs.js.map +1 -0
- package/dist/auth/GoogleServiceAccountStrategy.cjs.js +45 -0
- package/dist/auth/GoogleServiceAccountStrategy.cjs.js.map +1 -0
- package/dist/auth/GoogleStrategy.cjs.js +22 -0
- package/dist/auth/GoogleStrategy.cjs.js.map +1 -0
- package/dist/auth/OidcStrategy.cjs.js +34 -0
- package/dist/auth/OidcStrategy.cjs.js.map +1 -0
- package/dist/auth/ServiceAccountStrategy.cjs.js +33 -0
- package/dist/auth/ServiceAccountStrategy.cjs.js.map +1 -0
- package/dist/cluster-locator/CatalogClusterLocator.cjs.js +73 -0
- package/dist/cluster-locator/CatalogClusterLocator.cjs.js.map +1 -0
- package/dist/cluster-locator/ConfigClusterLocator.cjs.js +100 -0
- package/dist/cluster-locator/ConfigClusterLocator.cjs.js.map +1 -0
- package/dist/cluster-locator/GkeClusterLocator.cjs.js +126 -0
- package/dist/cluster-locator/GkeClusterLocator.cjs.js.map +1 -0
- package/dist/cluster-locator/LocalKubectlProxyLocator.cjs.js +35 -0
- package/dist/cluster-locator/LocalKubectlProxyLocator.cjs.js.map +1 -0
- package/dist/cluster-locator/index.cjs.js +67 -0
- package/dist/cluster-locator/index.cjs.js.map +1 -0
- package/dist/index.cjs.js +31 -1904
- package/dist/index.cjs.js.map +1 -1
- package/dist/package.json.cjs.js +156 -0
- package/dist/package.json.cjs.js.map +1 -0
- package/dist/plugin.cjs.js +155 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/routes/resourcesRoutes.cjs.js +65 -0
- package/dist/routes/resourcesRoutes.cjs.js.map +1 -0
- package/dist/service/KubernetesBuilder.cjs.js +367 -0
- package/dist/service/KubernetesBuilder.cjs.js.map +1 -0
- package/dist/service/KubernetesFanOutHandler.cjs.js +254 -0
- package/dist/service/KubernetesFanOutHandler.cjs.js.map +1 -0
- package/dist/service/KubernetesFetcher.cjs.js +231 -0
- package/dist/service/KubernetesFetcher.cjs.js.map +1 -0
- package/dist/service/KubernetesProxy.cjs.js +195 -0
- package/dist/service/KubernetesProxy.cjs.js.map +1 -0
- package/dist/service/router.cjs.js +11 -0
- package/dist/service/router.cjs.js.map +1 -0
- package/dist/service/runPeriodically.cjs.js +29 -0
- package/dist/service/runPeriodically.cjs.js.map +1 -0
- package/dist/service-locator/CatalogRelationServiceLocator.cjs.js +31 -0
- package/dist/service-locator/CatalogRelationServiceLocator.cjs.js.map +1 -0
- package/dist/service-locator/MultiTenantServiceLocator.cjs.js +15 -0
- package/dist/service-locator/MultiTenantServiceLocator.cjs.js.map +1 -0
- package/dist/service-locator/SingleTenantServiceLocator.cjs.js +24 -0
- package/dist/service-locator/SingleTenantServiceLocator.cjs.js.map +1 -0
- package/package.json +20 -20
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var errors = require('@backstage/errors');
|
|
4
|
+
var pluginKubernetesCommon = require('@backstage/plugin-kubernetes-common');
|
|
5
|
+
var pluginPermissionCommon = require('@backstage/plugin-permission-common');
|
|
6
|
+
var clientNode = require('@kubernetes/client-node');
|
|
7
|
+
var httpProxyMiddleware = require('http-proxy-middleware');
|
|
8
|
+
var fs = require('fs-extra');
|
|
9
|
+
var backendCommon = require('@backstage/backend-common');
|
|
10
|
+
|
|
11
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
12
|
+
|
|
13
|
+
var fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
|
|
14
|
+
|
|
15
|
+
const HEADER_KUBERNETES_CLUSTER = "Backstage-Kubernetes-Cluster";
|
|
16
|
+
const HEADER_KUBERNETES_AUTH = "Backstage-Kubernetes-Authorization";
|
|
17
|
+
class KubernetesProxy {
|
|
18
|
+
middlewareForClusterName = /* @__PURE__ */ new Map();
|
|
19
|
+
logger;
|
|
20
|
+
clusterSupplier;
|
|
21
|
+
authStrategy;
|
|
22
|
+
httpAuth;
|
|
23
|
+
constructor(options) {
|
|
24
|
+
this.logger = options.logger;
|
|
25
|
+
this.clusterSupplier = options.clusterSupplier;
|
|
26
|
+
this.authStrategy = options.authStrategy;
|
|
27
|
+
const legacy = backendCommon.createLegacyAuthAdapters({
|
|
28
|
+
discovery: options.discovery,
|
|
29
|
+
httpAuth: options.httpAuth
|
|
30
|
+
});
|
|
31
|
+
this.httpAuth = legacy.httpAuth;
|
|
32
|
+
}
|
|
33
|
+
createRequestHandler(options) {
|
|
34
|
+
const { permissionApi } = options;
|
|
35
|
+
return async (req, res, next) => {
|
|
36
|
+
const authorizeResponse = await permissionApi.authorize(
|
|
37
|
+
[{ permission: pluginKubernetesCommon.kubernetesProxyPermission }],
|
|
38
|
+
{
|
|
39
|
+
credentials: await this.httpAuth.credentials(req)
|
|
40
|
+
}
|
|
41
|
+
);
|
|
42
|
+
const auth = authorizeResponse[0];
|
|
43
|
+
if (auth.result === pluginPermissionCommon.AuthorizeResult.DENY) {
|
|
44
|
+
res.status(403).json({ error: new errors.NotAllowedError("Unauthorized") });
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const middleware = await this.getMiddleware(req);
|
|
48
|
+
if (req.header("connection")?.toLowerCase() === "upgrade" && req.header("upgrade")?.toLowerCase() === "websocket") {
|
|
49
|
+
middleware.upgrade(req, req.socket, void 0);
|
|
50
|
+
} else {
|
|
51
|
+
middleware(req, res, next);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
// We create one middleware per remote cluster and hold on to them, because
|
|
56
|
+
// the secure property isn't possible to decide on a per-request basis with a
|
|
57
|
+
// single middleware instance - and we don't expect it to change over time.
|
|
58
|
+
async getMiddleware(originalReq) {
|
|
59
|
+
const originalCluster = await this.getClusterForRequest(originalReq);
|
|
60
|
+
let middleware = this.middlewareForClusterName.get(originalCluster.name);
|
|
61
|
+
if (!middleware) {
|
|
62
|
+
const logger = this.logger.child({ cluster: originalCluster.name });
|
|
63
|
+
middleware = httpProxyMiddleware.createProxyMiddleware({
|
|
64
|
+
// TODO: Add 'log' to LoggerService
|
|
65
|
+
logProvider: () => backendCommon.loggerToWinstonLogger(logger),
|
|
66
|
+
ws: true,
|
|
67
|
+
secure: !originalCluster.skipTLSVerify,
|
|
68
|
+
changeOrigin: true,
|
|
69
|
+
pathRewrite: async (path, req) => {
|
|
70
|
+
const cluster = await this.getClusterForRequest(req);
|
|
71
|
+
const url = new URL(cluster.url);
|
|
72
|
+
return path.replace(
|
|
73
|
+
new RegExp(`^${originalReq.baseUrl}`),
|
|
74
|
+
url.pathname || ""
|
|
75
|
+
);
|
|
76
|
+
},
|
|
77
|
+
router: async (req) => {
|
|
78
|
+
const cluster = await this.getClusterForRequest(req);
|
|
79
|
+
const url = new URL(cluster.url);
|
|
80
|
+
const target = {
|
|
81
|
+
protocol: url.protocol,
|
|
82
|
+
host: url.hostname,
|
|
83
|
+
port: url.port,
|
|
84
|
+
ca: clientNode.bufferFromFileOrString(
|
|
85
|
+
cluster.caFile,
|
|
86
|
+
cluster.caData
|
|
87
|
+
)?.toString()
|
|
88
|
+
};
|
|
89
|
+
const authHeader = req.headers[HEADER_KUBERNETES_AUTH.toLocaleLowerCase("en-US")];
|
|
90
|
+
if (typeof authHeader === "string") {
|
|
91
|
+
req.headers.authorization = authHeader;
|
|
92
|
+
} else {
|
|
93
|
+
const authObj = KubernetesProxy.authHeadersToKubernetesRequestAuth(
|
|
94
|
+
req.headers
|
|
95
|
+
);
|
|
96
|
+
const credential = await this.getClusterForRequest(req).then((cd) => {
|
|
97
|
+
return this.authStrategy.getCredential(cd, authObj);
|
|
98
|
+
});
|
|
99
|
+
if (credential.type === "bearer token") {
|
|
100
|
+
req.headers.authorization = `Bearer ${credential.token}`;
|
|
101
|
+
} else if (credential.type === "x509 client certificate") {
|
|
102
|
+
target.key = credential.key;
|
|
103
|
+
target.cert = credential.cert;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return target;
|
|
107
|
+
},
|
|
108
|
+
onError: (error, req, res) => {
|
|
109
|
+
const wrappedError = new errors.ForwardedError(
|
|
110
|
+
`Cluster '${originalCluster.name}' request error`,
|
|
111
|
+
error
|
|
112
|
+
);
|
|
113
|
+
logger.error("Kubernetes proxy error", wrappedError);
|
|
114
|
+
const body = {
|
|
115
|
+
error: errors.serializeError(wrappedError, {
|
|
116
|
+
includeStack: process.env.NODE_ENV === "development"
|
|
117
|
+
}),
|
|
118
|
+
request: { method: req.method, url: req.originalUrl },
|
|
119
|
+
response: { statusCode: 500 }
|
|
120
|
+
};
|
|
121
|
+
res.status(500).json(body);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
this.middlewareForClusterName.set(originalCluster.name, middleware);
|
|
125
|
+
}
|
|
126
|
+
return middleware;
|
|
127
|
+
}
|
|
128
|
+
async getClusterForRequest(req) {
|
|
129
|
+
const clusterName = req.headers[HEADER_KUBERNETES_CLUSTER.toLowerCase()];
|
|
130
|
+
const clusters = await this.clusterSupplier.getClusters({
|
|
131
|
+
credentials: await this.httpAuth.credentials(req)
|
|
132
|
+
});
|
|
133
|
+
if (!clusters || clusters.length <= 0) {
|
|
134
|
+
throw new errors.NotFoundError(`No Clusters configured`);
|
|
135
|
+
}
|
|
136
|
+
const hasClusterNameHeader = typeof clusterName === "string" && clusterName.length > 0;
|
|
137
|
+
let cluster;
|
|
138
|
+
if (hasClusterNameHeader) {
|
|
139
|
+
cluster = clusters.find((c) => c.name === clusterName);
|
|
140
|
+
} else if (clusters.length === 1) {
|
|
141
|
+
cluster = clusters.at(0);
|
|
142
|
+
}
|
|
143
|
+
if (!cluster) {
|
|
144
|
+
throw new errors.NotFoundError(`Cluster '${clusterName}' not found`);
|
|
145
|
+
}
|
|
146
|
+
const authProvider = cluster.authMetadata[pluginKubernetesCommon.ANNOTATION_KUBERNETES_AUTH_PROVIDER];
|
|
147
|
+
if (authProvider === "serviceAccount" && fs__default.default.pathExistsSync(clientNode.Config.SERVICEACCOUNT_CA_PATH) && !cluster.authMetadata.serviceAccountToken) {
|
|
148
|
+
const kc = new clientNode.KubeConfig();
|
|
149
|
+
kc.loadFromCluster();
|
|
150
|
+
const clusterFromKubeConfig = kc.getCurrentCluster();
|
|
151
|
+
const url = new URL(clusterFromKubeConfig.server);
|
|
152
|
+
cluster.url = clusterFromKubeConfig.server;
|
|
153
|
+
if (url.protocol === "https:") {
|
|
154
|
+
cluster.caFile = clusterFromKubeConfig.caFile;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return cluster;
|
|
158
|
+
}
|
|
159
|
+
static authHeadersToKubernetesRequestAuth(originalHeaders) {
|
|
160
|
+
return Object.keys(originalHeaders).filter((header) => header.startsWith("backstage-kubernetes-authorization")).map(
|
|
161
|
+
(header) => KubernetesProxy.headerToDictionary(header, originalHeaders)
|
|
162
|
+
).filter((headerAsDic) => Object.keys(headerAsDic).length !== 0).reduce(KubernetesProxy.combineHeaders, {});
|
|
163
|
+
}
|
|
164
|
+
static headerToDictionary(header, originalHeaders) {
|
|
165
|
+
const obj = {};
|
|
166
|
+
const headerSplitted = header.split("-");
|
|
167
|
+
if (headerSplitted.length >= 4) {
|
|
168
|
+
const framework = headerSplitted[3].toLowerCase();
|
|
169
|
+
if (headerSplitted.length >= 5) {
|
|
170
|
+
const provider = headerSplitted.slice(4).join("-").toLowerCase();
|
|
171
|
+
obj[framework] = { [provider]: originalHeaders[header] };
|
|
172
|
+
} else {
|
|
173
|
+
obj[framework] = originalHeaders[header];
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return obj;
|
|
177
|
+
}
|
|
178
|
+
static combineHeaders(authObj, header) {
|
|
179
|
+
const framework = Object.keys(header)[0];
|
|
180
|
+
if (authObj[framework]) {
|
|
181
|
+
authObj[framework] = {
|
|
182
|
+
...authObj[framework],
|
|
183
|
+
...header[framework]
|
|
184
|
+
};
|
|
185
|
+
} else {
|
|
186
|
+
authObj[framework] = header[framework];
|
|
187
|
+
}
|
|
188
|
+
return authObj;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
exports.HEADER_KUBERNETES_AUTH = HEADER_KUBERNETES_AUTH;
|
|
193
|
+
exports.HEADER_KUBERNETES_CLUSTER = HEADER_KUBERNETES_CLUSTER;
|
|
194
|
+
exports.KubernetesProxy = KubernetesProxy;
|
|
195
|
+
//# sourceMappingURL=KubernetesProxy.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KubernetesProxy.cjs.js","sources":["../../src/service/KubernetesProxy.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n ErrorResponseBody,\n ForwardedError,\n NotAllowedError,\n NotFoundError,\n serializeError,\n} from '@backstage/errors';\nimport {\n ANNOTATION_KUBERNETES_AUTH_PROVIDER,\n kubernetesProxyPermission,\n KubernetesRequestAuth,\n} from '@backstage/plugin-kubernetes-common';\nimport { AuthorizeResult } from '@backstage/plugin-permission-common';\nimport {\n bufferFromFileOrString,\n Cluster,\n Config,\n KubeConfig,\n} from '@kubernetes/client-node';\nimport { createProxyMiddleware, RequestHandler } from 'http-proxy-middleware';\nimport fs from 'fs-extra';\n\nimport { AuthenticationStrategy } from '../auth';\nimport { ClusterDetails, KubernetesClustersSupplier } from '../types/types';\n\nimport type { Request } from 'express';\nimport { IncomingHttpHeaders } from 'http';\nimport {\n DiscoveryService,\n HttpAuthService,\n LoggerService,\n PermissionsService,\n} from '@backstage/backend-plugin-api';\nimport {\n createLegacyAuthAdapters,\n loggerToWinstonLogger,\n} from '@backstage/backend-common';\n\nexport const APPLICATION_JSON: string = 'application/json';\n\n/**\n * The header that is used to specify the cluster name.\n *\n * @public\n */\nexport const HEADER_KUBERNETES_CLUSTER: string = 'Backstage-Kubernetes-Cluster';\n\n/**\n * The header that is used to specify the Authentication Authorities token.\n * e.x if using the google auth provider as your authentication authority then this field would be the google provided bearer token.\n * @public\n */\nexport const HEADER_KUBERNETES_AUTH: string =\n 'Backstage-Kubernetes-Authorization';\n\n/**\n * The options object expected to be passed as a parameter to KubernetesProxy.createRequestHandler().\n *\n * @public\n */\nexport type KubernetesProxyCreateRequestHandlerOptions = {\n permissionApi: PermissionsService;\n};\n\n/**\n * Options accepted as a parameter by the KubernetesProxy\n *\n * @public\n */\nexport type KubernetesProxyOptions = {\n logger: LoggerService;\n clusterSupplier: KubernetesClustersSupplier;\n authStrategy: AuthenticationStrategy;\n discovery: DiscoveryService;\n httpAuth?: HttpAuthService;\n};\n\n/**\n * A proxy that routes requests to the Kubernetes API.\n *\n * @public\n */\nexport class KubernetesProxy {\n private readonly middlewareForClusterName = new Map<string, RequestHandler>();\n private readonly logger: LoggerService;\n private readonly clusterSupplier: KubernetesClustersSupplier;\n private readonly authStrategy: AuthenticationStrategy;\n private readonly httpAuth: HttpAuthService;\n\n constructor(options: KubernetesProxyOptions) {\n this.logger = options.logger;\n this.clusterSupplier = options.clusterSupplier;\n this.authStrategy = options.authStrategy;\n\n const legacy = createLegacyAuthAdapters({\n discovery: options.discovery,\n httpAuth: options.httpAuth,\n });\n\n this.httpAuth = legacy.httpAuth;\n }\n\n public createRequestHandler(\n options: KubernetesProxyCreateRequestHandlerOptions,\n ): RequestHandler {\n const { permissionApi } = options;\n return async (req, res, next) => {\n const authorizeResponse = await permissionApi.authorize(\n [{ permission: kubernetesProxyPermission }],\n {\n credentials: await this.httpAuth.credentials(req),\n },\n );\n const auth = authorizeResponse[0];\n\n if (auth.result === AuthorizeResult.DENY) {\n res.status(403).json({ error: new NotAllowedError('Unauthorized') });\n return;\n }\n\n const middleware = await this.getMiddleware(req);\n\n // If req is an upgrade handshake, use middleware upgrade instead of http request handler https://github.com/chimurai/http-proxy-middleware#external-websocket-upgrade\n if (\n req.header('connection')?.toLowerCase() === 'upgrade' &&\n req.header('upgrade')?.toLowerCase() === 'websocket'\n ) {\n // Missing the `head`, since it's optional we pass undefined to avoid type issues\n middleware.upgrade!(req, req.socket, undefined);\n } else {\n middleware(req, res, next);\n }\n };\n }\n\n // We create one middleware per remote cluster and hold on to them, because\n // the secure property isn't possible to decide on a per-request basis with a\n // single middleware instance - and we don't expect it to change over time.\n private async getMiddleware(originalReq: Request): Promise<RequestHandler> {\n const originalCluster = await this.getClusterForRequest(originalReq);\n let middleware = this.middlewareForClusterName.get(originalCluster.name);\n if (!middleware) {\n const logger = this.logger.child({ cluster: originalCluster.name });\n middleware = createProxyMiddleware({\n // TODO: Add 'log' to LoggerService\n logProvider: () => loggerToWinstonLogger(logger),\n ws: true,\n secure: !originalCluster.skipTLSVerify,\n changeOrigin: true,\n pathRewrite: async (path, req) => {\n // Re-evaluate the cluster on each request, in case it has changed\n const cluster = await this.getClusterForRequest(req);\n const url = new URL(cluster.url);\n return path.replace(\n new RegExp(`^${originalReq.baseUrl}`),\n url.pathname || '',\n );\n },\n router: async req => {\n // Re-evaluate the cluster on each request, in case it has changed\n const cluster = await this.getClusterForRequest(req);\n const url = new URL(cluster.url);\n\n const target: any = {\n protocol: url.protocol,\n host: url.hostname,\n port: url.port,\n ca: bufferFromFileOrString(\n cluster.caFile,\n cluster.caData,\n )?.toString(),\n };\n\n const authHeader =\n req.headers[HEADER_KUBERNETES_AUTH.toLocaleLowerCase('en-US')];\n if (typeof authHeader === 'string') {\n req.headers.authorization = authHeader;\n } else {\n // Map Backstage-Kubernetes-Authorization-X-X headers to a KubernetesRequestAuth object\n const authObj = KubernetesProxy.authHeadersToKubernetesRequestAuth(\n req.headers,\n );\n\n const credential = await this.getClusterForRequest(req).then(cd => {\n return this.authStrategy.getCredential(cd, authObj);\n });\n\n if (credential.type === 'bearer token') {\n req.headers.authorization = `Bearer ${credential.token}`;\n } else if (credential.type === 'x509 client certificate') {\n target.key = credential.key;\n target.cert = credential.cert;\n }\n }\n\n return target;\n },\n onError: (error, req, res) => {\n const wrappedError = new ForwardedError(\n `Cluster '${originalCluster.name}' request error`,\n error,\n );\n\n logger.error('Kubernetes proxy error', wrappedError);\n\n const body: ErrorResponseBody = {\n error: serializeError(wrappedError, {\n includeStack: process.env.NODE_ENV === 'development',\n }),\n request: { method: req.method, url: req.originalUrl },\n response: { statusCode: 500 },\n };\n res.status(500).json(body);\n },\n });\n this.middlewareForClusterName.set(originalCluster.name, middleware);\n }\n return middleware;\n }\n\n private async getClusterForRequest(req: Request): Promise<ClusterDetails> {\n const clusterName = req.headers[HEADER_KUBERNETES_CLUSTER.toLowerCase()];\n const clusters = await this.clusterSupplier.getClusters({\n credentials: await this.httpAuth.credentials(req),\n });\n\n if (!clusters || clusters.length <= 0) {\n throw new NotFoundError(`No Clusters configured`);\n }\n\n const hasClusterNameHeader =\n typeof clusterName === 'string' && clusterName.length > 0;\n\n let cluster: ClusterDetails | undefined;\n\n if (hasClusterNameHeader) {\n cluster = clusters.find(c => c.name === clusterName);\n } else if (clusters.length === 1) {\n cluster = clusters.at(0);\n }\n\n if (!cluster) {\n throw new NotFoundError(`Cluster '${clusterName}' not found`);\n }\n\n const authProvider =\n cluster.authMetadata[ANNOTATION_KUBERNETES_AUTH_PROVIDER];\n\n if (\n authProvider === 'serviceAccount' &&\n fs.pathExistsSync(Config.SERVICEACCOUNT_CA_PATH) &&\n !cluster.authMetadata.serviceAccountToken\n ) {\n const kc = new KubeConfig();\n kc.loadFromCluster();\n const clusterFromKubeConfig = kc.getCurrentCluster() as Cluster;\n\n const url = new URL(clusterFromKubeConfig.server);\n cluster.url = clusterFromKubeConfig.server;\n if (url.protocol === 'https:') {\n cluster.caFile = clusterFromKubeConfig.caFile;\n }\n }\n\n return cluster;\n }\n\n private static authHeadersToKubernetesRequestAuth(\n originalHeaders: IncomingHttpHeaders,\n ): KubernetesRequestAuth {\n return Object.keys(originalHeaders)\n .filter(header => header.startsWith('backstage-kubernetes-authorization'))\n .map(header =>\n KubernetesProxy.headerToDictionary(header, originalHeaders),\n )\n .filter(headerAsDic => Object.keys(headerAsDic).length !== 0)\n .reduce(KubernetesProxy.combineHeaders, {});\n }\n\n private static headerToDictionary(\n header: string,\n originalHeaders: IncomingHttpHeaders,\n ): KubernetesRequestAuth {\n const obj: KubernetesRequestAuth = {};\n const headerSplitted = header.split('-');\n if (headerSplitted.length >= 4) {\n const framework = headerSplitted[3].toLowerCase();\n if (headerSplitted.length >= 5) {\n const provider = headerSplitted.slice(4).join('-').toLowerCase();\n obj[framework] = { [provider]: originalHeaders[header] };\n } else {\n obj[framework] = originalHeaders[header];\n }\n }\n return obj;\n }\n\n private static combineHeaders(\n authObj: any,\n header: any,\n ): KubernetesRequestAuth {\n const framework = Object.keys(header)[0];\n\n if (authObj[framework]) {\n authObj[framework] = {\n ...authObj[framework],\n ...header[framework],\n };\n } else {\n authObj[framework] = header[framework];\n }\n\n return authObj;\n }\n}\n"],"names":["createLegacyAuthAdapters","kubernetesProxyPermission","AuthorizeResult","NotAllowedError","createProxyMiddleware","loggerToWinstonLogger","bufferFromFileOrString","ForwardedError","serializeError","NotFoundError","ANNOTATION_KUBERNETES_AUTH_PROVIDER","fs","Config","KubeConfig"],"mappings":";;;;;;;;;;;;;;AA4DO,MAAM,yBAAoC,GAAA,+BAAA;AAO1C,MAAM,sBACX,GAAA,qCAAA;AA6BK,MAAM,eAAgB,CAAA;AAAA,EACV,wBAAA,uBAA+B,GAA4B,EAAA,CAAA;AAAA,EAC3D,MAAA,CAAA;AAAA,EACA,eAAA,CAAA;AAAA,EACA,YAAA,CAAA;AAAA,EACA,QAAA,CAAA;AAAA,EAEjB,YAAY,OAAiC,EAAA;AAC3C,IAAA,IAAA,CAAK,SAAS,OAAQ,CAAA,MAAA,CAAA;AACtB,IAAA,IAAA,CAAK,kBAAkB,OAAQ,CAAA,eAAA,CAAA;AAC/B,IAAA,IAAA,CAAK,eAAe,OAAQ,CAAA,YAAA,CAAA;AAE5B,IAAA,MAAM,SAASA,sCAAyB,CAAA;AAAA,MACtC,WAAW,OAAQ,CAAA,SAAA;AAAA,MACnB,UAAU,OAAQ,CAAA,QAAA;AAAA,KACnB,CAAA,CAAA;AAED,IAAA,IAAA,CAAK,WAAW,MAAO,CAAA,QAAA,CAAA;AAAA,GACzB;AAAA,EAEO,qBACL,OACgB,EAAA;AAChB,IAAM,MAAA,EAAE,eAAkB,GAAA,OAAA,CAAA;AAC1B,IAAO,OAAA,OAAO,GAAK,EAAA,GAAA,EAAK,IAAS,KAAA;AAC/B,MAAM,MAAA,iBAAA,GAAoB,MAAM,aAAc,CAAA,SAAA;AAAA,QAC5C,CAAC,EAAE,UAAY,EAAAC,gDAAA,EAA2B,CAAA;AAAA,QAC1C;AAAA,UACE,WAAa,EAAA,MAAM,IAAK,CAAA,QAAA,CAAS,YAAY,GAAG,CAAA;AAAA,SAClD;AAAA,OACF,CAAA;AACA,MAAM,MAAA,IAAA,GAAO,kBAAkB,CAAC,CAAA,CAAA;AAEhC,MAAI,IAAA,I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A;AACvC,IAAI,IAAA,cAAA,CAAe,UAAU,CAAG,EAAA;AAC9B,MAAA,MAAM,SAAY,GAAA,cAAA,CAAe,CAAC,CAAA,CAAE,WAAY,EAAA,CAAA;AAChD,MAAI,IAAA,cAAA,CAAe,UAAU,CAAG,EAAA;AAC9B,QAAM,MAAA,QAAA,GAAW,eAAe,KAAM,CAAA,CAAC,EAAE,IAAK,CAAA,GAAG,EAAE,WAAY,EAAA,CAAA;AAC/D,QAAI,GAAA,CAAA,SAAS,IAAI,EAAE,CAAC,QAAQ,GAAG,eAAA,CAAgB,MAAM,CAAE,EAAA,CAAA;AAAA,OAClD,MAAA;AACL,QAAI,GAAA,CAAA,SAAS,CAAI,GAAA,eAAA,CAAgB,MAAM,CAAA,CAAA;AAAA,OACzC;AAAA,KACF;AACA,IAAO,OAAA,GAAA,CAAA;AAAA,GACT;AAAA,EAEA,OAAe,cACb,CAAA,OAAA,EACA,MACuB,EAAA;AACvB,IAAA,MAAM,SAAY,GAAA,MAAA,CAAO,IAAK,CAAA,MAAM,EAAE,CAAC,CAAA,CAAA;AAEvC,IAAI,IAAA,OAAA,CAAQ,SAAS,CAAG,EAAA;AACtB,MAAA,OAAA,CAAQ,SAAS,CAAI,GAAA;AAAA,QACnB,GAAG,QAAQ,SAAS,CAAA;AAAA,QACpB,GAAG,OAAO,SAAS,CAAA;AAAA,OACrB,CAAA;AAAA,KACK,MAAA;AACL,MAAQ,OAAA,CAAA,SAAS,CAAI,GAAA,MAAA,CAAO,SAAS,CAAA,CAAA;AAAA,KACvC;AAEA,IAAO,OAAA,OAAA,CAAA;AAAA,GACT;AACF;;;;;;"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var KubernetesBuilder = require('./KubernetesBuilder.cjs.js');
|
|
4
|
+
|
|
5
|
+
async function createRouter(options) {
|
|
6
|
+
const { router } = await KubernetesBuilder.KubernetesBuilder.createBuilder(options).setClusterSupplier(options.clusterSupplier).build();
|
|
7
|
+
return router;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
exports.createRouter = createRouter;
|
|
11
|
+
//# sourceMappingURL=router.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"router.cjs.js","sources":["../../src/service/router.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Logger } from 'winston';\nimport { KubernetesClustersSupplier } from '../types/types';\nimport express from 'express';\nimport { KubernetesBuilder } from './KubernetesBuilder';\nimport { CatalogApi } from '@backstage/catalog-client';\nimport { PermissionEvaluator } from '@backstage/plugin-permission-common';\nimport {\n DiscoveryService,\n RootConfigService,\n} from '@backstage/backend-plugin-api';\n\n/**\n * @deprecated Please migrate to the new backend system as this will be removed in the future.\n * @public\n */\nexport interface RouterOptions {\n logger: Logger;\n config: RootConfigService;\n catalogApi: CatalogApi;\n clusterSupplier?: KubernetesClustersSupplier;\n discovery: DiscoveryService;\n permissions: PermissionEvaluator;\n}\n\n/**\n * creates and configure a new router for handling the kubernetes backend APIs\n * @param options - specifies the options required by this plugin\n * @returns a new router\n * @deprecated Please migrate to the new backend system as this will be removed in the future.\n * ```\n * import { KubernetesBuilder } from '@backstage/plugin-kubernetes-backend';\n * const { router } = await KubernetesBuilder.createBuilder({\n * logger,\n * config,\n * }).build();\n * ```\n *\n * @public\n */\nexport async function createRouter(\n options: RouterOptions,\n): Promise<express.Router> {\n const { router } = await KubernetesBuilder.createBuilder(options)\n .setClusterSupplier(options.clusterSupplier)\n .build();\n return router;\n}\n"],"names":["KubernetesBuilder"],"mappings":";;;;AAuDA,eAAsB,aACpB,OACyB,EAAA;AACzB,EAAA,MAAM,EAAE,MAAA,EAAW,GAAA,MAAMA,mCAAkB,CAAA,aAAA,CAAc,OAAO,CAAA,CAC7D,kBAAmB,CAAA,OAAA,CAAQ,eAAe,CAAA,CAC1C,KAAM,EAAA,CAAA;AACT,EAAO,OAAA,MAAA,CAAA;AACT;;;;"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function runPeriodically(fn, delayMs) {
|
|
4
|
+
let cancel;
|
|
5
|
+
let cancelled = false;
|
|
6
|
+
const cancellationPromise = new Promise((resolve) => {
|
|
7
|
+
cancel = () => {
|
|
8
|
+
resolve();
|
|
9
|
+
cancelled = true;
|
|
10
|
+
};
|
|
11
|
+
});
|
|
12
|
+
const startRefresh = async () => {
|
|
13
|
+
while (!cancelled) {
|
|
14
|
+
try {
|
|
15
|
+
await fn();
|
|
16
|
+
} catch {
|
|
17
|
+
}
|
|
18
|
+
await Promise.race([
|
|
19
|
+
new Promise((resolve) => setTimeout(resolve, delayMs)),
|
|
20
|
+
cancellationPromise
|
|
21
|
+
]);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
startRefresh();
|
|
25
|
+
return cancel;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
exports.runPeriodically = runPeriodically;
|
|
29
|
+
//# sourceMappingURL=runPeriodically.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"runPeriodically.cjs.js","sources":["../../src/service/runPeriodically.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * Runs a function repeatedly, with a fixed wait between invocations.\n *\n * Supports async functions, and silently ignores exceptions and rejections.\n *\n * @param fn - The function to run. May return a Promise.\n * @param delayMs - The delay between a completed function invocation and the\n * next.\n * @returns A function that, when called, stops the invocation loop.\n */\nexport function runPeriodically(fn: () => any, delayMs: number): () => void {\n let cancel: () => void;\n let cancelled = false;\n const cancellationPromise = new Promise<void>(resolve => {\n cancel = () => {\n resolve();\n cancelled = true;\n };\n });\n\n const startRefresh = async () => {\n while (!cancelled) {\n try {\n await fn();\n } catch {\n // ignore intentionally\n }\n\n await Promise.race([\n new Promise(resolve => setTimeout(resolve, delayMs)),\n cancellationPromise,\n ]);\n }\n };\n startRefresh();\n\n return cancel!;\n}\n"],"names":[],"mappings":";;AA0BgB,SAAA,eAAA,CAAgB,IAAe,OAA6B,EAAA;AAC1E,EAAI,IAAA,MAAA,CAAA;AACJ,EAAA,IAAI,SAAY,GAAA,KAAA,CAAA;AAChB,EAAM,MAAA,mBAAA,GAAsB,IAAI,OAAA,CAAc,CAAW,OAAA,KAAA;AACvD,IAAA,MAAA,GAAS,MAAM;AACb,MAAQ,OAAA,EAAA,CAAA;AACR,MAAY,SAAA,GAAA,IAAA,CAAA;AAAA,KACd,CAAA;AAAA,GACD,CAAA,CAAA;AAED,EAAA,MAAM,eAAe,YAAY;AAC/B,IAAA,OAAO,CAAC,SAAW,EAAA;AACjB,MAAI,IAAA;AACF,QAAA,MAAM,EAAG,EAAA,CAAA;AAAA,OACH,CAAA,MAAA;AAAA,OAER;AAEA,MAAA,MAAM,QAAQ,IAAK,CAAA;AAAA,QACjB,IAAI,OAAQ,CAAA,CAAA,OAAA,KAAW,UAAW,CAAA,OAAA,EAAS,OAAO,CAAC,CAAA;AAAA,QACnD,mBAAA;AAAA,OACD,CAAA,CAAA;AAAA,KACH;AAAA,GACF,CAAA;AACA,EAAa,YAAA,EAAA,CAAA;AAEb,EAAO,OAAA,MAAA,CAAA;AACT;;;;"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class CatalogRelationServiceLocator {
|
|
4
|
+
clusterSupplier;
|
|
5
|
+
constructor(clusterSupplier) {
|
|
6
|
+
this.clusterSupplier = clusterSupplier;
|
|
7
|
+
}
|
|
8
|
+
// As this implementation always returns all clusters serviceId is ignored here
|
|
9
|
+
getClustersByEntity(entity, requestContext) {
|
|
10
|
+
if (entity.relations && entity.relations.some(
|
|
11
|
+
(r) => r.type === "dependsOn" && r.targetRef.includes("resource:")
|
|
12
|
+
)) {
|
|
13
|
+
return this.clusterSupplier.getClusters({ credentials: requestContext.credentials }).then((clusters) => {
|
|
14
|
+
return {
|
|
15
|
+
clusters: clusters.filter(
|
|
16
|
+
(c) => this.doesEntityDependOnCluster(entity, c)
|
|
17
|
+
)
|
|
18
|
+
};
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
return Promise.resolve({ clusters: [] });
|
|
22
|
+
}
|
|
23
|
+
doesEntityDependOnCluster(entity, cluster) {
|
|
24
|
+
return entity.relations.some(
|
|
25
|
+
(rel) => rel.type === "dependsOn" && rel.targetRef === `resource:${entity.metadata.namespace ?? "default"}/${cluster.name}`
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
exports.CatalogRelationServiceLocator = CatalogRelationServiceLocator;
|
|
31
|
+
//# sourceMappingURL=CatalogRelationServiceLocator.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CatalogRelationServiceLocator.cjs.js","sources":["../../src/service-locator/CatalogRelationServiceLocator.ts"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { Entity } from '@backstage/catalog-model';\nimport {\n ClusterDetails,\n KubernetesClustersSupplier,\n KubernetesServiceLocator,\n ServiceLocatorRequestContext,\n} from '@backstage/plugin-kubernetes-node';\n\n// This locator assumes that service is located on the clusters it depends on\n// Therefore it will return the clusters based on the relations it has or none otherwise\nexport class CatalogRelationServiceLocator implements KubernetesServiceLocator {\n private readonly clusterSupplier: KubernetesClustersSupplier;\n\n constructor(clusterSupplier: KubernetesClustersSupplier) {\n this.clusterSupplier = clusterSupplier;\n }\n\n // As this implementation always returns all clusters serviceId is ignored here\n getClustersByEntity(\n entity: Entity,\n requestContext: ServiceLocatorRequestContext,\n ): Promise<{ clusters: ClusterDetails[] }> {\n if (\n entity.relations &&\n entity.relations.some(\n r => r.type === 'dependsOn' && r.targetRef.includes('resource:'),\n )\n ) {\n return this.clusterSupplier\n .getClusters({ credentials: requestContext.credentials })\n .then(clusters => {\n return {\n clusters: clusters.filter(c =>\n this.doesEntityDependOnCluster(entity, c),\n ),\n };\n });\n }\n return Promise.resolve({ clusters: [] });\n }\n\n protected doesEntityDependOnCluster(\n entity: Entity,\n cluster: ClusterDetails,\n ): boolean {\n return entity.relations!.some(\n rel =>\n rel.type === 'dependsOn' &&\n rel.targetRef ===\n `resource:${entity.metadata.namespace ?? 'default'}/${cluster.name}`,\n );\n }\n}\n"],"names":[],"mappings":";;AAyBO,MAAM,6BAAkE,CAAA;AAAA,EAC5D,eAAA,CAAA;AAAA,EAEjB,YAAY,eAA6C,EAAA;AACvD,IAAA,IAAA,CAAK,eAAkB,GAAA,eAAA,CAAA;AAAA,GACzB;AAAA;AAAA,EAGA,mBAAA,CACE,QACA,cACyC,EAAA;AACzC,IACE,IAAA,MAAA,CAAO,SACP,IAAA,MAAA,CAAO,SAAU,CAAA,IAAA;AAAA,MACf,OAAK,CAAE,CAAA,IAAA,KAAS,eAAe,CAAE,CAAA,SAAA,CAAU,SAAS,WAAW,CAAA;AAAA,KAEjE,EAAA;AACA,MAAO,OAAA,IAAA,CAAK,eACT,CAAA,WAAA,CAAY,EAAE,WAAA,EAAa,eAAe,WAAY,EAAC,CACvD,CAAA,IAAA,CAAK,CAAY,QAAA,KAAA;AAChB,QAAO,OAAA;AAAA,UACL,UAAU,QAAS,CAAA,MAAA;AAAA,YAAO,CACxB,CAAA,KAAA,IAAA,CAAK,yBAA0B,CAAA,MAAA,EAAQ,CAAC,CAAA;AAAA,WAC1C;AAAA,SACF,CAAA;AAAA,OACD,CAAA,CAAA;AAAA,KACL;AACA,IAAA,OAAO,QAAQ,OAAQ,CAAA,EAAE,QAAU,EAAA,IAAI,CAAA,CAAA;AAAA,GACzC;AAAA,EAEU,yBAAA,CACR,QACA,OACS,EAAA;AACT,IAAA,OAAO,OAAO,SAAW,CAAA,IAAA;AAAA,MACvB,CACE,GAAA,KAAA,GAAA,CAAI,IAAS,KAAA,WAAA,IACb,GAAI,CAAA,SAAA,KACF,CAAY,SAAA,EAAA,MAAA,CAAO,QAAS,CAAA,SAAA,IAAa,SAAS,CAAA,CAAA,EAAI,QAAQ,IAAI,CAAA,CAAA;AAAA,KACxE,CAAA;AAAA,GACF;AACF;;;;"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class MultiTenantServiceLocator {
|
|
4
|
+
clusterSupplier;
|
|
5
|
+
constructor(clusterSupplier) {
|
|
6
|
+
this.clusterSupplier = clusterSupplier;
|
|
7
|
+
}
|
|
8
|
+
// As this implementation always returns all clusters serviceId is ignored here
|
|
9
|
+
getClustersByEntity(_entity, requestContext) {
|
|
10
|
+
return this.clusterSupplier.getClusters({ credentials: requestContext.credentials }).then((clusters) => ({ clusters }));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
exports.MultiTenantServiceLocator = MultiTenantServiceLocator;
|
|
15
|
+
//# sourceMappingURL=MultiTenantServiceLocator.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MultiTenantServiceLocator.cjs.js","sources":["../../src/service-locator/MultiTenantServiceLocator.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Entity } from '@backstage/catalog-model';\nimport {\n ClusterDetails,\n KubernetesClustersSupplier,\n KubernetesServiceLocator,\n ServiceLocatorRequestContext,\n} from '../types/types';\n\n// This locator assumes that every service is located on every cluster\n// Therefore it will always return all clusters provided\nexport class MultiTenantServiceLocator implements KubernetesServiceLocator {\n private readonly clusterSupplier: KubernetesClustersSupplier;\n\n constructor(clusterSupplier: KubernetesClustersSupplier) {\n this.clusterSupplier = clusterSupplier;\n }\n\n // As this implementation always returns all clusters serviceId is ignored here\n getClustersByEntity(\n _entity: Entity,\n requestContext: ServiceLocatorRequestContext,\n ): Promise<{ clusters: ClusterDetails[] }> {\n return this.clusterSupplier\n .getClusters({ credentials: requestContext.credentials })\n .then(clusters => ({ clusters }));\n }\n}\n"],"names":[],"mappings":";;AA0BO,MAAM,yBAA8D,CAAA;AAAA,EACxD,eAAA,CAAA;AAAA,EAEjB,YAAY,eAA6C,EAAA;AACvD,IAAA,IAAA,CAAK,eAAkB,GAAA,eAAA,CAAA;AAAA,GACzB;AAAA;AAAA,EAGA,mBAAA,CACE,SACA,cACyC,EAAA;AACzC,IAAA,OAAO,IAAK,CAAA,eAAA,CACT,WAAY,CAAA,EAAE,WAAa,EAAA,cAAA,CAAe,WAAY,EAAC,CACvD,CAAA,IAAA,CAAK,CAAa,QAAA,MAAA,EAAE,UAAW,CAAA,CAAA,CAAA;AAAA,GACpC;AACF;;;;"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class SingleTenantServiceLocator {
|
|
4
|
+
clusterSupplier;
|
|
5
|
+
constructor(clusterSupplier) {
|
|
6
|
+
this.clusterSupplier = clusterSupplier;
|
|
7
|
+
}
|
|
8
|
+
// As this implementation always returns all clusters serviceId is ignored here
|
|
9
|
+
getClustersByEntity(_entity, requestContext) {
|
|
10
|
+
return this.clusterSupplier.getClusters({ credentials: requestContext.credentials }).then((clusters) => {
|
|
11
|
+
if (_entity.metadata?.annotations?.["backstage.io/kubernetes-cluster"]) {
|
|
12
|
+
return {
|
|
13
|
+
clusters: clusters.filter(
|
|
14
|
+
(c) => c.name === _entity.metadata?.annotations?.["backstage.io/kubernetes-cluster"]
|
|
15
|
+
)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
return { clusters };
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
exports.SingleTenantServiceLocator = SingleTenantServiceLocator;
|
|
24
|
+
//# sourceMappingURL=SingleTenantServiceLocator.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SingleTenantServiceLocator.cjs.js","sources":["../../src/service-locator/SingleTenantServiceLocator.ts"],"sourcesContent":["/*\n * Copyright 2023 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { Entity } from '@backstage/catalog-model';\nimport {\n ClusterDetails,\n KubernetesClustersSupplier,\n KubernetesServiceLocator,\n ServiceLocatorRequestContext,\n} from '../types/types';\n\n// This locator assumes that service is located on one cluster\n// Therefore it will always return specified cluster provided in backstage.io/kubernetes-cluster annotation\n// If backstage.io/kubernetes-cluster annotation not provided will always return all cluster provided\nexport class SingleTenantServiceLocator implements KubernetesServiceLocator {\n private readonly clusterSupplier: KubernetesClustersSupplier;\n\n constructor(clusterSupplier: KubernetesClustersSupplier) {\n this.clusterSupplier = clusterSupplier;\n }\n\n // As this implementation always returns all clusters serviceId is ignored here\n getClustersByEntity(\n _entity: Entity,\n requestContext: ServiceLocatorRequestContext,\n ): Promise<{ clusters: ClusterDetails[] }> {\n return this.clusterSupplier\n .getClusters({ credentials: requestContext.credentials })\n .then(clusters => {\n if (\n _entity.metadata?.annotations?.['backstage.io/kubernetes-cluster']\n ) {\n return {\n clusters: clusters.filter(\n c =>\n c.name ===\n _entity.metadata?.annotations?.[\n 'backstage.io/kubernetes-cluster'\n ],\n ),\n };\n }\n return { clusters };\n });\n }\n}\n"],"names":[],"mappings":";;AA0BO,MAAM,0BAA+D,CAAA;AAAA,EACzD,eAAA,CAAA;AAAA,EAEjB,YAAY,eAA6C,EAAA;AACvD,IAAA,IAAA,CAAK,eAAkB,GAAA,eAAA,CAAA;AAAA,GACzB;AAAA;AAAA,EAGA,mBAAA,CACE,SACA,cACyC,EAAA;AACzC,IAAO,OAAA,IAAA,CAAK,eACT,CAAA,WAAA,CAAY,EAAE,WAAA,EAAa,eAAe,WAAY,EAAC,CACvD,CAAA,IAAA,CAAK,CAAY,QAAA,KAAA;AAChB,MAAA,IACE,OAAQ,CAAA,QAAA,EAAU,WAAc,GAAA,iCAAiC,CACjE,EAAA;AACA,QAAO,OAAA;AAAA,UACL,UAAU,QAAS,CAAA,MAAA;AAAA,YACjB,OACE,CAAE,CAAA,IAAA,KACF,OAAQ,CAAA,QAAA,EAAU,cAChB,iCACF,CAAA;AAAA,WACJ;AAAA,SACF,CAAA;AAAA,OACF;AACA,MAAA,OAAO,EAAE,QAAS,EAAA,CAAA;AAAA,KACnB,CAAA,CAAA;AAAA,GACL;AACF;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/plugin-kubernetes-backend",
|
|
3
|
-
"version": "0.18.7-next.
|
|
3
|
+
"version": "0.18.7-next.1",
|
|
4
4
|
"description": "A Backstage backend plugin that integrates towards Kubernetes",
|
|
5
5
|
"backstage": {
|
|
6
6
|
"role": "backend-plugin",
|
|
@@ -63,19 +63,19 @@
|
|
|
63
63
|
"@aws-sdk/signature-v4": "^3.347.0",
|
|
64
64
|
"@azure/identity": "^4.0.0",
|
|
65
65
|
"@backstage/backend-common": "^0.25.0",
|
|
66
|
-
"@backstage/backend-plugin-api": "
|
|
67
|
-
"@backstage/catalog-client": "
|
|
68
|
-
"@backstage/catalog-model": "
|
|
69
|
-
"@backstage/config": "
|
|
70
|
-
"@backstage/errors": "
|
|
71
|
-
"@backstage/integration-aws-node": "
|
|
72
|
-
"@backstage/plugin-auth-node": "
|
|
73
|
-
"@backstage/plugin-catalog-node": "
|
|
74
|
-
"@backstage/plugin-kubernetes-common": "
|
|
75
|
-
"@backstage/plugin-kubernetes-node": "
|
|
76
|
-
"@backstage/plugin-permission-common": "
|
|
77
|
-
"@backstage/plugin-permission-node": "
|
|
78
|
-
"@backstage/types": "
|
|
66
|
+
"@backstage/backend-plugin-api": "1.0.1-next.1",
|
|
67
|
+
"@backstage/catalog-client": "1.7.1-next.0",
|
|
68
|
+
"@backstage/catalog-model": "1.7.0",
|
|
69
|
+
"@backstage/config": "1.2.0",
|
|
70
|
+
"@backstage/errors": "1.2.4",
|
|
71
|
+
"@backstage/integration-aws-node": "0.1.12",
|
|
72
|
+
"@backstage/plugin-auth-node": "0.5.3-next.1",
|
|
73
|
+
"@backstage/plugin-catalog-node": "1.13.1-next.1",
|
|
74
|
+
"@backstage/plugin-kubernetes-common": "0.8.3",
|
|
75
|
+
"@backstage/plugin-kubernetes-node": "0.1.20-next.1",
|
|
76
|
+
"@backstage/plugin-permission-common": "0.8.1",
|
|
77
|
+
"@backstage/plugin-permission-node": "0.8.4-next.1",
|
|
78
|
+
"@backstage/types": "1.1.1",
|
|
79
79
|
"@google-cloud/container": "^5.0.0",
|
|
80
80
|
"@jest-mock/express": "^2.0.1",
|
|
81
81
|
"@kubernetes/client-node": "0.20.0",
|
|
@@ -98,12 +98,12 @@
|
|
|
98
98
|
"yn": "^4.0.0"
|
|
99
99
|
},
|
|
100
100
|
"devDependencies": {
|
|
101
|
-
"@backstage/backend-app-api": "
|
|
102
|
-
"@backstage/backend-defaults": "
|
|
103
|
-
"@backstage/backend-test-utils": "
|
|
104
|
-
"@backstage/cli": "
|
|
105
|
-
"@backstage/plugin-permission-backend": "
|
|
106
|
-
"@backstage/plugin-permission-backend-module-allow-all-policy": "
|
|
101
|
+
"@backstage/backend-app-api": "1.0.1-next.1",
|
|
102
|
+
"@backstage/backend-defaults": "0.5.1-next.2",
|
|
103
|
+
"@backstage/backend-test-utils": "1.0.1-next.2",
|
|
104
|
+
"@backstage/cli": "0.28.0-next.2",
|
|
105
|
+
"@backstage/plugin-permission-backend": "0.5.50-next.1",
|
|
106
|
+
"@backstage/plugin-permission-backend-module-allow-all-policy": "0.2.1-next.1",
|
|
107
107
|
"@types/aws4": "^1.5.1",
|
|
108
108
|
"msw": "^1.0.0",
|
|
109
109
|
"supertest": "^7.0.0",
|