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

New backend system #9

Merged
merged 7 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ app:

backend:
baseUrl: http://localhost:7007
auth:
dangerouslyDisableDefaultAuthPolicy: true
2 changes: 2 additions & 0 deletions packages/plugin-backend/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Support for new Backstage backend system

## [1.0.0] - 2024-04-12

- First release
83 changes: 62 additions & 21 deletions packages/plugin-backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,75 @@ This plugin requires [`@mia-platform/backstage-plugin-frontend`](https://github.

## Configuration

### New backend system

1. Install the plugin to your Backstage app:
```sh
yarn workspace backend add @mia-platform/backstage-plugin-backend
```

2. In `packages/backend/src/index.ts` import the plugin and add it to the backend:

```ts
import miaPlatformModule from '@mia-platform/backstage-plugin-backend';

// ...

backend.add(import('@backstage/plugin-catalog-backend-module-scaffolder-entity-model'));
backend.add(import('@backstage/plugin-catalog-backend/alpha'));

// ...

backend.add(miaPlatformModule());
```

3. Add to your `app-config.yaml` file the section for the configuration of support button and the Mia-Platform console

Under `app` add the configuration for the support button

```yaml
support:
url: https://github.com/mia-platform/backstage-plugin/blob/main/README.md
items:
- title: Documentation
icon: docs
links:
- url: https://github.com/mia-platform/backstage-plugin/blob/main/README.md
title: Repository
```

And then the configuration for the Mia-Platform console:

```yaml
miaPlatform:
baseUrl: ...
gitBaseUrl: ...
authorizations:
- authMode: 'BASIC'
clientId: ...
clientSecret: ...
companyId: ...
- authMode: 'JWT'
clientId: ...
kid: ...
privateKeyPath: ...
expirationTime: ...
companyId: ...
```

- `baseUrl`: Mia Platform console url
- `authorizations`: array of service accounts to use to retrieve projects information

### Legacy backend system

1. Install the plugin to your Backstage app:
```sh
yarn workspace backend add @mia-platform/backstage-plugin-backend
```
2. Create a new file `packages/backend/src/plugins/mia-platform.ts` with content:

```ts
import { MiaPlatformEntityProvider, createRouter } from '@mia-platform/backstage-plugin-backend';
import { MiaPlatformEntityProvider, createRouter } from '@mia-platform/backstage-plugin-backend/legacy';
import { Router } from 'express';
import { PluginEnvironment } from '../types';
import { CatalogBuilder, CatalogEnvironment } from '@backstage/plugin-catalog-backend';
Expand All @@ -38,7 +99,6 @@ export default async function createPlugin(

return await createRouter({
logger: env.logger,
config: env.config,
miaPlatformEntityProvider: miaPlatformProvider,
});
}
Expand All @@ -57,24 +117,6 @@ const miaPlatformEnv = useHotMemoize(module, () => createEnv('mia-platform'));

apiRouter.use('/mia-platform', await miaPlatform(miaPlatformEnv, catalogEnv));
```
4. In `packages/backend/src/plugins/catalog.ts` add the following lines of code to the existing file:

```ts
import { MiaPlatformEntityProvider } from '@mia-platform/backstage-plugin-backend';

// ...
// under line builder.addProcessor(new ScaffolderEntitiesProcessor());

const miaPlatformProvider = MiaPlatformEntityProvider.create(env.config, env.logger);
builder.addEntityProvider(miaPlatformProvider);

// ...
// under line await processingEngine.start();

miaPlatformProvider.full_mutation()
```

This part is needed to insert all the Mia-Platform resources to the catalog of your Backstage application.

5. Add to your `app-config.yaml` file the section for the configuration of support button and the Mia-Platform console

Expand Down Expand Up @@ -113,7 +155,6 @@ miaPlatform:
- `baseUrl`: Mia Platform console url
- `authorizations`: array of service accounts to use to retrieve projects information


## Authorization

The authorization elements inside the array should match one of the following:
Expand Down
10 changes: 10 additions & 0 deletions packages/plugin-backend/dev/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createBackend } from '@backstage/backend-defaults';
import miaPlatformModule from '../src'

const backend = createBackend();

backend.add(import('@backstage/plugin-catalog-backend/alpha'));
backend.add(import('@backstage/plugin-catalog-backend-module-scaffolder-entity-model'));
backend.add(miaPlatformModule())

