Skip to content

Commit

Permalink
Use the interfaces available in the new node package
Browse files Browse the repository at this point in the history
Updated the imports in the backend plugin to use the new package
instead of having duplicated definitions
  • Loading branch information
ivgo committed Feb 22, 2024
1 parent c947924 commit 692b6ec
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 166 deletions.
6 changes: 6 additions & 0 deletions .changeset/flat-ducks-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@spreadshirt/backstage-plugin-s3-viewer-backend': minor
---

**BREAKING**: Some interfaces are now part of the `@spreadshirt/plugin-s3-viewer-node` package. If you were
using them, switch to this new package.
1 change: 1 addition & 0 deletions plugins/s3-viewer-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"@backstage/plugin-permission-node": "^0.7.21",
"@backstage/types": "^1.1.1",
"@spreadshirt/backstage-plugin-s3-viewer-common": "^0.3.9",
"@spreadshirt/backstage-plugin-s3-viewer-node": "0.0.0",
"@types/express": "*",
"cookie-parser": "^1.4.5",
"cross-fetch": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { Config } from '@backstage/config';
import { LoggerService } from '@backstage/backend-plugin-api';
import {
AllowedBuckets,
BucketCredentials,
CredentialsProvider,
S3Platform,
} from '../types';
import { AllowedBuckets, BucketCredentials, S3Platform } from '../types';
import { CredentialsProvider } from '@spreadshirt/backstage-plugin-s3-viewer-node';
import { fetchBucketsForPlatform } from './utils';

export class ConfigCredentialsProvider implements CredentialsProvider {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { LoggerService } from '@backstage/backend-plugin-api';
import {
AllowedBuckets,
BucketCredentials,
CredentialsProvider,
S3Platform,
} from '../types';
import { AllowedBuckets, BucketCredentials, S3Platform } from '../types';
import { CredentialsProvider } from '@spreadshirt/backstage-plugin-s3-viewer-node';
import { Config } from '@backstage/config';
import { fetchBucketsForPlatform } from './utils';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ import { Config } from '@backstage/config';
import { SignatureV4 } from '@aws-sdk/signature-v4';
import { Sha256 } from '@aws-crypto/sha256-browser';
import { HttpRequest } from '@aws-sdk/protocol-http';
import {
AllowedBuckets,
BucketCredentials,
CredentialsProvider,
S3Platform,
} from '../types';
import { AllowedBuckets, BucketCredentials, S3Platform } from '../types';
import { CredentialsProvider } from '@spreadshirt/backstage-plugin-s3-viewer-node';
import fetch from 'cross-fetch';
import { LoggerService } from '@backstage/backend-plugin-api';

Expand Down
7 changes: 2 additions & 5 deletions plugins/s3-viewer-backend/src/credentials-provider/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Config } from '@backstage/config';
import { LoggerService } from '@backstage/backend-plugin-api';
import {
AllowedBuckets,
BucketCredentials,
CredentialsProvider,
} from '../types';
import { AllowedBuckets, BucketCredentials } from '../types';
import { CredentialsProvider } from '@spreadshirt/backstage-plugin-s3-viewer-node';
import { ConfigCredentialsProvider } from './ConfigCredentialsProvider';
import { RadosGwCredentialsProvider } from './RadosGwCredentialsProvider';
import { IAMRoleCredentialsProvider } from './IAMRoleCredentialsProvider';
Expand Down
54 changes: 4 additions & 50 deletions plugins/s3-viewer-backend/src/service/S3Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
import { GetObjectCommand, S3 } from '@aws-sdk/client-s3';
import moment from 'moment';
import { Readable } from 'stream';
import { BucketsProvider } from '../types';
import {
BucketsProvider,
S3Api,
} from '@spreadshirt/backstage-plugin-s3-viewer-node';
import { DiscoveryService } from '@backstage/backend-plugin-api';
import { NotFoundError } from '@backstage/errors';

Expand All @@ -15,55 +18,6 @@ export interface S3ClientEnvironment {
bucketsProvider: BucketsProvider;
}

export interface S3Api {
/**
* Sets the bucketsProvider, which might be needed to fetch credentials. This method
* is optional. Use it only if required.
* @param bucketsProvider The buckets provider used to get credentials for buckets
*/
setBucketsProvider?(bucketsProvider: BucketsProvider): void;
/**
* List the keys for a bucket.
* @param endpoint The endpoint where the bucket is
* @param bucket The bucket name
* @param continuationToken The continuation token to make pagination
* @param pageSize The page size, which can be changed in the UI
* @param folder The folder name where the keys are located
* @param prefix The prefix to filter the listed keys
*/
listBucketKeys(
endpoint: string,
bucket: string,
continuationToken: string,
pageSize: number,
folder: string,
prefix: string,
): Promise<ListBucketKeysResult>;
/**
* Makes a `HEAD` request to fetch the metadata for an object.
* @param endpoint The endpoint where the bucket is
* @param bucket The bucket name
* @param key The key to obtain the HEAD data
*/
headObject(
endpoint: string,
bucket: string,
key: string,
): Promise<FetchObjectResult>;
/**
* Returns a stream used to preview the file (if possible),
* and also used to download that file dynamically.
* @param endpoint The endpoint where the bucket is
* @param bucket The bucket name
* @param key The name of the key to fetch its data
*/
streamObject(
endpoint: string,
bucket: string,
key: string,
): Promise<Readable>;
}

