From 66b476115438d34407f4f894e01677d5deb32777 Mon Sep 17 00:00:00 2001 From: gearnode Date: Fri, 28 Feb 2025 14:13:07 +0100 Subject: [PATCH] Configure s3 client when probod start Signed-off-by: gearnode --- pkg/probo/probo.go | 19 ++++++++++++++----- pkg/probod/awsConfig.go | 25 +++++++++++++++++++++++++ pkg/probod/probod.go | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 pkg/probod/awsConfig.go diff --git a/pkg/probo/probo.go b/pkg/probo/probo.go index a58cd015f..7f25e9cb3 100644 --- a/pkg/probo/probo.go +++ b/pkg/probo/probo.go @@ -18,6 +18,7 @@ import ( "context" "fmt" + "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/getprobo/probo/pkg/probo/coredata" "go.gearno.de/kit/migrator" "go.gearno.de/kit/pg" @@ -25,19 +26,27 @@ import ( type ( Service struct { - pg *pg.Client - scope *coredata.Scope + pg *pg.Client + scope *coredata.Scope + s3 *s3.Client + bucket string } ) -func NewService(ctx context.Context, pgClient *pg.Client) (*Service, error) { +func NewService(ctx context.Context, pgClient *pg.Client, s3Client *s3.Client, bucket string) (*Service, error) { err := migrator.NewMigrator(pgClient, coredata.Migrations).Run(ctx, "migrations") if err != nil { return nil, fmt.Errorf("cannot migrate database schema: %w", err) } + if bucket == "" { + return nil, fmt.Errorf("bucket is required") + } + return &Service{ - pg: pgClient, - scope: coredata.NewScope(), // must be created from auth + pg: pgClient, + s3: s3Client, + scope: coredata.NewScope(), // must be created from auth + bucket: bucket, }, nil } diff --git a/pkg/probod/awsConfig.go b/pkg/probod/awsConfig.go new file mode 100644 index 000000000..2ae3de991 --- /dev/null +++ b/pkg/probod/awsConfig.go @@ -0,0 +1,25 @@ +// Copyright (c) 2025 Probo Inc . +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +package probod + +type ( + awsConfig struct { + Region string `json:"region"` + Bucket string `json:"bucket"` + AccessKeyID string `json:"access-key-id"` + SecretAccessKey string `json:"secret-access-key"` + Endpoint string `json:"endpoint"` + } +) diff --git a/pkg/probod/probod.go b/pkg/probod/probod.go index b8bf207be..674fc444b 100644 --- a/pkg/probod/probod.go +++ b/pkg/probod/probod.go @@ -23,11 +23,14 @@ import ( "sync" "time" + "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/getprobo/probo/pkg/api" console_v1 "github.com/getprobo/probo/pkg/api/console/v1" + "github.com/getprobo/probo/pkg/awsconfig" "github.com/getprobo/probo/pkg/probo" "github.com/getprobo/probo/pkg/usrmgr" "github.com/prometheus/client_golang/prometheus" + "go.gearno.de/kit/httpclient" "go.gearno.de/kit/httpserver" "go.gearno.de/kit/log" "go.gearno.de/kit/pg" @@ -44,6 +47,7 @@ type ( Pg pgConfig `json:"pg"` Api apiConfig `json:"api"` Auth authConfig `json:"auth"` + AWS awsConfig `json:"aws"` } ) @@ -77,6 +81,13 @@ func New() *Implm { CookieDomain: "localhost", CookiePath: "/", }, + AWS: awsConfig{ + Region: "us-east-1", + Bucket: "probod", + AccessKeyID: "probod", + SecretAccessKey: "thisisnotasecret", + Endpoint: "http://127.0.0.1:9000", + }, }, } } @@ -111,13 +122,30 @@ func (impl *Implm) Run( return fmt.Errorf("cannot get pepper bytes: %w", err) } - // Initialize the user management service with the pepper + awsConfig := awsconfig.NewConfig( + l, + httpclient.DefaultPooledClient( + httpclient.WithLogger(l), + httpclient.WithTracerProvider(tp), + httpclient.WithRegisterer(r), + ), + awsconfig.Options{ + Region: impl.cfg.AWS.Region, + AccessKeyID: impl.cfg.AWS.AccessKeyID, + SecretAccessKey: impl.cfg.AWS.SecretAccessKey, + Endpoint: impl.cfg.AWS.Endpoint, + }, + ) + + s3Client := s3.NewFromConfig(awsConfig) + + // TODO: merge usrmgr and probo service usrmgrService, err := usrmgr.NewService(ctx, pgClient, pepper) if err != nil { return fmt.Errorf("cannot create usrmgr service: %w", err) } - proboService, err := probo.NewService(ctx, pgClient) + proboService, err := probo.NewService(ctx, pgClient, s3Client, impl.cfg.AWS.Bucket) if err != nil { return fmt.Errorf("cannot create probo service: %w", err) }