Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
2d0346ff23 | ||
![]() |
4b26e1ce85 | ||
![]() |
8212e81023 | ||
![]() |
642581eefd | ||
![]() |
b7357dde21 | ||
![]() |
a1df2ce31f | ||
![]() |
748761926d |
6
Makefile
6
Makefile
@@ -35,6 +35,7 @@ test-dynamodb:
|
|||||||
docker rm -vf dynamodb-local-test
|
docker rm -vf dynamodb-local-test
|
||||||
test-couchbase:
|
test-couchbase:
|
||||||
docker run -d --name couchbase-local-test -p 8091-8097:8091-8097 -p 11210:11210 -p 11207:11207 -p 18091-18095:18091-18095 -p 18096:18096 -p 18097:18097 couchbase:latest
|
docker run -d --name couchbase-local-test -p 8091-8097:8091-8097 -p 11210:11210 -p 11207:11207 -p 18091-18095:18091-18095 -p 18096:18096 -p 18097:18097 couchbase:latest
|
||||||
|
sh scripts/couchbase-test.sh
|
||||||
cd server && go clean --testcache && TEST_DBS="couchbase" go test -p 1 -v ./test
|
cd server && go clean --testcache && TEST_DBS="couchbase" go test -p 1 -v ./test
|
||||||
docker rm -vf couchbase-local-test
|
docker rm -vf couchbase-local-test
|
||||||
test-all-db:
|
test-all-db:
|
||||||
@@ -44,11 +45,12 @@ test-all-db:
|
|||||||
docker run -d --name authorizer_arangodb -p 8529:8529 -e ARANGO_NO_AUTH=1 arangodb/arangodb:3.8.4
|
docker run -d --name authorizer_arangodb -p 8529:8529 -e ARANGO_NO_AUTH=1 arangodb/arangodb:3.8.4
|
||||||
docker run -d --name dynamodb-local-test -p 8000:8000 amazon/dynamodb-local:latest
|
docker run -d --name dynamodb-local-test -p 8000:8000 amazon/dynamodb-local:latest
|
||||||
docker run -d --name couchbase-local-test -p 8091-8097:8091-8097 -p 11210:11210 -p 11207:11207 -p 18091-18095:18091-18095 -p 18096:18096 -p 18097:18097 couchbase:latest
|
docker run -d --name couchbase-local-test -p 8091-8097:8091-8097 -p 11210:11210 -p 11207:11207 -p 18091-18095:18091-18095 -p 18096:18096 -p 18097:18097 couchbase:latest
|
||||||
cd server && go clean --testcache && TEST_DBS="sqlite,mongodb,arangodb,scylladb,dynamodb,couchbase" go test -p 1 -v ./test
|
sh scripts/couchbase-test.sh
|
||||||
|
cd server && go clean --testcache && TEST_DBS="sqlite,mongodb,arangodb,scylladb,dynamodb" go test -p 1 -v ./test
|
||||||
docker rm -vf authorizer_scylla_db
|
docker rm -vf authorizer_scylla_db
|
||||||
docker rm -vf authorizer_mongodb_db
|
docker rm -vf authorizer_mongodb_db
|
||||||
docker rm -vf authorizer_arangodb
|
docker rm -vf authorizer_arangodb
|
||||||
docker rm -vf dynamodb-local-test
|
docker rm -vf dynamodb-local-test
|
||||||
docker rm -vf couchbase-local-test
|
# docker rm -vf couchbase-local-test
|
||||||
generate:
|
generate:
|
||||||
cd server && go run github.com/99designs/gqlgen generate && go mod tidy
|
cd server && go run github.com/99designs/gqlgen generate && go mod tidy
|
||||||
|
39
scripts/couchbase-test.sh
Normal file
39
scripts/couchbase-test.sh
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -x
|
||||||
|
set -m
|
||||||
|
|
||||||
|
sleep 15
|
||||||
|
|
||||||
|
# Setup index and memory quota
|
||||||
|
# curl -v -X POST http://127.0.0.1:8091/pools/default -d memoryQuota=300 -d indexMemoryQuota=300
|
||||||
|
|
||||||
|
# Setup services
|
||||||
|
curl -v http://127.0.0.1:8091/node/controller/setupServices -d services=kv%2Cn1ql%2Cindex
|
||||||
|
|
||||||
|
# Setup credentials
|
||||||
|
curl -v http://127.0.0.1:8091/settings/web -d port=8091 -d username=Administrator -d password=password
|
||||||
|
|
||||||
|
# Setup Memory Optimized Indexes
|
||||||
|
curl -i -u Administrator:password -X POST http://127.0.0.1:8091/settings/indexes -d 'storageMode=memory_optimized'
|
||||||
|
|
||||||
|
# Load travel-sample bucket
|
||||||
|
#curl -v -u Administrator:password -X POST http://127.0.0.1:8091/sampleBuckets/install -d '["travel-sample"]'
|
||||||
|
|
||||||
|
echo "Type: $TYPE"
|
||||||
|
|
||||||
|
if [ "$TYPE" = "WORKER" ]; then
|
||||||
|
echo "Sleeping ..."
|
||||||
|
sleep 15
|
||||||
|
|
||||||
|
#IP=`hostname -s`
|
||||||
|
IP=`hostname -I | cut -d ' ' -f1`
|
||||||
|
echo "IP: " $IP
|
||||||
|
|
||||||
|
echo "Auto Rebalance: $AUTO_REBALANCE"
|
||||||
|
if [ "$AUTO_REBALANCE" = "true" ]; then
|
||||||
|
couchbase-cli rebalance --cluster=$COUCHBASE_MASTER:8091 --user=Administrator --password=password --server-add=$IP --server-add-username=Administrator --server-add-password=password
|
||||||
|
else
|
||||||
|
couchbase-cli server-add --cluster=$COUCHBASE_MASTER:8091 --user=Administrator --password=password --server-add=$IP --server-add-username=Administrator --server-add-password=password
|
||||||
|
fi;
|
||||||
|
fi;
|
@@ -45,6 +45,9 @@ const (
|
|||||||
EnvKeyDatabaseCACert = "DATABASE_CA_CERT"
|
EnvKeyDatabaseCACert = "DATABASE_CA_CERT"
|
||||||
// EnvCouchbaseBucket key for env variable COUCHBASE_BUCKET
|
// EnvCouchbaseBucket key for env variable COUCHBASE_BUCKET
|
||||||
EnvCouchbaseBucket = "COUCHBASE_BUCKET"
|
EnvCouchbaseBucket = "COUCHBASE_BUCKET"
|
||||||
|
// EnvCouchbaseBucketRAMQuotaMB key for env variable COUCHBASE_BUCKET_RAM_QUOTA
|
||||||
|
// This value should be parsed as number
|
||||||
|
EnvCouchbaseBucketRAMQuotaMB = "COUCHBASE_BUCKET_RAM_QUOTA"
|
||||||
// EnvCouchbaseBucket key for env variable COUCHBASE_SCOPE
|
// EnvCouchbaseBucket key for env variable COUCHBASE_SCOPE
|
||||||
EnvCouchbaseScope = "COUCHBASE_SCOPE"
|
EnvCouchbaseScope = "COUCHBASE_SCOPE"
|
||||||
// EnvKeySmtpHost key for env variable SMTP_HOST
|
// EnvKeySmtpHost key for env variable SMTP_HOST
|
||||||
|
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/authorizerdev/authorizer/server/db/providers"
|
"github.com/authorizerdev/authorizer/server/db/providers"
|
||||||
"github.com/authorizerdev/authorizer/server/db/providers/arangodb"
|
"github.com/authorizerdev/authorizer/server/db/providers/arangodb"
|
||||||
"github.com/authorizerdev/authorizer/server/db/providers/cassandradb"
|
"github.com/authorizerdev/authorizer/server/db/providers/cassandradb"
|
||||||
|
"github.com/authorizerdev/authorizer/server/db/providers/couchbase"
|
||||||
"github.com/authorizerdev/authorizer/server/db/providers/dynamodb"
|
"github.com/authorizerdev/authorizer/server/db/providers/dynamodb"
|
||||||
"github.com/authorizerdev/authorizer/server/db/providers/mongodb"
|
"github.com/authorizerdev/authorizer/server/db/providers/mongodb"
|
||||||
"github.com/authorizerdev/authorizer/server/db/providers/sql"
|
"github.com/authorizerdev/authorizer/server/db/providers/sql"
|
||||||
@@ -26,6 +27,7 @@ func InitDB() error {
|
|||||||
isMongoDB := envs.DatabaseType == constants.DbTypeMongodb
|
isMongoDB := envs.DatabaseType == constants.DbTypeMongodb
|
||||||
isCassandra := envs.DatabaseType == constants.DbTypeCassandraDB || envs.DatabaseType == constants.DbTypeScyllaDB
|
isCassandra := envs.DatabaseType == constants.DbTypeCassandraDB || envs.DatabaseType == constants.DbTypeScyllaDB
|
||||||
isDynamoDB := envs.DatabaseType == constants.DbTypeDynamoDB
|
isDynamoDB := envs.DatabaseType == constants.DbTypeDynamoDB
|
||||||
|
isCouchbaseDB := envs.DatabaseType == constants.DbTypeCouchbaseDB
|
||||||
|
|
||||||
if isSQL {
|
if isSQL {
|
||||||
log.Info("Initializing SQL Driver for: ", envs.DatabaseType)
|
log.Info("Initializing SQL Driver for: ", envs.DatabaseType)
|
||||||
@@ -72,5 +74,14 @@ func InitDB() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if isCouchbaseDB {
|
||||||
|
log.Info("Initializing CouchbaseDB Driver for: ", envs.DatabaseType)
|
||||||
|
Provider, err = couchbase.NewProvider()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Failed to initialize Couchbase driver: ", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@@ -4,10 +4,11 @@ package models
|
|||||||
|
|
||||||
// Env model for db
|
// Env model for db
|
||||||
type Env struct {
|
type Env struct {
|
||||||
Key string `json:"_key,omitempty" bson:"_key,omitempty" cql:"_key,omitempty" dynamo:"key,omitempty"` // for arangodb
|
Key string `json:"_key,omitempty" bson:"_key,omitempty" cql:"_key,omitempty" dynamo:"key,omitempty"` // for arangodb
|
||||||
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id" cql:"id" dynamo:"id,hash"`
|
ID string `gorm:"primaryKey;type:char(36)" json:"_id" bson:"_id" cql:"id" dynamo:"id,hash"`
|
||||||
EnvData string `json:"env" bson:"env" cql:"env" dynamo:"env"`
|
EnvData string `json:"env" bson:"env" cql:"env" dynamo:"env"`
|
||||||
Hash string `json:"hash" bson:"hash" cql:"hash" dynamo:"hash"`
|
Hash string `json:"hash" bson:"hash" cql:"hash" dynamo:"hash"`
|
||||||
UpdatedAt int64 `json:"updated_at" bson:"updated_at" cql:"updated_at" dynamo:"updated_at"`
|
EncryptionKey string `json:"encryption_key" bson:"encryption_key" cql:"encryption_key" dynamo:"encryption_key"` // couchbase has "hash" as reserved keyword so we cannot use it. This will be empty for other dbs.
|
||||||
CreatedAt int64 `json:"created_at" bson:"created_at" cql:"created_at" dynamo:"created_at"`
|
UpdatedAt int64 `json:"updated_at" bson:"updated_at" cql:"updated_at" dynamo:"updated_at"`
|
||||||
|
CreatedAt int64 `json:"created_at" bson:"created_at" cql:"created_at" dynamo:"created_at"`
|
||||||
}
|
}
|
||||||
|
@@ -70,9 +70,11 @@ func (p *provider) UpdateEmailTemplate(ctx context.Context, emailTemplate models
|
|||||||
func (p *provider) ListEmailTemplate(ctx context.Context, pagination model.Pagination) (*model.EmailTemplates, error) {
|
func (p *provider) ListEmailTemplate(ctx context.Context, pagination model.Pagination) (*model.EmailTemplates, error) {
|
||||||
emailTemplates := []*model.EmailTemplate{}
|
emailTemplates := []*model.EmailTemplate{}
|
||||||
paginationClone := pagination
|
paginationClone := pagination
|
||||||
|
total, err := p.GetTotalDocs(ctx, models.Collections.EmailTemplate)
|
||||||
_, paginationClone.Total = p.GetTotalDocs(ctx, models.Collections.EmailTemplate)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
paginationClone.Total = total
|
||||||
userQuery := fmt.Sprintf("SELECT _id, event_name, subject, design, template, created_at, updated_at FROM %s.%s ORDER BY _id OFFSET $1 LIMIT $2", p.scopeName, models.Collections.EmailTemplate)
|
userQuery := fmt.Sprintf("SELECT _id, event_name, subject, design, template, created_at, updated_at FROM %s.%s ORDER BY _id OFFSET $1 LIMIT $2", p.scopeName, models.Collections.EmailTemplate)
|
||||||
|
|
||||||
queryResult, err := p.db.Query(userQuery, &gocb.QueryOptions{
|
queryResult, err := p.db.Query(userQuery, &gocb.QueryOptions{
|
||||||
|
@@ -18,6 +18,7 @@ func (p *provider) AddEnv(ctx context.Context, env models.Env) (models.Env, erro
|
|||||||
env.CreatedAt = time.Now().Unix()
|
env.CreatedAt = time.Now().Unix()
|
||||||
env.UpdatedAt = time.Now().Unix()
|
env.UpdatedAt = time.Now().Unix()
|
||||||
env.Key = env.ID
|
env.Key = env.ID
|
||||||
|
env.EncryptionKey = env.Hash
|
||||||
|
|
||||||
insertOpt := gocb.InsertOptions{
|
insertOpt := gocb.InsertOptions{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
@@ -32,6 +33,7 @@ func (p *provider) AddEnv(ctx context.Context, env models.Env) (models.Env, erro
|
|||||||
// UpdateEnv to update environment information in database
|
// UpdateEnv to update environment information in database
|
||||||
func (p *provider) UpdateEnv(ctx context.Context, env models.Env) (models.Env, error) {
|
func (p *provider) UpdateEnv(ctx context.Context, env models.Env) (models.Env, error) {
|
||||||
env.UpdatedAt = time.Now().Unix()
|
env.UpdatedAt = time.Now().Unix()
|
||||||
|
env.EncryptionKey = env.Hash
|
||||||
|
|
||||||
updateEnvQuery := fmt.Sprintf("UPDATE %s.%s SET env = $1, updated_at = $2 WHERE _id = $3", p.scopeName, models.Collections.Env)
|
updateEnvQuery := fmt.Sprintf("UPDATE %s.%s SET env = $1, updated_at = $2 WHERE _id = $3", p.scopeName, models.Collections.Env)
|
||||||
_, err := p.db.Query(updateEnvQuery, &gocb.QueryOptions{
|
_, err := p.db.Query(updateEnvQuery, &gocb.QueryOptions{
|
||||||
@@ -50,7 +52,7 @@ func (p *provider) UpdateEnv(ctx context.Context, env models.Env) (models.Env, e
|
|||||||
func (p *provider) GetEnv(ctx context.Context) (models.Env, error) {
|
func (p *provider) GetEnv(ctx context.Context) (models.Env, error) {
|
||||||
var env models.Env
|
var env models.Env
|
||||||
|
|
||||||
query := fmt.Sprintf("SELECT _id, env, created_at, updated_at FROM %s.%s LIMIT 1", p.scopeName, models.Collections.Env)
|
query := fmt.Sprintf("SELECT _id, env, encryption_key, created_at, updated_at FROM %s.%s LIMIT 1", p.scopeName, models.Collections.Env)
|
||||||
q, err := p.db.Query(query, &gocb.QueryOptions{
|
q, err := p.db.Query(query, &gocb.QueryOptions{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||||
@@ -63,5 +65,6 @@ func (p *provider) GetEnv(ctx context.Context) (models.Env, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return env, err
|
return env, err
|
||||||
}
|
}
|
||||||
|
env.Hash = env.EncryptionKey
|
||||||
return env, nil
|
return env, nil
|
||||||
}
|
}
|
||||||
|
@@ -4,70 +4,77 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/couchbase/gocb/v2"
|
||||||
|
|
||||||
"github.com/authorizerdev/authorizer/server/constants"
|
|
||||||
"github.com/authorizerdev/authorizer/server/db/models"
|
"github.com/authorizerdev/authorizer/server/db/models"
|
||||||
"github.com/authorizerdev/authorizer/server/memorystore"
|
"github.com/authorizerdev/authorizer/server/memorystore"
|
||||||
"github.com/couchbase/gocb/v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO change following provider to new db provider
|
const (
|
||||||
|
defaultBucketName = "authorizer"
|
||||||
|
defaultScope = "_default"
|
||||||
|
)
|
||||||
|
|
||||||
type provider struct {
|
type provider struct {
|
||||||
db *gocb.Scope
|
db *gocb.Scope
|
||||||
scopeName string
|
scopeName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewProvider returns a new SQL provider
|
// NewProvider returns a new Couchbase provider
|
||||||
// TODO change following provider to new db provider
|
|
||||||
func NewProvider() (*provider, error) {
|
func NewProvider() (*provider, error) {
|
||||||
// scopeName := os.Getenv(constants.EnvCouchbaseScope)
|
bucketName := memorystore.RequiredEnvStoreObj.GetRequiredEnv().CouchbaseBucket
|
||||||
bucketName := os.Getenv(constants.EnvCouchbaseBucket)
|
scopeName := memorystore.RequiredEnvStoreObj.GetRequiredEnv().CouchbaseScope
|
||||||
scopeName := os.Getenv(constants.EnvCouchbaseScope)
|
|
||||||
|
|
||||||
dbURL := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseURL
|
dbURL := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseURL
|
||||||
userName := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseUsername
|
userName := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseUsername
|
||||||
password := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabasePassword
|
password := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabasePassword
|
||||||
|
|
||||||
opts := gocb.ClusterOptions{
|
opts := gocb.ClusterOptions{
|
||||||
Username: userName,
|
Username: userName,
|
||||||
Password: password,
|
Password: password,
|
||||||
}
|
}
|
||||||
|
if bucketName == "" {
|
||||||
|
bucketName = defaultBucketName
|
||||||
|
}
|
||||||
|
if scopeName == "" {
|
||||||
|
scopeName = defaultScope
|
||||||
|
}
|
||||||
cluster, err := gocb.Connect(dbURL, opts)
|
cluster, err := gocb.Connect(dbURL, opts)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// To create the bucket and scope if not exist
|
// To create the bucket and scope if not exist
|
||||||
bucket, err := CreateBucketAndScope(cluster, bucketName, scopeName)
|
bucket, err := CreateBucketAndScope(cluster, bucketName, scopeName)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
scope := bucket.Scope(scopeName)
|
scope := bucket.Scope(scopeName)
|
||||||
|
|
||||||
scopeIdentifier := fmt.Sprintf("%s.%s", bucketName, scopeName)
|
scopeIdentifier := fmt.Sprintf("%s.%s", bucketName, scopeName)
|
||||||
|
|
||||||
v := reflect.ValueOf(models.Collections)
|
v := reflect.ValueOf(models.Collections)
|
||||||
for i := 0; i < v.NumField(); i++ {
|
for i := 0; i < v.NumField(); i++ {
|
||||||
field := v.Field(i)
|
collectionName := v.Field(i)
|
||||||
user := gocb.CollectionSpec{
|
user := gocb.CollectionSpec{
|
||||||
Name: field.String(),
|
Name: collectionName.String(),
|
||||||
ScopeName: scopeName,
|
ScopeName: scopeName,
|
||||||
}
|
}
|
||||||
collectionOpts := gocb.CreateCollectionOptions{
|
collectionOpts := gocb.CreateCollectionOptions{
|
||||||
Context: context.TODO(),
|
Context: context.TODO(),
|
||||||
}
|
}
|
||||||
_ = bucket.Collections().CreateCollection(user, &collectionOpts)
|
err = bucket.Collections().CreateCollection(user, &collectionOpts)
|
||||||
// if err != nil && !errors.Is(err, gocb.ErrCollectionExists) {
|
if err != nil && !errors.Is(err, gocb.ErrCollectionExists) {
|
||||||
// return nil, err
|
return nil, err
|
||||||
// }
|
}
|
||||||
indexQuery := fmt.Sprintf("CREATE PRIMARY INDEX ON %s.%s", scopeIdentifier, field.String())
|
// TODO: find how to fix this sleep time.
|
||||||
scope.Query(indexQuery, nil)
|
// Add wait time for successful collection creation
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
indexQuery := fmt.Sprintf("CREATE PRIMARY INDEX ON %s.%s", scopeIdentifier, collectionName.String())
|
||||||
|
_, err = scope.Query(indexQuery, nil)
|
||||||
|
if err != nil && !strings.Contains(err.Error(), "The index #primary already exists") {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
indices := GetIndex(scopeIdentifier)
|
indices := GetIndex(scopeIdentifier)
|
||||||
@@ -84,33 +91,45 @@ func NewProvider() (*provider, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CreateBucketAndScope(cluster *gocb.Cluster, bucketName string, scopeName string) (*gocb.Bucket, error) {
|
func CreateBucketAndScope(cluster *gocb.Cluster, bucketName string, scopeName string) (*gocb.Bucket, error) {
|
||||||
|
bucketRAMQuotaMB := memorystore.RequiredEnvStoreObj.GetRequiredEnv().CouchbaseBucketRAMQuotaMB
|
||||||
|
if bucketRAMQuotaMB == "" {
|
||||||
|
bucketRAMQuotaMB = "1000"
|
||||||
|
}
|
||||||
|
bucketRAMQuota, err := strconv.ParseInt(bucketRAMQuotaMB, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
settings := gocb.BucketSettings{
|
settings := gocb.BucketSettings{
|
||||||
Name: bucketName,
|
Name: bucketName,
|
||||||
RAMQuotaMB: 1000,
|
RAMQuotaMB: uint64(bucketRAMQuota),
|
||||||
NumReplicas: 1,
|
|
||||||
BucketType: gocb.CouchbaseBucketType,
|
BucketType: gocb.CouchbaseBucketType,
|
||||||
EvictionPolicy: gocb.EvictionPolicyTypeValueOnly,
|
EvictionPolicy: gocb.EvictionPolicyTypeValueOnly,
|
||||||
FlushEnabled: true,
|
FlushEnabled: true,
|
||||||
CompressionMode: gocb.CompressionModeActive,
|
CompressionMode: gocb.CompressionModeActive,
|
||||||
}
|
}
|
||||||
|
shouldCreateBucket := false
|
||||||
err := cluster.Buckets().CreateBucket(gocb.CreateBucketSettings{
|
// check if bucket exists
|
||||||
BucketSettings: settings,
|
_, err = cluster.Buckets().GetBucket(bucketName, nil)
|
||||||
ConflictResolutionType: gocb.ConflictResolutionTypeSequenceNumber,
|
if err != nil {
|
||||||
}, nil)
|
// bucket not found
|
||||||
|
shouldCreateBucket = true
|
||||||
|
}
|
||||||
|
if shouldCreateBucket {
|
||||||
|
err = cluster.Buckets().CreateBucket(gocb.CreateBucketSettings{
|
||||||
|
BucketSettings: settings,
|
||||||
|
ConflictResolutionType: gocb.ConflictResolutionTypeSequenceNumber,
|
||||||
|
}, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
bucket := cluster.Bucket(bucketName)
|
bucket := cluster.Bucket(bucketName)
|
||||||
|
if scopeName != defaultScope {
|
||||||
if err != nil && !errors.Is(err, gocb.ErrBucketExists) {
|
err = bucket.Collections().CreateScope(scopeName, nil)
|
||||||
return bucket, err
|
if err != nil && !errors.Is(err, gocb.ErrScopeExists) {
|
||||||
|
return bucket, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = bucket.Collections().CreateScope(scopeName, nil)
|
|
||||||
|
|
||||||
if err != nil && !errors.Is(err, gocb.ErrScopeExists) {
|
|
||||||
return bucket, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return bucket, nil
|
return bucket, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -44,7 +44,7 @@ func GetSetFields(webhookMap map[string]interface{}) (string, map[string]interfa
|
|||||||
return updateFields, params
|
return updateFields, params
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *provider) GetTotalDocs(ctx context.Context, collection string) (error, int64) {
|
func (p *provider) GetTotalDocs(ctx context.Context, collection string) (int64, error) {
|
||||||
totalDocs := TotalDocs{}
|
totalDocs := TotalDocs{}
|
||||||
|
|
||||||
countQuery := fmt.Sprintf("SELECT COUNT(*) as Total FROM %s.%s", p.scopeName, collection)
|
countQuery := fmt.Sprintf("SELECT COUNT(*) as Total FROM %s.%s", p.scopeName, collection)
|
||||||
@@ -55,9 +55,9 @@ func (p *provider) GetTotalDocs(ctx context.Context, collection string) (error,
|
|||||||
queryRes.One(&totalDocs)
|
queryRes.One(&totalDocs)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err, totalDocs.Total
|
return totalDocs.Total, err
|
||||||
}
|
}
|
||||||
return nil, totalDocs.Total
|
return totalDocs.Total, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type TotalDocs struct {
|
type TotalDocs struct {
|
||||||
|
@@ -77,13 +77,14 @@ func (p *provider) ListUsers(ctx context.Context, pagination model.Pagination) (
|
|||||||
Context: ctx,
|
Context: ctx,
|
||||||
PositionalParameters: []interface{}{paginationClone.Offset, paginationClone.Limit},
|
PositionalParameters: []interface{}{paginationClone.Offset, paginationClone.Limit},
|
||||||
})
|
})
|
||||||
|
|
||||||
_, paginationClone.Total = p.GetTotalDocs(ctx, models.Collections.User)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
total, err := p.GetTotalDocs(ctx, models.Collections.User)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
paginationClone.Total = total
|
||||||
for queryResult.Next() {
|
for queryResult.Next() {
|
||||||
var user models.User
|
var user models.User
|
||||||
err := queryResult.Row(&user)
|
err := queryResult.Row(&user)
|
||||||
@@ -92,12 +93,9 @@ func (p *provider) ListUsers(ctx context.Context, pagination model.Pagination) (
|
|||||||
}
|
}
|
||||||
users = append(users, user.AsAPIUser())
|
users = append(users, user.AsAPIUser())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := queryResult.Err(); err != nil {
|
if err := queryResult.Err(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &model.Users{
|
return &model.Users{
|
||||||
Pagination: &paginationClone,
|
Pagination: &paginationClone,
|
||||||
Users: users,
|
Users: users,
|
||||||
@@ -150,10 +148,8 @@ func (p *provider) GetUserByID(ctx context.Context, id string) (models.User, err
|
|||||||
func (p *provider) UpdateUsers(ctx context.Context, data map[string]interface{}, ids []string) error {
|
func (p *provider) UpdateUsers(ctx context.Context, data map[string]interface{}, ids []string) error {
|
||||||
// set updated_at time for all users
|
// set updated_at time for all users
|
||||||
data["updated_at"] = time.Now().Unix()
|
data["updated_at"] = time.Now().Unix()
|
||||||
|
|
||||||
updateFields, params := GetSetFields(data)
|
updateFields, params := GetSetFields(data)
|
||||||
|
if len(ids) > 0 {
|
||||||
if ids != nil && len(ids) > 0 {
|
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
params["id"] = id
|
params["id"] = id
|
||||||
userQuery := fmt.Sprintf("UPDATE %s.%s SET %s WHERE _id = $id", p.scopeName, models.Collections.User, updateFields)
|
userQuery := fmt.Sprintf("UPDATE %s.%s SET %s WHERE _id = $id", p.scopeName, models.Collections.User, updateFields)
|
||||||
|
@@ -83,16 +83,17 @@ func (p *provider) GetVerificationRequestByEmail(ctx context.Context, email stri
|
|||||||
func (p *provider) ListVerificationRequests(ctx context.Context, pagination model.Pagination) (*model.VerificationRequests, error) {
|
func (p *provider) ListVerificationRequests(ctx context.Context, pagination model.Pagination) (*model.VerificationRequests, error) {
|
||||||
var verificationRequests []*model.VerificationRequest
|
var verificationRequests []*model.VerificationRequest
|
||||||
paginationClone := pagination
|
paginationClone := pagination
|
||||||
|
total, err := p.GetTotalDocs(ctx, models.Collections.VerificationRequest)
|
||||||
_, paginationClone.Total = p.GetTotalDocs(ctx, models.Collections.VerificationRequest)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
paginationClone.Total = total
|
||||||
query := fmt.Sprintf("SELECT _id, env, created_at, updated_at FROM %s.%s OFFSET $1 LIMIT $2", p.scopeName, models.Collections.VerificationRequest)
|
query := fmt.Sprintf("SELECT _id, env, created_at, updated_at FROM %s.%s OFFSET $1 LIMIT $2", p.scopeName, models.Collections.VerificationRequest)
|
||||||
queryResult, err := p.db.Query(query, &gocb.QueryOptions{
|
queryResult, err := p.db.Query(query, &gocb.QueryOptions{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||||
PositionalParameters: []interface{}{paginationClone.Offset, paginationClone.Limit},
|
PositionalParameters: []interface{}{paginationClone.Offset, paginationClone.Limit},
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -104,7 +105,6 @@ func (p *provider) ListVerificationRequests(ctx context.Context, pagination mode
|
|||||||
}
|
}
|
||||||
verificationRequests = append(verificationRequests, verificationRequest.AsAPIVerificationRequest())
|
verificationRequests = append(verificationRequests, verificationRequest.AsAPIVerificationRequest())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := queryResult.Err(); err != nil {
|
if err := queryResult.Err(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
||||||
|
@@ -76,17 +76,17 @@ func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination)
|
|||||||
params := make(map[string]interface{}, 1)
|
params := make(map[string]interface{}, 1)
|
||||||
params["offset"] = paginationClone.Offset
|
params["offset"] = paginationClone.Offset
|
||||||
params["limit"] = paginationClone.Limit
|
params["limit"] = paginationClone.Limit
|
||||||
|
total, err := p.GetTotalDocs(ctx, models.Collections.Webhook)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
paginationClone.Total = total
|
||||||
query := fmt.Sprintf("SELECT _id, env, created_at, updated_at FROM %s.%s OFFSET $offset LIMIT $limit", p.scopeName, models.Collections.Webhook)
|
query := fmt.Sprintf("SELECT _id, env, created_at, updated_at FROM %s.%s OFFSET $offset LIMIT $limit", p.scopeName, models.Collections.Webhook)
|
||||||
|
|
||||||
_, paginationClone.Total = p.GetTotalDocs(ctx, models.Collections.Webhook)
|
|
||||||
|
|
||||||
queryResult, err := p.db.Query(query, &gocb.QueryOptions{
|
queryResult, err := p.db.Query(query, &gocb.QueryOptions{
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
ScanConsistency: gocb.QueryScanConsistencyRequestPlus,
|
||||||
NamedParameters: params,
|
NamedParameters: params,
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -98,10 +98,8 @@ func (p *provider) ListWebhook(ctx context.Context, pagination model.Pagination)
|
|||||||
}
|
}
|
||||||
webhooks = append(webhooks, webhook.AsAPIWebhook())
|
webhooks = append(webhooks, webhook.AsAPIWebhook())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := queryResult.Err(); err != nil {
|
if err := queryResult.Err(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|
||||||
}
|
}
|
||||||
return &model.Webhooks{
|
return &model.Webhooks{
|
||||||
Pagination: &paginationClone,
|
Pagination: &paginationClone,
|
||||||
|
@@ -45,9 +45,11 @@ func (p *provider) ListWebhookLogs(ctx context.Context, pagination model.Paginat
|
|||||||
params["webhookID"] = webhookID
|
params["webhookID"] = webhookID
|
||||||
params["offset"] = paginationClone.Offset
|
params["offset"] = paginationClone.Offset
|
||||||
params["limit"] = paginationClone.Limit
|
params["limit"] = paginationClone.Limit
|
||||||
|
total, err := p.GetTotalDocs(ctx, models.Collections.WebhookLog)
|
||||||
_, paginationClone.Total = p.GetTotalDocs(ctx, models.Collections.WebhookLog)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
paginationClone.Total = total
|
||||||
if webhookID != "" {
|
if webhookID != "" {
|
||||||
query = fmt.Sprintf(`SELECT _id, http_status, response, request, webhook_id, created_at, updated_at FROM %s.%s WHERE webhook_id=$webhookID`, p.scopeName, models.Collections.WebhookLog)
|
query = fmt.Sprintf(`SELECT _id, http_status, response, request, webhook_id, created_at, updated_at FROM %s.%s WHERE webhook_id=$webhookID`, p.scopeName, models.Collections.WebhookLog)
|
||||||
} else {
|
} else {
|
||||||
|
8
server/env/env.go
vendored
8
server/env/env.go
vendored
@@ -83,6 +83,7 @@ func InitAllEnv() error {
|
|||||||
osAwsSecretKey := os.Getenv(constants.EnvAwsSecretAccessKey)
|
osAwsSecretKey := os.Getenv(constants.EnvAwsSecretAccessKey)
|
||||||
osCouchbaseBucket := os.Getenv(constants.EnvCouchbaseBucket)
|
osCouchbaseBucket := os.Getenv(constants.EnvCouchbaseBucket)
|
||||||
osCouchbaseScope := os.Getenv(constants.EnvCouchbaseScope)
|
osCouchbaseScope := os.Getenv(constants.EnvCouchbaseScope)
|
||||||
|
osCouchbaseBucketRAMQuotaMB := os.Getenv(constants.EnvCouchbaseBucketRAMQuotaMB)
|
||||||
|
|
||||||
// os bool vars
|
// os bool vars
|
||||||
osAppCookieSecure := os.Getenv(constants.EnvKeyAppCookieSecure)
|
osAppCookieSecure := os.Getenv(constants.EnvKeyAppCookieSecure)
|
||||||
@@ -154,6 +155,13 @@ func InitAllEnv() error {
|
|||||||
envData[constants.EnvCouchbaseBucket] = osCouchbaseBucket
|
envData[constants.EnvCouchbaseBucket] = osCouchbaseBucket
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if val, ok := envData[constants.EnvCouchbaseBucketRAMQuotaMB]; !ok || val == "" {
|
||||||
|
envData[constants.EnvCouchbaseBucketRAMQuotaMB] = osCouchbaseBucketRAMQuotaMB
|
||||||
|
}
|
||||||
|
if osCouchbaseBucketRAMQuotaMB != "" && envData[constants.EnvCouchbaseBucketRAMQuotaMB] != osCouchbaseBucketRAMQuotaMB {
|
||||||
|
envData[constants.EnvCouchbaseBucketRAMQuotaMB] = osCouchbaseBucketRAMQuotaMB
|
||||||
|
}
|
||||||
|
|
||||||
if val, ok := envData[constants.EnvCouchbaseScope]; !ok || val == "" {
|
if val, ok := envData[constants.EnvCouchbaseScope]; !ok || val == "" {
|
||||||
envData[constants.EnvCouchbaseScope] = osCouchbaseScope
|
envData[constants.EnvCouchbaseScope] = osCouchbaseScope
|
||||||
}
|
}
|
||||||
|
@@ -33,8 +33,9 @@ type RequiredEnv struct {
|
|||||||
AwsAccessKeyID string `json:"AWS_ACCESS_KEY_ID"`
|
AwsAccessKeyID string `json:"AWS_ACCESS_KEY_ID"`
|
||||||
AwsSecretAccessKey string `json:"AWS_SECRET_ACCESS_KEY"`
|
AwsSecretAccessKey string `json:"AWS_SECRET_ACCESS_KEY"`
|
||||||
// Couchbase related envs
|
// Couchbase related envs
|
||||||
CouchbaseBucket string `json:"COUCHBASE_BUCKET"`
|
CouchbaseBucket string `json:"COUCHBASE_BUCKET"`
|
||||||
CouchbaseScope string `json:"COUCHBASE_SCOPE"`
|
CouchbaseScope string `json:"COUCHBASE_SCOPE"`
|
||||||
|
CouchbaseBucketRAMQuotaMB string `json:"COUCHBASE_BUCKET_RAM_QUOTA"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// RequiredEnvObj is a simple in-memory store for sessions.
|
// RequiredEnvObj is a simple in-memory store for sessions.
|
||||||
@@ -98,6 +99,7 @@ func InitRequiredEnv() error {
|
|||||||
awsSecretAccessKey := os.Getenv(constants.EnvAwsSecretAccessKey)
|
awsSecretAccessKey := os.Getenv(constants.EnvAwsSecretAccessKey)
|
||||||
couchbaseBucket := os.Getenv(constants.EnvCouchbaseBucket)
|
couchbaseBucket := os.Getenv(constants.EnvCouchbaseBucket)
|
||||||
couchbaseScope := os.Getenv(constants.EnvCouchbaseScope)
|
couchbaseScope := os.Getenv(constants.EnvCouchbaseScope)
|
||||||
|
couchbaseBucketRAMQuotaMB := os.Getenv(constants.EnvCouchbaseBucketRAMQuotaMB)
|
||||||
|
|
||||||
if strings.TrimSpace(redisURL) == "" {
|
if strings.TrimSpace(redisURL) == "" {
|
||||||
if cli.ARG_REDIS_URL != nil && *cli.ARG_REDIS_URL != "" {
|
if cli.ARG_REDIS_URL != nil && *cli.ARG_REDIS_URL != "" {
|
||||||
@@ -140,24 +142,25 @@ func InitRequiredEnv() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
requiredEnv := RequiredEnv{
|
requiredEnv := RequiredEnv{
|
||||||
EnvPath: envPath,
|
EnvPath: envPath,
|
||||||
DatabaseURL: dbURL,
|
DatabaseURL: dbURL,
|
||||||
DatabaseType: dbType,
|
DatabaseType: dbType,
|
||||||
DatabaseName: dbName,
|
DatabaseName: dbName,
|
||||||
DatabaseHost: dbHost,
|
DatabaseHost: dbHost,
|
||||||
DatabasePort: dbPort,
|
DatabasePort: dbPort,
|
||||||
DatabaseUsername: dbUsername,
|
DatabaseUsername: dbUsername,
|
||||||
DatabasePassword: dbPassword,
|
DatabasePassword: dbPassword,
|
||||||
DatabaseCert: dbCert,
|
DatabaseCert: dbCert,
|
||||||
DatabaseCertKey: dbCertKey,
|
DatabaseCertKey: dbCertKey,
|
||||||
DatabaseCACert: dbCACert,
|
DatabaseCACert: dbCACert,
|
||||||
RedisURL: redisURL,
|
RedisURL: redisURL,
|
||||||
DisableRedisForEnv: disableRedisForEnv,
|
DisableRedisForEnv: disableRedisForEnv,
|
||||||
AwsRegion: awsRegion,
|
AwsRegion: awsRegion,
|
||||||
AwsAccessKeyID: awsAccessKeyID,
|
AwsAccessKeyID: awsAccessKeyID,
|
||||||
AwsSecretAccessKey: awsSecretAccessKey,
|
AwsSecretAccessKey: awsSecretAccessKey,
|
||||||
CouchbaseBucket: couchbaseBucket,
|
CouchbaseBucket: couchbaseBucket,
|
||||||
CouchbaseScope: couchbaseScope,
|
CouchbaseScope: couchbaseScope,
|
||||||
|
CouchbaseBucketRAMQuotaMB: couchbaseBucketRAMQuotaMB,
|
||||||
}
|
}
|
||||||
|
|
||||||
RequiredEnvStoreObj = &RequiredEnvStore{
|
RequiredEnvStoreObj = &RequiredEnvStore{
|
||||||
|
@@ -25,7 +25,6 @@ func adminSignupTests(t *testing.T, s TestSetup) {
|
|||||||
_, err = resolvers.AdminSignupResolver(ctx, model.AdminSignupInput{
|
_, err = resolvers.AdminSignupResolver(ctx, model.AdminSignupInput{
|
||||||
AdminSecret: "admin123",
|
AdminSecret: "admin123",
|
||||||
})
|
})
|
||||||
|
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -58,6 +58,12 @@ func TestResolvers(t *testing.T) {
|
|||||||
memorystore.Provider.UpdateEnvVariable(constants.EnvAwsRegion, "ap-south-1")
|
memorystore.Provider.UpdateEnvVariable(constants.EnvAwsRegion, "ap-south-1")
|
||||||
os.Setenv(constants.EnvAwsRegion, "ap-south-1")
|
os.Setenv(constants.EnvAwsRegion, "ap-south-1")
|
||||||
}
|
}
|
||||||
|
if dbType == constants.DbTypeCouchbaseDB {
|
||||||
|
memorystore.Provider.UpdateEnvVariable(constants.EnvKeyDatabaseUsername, "Administrator")
|
||||||
|
os.Setenv(constants.EnvKeyDatabaseUsername, "Administrator")
|
||||||
|
memorystore.Provider.UpdateEnvVariable(constants.EnvKeyDatabasePassword, "password")
|
||||||
|
os.Setenv(constants.EnvKeyDatabasePassword, "password")
|
||||||
|
}
|
||||||
|
|
||||||
memorystore.InitRequiredEnv()
|
memorystore.InitRequiredEnv()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user