Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/missing db indexes #416

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions api_test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httputil"
Expand Down Expand Up @@ -157,13 +157,13 @@ type (

func validateJSON(validator validatorFunc) assert.Func {
return func(res *http.Response, req *http.Request) error {
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}

// Re-fill body reader stream after reading it
res.Body = ioutil.NopCloser(bytes.NewBuffer(body))
res.Body = io.NopCloser(bytes.NewBuffer(body))

// parse the data
var data map[string]interface{}
Expand All @@ -174,13 +174,13 @@ func validateJSON(validator validatorFunc) assert.Func {

func validateBodyText(validator validatorFuncText) assert.Func {
return func(res *http.Response, req *http.Request) error {
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}

// Re-fill body reader stream after reading it
res.Body = ioutil.NopCloser(bytes.NewBuffer(body))
res.Body = io.NopCloser(bytes.NewBuffer(body))

return validator(string(body))
}
Expand Down
6 changes: 3 additions & 3 deletions api_test/misconfig_test/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -106,13 +106,13 @@ type validatorFunc = func(map[string]interface{}) error

func validateJSON(validator validatorFunc) assert.Func {
return func(res *http.Response, req *http.Request) error {
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}

// Re-fill body reader stream after reading it
res.Body = ioutil.NopCloser(bytes.NewBuffer(body))
res.Body = io.NopCloser(bytes.NewBuffer(body))

// parse the data
var data map[string]interface{}
Expand Down
4 changes: 2 additions & 2 deletions config/importer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package config

import (
"io/ioutil"
"io"
"os"

"github.com/madappgang/identifo/v2/model"
Expand Down Expand Up @@ -45,5 +45,5 @@ func dataFromFile(filename string) ([]byte, error) {
return nil, err
}
defer file.Close()
return ioutil.ReadAll(file)
return io.ReadAll(file)
}
6 changes: 3 additions & 3 deletions jwt/pem.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"encoding/pem"
"errors"
"fmt"
"io/ioutil"
"os"

"github.com/madappgang/identifo/v2/model"
)
Expand Down Expand Up @@ -51,7 +51,7 @@ func LoadPrivateKeyFromPEMString(s string) (interface{}, model.TokenSignatureAlg

// LoadPrivateKeyFromPEM loads private key from PEM file.
func LoadPrivateKeyFromPEMFile(file string) (interface{}, model.TokenSignatureAlgorithm, error) {
prkb, err := ioutil.ReadFile(file)
prkb, err := os.ReadFile(file)
if err != nil {
return nil, model.TokenSignatureAlgorithmInvalid, err
}
Expand Down Expand Up @@ -82,7 +82,7 @@ func LoadPublicKeyFromString(s string) (interface{}, model.TokenSignatureAlgorit

// LoadPublicKeyFromPEM loads public key from file
func LoadPublicKeyFromPEM(file string) (interface{}, model.TokenSignatureAlgorithm, error) {
prkb, err := ioutil.ReadFile(file)
prkb, err := os.ReadFile(file)
if err != nil {
return nil, model.TokenSignatureAlgorithmInvalid, err
}
Expand Down
16 changes: 4 additions & 12 deletions services/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,10 @@ func (es *EmailService) Transport() model.EmailTransport {
}

func (es *EmailService) watch() {
for {
select {
case files, ok := <-es.watcher.WatchChan():
// the channel is closed
if ok == false {
return
}
for _, f := range files {
es.cache.Delete(f)
log.Printf("email template changed, the email template cache has been invalidated: %v", f)

}
for files := range es.watcher.WatchChan() {
for _, f := range files {
es.cache.Delete(f)
log.Printf("email template changed, the email template cache has been invalidated: %v", f)
}
}
}
4 changes: 2 additions & 2 deletions services/mail/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func NewTransport() model.EmailTransport {
// SendMessage returns nil error.
func (es *EmailService) SendMessage(subject, body, recipient string) error {
msg := fmt.Sprintf("✉️: MOCK EMAIL SERVICE: Sending message \nsubject: %s\nbody: %s\n recipient: %s\n\n", subject, body, recipient)
fmt.Printf(msg)
fmt.Print(msg)
es.SendMessages = append(es.SendMessages, msg)
return nil
}

// SendHTML returns nil error.
func (es *EmailService) SendHTML(subject, html, recipient string) error {
msg := fmt.Sprintf("✉️: MOCK EMAIL SERVICE: Sending HTML \nsubject: %s\nhtml: %s\n recipient: %s\n\n", subject, html, recipient)
fmt.Printf(msg)
fmt.Print(msg)
es.SendMessages = append(es.SendMessages, msg)
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions services/sms/routemobile/routemobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package routemobile

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -52,7 +52,7 @@ func (ss *SMSService) SendSMS(recipient, message string) error {
}
defer resp.Body.Close()

respBytes, err := ioutil.ReadAll(resp.Body)
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
Expand Down
9 changes: 6 additions & 3 deletions storage/boltdb/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ func TestBoltDBAppCreateApp(t *testing.T) {
Path: dbpath,
}
apps, err := boltdb.NewAppStorage(sts)
defer apps.Close()
require.NoError(t, err)

defer apps.Close()

a, err := apps.CreateApp(testApp)
require.NoError(t, err)

Expand All @@ -64,9 +65,10 @@ func TestBoltDBAppFindAppById(t *testing.T) {
Path: dbpath,
}
apps, err := boltdb.NewAppStorage(sts)
defer apps.Close()
require.NoError(t, err)

defer apps.Close()

testApp2 := testApp
testApp2.ID = ""

Expand All @@ -86,9 +88,10 @@ func TestBoltDBAppFindAppFetchApps(t *testing.T) {
Path: dbpath,
}
apps, err := boltdb.NewAppStorage(sts)
defer apps.Close()
require.NoError(t, err)

defer apps.Close()

a, err := apps.CreateApp(testApp)
require.NoError(t, err)

Expand Down
5 changes: 2 additions & 3 deletions storage/fs/config_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fs

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -72,7 +71,7 @@ func (cs *ConfigurationStorage) WriteConfig(settings model.ServerSettings) error
return fmt.Errorf("Cannot marshall configuration: %s", err)
}

if err = ioutil.WriteFile(cs.ServerConfigPath, ss, 0o644); err != nil {
if err = os.WriteFile(cs.ServerConfigPath, ss, 0o644); err != nil {
return fmt.Errorf("Cannot write configuration file: %s", err)
}

Expand All @@ -97,7 +96,7 @@ func (cs *ConfigurationStorage) LoadServerSettings(validate bool) (model.ServerS
return model.ServerSettings{}, cs.errors
}

yamlFile, err := ioutil.ReadFile(filepath.Join(dir, cs.ServerConfigPath))
yamlFile, err := os.ReadFile(filepath.Join(dir, cs.ServerConfigPath))
if err != nil {
cs.errors = append(cs.errors, fmt.Errorf("Cannot read server configuration file: %s", err))
return model.ServerSettings{}, cs.errors
Expand Down
3 changes: 1 addition & 2 deletions storage/fs_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package storage_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -152,7 +151,7 @@ func makeFiles() {

func updateFile(name string) {
data := fmt.Sprintf("This is file data has been created at: %v", time.Now())
_ = ioutil.WriteFile(name, []byte(data), 0o644)
_ = os.WriteFile(name, []byte(data), 0o644)
}

func deleteFiles() {
Expand Down
4 changes: 2 additions & 2 deletions storage/mem/map_overlay_fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

func TestNewMapOverlayFS(t *testing.T) {
base := afero.NewMemMapFs()
afero.WriteFile(base, "./test1.txt", []byte("file1"), 644)
afero.WriteFile(base, "./path/test2.txt", []byte("file2"), 644)
afero.WriteFile(base, "./test1.txt", []byte("file1"), 0644)
afero.WriteFile(base, "./path/test2.txt", []byte("file2"), 0644)

files := map[string][]byte{
"./path/test3.txt": []byte("file3"),
Expand Down
3 changes: 2 additions & 1 deletion storage/mongo/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mongo
import (
"bytes"
"context"
"errors"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -80,7 +81,7 @@ func generateIndexName(index mongo.IndexModel) (string, error) {

keys, ok := index.Keys.(bson.D)
if !ok {
return "", fmt.Errorf("Incorrect index keys type - expecting bsonx.Doc")
return "", errors.New("incorrect index keys type - expecting bsonx.Doc")
}
for _, elem := range keys {
if !first {
Expand Down
14 changes: 14 additions & 0 deletions storage/mongo/token.go → storage/mongo/token_blacklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mongo

import (
"context"
"fmt"
"time"

"github.com/madappgang/identifo/v2/model"
Expand All @@ -25,6 +26,19 @@ func NewTokenBlacklist(settings model.MongoDatabaseSettings) (model.TokenBlackli
}

coll := db.Database.Collection(blacklistedTokensCollectionName)

// TODO: check that index exists for other DB's
err = db.EnsureCollectionIndices(blacklistedTokensCollectionName, []mongo.IndexModel{
{
Keys: bson.D{{Key: "token", Value: 1}},
},
})
if err != nil {
return nil, fmt.Errorf("failed to create indexes for %s: %w",
blacklistedTokensCollectionName,
err)
}

return &TokenBlacklist{coll: coll, timeout: 30 * time.Second}, nil
}

Expand Down
14 changes: 14 additions & 0 deletions storage/mongo/token_storage.go → storage/mongo/token_refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mongo

import (
"context"
"fmt"
"time"

"github.com/madappgang/identifo/v2/model"
Expand All @@ -25,6 +26,19 @@ func NewTokenStorage(settings model.MongoDatabaseSettings) (model.TokenStorage,
}

coll := db.Database.Collection(tokensCollectionName)

// TODO: check that index exists for other DB's
err = db.EnsureCollectionIndices(tokensCollectionName, []mongo.IndexModel{
{
Keys: bson.D{{Key: "token", Value: 1}},
},
})
if err != nil {
return nil, fmt.Errorf("failed to create indexes for %s: %w",
tokensCollectionName,
err)
}

return &TokenStorage{coll: coll, timeout: 30 * time.Second}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion storage/mongo/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (us *UserStorage) AddUserWithFederatedID(user model.User, provider string,
}

// unknown error during user existnce check
if err != nil && !errors.Is(err, model.ErrUserNotFound) {
if !errors.Is(err, model.ErrUserNotFound) {
return model.User{}, err
}

Expand Down
4 changes: 2 additions & 2 deletions storage/s3/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package s3
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"log"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -69,7 +69,7 @@ func (ks *KeyStorage) LoadPrivateKey() (interface{}, error) {
}
defer resp.Body.Close()

keyData, err := ioutil.ReadAll(resp.Body)
keyData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Cannot decode S3 response: %s", err)
}
Expand Down
Loading
Loading