backend.start();
31 changes: 25 additions & 6 deletions packages/plugin-backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mia-platform/backstage-plugin-backend",
"version": "1.0.0",
"version": "1.1.0-rc.0",
"license": "SEE LICENSE IN LICENSE",
"author": "Mia Platform Core Team <core@mia-platform.eu>",
"repository": {
Expand All @@ -10,10 +10,23 @@
"homepage": "https://www.mia-platform.eu/",
"main": "src/index.ts",
"types": "src/index.ts",
"typesVersions": {
"*": {
"legacy": [
"src/legacy.ts"
],
"package.json": [
"package.json"
]
}
},
"publishConfig": {
"access": "public",
"main": "dist/index.cjs.js",
"types": "dist/index.d.ts"
"access": "public"
},
"exports": {
".": "./src/index.ts",
"./legacy": "./src/legacy.ts",
"./package.json": "./package.json"
},
"files": [
"config-schema.d.ts",
Expand All @@ -23,7 +36,7 @@
"role": "backend-plugin"
},
"scripts": {
"start": "backstage-cli package start",
"start": "backstage-cli package start --config ../../app-config.yaml",
"tsc": "tsc",
"build": "yarn tsc && backstage-cli package build",
"lint": "backstage-cli package lint",
Expand All @@ -34,6 +47,7 @@
},
"dependencies": {
"@backstage/backend-common": "^0.21.6",
"@backstage/backend-plugin-api": "^0.6.17",
"@backstage/config": "^1.2.0",
"@backstage/plugin-catalog-node": "^1.11.0",
"express": "^4.19.2",
Expand All @@ -43,12 +57,17 @@
"yn": "^5.0.0"
},
"devDependencies": {
"@backstage/backend-defaults": "^0.2.17",
"@backstage/cli": "^0.26.2",
"@backstage/plugin-catalog-backend": "^1.21.1",
"@backstage/plugin-catalog-backend-module-scaffolder-entity-model": "^0.1.15",
"@types/express": "^4.17.21",
"@types/jsonwebtoken": "^9.0.6",
"@types/supertest": "^6.0.2",
"better-sqlite3": "^9.5.0",
"supertest": "^6.3.4",
"ts-node": "^10.9.2",
"typescript": "^5.4.4"
}
},
"stableVersion": "1.0.0"
}
37 changes: 35 additions & 2 deletions packages/plugin-backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,38 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
export * from './service/router';
export * from './service/miaPlatformEntityProvider'
import { coreServices, createBackendModule } from '@backstage/backend-plugin-api';
import { catalogProcessingExtensionPoint } from '@backstage/plugin-catalog-node/alpha';
import { loggerToWinstonLogger } from '@backstage/backend-common';

import { createRouter } from './service/router';
import { MiaPlatformEntityProvider } from './service/miaPlatformEntityProvider';

const miaPlatformModule = createBackendModule({
moduleId: 'mia-platform',
pluginId: 'catalog',
register(env) {
env.registerInit({
deps: {
config: coreServices.rootConfig,
catalog: catalogProcessingExtensionPoint,
logger: coreServices.logger,
httpRouter: coreServices.httpRouter,
},
async init({ logger, catalog, httpRouter, config }) {
const miaPlatformEntityProvider = MiaPlatformEntityProvider.create(config, loggerToWinstonLogger(logger));
catalog.addEntityProvider(miaPlatformEntityProvider);

const router = await createRouter({
miaPlatformEntityProvider,
logger: loggerToWinstonLogger(logger),
prefix: '/modules/mia-platform/'
});

httpRouter.use(router);
},
});
},
});

export { miaPlatformModule as default };
17 changes: 17 additions & 0 deletions packages/plugin-backend/src/legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Copyright 2024 Mia srl

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 * from './service/router';
export * from './service/miaPlatformEntityProvider'
34 changes: 0 additions & 34 deletions packages/plugin-backend/src/run.ts

This file was deleted.

19 changes: 13 additions & 6 deletions packages/plugin-backend/src/service/miaPlatformEntityProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,24 @@ type MiaPlatformAuthorizationBasic = {
clientId: string,
clientSecret: string,
}

type MiaPlatformAuthorizationJwt = {
authMode: 'JWT',
clientId: string,
kid: string,
privateKeyPath: string,
expirationTime: number,
}

type MiaPlatformAuthorization = MiaPlatformAuthorizationBasic | MiaPlatformAuthorizationJwt

type MiaPlatformConfiguration = {
baseUrl: string,
gitBaseUrl: string,
companies: Record<string, MiaPlatformAuthorization>,
appBaseUrl: string,
}

export type AuthResponse = {
access_token: String,
tokent_type: String,
Expand All @@ -56,6 +60,7 @@ type Dashboard = {
type: string,
url: string
}

type Host = {
host: string,
isBackoffice: boolean,
Expand Down Expand Up @@ -108,7 +113,8 @@ type ProjectPublicVariable = {
name: string,
environments: Record<string, { value: string }>
}
type ProjectAnnotion = {

type ProjectAnnotation = {
name: string,
description: string,
value: string,
Expand All @@ -117,7 +123,7 @@ type ProjectAnnotion = {

type ProjectService = {
name: string,
annotations: ProjectAnnotion[],
annotations: ProjectAnnotation[],
description: string,
unsecretedVariables: ProjectPublicVariable[]
}
Expand Down Expand Up @@ -184,21 +190,22 @@ export class MiaPlatformEntityProvider implements EntityProvider {
static create(config: Config, logger: Logger) {
return new MiaPlatformEntityProvider(config, logger)
}
private constructor(
config: Config,
logger: Logger
) {

private constructor(config: Config, logger: Logger) {
const miaPlatformConfiguration = this.readMiaPlatformConfiguration(config)
this.consoleBaseUrl = miaPlatformConfiguration.baseUrl
this.gitBaseUrl = miaPlatformConfiguration.gitBaseUrl
this.companies = miaPlatformConfiguration.companies
this.logger = logger
}

getProviderName(): string {
return `miaplatform-entity-provider`;
}

async connect(connection: EntityProviderConnection): Promise<void> {
this.connection = connection;
this.full_mutation()
}

async full_mutation() {
Expand Down
Loading