Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
added BoltDB repository
Browse files Browse the repository at this point in the history
  • Loading branch information
ortuman committed Mar 2, 2022
1 parent 414d2eb commit dbdcf3c
Show file tree
Hide file tree
Showing 28 changed files with 2,413 additions and 41 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ build

# Mock files
*.mock_test.go

# BoltDB default database file
.jackal.db
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jackal supports the following features:
- Customizable
- Enforced SSL/TLS
- Stream compression (zlib)
- Database connectivity for storing offline messages and user settings (PostgreSQL 9.5+)
- Database connectivity for storing offline messages and user settings (PostgreSQL 9.5+, BoltDB)
- Caching (Redis 6.2+)
- Clustering capabilities (etcd 3.4+)
- Expose [prometheus](https://prometheus.io/) metrics
Expand Down
51 changes: 25 additions & 26 deletions config/example.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ peppers:
# cert_file: ""
# privkey_file: ""

storage:
type: pgsql
pgsql:
host: 127.0.0.1:5432
user: jackal
password: password
database: jackal
max_open_conns: 16

cache:
type: redis
redis:
addresses:
- localhost:6379

cluster:
type: kv
kv:
type: etcd
etcd:
endpoints:
- http://127.0.0.1:2379

server:
port: 14369
#storage:
# type: pgsql
# pgsql:
# host: 127.0.0.1:5432
# user: jackal
# password: password
# database: jackal
# max_open_conns: 16

# cache:
# type: redis
# redis:
# addresses:
# - localhost:6379

#cluster:
# type: kv
# kv:
# type: etcd
# etcd:
# endpoints:
# - http://127.0.0.1:2379
#
# server:
# port: 14369

shapers:
- name: super
Expand Down Expand Up @@ -136,4 +136,3 @@ components:
listeners:
- port: 5275
secret: hqcUrfHtgE73FktcXwfrP

8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,22 @@ require (
github.com/google/uuid v1.1.2
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/jackal-xmpp/runqueue/v2 v2.0.0
github.com/jackal-xmpp/stravaganza v1.1.1
github.com/jackal-xmpp/stravaganza v1.2.1
github.com/kkyr/fig v0.2.0
github.com/lib/pq v1.8.0
github.com/mattn/go-sqlite3 v1.14.5 // indirect
github.com/prometheus/client_golang v1.11.0
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
go.etcd.io/bbolt v1.3.5
go.etcd.io/etcd/client/v3 v3.5.1
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
google.golang.org/grpc v1.38.0
google.golang.org/protobuf v1.26.0
)

require github.com/gogo/protobuf v1.3.2

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
Expand All @@ -42,6 +41,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/kr/text v0.2.0 // indirect
Expand Down Expand Up @@ -69,3 +69,5 @@ require (
)

replace go.etcd.io/etcd/v3 => github.com/etcd-io/etcd/v3 v3.5.1

replace go.etcd.io/bbolt => github.com/etcd-io/bbolt v1.3.5
8 changes: 5 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw=
github.com/etcd-io/bbolt v1.3.5 h1:3Uslx5o2Ds0IBbRW/L7/kiV5oR/kcG85nSnLsvEdKQI=
github.com/etcd-io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
Expand Down Expand Up @@ -223,8 +225,8 @@ github.com/iris-contrib/i18n v0.0.0-20171121225848-987a633949d0/go.mod h1:pMCz62
github.com/iris-contrib/schema v0.0.1/go.mod h1:urYA3uvUNG1TIIjOSCzHr9/LmbQo8LrOcOqfqxa4hXw=
github.com/jackal-xmpp/runqueue/v2 v2.0.0 h1:QfvOfL6zF5yK1LN5TKabpj+VBuELMwtR8Xpkz0CrjoI=
github.com/jackal-xmpp/runqueue/v2 v2.0.0/go.mod h1:tXZARVqBMGeV8BTc/qDPg0qXILTUWmER7wlYbN9Xcac=
github.com/jackal-xmpp/stravaganza v1.1.1 h1:P7mpUNc+B5d8TF+PErmiK9ayCo+AC+xIxQBEV1dD6Eo=
github.com/jackal-xmpp/stravaganza v1.1.1/go.mod h1:C2sH3I3kQQWsOs6+Cg+il0mzGPLnyy+QgVwTxbyJ5kk=
github.com/jackal-xmpp/stravaganza v1.2.1 h1:FDCWRhBmrKAxIBr4ix7Bugd557f18Lg+lTmPWom0g9A=
github.com/jackal-xmpp/stravaganza v1.2.1/go.mod h1:C2sH3I3kQQWsOs6+Cg+il0mzGPLnyy+QgVwTxbyJ5kk=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
Expand Down Expand Up @@ -414,7 +416,6 @@ github.com/yudai/pp v2.0.1+incompatible/go.mod h1:PuxR/8QJ7cyCkFp/aUDS+JY727OFEZ
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/etcd/api/v3 v3.5.1 h1:v28cktvBq+7vGyJXF8G+rWJmj+1XUmMtqcLnH8hDocM=
go.etcd.io/etcd/api/v3 v3.5.1/go.mod h1:cbVKeC6lCfl7j/8jBhAK6aIYO9XOjdptoxU/nLQcPvs=
go.etcd.io/etcd/client/pkg/v3 v3.5.1 h1:XIQcHCFSG53bJETYeRJtIxdLv2EWRGxcfzR8lSnTH4E=
Expand Down Expand Up @@ -529,6 +530,7 @@ golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
5 changes: 4 additions & 1 deletion pkg/admin/server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ func (s *usersService) upsertUser(ctx context.Context, username, password string
hSHA512 := hashPassword([]byte(password), pepperedSalt, iterationCount, sha512.Size, sha512.New)
hSHA3512 := hashPassword([]byte(password), pepperedSalt, iterationCount, sha512.Size, sha3.New512)

usr := usermodel.User{Username: username}
usr := usermodel.User{
Username: username,
Scram: &usermodel.Scram{},
}
usr.Scram.Sha1 = base64.RawURLEncoding.EncodeToString(hSHA1)
usr.Scram.Sha256 = base64.RawURLEncoding.EncodeToString(hSHA256)
usr.Scram.Sha512 = base64.RawURLEncoding.EncodeToString(hSHA512)
Expand Down
113 changes: 113 additions & 0 deletions pkg/storage/boltdb/blocklist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2022 The jackal Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package boltdb

import (
"context"
"fmt"

blocklistmodel "github.com/ortuman/jackal/pkg/model/blocklist"
bolt "go.etcd.io/bbolt"
)

type boltDBBlockListRep struct {
tx *bolt.Tx
}

func newBlockListRep(tx *bolt.Tx) *boltDBBlockListRep {
return &boltDBBlockListRep{tx: tx}
}

func (r *boltDBBlockListRep) UpsertBlockListItem(_ context.Context, item *blocklistmodel.Item) error {
op := upsertKeyOp{
tx: r.tx,
bucket: blockListBucket(item.Username),
key: item.Jid,
obj: item,
}
return op.do()
}

func (r *boltDBBlockListRep) DeleteBlockListItem(_ context.Context, item *blocklistmodel.Item) error {
op := delKeyOp{
tx: r.tx,
bucket: blockListBucket(item.Username),
key: item.Jid,
}
return op.do()
}

func (r *boltDBBlockListRep) FetchBlockListItems(_ context.Context, username string) ([]*blocklistmodel.Item, error) {
var retVal []*blocklistmodel.Item

op := iterKeysOp{
tx: r.tx,
bucket: blockListBucket(username),
iterFn: func(_, b []byte) error {
var item blocklistmodel.Item
if err := item.UnmarshalBinary(b); err != nil {
return err
}
retVal = append(retVal, &item)
return nil
},
}
if err := op.do(); err != nil {
return nil, err
}
return retVal, nil
}

func (r *boltDBBlockListRep) DeleteBlockListItems(_ context.Context, username string) error {
op := delBucketOp{
tx: r.tx,
bucket: blockListBucket(username),
}
return op.do()
}

func blockListBucket(username string) string {
return fmt.Sprintf("blocklist:%s", username)
}

// UpsertBlockListItem satisfies repository.BlockList interface.
func (r *Repository) UpsertBlockListItem(ctx context.Context, item *blocklistmodel.Item) error {
return r.db.Update(func(tx *bolt.Tx) error {
return newBlockListRep(tx).UpsertBlockListItem(ctx, item)
})
}

// DeleteBlockListItem deletes a block list item entity from storage.
func (r *Repository) DeleteBlockListItem(ctx context.Context, item *blocklistmodel.Item) error {
return r.db.Update(func(tx *bolt.Tx) error {
return newBlockListRep(tx).DeleteBlockListItem(ctx, item)
})
}

// FetchBlockListItems retrieves from storage all block list items associated to a user.
func (r *Repository) FetchBlockListItems(ctx context.Context, username string) (items []*blocklistmodel.Item, err error) {
err = r.db.View(func(tx *bolt.Tx) error {
items, err = newBlockListRep(tx).FetchBlockListItems(ctx, username)
return err
})
return
}

// DeleteBlockListItems deletes all block list items associated to a user.
func (r *Repository) DeleteBlockListItems(ctx context.Context, username string) error {
return r.db.Update(func(tx *bolt.Tx) error {
return newBlockListRep(tx).DeleteBlockListItems(ctx, username)
})
}
Loading

0 comments on commit dbdcf3c

Please sign in to comment.