2022-10-02 19:38:12 +00:00
|
|
|
package dynamodb
|
|
|
|
|
|
|
|
import (
|
2022-10-20 10:57:00 +00:00
|
|
|
"fmt"
|
2022-10-08 19:19:31 +00:00
|
|
|
"os"
|
|
|
|
|
2022-10-02 19:38:12 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2022-10-05 10:02:32 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
2022-10-02 19:38:12 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/session"
|
|
|
|
"github.com/guregu/dynamo"
|
2022-10-08 19:19:31 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-10-20 10:57:00 +00:00
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
|
|
|
"github.com/authorizerdev/authorizer/server/db/models"
|
|
|
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
2022-10-02 19:38:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type provider struct {
|
|
|
|
db *dynamo.DB
|
|
|
|
}
|
|
|
|
|
2022-10-05 10:02:32 +00:00
|
|
|
// NewProvider returns a new Dynamo provider
|
2022-10-02 19:38:12 +00:00
|
|
|
func NewProvider() (*provider, error) {
|
2022-10-05 10:02:32 +00:00
|
|
|
dbURL := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseURL
|
2022-10-08 19:19:31 +00:00
|
|
|
awsRegion := os.Getenv(constants.EnvAwsRegion)
|
2022-10-20 10:57:00 +00:00
|
|
|
accessKey := os.Getenv(constants.EnvAwsAccessKeyID)
|
|
|
|
secretKey := os.Getenv(constants.EnvAwsSecretAccessKey)
|
2022-10-08 19:19:31 +00:00
|
|
|
|
2022-10-02 19:38:12 +00:00
|
|
|
config := aws.Config{
|
2022-10-08 19:19:31 +00:00
|
|
|
MaxRetries: aws.Int(3),
|
|
|
|
CredentialsChainVerboseErrors: aws.Bool(true), // for full error logs
|
|
|
|
}
|
|
|
|
|
|
|
|
if awsRegion != "" {
|
|
|
|
config.Region = aws.String(awsRegion)
|
2022-10-02 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 10:57:00 +00:00
|
|
|
if accessKey == "" {
|
|
|
|
log.Debugf("%s not found", constants.EnvAwsAccessKeyID)
|
|
|
|
return nil, fmt.Errorf("invalid aws credentials. %s not found", constants.EnvAwsAccessKeyID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if secretKey == "" {
|
|
|
|
log.Debugf("%s not found", constants.EnvAwsSecretAccessKey)
|
|
|
|
return nil, fmt.Errorf("invalid aws credentials. %s not found", constants.EnvAwsSecretAccessKey)
|
|
|
|
}
|
|
|
|
|
2022-10-05 10:02:32 +00:00
|
|
|
// custom accessKey, secretkey took first priority, if not then fetch config from aws credentials
|
|
|
|
if accessKey != "" && secretKey != "" {
|
|
|
|
config.Credentials = credentials.NewStaticCredentials(accessKey, secretKey, "")
|
|
|
|
} else if dbURL != "" {
|
|
|
|
// static config in case of testing or local-setup
|
|
|
|
config.Credentials = credentials.NewStaticCredentials("key", "key", "")
|
|
|
|
config.Endpoint = aws.String(dbURL)
|
2022-10-02 19:38:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 10:02:32 +00:00
|
|
|
session := session.Must(session.NewSession(&config))
|
|
|
|
db := dynamo.New(session)
|
|
|
|
|
|
|
|
db.CreateTable(models.Collections.User, models.User{}).Wait()
|
|
|
|
db.CreateTable(models.Collections.Session, models.Session{}).Wait()
|
|
|
|
db.CreateTable(models.Collections.EmailTemplate, models.EmailTemplate{}).Wait()
|
|
|
|
db.CreateTable(models.Collections.Env, models.Env{}).Wait()
|
|
|
|
db.CreateTable(models.Collections.OTP, models.OTP{}).Wait()
|
|
|
|
db.CreateTable(models.Collections.VerificationRequest, models.VerificationRequest{}).Wait()
|
|
|
|
db.CreateTable(models.Collections.Webhook, models.Webhook{}).Wait()
|
|
|
|
db.CreateTable(models.Collections.WebhookLog, models.WebhookLog{}).Wait()
|
|
|
|
|
2022-10-02 19:38:12 +00:00
|
|
|
return &provider{
|
|
|
|
db: db,
|
|
|
|
}, nil
|
|
|
|
}
|