Skip to content

Commit

Permalink
Add initial setup for AWS S3 SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
SanjalKatiyar committed Sep 12, 2024
1 parent 376ebbf commit 8f973cb
Show file tree
Hide file tree
Showing 9 changed files with 1,258 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"dev:c": "yarn ocp-console & PLUGIN=${PLUGIN} I8N_NS=${I8N_NS} yarn server:plugin & wait"
},
"dependencies": {
"@aws-sdk/client-s3": "3.614.0",
"@openshift-console/dynamic-plugin-sdk": "1.3.0",
"@openshift-console/dynamic-plugin-sdk-internal": "1.0.0",
"@openshift-console/dynamic-plugin-sdk-webpack": "1.1.0",
Expand Down
81 changes: 81 additions & 0 deletions packages/odf/components/s3-browser/noobaa-context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as React from 'react';
import { useSafeK8sGet } from '@odf/core/hooks';
import { useODFNamespaceSelector } from '@odf/core/redux';
import { StatusBox } from '@odf/shared/generic/status-box';
import { SecretModel, RouteModel } from '@odf/shared/models';
import { S3Commands } from '@odf/shared/s3';
import { SecretKind, K8sResourceKind } from '@odf/shared/types';
import * as _ from 'lodash-es';
import {
NOOBAA_ADMIN_SECRET,
NOOBAA_S3_ROUTE,
NOOBAA_ACCESS_KEY_ID,
NOOBAA_SECRET_ACCESS_KEY,
} from '../../constants';

type NoobaaS3ContextType = {
noobaaS3: S3Commands;
};

type NoobaaS3ProviderType = {
loading?: boolean;
error?: unknown;
};

export const NoobaaS3Context = React.createContext<NoobaaS3ContextType>(
{} as NoobaaS3ContextType
);

// ToDo: In case this provider is needed at too many places, consider applying it to the console's root or use redux instead
export const NoobaaS3Provider: React.FC<NoobaaS3ProviderType> = ({
children,
loading,
error,
}) => {
const { isODFNsLoaded, odfNsLoadError } = useODFNamespaceSelector();

const [secretData, secretLoaded, secretError] = useSafeK8sGet<SecretKind>(
SecretModel,
NOOBAA_ADMIN_SECRET
);
// ToDo: Configure ConsolePlugin proxy instead of using Route
const [routeData, routeLoaded, routeError] = useSafeK8sGet<K8sResourceKind>(
RouteModel,
NOOBAA_S3_ROUTE
);

const [noobaaS3, noobaaS3Error]: [S3Commands, unknown] = React.useMemo(() => {
if (!_.isEmpty(secretData) && !_.isEmpty(routeData)) {
try {
// ToDo: Remove this once ConsolePlugin proxy is configured
const endpoint = `http://${routeData.spec.host}`;
const accessKeyId = atob(secretData.data?.[NOOBAA_ACCESS_KEY_ID]);
const secretAccessKey = atob(
secretData.data?.[NOOBAA_SECRET_ACCESS_KEY]
);

return [new S3Commands(endpoint, accessKeyId, secretAccessKey), null];
} catch (err) {
return [{} as S3Commands, err];
}
}
return [{} as S3Commands, null];
}, [secretData, routeData]);

const allLoaded =
isODFNsLoaded &&
secretLoaded &&
routeLoaded &&
!loading &&
!_.isEmpty(noobaaS3);
const anyError =
odfNsLoadError || secretError || routeError || noobaaS3Error || error;

return allLoaded && !anyError ? (
<NoobaaS3Context.Provider value={{ noobaaS3 }}>
{children}
</NoobaaS3Context.Provider>
) : (
<StatusBox loaded={allLoaded} loadError={anyError} />
);
};
1 change: 1 addition & 0 deletions packages/odf/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './tooltips';
export * from './mcg';
export * from './providerSchema';
export * from './data-protection';
export * from './s3-browser';
4 changes: 4 additions & 0 deletions packages/odf/constants/s3-browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const NOOBAA_ADMIN_SECRET = 'noobaa-admin';
export const NOOBAA_S3_ROUTE = 's3';
export const NOOBAA_ACCESS_KEY_ID = 'AWS_ACCESS_KEY_ID';
export const NOOBAA_SECRET_ACCESS_KEY = 'AWS_SECRET_ACCESS_KEY';
14 changes: 14 additions & 0 deletions packages/shared/src/models/console-models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,17 @@ export const ProjectModel: K8sKind = {
// t('Projects')
labelPluralKey: 'Projects',
};

export const RouteModel: K8sKind = {
label: 'Route',
labelKey: 'Route',
labelPlural: 'Routes',
labelPluralKey: 'Routes',
apiGroup: 'route.openshift.io',
apiVersion: 'v1',
plural: 'routes',
abbr: 'RT',
namespaced: true,
kind: 'Route',
id: 'route',
};
31 changes: 31 additions & 0 deletions packages/shared/src/s3/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
S3Client,
ListBucketsCommand,
ListObjectsCommand,
} from '@aws-sdk/client-s3';
import { ListBuckets, ListObjects } from './types';

export class S3Commands {
private s3Client: S3Client;

constructor(endpoint: string, accessKeyId: string, secretAccessKey: string) {
this.s3Client = new S3Client({
// "region" is a required parameter for the SDK, using "none" as a workaround
region: 'none',
endpoint,
credentials: {
accessKeyId,
secretAccessKey,
},
forcePathStyle: true,
});
}

// Bucket command members
listBuckets: ListBuckets = (input) =>
this.s3Client.send(new ListBucketsCommand(input));

// Object command members
listObjects: ListObjects = (input) =>
this.s3Client.send(new ListObjectsCommand(input));
}
2 changes: 2 additions & 0 deletions packages/shared/src/s3/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './commands';
export * from './types';
16 changes: 16 additions & 0 deletions packages/shared/src/s3/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
ListBucketsCommandInput,
ListBucketsCommandOutput,
ListObjectsCommandInput,
ListObjectsCommandOutput,
} from '@aws-sdk/client-s3';

// Bucket command types
export type ListBuckets = (
input?: ListBucketsCommandInput
) => Promise<ListBucketsCommandOutput>;

// Object command types
export type ListObjects = (
input: ListObjectsCommandInput
) => Promise<ListObjectsCommandOutput>;
Loading

0 comments on commit 8f973cb

Please sign in to comment.