Merge pull request #10588 from Budibase/budi-6932/verify_s3
Implement S3 connection verification
This commit is contained in:
commit
328514afdd
|
@ -6,8 +6,8 @@ import {
|
||||||
DatasourceFeature,
|
DatasourceFeature,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
|
|
||||||
const AWS = require("aws-sdk")
|
import AWS from "aws-sdk"
|
||||||
const csv = require("csvtojson")
|
import csv from "csvtojson"
|
||||||
|
|
||||||
interface S3Config {
|
interface S3Config {
|
||||||
region: string
|
region: string
|
||||||
|
@ -154,7 +154,7 @@ const SCHEMA: Integration = {
|
||||||
|
|
||||||
class S3Integration implements IntegrationBase {
|
class S3Integration implements IntegrationBase {
|
||||||
private readonly config: S3Config
|
private readonly config: S3Config
|
||||||
private client: any
|
private client
|
||||||
|
|
||||||
constructor(config: S3Config) {
|
constructor(config: S3Config) {
|
||||||
this.config = config
|
this.config = config
|
||||||
|
@ -167,6 +167,15 @@ class S3Integration implements IntegrationBase {
|
||||||
this.client = new AWS.S3(this.config)
|
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: {
|
async create(query: {
|
||||||
bucket: string
|
bucket: string
|
||||||
location: string
|
location: string
|
||||||
|
|
|
@ -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.",
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
|
@ -1,3 +1,3 @@
|
||||||
const Chance = require("chance")
|
import Chance from "chance"
|
||||||
|
|
||||||
export default new Chance()
|
export default new Chance()
|
||||||
|
|
Loading…
Reference in New Issue