Files
backstage/plugins/azure-sites-backend
Fredrik Adelöw e97cc0b0a3 updated the knip rules a bit
Signed-off-by: Fredrik Adelöw <freben@gmail.com>
2024-02-08 09:53:18 +01:00
..
2024-01-24 13:31:29 +01:00
2024-02-06 14:58:46 +00:00
2024-02-08 09:53:18 +01:00
2024-02-06 14:58:46 +00:00

Azure Sites Backend

Simple plugin that proxies requests to the Azure Portal API through Azure SDK JavaScript libraries.

Inspired by roadie.io AWS Lambda plugin

Setup

The following sections will help you get the Azure Sites Backend plugin setup and running.

Configuration

The Azure plugin requires the following YAML to be added to your app-config.yaml:

azureSites:
  domain:
  tenantId:
  clientId:
  clientSecret:
  allowedSubscriptions:
    - id:

Configuration Details:

Integrating

Here's how to get the backend plugin up and running:

  1. First we need to add the @backstage/plugin-azure-sites-backend package to your backend:

    # From the Backstage root directory
    yarn --cwd packages/backend add @backstage/plugin-azure-sites-backend
    
  2. Then we will create a new file named packages/backend/src/plugins/azure-sites.ts, and add the following to it:

    import {
      createRouter,
      AzureSitesApi,
    } from '@backstage/plugin-azure-sites-backend';
    import { Router } from 'express';
    import { PluginEnvironment } from '../types';
    
    export default async function createPlugin(
      env: PluginEnvironment,
    ): Promise<Router> {
      return await createRouter({
        logger: env.logger,
        azureSitesApi: AzureSitesApi.fromConfig(env.config),
        permissions: env.permissions,
      });
    }
    
  3. Next we wire this into the overall backend router, edit packages/backend/src/index.ts:

    import azureSites from './plugins/azure-sites';
    
    // Removed for clarity...
    
    async function main() {
      // ...
      // Add this line under the other lines that follow the useHotMemoize pattern
      const azureSitesEnv = useHotMemoize(module, () =>
        createEnv('azure-sites'),
      );
    
      // ...
      // Insert this line under the other lines that add their routers to apiRouter in the same way
      apiRouter.use('/azure-sites', await azureSites(azureSitesEnv));
    }
    
  4. Enable permissions and that the below is just an example policy that forbids anyone but the owner of the catalog entity to trigger actions towards a site tied to an entity, edit your packages/backend/src/plugins/permission.ts

       // packages/backend/src/plugins/permission.ts
    +  import { azureSitesActionPermission } from '@backstage/plugin-azure-sites-common';
       ...
       class TestPermissionPolicy implements PermissionPolicy {
    -  async handle(): Promise<PolicyDecision> {
    +  async handle(request: PolicyQuery, user?: BackstageIdentityResponse): Promise<PolicyDecision> {
         if (isPermission(request.permission, azureSitesActionPermission)) {
           return createCatalogConditionalDecision(
             request.permission,
             catalogConditions.isEntityOwner({
               claims: user?.identity.ownershipEntityRefs ??  [],
             }),
           );
         }
         ...
         return {
           result: AuthorizeResult.ALLOW,
         };
       }
    
  5. Now run yarn start-backend from the repo root.

  6. Finally, open http://localhost:7007/api/azure/health in a browser, it should return {"status":"ok"}.