Skip to content

Commit

Permalink
Storage as extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Borovikov committed Nov 12, 2021
1 parent 9189206 commit f61dad8
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 32 deletions.
4 changes: 2 additions & 2 deletions cmd/secretable.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"secretable/pkg/handlers"
"secretable/pkg/localizator"
"secretable/pkg/log"
"secretable/pkg/tables"
"secretable/pkg/providers"

tb "gopkg.in/tucnak/telebot.v2"

Expand Down Expand Up @@ -76,7 +76,7 @@ func main() {
return
}

tableProvider, err := tables.NewTablesProvider(conf.GoogleCredentials, conf.SpreadsheetID)
tableProvider, err := providers.NewGoogleSheetsStorage(conf.GoogleCredentials, conf.SpreadsheetID)
if err != nil {
log.Fatal("Unable to create tables provider: " + err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"secretable/pkg/crypto"
"secretable/pkg/localizator"
"secretable/pkg/log"
"secretable/pkg/tables"
"secretable/pkg/providers"
"strconv"
"strings"
"sync"
Expand All @@ -45,7 +45,7 @@ const (

type Handler struct {
Bot *tb.Bot
TablesProvider *tables.TablesProvider
TablesProvider providers.StorageProvider
Locales *localizator.Localizator
Config *config.Config

Expand All @@ -63,7 +63,7 @@ func (h *Handler) Delete(msg *tb.Message) {
return
}

err = h.TablesProvider.DeleteSecrets(index - 1)
err = h.TablesProvider.DeleteSecret(index - 1)

if err != nil {
h.sendMessage(msg, h.Locales.Get(msg.Sender.LanguageCode, "delete_unable_delete"))
Expand Down
8 changes: 4 additions & 4 deletions pkg/handlers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"html"
"secretable/pkg/crypto"
"secretable/pkg/log"
"secretable/pkg/tables"
"secretable/pkg/providers"
"time"

"github.com/mr-tron/base58/base58"
Expand Down Expand Up @@ -67,7 +67,7 @@ func (h *Handler) hasAccess(msg *tb.Message) bool {
return false
}

func getPrivkeyAsBytes(tp *tables.TablesProvider, salt, masterPass string) ([]byte, bool, error) {
func getPrivkeyAsBytes(tp providers.StorageProvider, salt, masterPass string) ([]byte, bool, error) {
k := tp.GetKey()

key, err := base58.Decode(k)
Expand All @@ -90,7 +90,7 @@ func getPrivkeyAsBytes(tp *tables.TablesProvider, salt, masterPass string) ([]by
return decPrivkey, true, nil
}

func getPrivkey(tp *tables.TablesProvider, salt, masterPass string) (*ecdsa.PrivateKey, error) {
func getPrivkey(tp providers.StorageProvider, salt, masterPass string) (*ecdsa.PrivateKey, error) {
decPrivkey, ok, err := getPrivkeyAsBytes(tp, salt, masterPass)
if err != nil {
return nil, err
Expand All @@ -108,7 +108,7 @@ func getPrivkey(tp *tables.TablesProvider, salt, masterPass string) (*ecdsa.Priv
return privkey.(*ecdsa.PrivateKey), nil
}

func makeQueryResponse(index int, secret tables.SecretsData) string {
func makeQueryResponse(index int, secret providers.SecretsData) string {
return fmt.Sprintf("(%d) <b>%s</b>\n<code>%s</code>\n<code>%s</code>",
index,
html.EscapeString(secret.Description),
Expand Down
4 changes: 2 additions & 2 deletions pkg/handlers/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"crypto/x509"
"secretable/pkg/crypto"
"secretable/pkg/log"
"secretable/pkg/tables"
"secretable/pkg/providers"
"strings"

"github.com/mr-tron/base58/base58"
Expand Down Expand Up @@ -166,7 +166,7 @@ func (h *Handler) querySetNewSecretsSecret(msg *tb.Message, masterPass string) {
arr[1] = base58.Encode(cypher1)
arr[2] = base58.Encode(cypher2)

err = h.TablesProvider.AddSecrets(tables.SecretsData{
err = h.TablesProvider.AddSecret(providers.SecretsData{
Description: arr[0],
Username: arr[1],
Secret: arr[2],
Expand Down
36 changes: 15 additions & 21 deletions pkg/tables/tables.go → pkg/providers/sheets.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package tables
package providers

import (
"context"
Expand All @@ -35,13 +35,7 @@ const (
updateTimeout = 10 // in sec
)

type SecretsData struct {
Description string
Username string
Secret string
}

type TablesProvider struct {
type GoogleSheetsStorage struct {
service *sheets.Service
spreadsheetID string

Expand All @@ -54,13 +48,13 @@ type TablesProvider struct {
mx sync.RWMutex
}

func NewTablesProvider(googleCredsFile, spreadsheetID string) (*TablesProvider, error) {
func NewGoogleSheetsStorage(googleCredsFile, spreadsheetID string) (*GoogleSheetsStorage, error) {
service, err := sheets.NewService(context.Background(), option.WithCredentialsFile(googleCredsFile))
if err != nil {
return nil, errors.Wrap(err, "init sheets service")
}

tableProvider := new(TablesProvider)
tableProvider := new(GoogleSheetsStorage)
tableProvider.service = service
tableProvider.spreadsheetID = spreadsheetID

Expand Down Expand Up @@ -108,7 +102,7 @@ func createTable(service *sheets.Service, spreadsheetID, tableTitle string) (err
return nil
}

func (t *TablesProvider) AddSecrets(data SecretsData) error {
func (t *GoogleSheetsStorage) AddSecret(data SecretsData) error {
_, err := t.service.Spreadsheets.Values.Append(t.spreadsheetID, secretesRange, &sheets.ValueRange{
Values: [][]interface{}{
{
Expand All @@ -129,7 +123,7 @@ func (t *TablesProvider) AddSecrets(data SecretsData) error {
return nil
}

func (t *TablesProvider) SetKey(key string) error {
func (t *GoogleSheetsStorage) SetKey(key string) error {
_, err := t.service.Spreadsheets.Values.Update(t.spreadsheetID, keysRange, &sheets.ValueRange{
Values: [][]interface{}{
{
Expand All @@ -150,11 +144,11 @@ func (t *TablesProvider) SetKey(key string) error {
return nil
}

func (t *TablesProvider) DeleteSecrets(index int) error {
func (t *GoogleSheetsStorage) DeleteSecret(index int) error {
return t.delete(t.secretsID, index)
}

func (t *TablesProvider) delete(sheetID int64, index int) error {
func (t *GoogleSheetsStorage) delete(sheetID int64, index int) error {
_, err := t.service.Spreadsheets.BatchUpdate(t.spreadsheetID, &sheets.BatchUpdateSpreadsheetRequest{
Requests: []*sheets.Request{
{
Expand All @@ -178,7 +172,7 @@ func (t *TablesProvider) delete(sheetID int64, index int) error {
return nil
}

func (t *TablesProvider) updateSecrets(data []*sheets.GridData) {
func (t *GoogleSheetsStorage) updateSecrets(data []*sheets.GridData) {
var newrows []SecretsData

for _, item := range data {
Expand All @@ -198,7 +192,7 @@ func (t *TablesProvider) updateSecrets(data []*sheets.GridData) {
t.setSecrets(newrows)
}

func (t *TablesProvider) updateKey(data []*sheets.GridData) {
func (t *GoogleSheetsStorage) updateKey(data []*sheets.GridData) {
for _, item := range data {
if len(item.RowData) == 0 {
continue
Expand All @@ -220,7 +214,7 @@ func (t *TablesProvider) updateKey(data []*sheets.GridData) {
}
}

func (t *TablesProvider) update() error {
func (t *GoogleSheetsStorage) update() error {
ss, err := t.service.Spreadsheets.Get(t.spreadsheetID).IncludeGridData(true).Do()
if err != nil {
return errors.Wrap(err, "get spreadsheet")
Expand All @@ -240,14 +234,14 @@ func (t *TablesProvider) update() error {
return nil
}

func (t *TablesProvider) setSecrets(secrets []SecretsData) {
func (t *GoogleSheetsStorage) setSecrets(secrets []SecretsData) {
t.mx.Lock()
t.secrets = make([]SecretsData, len(secrets))
copy(t.secrets, secrets)
t.mx.Unlock()
}

func (t *TablesProvider) GetSecrets() (secrets []SecretsData) {
func (t *GoogleSheetsStorage) GetSecrets() (secrets []SecretsData) {
t.mx.RLock()
secrets = make([]SecretsData, len(t.secrets))
copy(secrets, t.secrets)
Expand All @@ -256,13 +250,13 @@ func (t *TablesProvider) GetSecrets() (secrets []SecretsData) {
return secrets
}

func (t *TablesProvider) setKey(key string) {
func (t *GoogleSheetsStorage) setKey(key string) {
t.mx.Lock()
t.key = key
t.mx.Unlock()
}

func (t *TablesProvider) GetKey() string {
func (t *GoogleSheetsStorage) GetKey() string {
t.mx.RLock()
key := t.key
t.mx.RUnlock()
Expand Down
25 changes: 25 additions & 0 deletions pkg/providers/storage.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
// Copyright 2021 Mikhail Borovikov and The Secretable 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 providers

type SecretsData struct {
Description string
Username string
Secret string
}

type StorageProvider interface {
AddSecret(SecretsData) error
DeleteSecret(index int) error
GetSecrets() []SecretsData
SetKey(key string) error
GetKey() string
}

0 comments on commit f61dad8

Please sign in to comment.