export class S3Client implements S3Api {
private discoveryApi: DiscoveryService;
private bucketsProvider: BucketsProvider;
Expand Down
6 changes: 3 additions & 3 deletions plugins/s3-viewer-backend/src/service/S3BucketsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BucketCredentials } from '../types';
import {
BucketCredentials,
BucketsProvider,
BucketStatsProvider,
BucketsProvider,
CredentialsProvider,
} from '../types';
} from '@spreadshirt/backstage-plugin-s3-viewer-node';
import { BucketDetails } from '@spreadshirt/backstage-plugin-s3-viewer-common';
import { LoggerService } from '@backstage/backend-plugin-api';
import { S3 } from '@aws-sdk/client-s3';
Expand Down
7 changes: 4 additions & 3 deletions plugins/s3-viewer-backend/src/service/S3Builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { Config } from '@backstage/config';
import express from 'express';
import Router from 'express-promise-router';
import {
BucketsProvider,
BucketStatsProvider,
BucketsProvider,
CredentialsProvider,
} from '../types';
S3Api,
} from '@spreadshirt/backstage-plugin-s3-viewer-node';
import { S3BucketsProvider } from './S3BucketsProvider';
import { S3Api, S3Client } from './S3Api';
import { S3Client } from './S3Api';
import { errorHandler } from '@backstage/backend-common';
import {
DiscoveryService,
Expand Down
87 changes: 0 additions & 87 deletions plugins/s3-viewer-backend/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import {
BucketDetails,
BucketStats,
} from '@spreadshirt/backstage-plugin-s3-viewer-common';
import { BucketDetailsFilters } from './permissions';

export type S3Platform = {
endpoint: string;
endpointName: string;
Expand All @@ -22,84 +16,3 @@ export type AllowedBuckets = {
platform: string;
buckets: string[];
};

export interface CredentialsProvider {
/**
* Iterates over the configured platforms and fetches the
* credentials for each bucket found in there. These credentials
* will be used to fetch keys & objects in the UI.
*/
getBucketCredentials(): Promise<BucketCredentials[]>;
}

export interface BucketsProvider {
/**
* Fetches all the buckets for the configured endpoints.
* This method is called by `start()`, but this is exported
* so it can be called from other places if needed.
*/
fetchBuckets(): Promise<void>;

/**
* Returns all the discovered bucket names. The list can be filtered out
* if a conditional permission was used in a custom permission policy.
*
* @param filter The filter used to filter the buckets
*/
getAllBuckets(filter?: BucketDetailsFilters): string[];

/**
* Returns all the bucket names found for a certain endpoint. This list can
* be filtered out if a conditional permission has been defined in the
* permission backend.
*
* @param endpoint The endpoint to fetch the bucket names
* @param filter The filter used to filter the buckets
*/
getBucketsByEndpoint(
endpoint: string,
filter?: BucketDetailsFilters,
): string[];

/**
* Returns all the bucket names grouped by the endpoint where
* they are located. Used for the tree view in the UI. This list
* can be filtered out if a conditional permission has been
* defined in the permission backend.
*
* @param filter The filter used to filter the buckets
*/
getGroupedBuckets(filter?: BucketDetailsFilters): Record<string, string[]>;

/**
* Gets the bucket details or `undefined` if not found.
*
* @param endpoint The endpoint where the bucket is located
* @param bucket The bucket name to fetch info from
*/
getBucketInfo(endpoint: string, bucket: string): BucketDetails | undefined;

/**
* Gets the credentials stored to read from a bucket in a certain endpoint,
* or `undefined` if not found.
*
* @param endpoint The endpoint where the bucket is located
* @param bucket The bucket name to fetch the credentials
*/
getCredentialsForBucket(
endpoint: string,
bucket: string,
): BucketCredentials | undefined;
}

export interface BucketStatsProvider {
/**
* Returns the bucket stats for a certain bucket. It's not possible
* to fetch this data from the S3 API, so it has to be a custom
* method fetching the data from an external source.
*
* @param endpoint The endpoint where the bucket is located
* @param bucket The bucket name to fetch the stats
*/
getStats(endpoint: string, bucket: string): Promise<BucketStats>;
}

0 comments on commit 692b6ec

Please sign in to comment.