1
0
Fork 0
mirror of synced 2024-07-03 21:40:55 +12:00

Merge pull request #10588 from Budibase/budi-6932/verify_s3

Implement S3 connection verification
This commit is contained in:
Michael Drury 2023-05-15 17:10:38 +01:00 committed by GitHub
commit 328514afdd
3 changed files with 64 additions and 4 deletions

View file

@ -6,8 +6,8 @@ import {
DatasourceFeature,
} from "@budibase/types"
const AWS = require("aws-sdk")
const csv = require("csvtojson")
import AWS from "aws-sdk"
import csv from "csvtojson"
interface S3Config {
region: string
@ -154,7 +154,7 @@ const SCHEMA: Integration = {
class S3Integration implements IntegrationBase {
private readonly config: S3Config
private client: any
private client
constructor(config: S3Config) {
this.config = config
@ -167,6 +167,15 @@ class S3Integration implements IntegrationBase {
this.client = new AWS.S3(this.config)
}
async testConnection() {
try {
const buckets = await this.client.listBuckets().promise()
return true
} catch (e: any) {
return { error: e.message as string }
}
}
async create(query: {
bucket: string
location: string

View file

@ -0,0 +1,51 @@
import s3 from "../../../../packages/server/src/integrations/s3"
import { GenericContainer } from "testcontainers"
jest.unmock("aws-sdk")
describe("datasource validators", () => {
describe("s3", () => {
let host: string
let port: number
beforeAll(async () => {
const container = await new GenericContainer("localstack/localstack")
.withExposedPorts(4566)
.withEnv("SERVICES", "s3")
.withEnv("DEFAULT_REGION", "eu-west-1")
.withEnv("AWS_ACCESS_KEY_ID", "testkey")
.withEnv("AWS_SECRET_ACCESS_KEY", "testsecret")
.start()
host = container.getContainerIpAddress()
port = container.getMappedPort(4566)
})
it("test valid connection", async () => {
const integration = new s3.integration({
region: "eu-west-1",
accessKeyId: "testkey",
secretAccessKey: "testsecret",
s3ForcePathStyle: false,
endpoint: `http://${host}:${port}`,
})
const result = await integration.testConnection()
expect(result).toBe(true)
})
it("test wrong endpoint", async () => {
const integration = new s3.integration({
region: "eu-west-2",
accessKeyId: "testkey",
secretAccessKey: "testsecret",
s3ForcePathStyle: false,
endpoint: `http://wrong:123`,
})
const result = await integration.testConnection()
expect(result).toEqual({
error:
"Inaccessible host: `wrong' at port `undefined'. This service may not be available in the `eu-west-2' region.",
})
})
})
})

View file

@ -1,3 +1,3 @@
const Chance = require("chance")
import Chance from "chance"
export default new Chance()