(feature): remove unused configuration and improved permission management, describe the permission integration in detail.

Signed-off-by: Hasan Oezdemir <21654050+nodify-at@users.noreply.github.com>
This commit is contained in:
Hasan Oezdemir
2022-03-01 23:45:01 +01:00
parent 1543dcf31a
commit 70f29c5423
10 changed files with 27 additions and 147 deletions
-1
View File
@@ -21,4 +21,3 @@
*/
export * from './service';
export * from './permissions';
@@ -1,27 +0,0 @@
/*
* Copyright 2022 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 { jenkinsPermissionRules } from './rules';
import { createConditionExports } from '@backstage/plugin-permission-node';
import { RESOURCE_TYPE_CATALOG_ENTITY } from '@backstage/plugin-catalog-common';
const { conditions, createPolicyDecision } = createConditionExports({
pluginId: 'jenkins',
resourceType: RESOURCE_TYPE_CATALOG_ENTITY,
rules: jenkinsPermissionRules,
});
export const jenkinsConditions = conditions;
export const createJenkinsPermissionPolicy = createPolicyDecision;
@@ -1,20 +0,0 @@
/*
* Copyright 2022 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.
*/
export {
createJenkinsPermissionPolicy,
jenkinsConditions,
} from './conditionExports';
export * from './rules';
@@ -1,30 +0,0 @@
/*
* Copyright 2022 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 { permissionRules } from '@backstage/plugin-catalog-backend';
/**
*
* Jenkins' permission rules can be used to defined different kind of rules to check if the user authorizes an action
* (or listing the jobs ever)
*
* Provided rules:
* {isEntityOwner} can be used to determine if a user or the group of the user
* owns an entity.
*
* @public
*/
export const jenkinsPermissionRules = {
isEntityOwner: permissionRules.isEntityOwner,
};
@@ -14,9 +14,9 @@
* limitations under the License.
*/
import { JenkinsInfo } from './jenkinsInfoProvider';
import type { JenkinsInfo } from './jenkinsInfoProvider';
import jenkins from 'jenkins';
import {
import type {
BackstageBuild,
BackstageProject,
JenkinsBuild,
@@ -139,13 +139,14 @@ export class JenkinsApiImpl {
async buildProject(
jenkinsInfo: JenkinsInfo,
jobFullName: string,
resourceRef?: string,
options?: { token?: string },
) {
const client = await JenkinsApiImpl.getClient(jenkinsInfo);
if (this.permissionApi) {
const response = await this.permissionApi.authorize(
[{ permission: jenkinsExecutePermission }],
[{ permission: jenkinsExecutePermission, resourceRef }],
{ token: options?.token },
);
// permission api returns always at least one item, we need to check only one result since we do not expect any additional results
+9 -12
View File
@@ -14,25 +14,19 @@
* limitations under the License.
*/
import {
errorHandler,
PluginEndpointDiscovery,
} from '@backstage/backend-common';
import { errorHandler } from '@backstage/backend-common';
import express from 'express';
import Router from 'express-promise-router';
import { Logger } from 'winston';
import { JenkinsInfoProvider } from './jenkinsInfoProvider';
import type { Logger } from 'winston';
import type { JenkinsInfoProvider } from './jenkinsInfoProvider';
import { JenkinsApiImpl } from './jenkinsApi';
import { Config } from '@backstage/config';
import { PermissionAuthorizer } from '@backstage/plugin-permission-common';
import type { PermissionAuthorizer } from '@backstage/plugin-permission-common';
import { getBearerTokenFromAuthorizationHeader } from '@backstage/plugin-auth-node';
import { stringifyEntityRef } from '@backstage/catalog-model';
export interface RouterOptions {
logger: Logger;
jenkinsInfoProvider: JenkinsInfoProvider;
config?: Config;
discovery?: PluginEndpointDiscovery;
fetchApi?: { fetch: typeof fetch };
permissions?: PermissionAuthorizer;
}
@@ -125,7 +119,10 @@ export async function createRouter(
request.header('authorization'),
);
await jenkinsApi.buildProject(jenkinsInfo, jobFullName, { token });
const resourceRef = stringifyEntityRef({ kind, namespace, name });
await jenkinsApi.buildProject(jenkinsInfo, jobFullName, resourceRef, {
token,
});
response.json({});
},
);