Skip to content

Commit

Permalink
Merge pull request #1532 from fidelity-contributions/fix-issue-1464
Browse files Browse the repository at this point in the history
fix the issue #1464
  • Loading branch information
Xantier authored Sep 13, 2024
2 parents c868f86 + 30169d5 commit f3845e9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/lucky-ducks-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@roadiehq/scaffolder-backend-module-http-request': patch
---

Fix authentication issue to be compatible to the latest backstage
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts

import { Config } from '@backstage/config';
import { TemplateAction } from '@backstage/plugin-scaffolder-backend';

// @public (undocumented)
export function createHttpBackstageAction(options: {
config: Config;
config: Config;
}): TemplateAction<any>;


// (No @packageDocumentation comment for this package)

```
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@
* limitations under the License.
*/

import { createTemplateAction } from '@backstage/plugin-scaffolder-backend';
import { createTemplateAction } from '@backstage/plugin-scaffolder-node';
import {
generateBackstageUrl,
http,
getObjFieldCaseInsensitively,
getPluginId,
} from './helpers';
import { HttpOptions, Headers, Params, Methods, Body } from './types';
import { DiscoveryApi } from '@backstage/core-plugin-api';
import { AuthService } from '@backstage/backend-plugin-api';

export function createHttpBackstageAction(options: {
discovery: DiscoveryApi;
auth?: AuthService;
}) {
const { discovery } = options;
const { discovery, auth } = options;
return createTemplateAction<{
path: string;
method: Methods;
Expand Down Expand Up @@ -117,12 +120,15 @@ export function createHttpBackstageAction(options: {

async handler(ctx) {
const { input } = ctx;
const token = ctx.secrets?.backstageToken;
const pluginId = getPluginId(input.path);
const { token } = (await auth?.getPluginRequestToken({
onBehalfOf: await ctx.getInitiatorCredentials(),
targetPluginId: pluginId,
})) ?? { token: ctx.secrets?.backstageToken };
const { method, params } = input;
const logRequestPath = input.logRequestPath ?? true;
const continueOnBadResponse = input.continueOnBadResponse || false;
const url = await generateBackstageUrl(discovery, input.path);

if (logRequestPath) {
ctx.logger.info(
`Creating ${method} request with ${this.id} scaffolder action against ${input.path}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
generateBackstageUrl,
http,
getObjFieldCaseInsensitively,
getPluginId,
} from './helpers';
import { HttpOptions } from './types';
import { getRootLogger } from '@backstage/backend-common';
Expand Down Expand Up @@ -94,6 +95,16 @@ describe('http', () => {
});
});

describe('#getPluginId', () => {
describe('with happy path proxy configuration', () => {
describe('with valid path', () => {
it('returns the plugin Id as passed in', async () => {
expect(getPluginId(proxyPath)).toEqual(`proxy`);
});
});
});
});

describe('#http', () => {
beforeEach(() => {
mockResponse = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ import { HttpOptions } from './types';
class HttpError extends Error {}
const DEFAULT_TIMEOUT = 60_000;

export const getPluginId = (path: string): string => {
const pluginId = (path.startsWith('/') ? path.substring(1) : path).split(
'/',
)[0];
return pluginId;
};

export const generateBackstageUrl = async (
discovery: DiscoveryApi,
path: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ export const scaffolderBackendModuleHttpRequest = createBackendModule({
deps: {
scaffolder: scaffolderActionsExtensionPoint,
discovery: coreServices.discovery,
auth: coreServices.auth,
},
async init({ scaffolder, discovery }) {
async init({ scaffolder, discovery, auth }) {
scaffolder.addActions(
backendModuleHttp.createHttpBackstageAction({ discovery }),
backendModuleHttp.createHttpBackstageAction({ discovery, auth }),
);
},
});
Expand Down

0 comments on commit f3845e9

Please sign in to comment.