diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index ad145ca2..20376cc9 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -34,8 +34,18 @@ jobs: AC_PASSWORD: ${{ secrets.AC_PASSWORD }} AC_PROVIDER: ${{ secrets.AC_PROVIDER }} goreleaser-docker: + permissions: + id-token: write + contents: read runs-on: ubuntu-latest steps: + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: arn:aws:iam::168442440833:role/GitHubActionsECRPushRole-baton-postgresql + aws-region: us-west-2 + - name: Login to Amazon ECR + uses: aws-actions/amazon-ecr-login@v2 - name: Checkout uses: actions/checkout@v4 with: diff --git a/.goreleaser.docker.yaml b/.goreleaser.docker.yaml index 7b4382a0..76c12918 100644 --- a/.goreleaser.docker.yaml +++ b/.goreleaser.docker.yaml @@ -11,6 +11,8 @@ builds: goarch: - amd64 - arm64 + tags: + - "baton_lambda_support" dockers: - use: buildx goos: linux @@ -38,6 +40,19 @@ dockers: - "--label=org.opencontainers.image.revision={{.FullCommit}}" - "--label=org.opencontainers.image.version={{.Version}}" - "--label=org.opencontainers.image.source=https://github.com/conductorone/baton-postgresql" + - use: buildx + goos: linux + goarch: arm64 + dockerfile: Dockerfile.lambda + image_templates: + - "168442440833.dkr.ecr.us-west-2.amazonaws.com/baton-postgresql:{{ .Version }}-arm64" + build_flag_templates: + - "--platform=linux/arm64/v8" + - "--label=org.opencontainers.image.created={{.Date}}" + - "--label=org.opencontainers.image.title=baton-postgresql" + - "--label=org.opencontainers.image.revision={{.FullCommit}}" + - "--label=org.opencontainers.image.version={{.Version}}" + - "--label=org.opencontainers.image.source=https://github.com/conductorone/baton-postgresql" docker_manifests: - name_template: ghcr.io/conductorone/baton-postgresql:{{ .Version }} image_templates: diff --git a/Dockerfile.lambda b/Dockerfile.lambda new file mode 100644 index 00000000..12f59061 --- /dev/null +++ b/Dockerfile.lambda @@ -0,0 +1,3 @@ +FROM public.ecr.aws/lambda/provided:al2023 +ENTRYPOINT ["/baton-postgresql", "lambda"] +COPY baton-postgresql / \ No newline at end of file diff --git a/Makefile b/Makefile index 73634861..b8f79c68 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ GOOS = $(shell go env GOOS) GOARCH = $(shell go env GOARCH) BUILD_DIR = dist/${GOOS}_${GOARCH} +GENERATED_CONF = pkg/config/conf.gen.go ifeq ($(GOOS),windows) OUTPUT_PATH = ${BUILD_DIR}/baton-postgresql.exe @@ -8,9 +9,22 @@ else OUTPUT_PATH = ${BUILD_DIR}/baton-postgresql endif +# Set the build tag conditionally based on BATON_LAMBDA_SUPPORT +ifdef BATON_LAMBDA_SUPPORT + BUILD_TAGS=-tags baton_lambda_support +else + BUILD_TAGS= +endif + .PHONY: build -build: - go build -o ${OUTPUT_PATH} ./cmd/baton-postgresql +build: $(GENERATED_CONF) + go build ${BUILD_TAGS} -o ${OUTPUT_PATH} ./cmd/baton-postgresql + +$(GENERATED_CONF): pkg/config/config.go go.mod + @echo "Generating $(GENERATED_CONF)..." + go generate ./pkg/config + +generate: $(GENERATED_CONF) .PHONY: update-deps update-deps: diff --git a/cmd/baton-postgresql/main.go b/cmd/baton-postgresql/main.go index b53025fe..e3e469e4 100644 --- a/cmd/baton-postgresql/main.go +++ b/cmd/baton-postgresql/main.go @@ -5,10 +5,10 @@ import ( "fmt" "os" + cfg "github.com/conductorone/baton-postgresql/pkg/config" "github.com/conductorone/baton-sdk/pkg/connectorbuilder" "github.com/conductorone/baton-sdk/pkg/types" "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" - "github.com/spf13/viper" "go.uber.org/zap" "github.com/conductorone/baton-postgresql/pkg/connector" @@ -20,7 +20,7 @@ var version = "dev" func main() { ctx := context.Background() - _, cmd, err := configschema.DefineConfiguration(ctx, "baton-postgresql", getConnector, configuration) + _, cmd, err := configschema.DefineConfiguration(ctx, "baton-postgresql", getConnector, cfg.Config) if err != nil { fmt.Fprintln(os.Stderr, err.Error()) os.Exit(1) @@ -35,10 +35,10 @@ func main() { } } -func getConnector(ctx context.Context, v *viper.Viper) (types.ConnectorServer, error) { +func getConnector(ctx context.Context, pgc *cfg.Postgresql) (types.ConnectorServer, error) { l := ctxzap.Extract(ctx) - cb, err := connector.New(ctx, v.GetString("dsn"), v.GetStringSlice("schemas"), v.GetBool("include-columns"), v.GetBool("include-large-objects")) + cb, err := connector.New(ctx, pgc.Dsn, pgc.Schemas, pgc.IncludeColumns, pgc.IncludeLargeObjects) if err != nil { l.Error("error creating connector", zap.Error(err)) return nil, err diff --git a/pkg/config/conf.gen.go b/pkg/config/conf.gen.go new file mode 100644 index 00000000..3bdbf1f6 --- /dev/null +++ b/pkg/config/conf.gen.go @@ -0,0 +1,86 @@ +// Code generated by baton-sdk. DO NOT EDIT!!! +package config + +import "reflect" + +type Postgresql struct { + Dsn string `mapstructure:"dsn"` + Schemas []string `mapstructure:"schemas"` + IncludeColumns bool `mapstructure:"include-columns"` + IncludeLargeObjects bool `mapstructure:"include-large-objects"` +} + +func (c* Postgresql) findFieldByTag(tagValue string) (any, bool) { + v := reflect.ValueOf(c).Elem() // Dereference pointer to struct + t := v.Type() + + for i := 0; i < t.NumField(); i++ { + field := t.Field(i) + tag := field.Tag.Get("mapstructure") + + if tag == tagValue { + return v.Field(i).Interface(), true + } + } + return nil, false +} + +func (c *Postgresql) GetStringSlice(fieldName string) []string { + v, ok := c.findFieldByTag(fieldName) + if !ok { + return []string{} + } + t, ok := v.([]string) + if !ok { + panic("wrong type") + } + return t +} + +func (c *Postgresql) GetString(fieldName string) string { + v, ok := c.findFieldByTag(fieldName) + if !ok { + return "" + } + t, ok := v.(string) + if !ok { + panic("wrong type") + } + return t +} + +func (c *Postgresql) GetInt(fieldName string) int { + v, ok := c.findFieldByTag(fieldName) + if !ok { + return 0 + } + t, ok := v.(int) + if !ok { + panic("wrong type") + } + return t +} + +func (c *Postgresql) GetBool(fieldName string) bool { + v, ok := c.findFieldByTag(fieldName) + if !ok { + return false + } + t, ok := v.(bool) + if !ok { + panic("wrong type") + } + return t +} + +func (c *Postgresql) GetStringMap(fieldName string) map[string]any { + v, ok := c.findFieldByTag(fieldName) + if !ok { + return map[string]any{} + } + t, ok := v.(map[string]any) + if !ok { + panic("wrong type") + } + return t +} diff --git a/cmd/baton-postgresql/config.go b/pkg/config/config.go similarity index 89% rename from cmd/baton-postgresql/config.go rename to pkg/config/config.go index 9a492a00..bb4b164e 100644 --- a/cmd/baton-postgresql/config.go +++ b/pkg/config/config.go @@ -1,4 +1,4 @@ -package main +package config import ( "github.com/conductorone/baton-sdk/pkg/field" @@ -13,6 +13,7 @@ var ( var relationships = []field.SchemaFieldRelationship{} -var configuration = field.NewConfiguration([]field.SchemaField{ +//go:generate go run ./gen +var Config = field.NewConfiguration([]field.SchemaField{ dsn, schemas, includeColumns, includeLargeObjects, }, relationships...) diff --git a/pkg/config/gen/gen.go b/pkg/config/gen/gen.go new file mode 100644 index 00000000..2c8a49b8 --- /dev/null +++ b/pkg/config/gen/gen.go @@ -0,0 +1,10 @@ +package main + +import ( + cfg "github.com/conductorone/baton-postgresql/pkg/config" + "github.com/conductorone/baton-sdk/pkg/config" +) + +func main() { + config.Generate("postgresql", cfg.Config) +}