Skip to content

Commit

Permalink
Delete unencrypted mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail Borovikov committed Nov 11, 2021
1 parent a5bcfc9 commit 9924805
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 127 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Secretable

Secretable is a telegram bot for managing passwords and others secrets stored in Google Sheets. Convenient management of your secrets in the messenger. Strong encryption AES256 + PKCS 8 + PBKDF2. Using Google Sheets as storage allows you to track changes, easily make backups, and work without encryption with raw data. Share passwords for other users and chats.
Secretable is a telegram bot for managing passwords and others secrets stored in Google Sheets. Convenient management of your secrets in the messenger. Strong encryption AES256 + PKCS 8 + PBKDF2. Using Google Sheets as storage allows you to track changes, easily make backups. Share passwords for other users and chats.

## Install
To install the bot, just download the binary file of the latest release for your OS from the [releases page](https://github.com/secretable/secretable/releases)
Expand Down Expand Up @@ -38,7 +38,6 @@ telegram_bot_token: "Telegram bot token"
google_credentials_file: "Path to Google credentials JSON file"
spreadsheet_id: "Spreadsheet ID"
cleanup_timeout: 30 # Received and send messages cleanup timeout in seconds
unencrypted: false # Unencrypted mode
salt: "Salt" # Salt for encryption with a master password. If not specified, a new one is generated and setted
```

Expand All @@ -54,7 +53,7 @@ Help Options:
-h, --help Show this help message
```
### About security:
- In encrypted mode, Google Sheets do not store any open data other than description.
- Google Sheets do not store any open data other than description.

- In the environment in which the bot is launched, the "salt" is generated and stored, which is necessary for encryption using the master password.

Expand Down
9 changes: 1 addition & 8 deletions cmd/secretable.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func main() {
Bot: bot,
TablesProvider: tableProvider,
Locales: locales,
EncriptionMode: !conf.Unencrypted,
Config: conf,
},
conf,
Expand Down Expand Up @@ -143,13 +142,7 @@ func getConf(path string) (conf *config.Config, err error) {
log.Info("📄 Spreadsheet ID: " + conf.SpreadsheetID)
log.Info("🧹 Cleanup timeout: " + fmt.Sprint(conf.CleanupTimeout, " sec"))

if conf.Unencrypted {
log.Info("🔓 Unecrypted mode")
} else {
log.Info("🔐 Encrypted mode")
}

if !conf.Unencrypted && conf.Salt == "" {
if conf.Salt == "" {
s, _ := crypto.MakeRandom(saltLength)
conf.Salt = base58.Encode(s)

Expand Down
1 change: 0 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type Config struct {
GoogleCredentials string `yaml:"google_credentials_file"`
SpreadsheetID string `yaml:"spreadsheet_id"`
CleanupTimeout int `yaml:"cleanup_timeout"`
Unencrypted bool `yaml:"unencrypted"`
Salt string `yaml:"salt"`
AllowedList []string `yaml:"allowed_list"`
}
Expand Down
41 changes: 2 additions & 39 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const (
)

type Handler struct {
EncriptionMode bool
Bot *tb.Bot
TablesProvider *tables.TablesProvider
Locales *localizator.Localizator
Expand All @@ -63,11 +62,7 @@ func (h *Handler) Delete(m *tb.Message) {
return
}

if h.EncriptionMode {
err = h.TablesProvider.DeletEncrypted(index - 1)
} else {
err = h.TablesProvider.DeletSecrets(index - 1)
}
err = h.TablesProvider.DeletEncrypted(index - 1)

if err != nil {
h.sendMessage(m, h.Locales.Get(m.Sender.LanguageCode, "delete_unable_delete"))
Expand Down Expand Up @@ -101,35 +96,7 @@ func (h *Handler) ID(m *tb.Message) {
}

func (h *Handler) Query(m *tb.Message) {
if h.EncriptionMode {
h.queryEncrypted(m)
} else {
h.query(m)
}
}

func (h *Handler) query(m *tb.Message) {
rows := h.TablesProvider.GetSecrets()
q := strings.ToLower(m.Text)

ok := false
for i, row := range rows {
if len(row) != numbQueryColumns {
continue
}

for _, v := range row[:2] {
if strings.Contains(strings.ToLower(v), q) {
ok = true
h.sendMessage(m, makeQueryResponse(i, row))
break
}
}
}

if !ok {
h.sendMessage(m, h.Locales.Get(m.Sender.LanguageCode, "query_no_secrets"))
}
h.queryEncrypted(m)
}

func (h *Handler) queryEncrypted(m *tb.Message) {
Expand Down Expand Up @@ -184,10 +151,6 @@ func (h *Handler) queryEncrypted(m *tb.Message) {
}

func (h *Handler) ResetPass(m *tb.Message) {
if !h.EncriptionMode {
return
}

data := strings.TrimSpace(strings.TrimPrefix(m.Text, "/setpass"))

if data == "" {
Expand Down
26 changes: 3 additions & 23 deletions pkg/handlers/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (h *Handler) AccessMiddleware(next func(m *tb.Message)) func(m *tb.Message)

func (h *Handler) ControlMasterPassMiddleware(use bool, isSetHandler bool, next func(m *tb.Message)) func(m *tb.Message) {
return func(m *tb.Message) {
if !h.EncriptionMode || h.mastePass != "" {
if h.mastePass != "" {
next(m)
return
}
Expand Down Expand Up @@ -116,11 +116,8 @@ func (h *Handler) ControlSetSecretMiddleware(isSetHandler bool, next func(m *tb.
h.setstates.Delete(m.Chat.ID)

if isSetHandler && ok {
if h.EncriptionMode {
h.querySetNewEncryptedSecret(h.Bot, h.TablesProvider, m, h.mastePass)
} else {
h.querySetNewSecret(h.Bot, h.TablesProvider, m)
}
h.querySetNewEncryptedSecret(h.Bot, h.TablesProvider, m, h.mastePass)

return
}

Expand All @@ -134,23 +131,6 @@ func (h *Handler) LoggerMiddleware(next func(m *tb.Message)) func(m *tb.Message)
}
}

func (h *Handler) querySetNewSecret(b *tb.Bot, tp *tables.TablesProvider, m *tb.Message) {
arr := strings.Split(m.Text, "\n")

if len(arr) < numbAppendSecretsLines {
h.sendMessage(m, "Need 3 lines:\nDescription\nUser\nSecret\n\nTry repeat /set")
return
}
arr = arr[:numbAppendSecretsLines]

if err := tp.AppendSecrets(arr); err != nil {
h.sendMessage(m, "Error of appending new encrypted")
return
}

h.sendMessage(m, "New secret appened")
}

func (h *Handler) querySetNewEncryptedSecret(b *tb.Bot, tp *tables.TablesProvider, m *tb.Message, masterPass string) {
arr := strings.Split(m.Text, "\n")

Expand Down
56 changes: 3 additions & 53 deletions pkg/tables/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ import (

const (
encryptedRange = "Encrypted!A1:E"
secretsRange = "Secrets!A1:C"
keysRange = "Keys!A1:E"

secretsTitle = "Secrets"
encryptedTitle = "Encrypted"
keysTitle = "Keys"

Expand All @@ -42,13 +39,10 @@ type TablesProvider struct {
service *sheets.Service
spreadsheetId string

secretsID int64
encryptedID int64
keysID int64

secrets [][]string
encrypted [][]string
keys []string
encrypted [][]string
keys []string

mx sync.RWMutex
}
Expand All @@ -63,7 +57,7 @@ func NewTablesProvider(googleCredsFile, spreadsheetId string) (*TablesProvider,
tp.service = service
tp.spreadsheetId = spreadsheetId

for _, tab := range []string{secretsTitle, encryptedTitle, keysTitle} {
for _, tab := range []string{encryptedTitle, keysTitle} {
err = createTable(service, spreadsheetId, tab)
if err != nil {
return nil, err
Expand Down Expand Up @@ -106,10 +100,6 @@ func createTable(service *sheets.Service, spreadsheetId, tableTitle string) (err
return nil
}

func (t *TablesProvider) AppendSecrets(arr []string) error {
return t.append(secretsRange, arr)
}

func (t *TablesProvider) AppendEncrypted(arr []string) error {
return t.append(encryptedRange, arr)
}
Expand Down Expand Up @@ -152,10 +142,6 @@ func (t *TablesProvider) SetKey(key string) error {
return nil
}

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

func (t *TablesProvider) DeletEncrypted(index int) error {
return t.delete(t.encryptedID, index)
}
Expand Down Expand Up @@ -183,24 +169,6 @@ func (t *TablesProvider) delete(sheetID int64, index int) error {
return nil
}

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

for _, item := range data {
for _, row := range item.RowData {
var newrowsItem []string

for _, cell := range row.Values {
newrowsItem = append(newrowsItem, cell.FormattedValue)
}

newrows = append(newrows, newrowsItem)
}
}

t.setSecrets(newrows)
}

func (t *TablesProvider) updateEncrypted(data []*sheets.GridData) {
var newrows [][]string

Expand Down Expand Up @@ -240,9 +208,6 @@ func (t *TablesProvider) update() error {

for _, sheet := range ss.Sheets {
switch sheet.Properties.Title {
case secretsTitle:
t.secretsID = sheet.Properties.SheetId
t.updateSecrets(sheet.Data)
case encryptedTitle:
t.encryptedID = sheet.Properties.SheetId
t.updateEncrypted(sheet.Data)
Expand All @@ -254,28 +219,13 @@ func (t *TablesProvider) update() error {
return nil
}

func (s *TablesProvider) setSecrets(rows [][]string) {
s.mx.Lock()
s.secrets = make([][]string, len(rows))
copy(s.secrets, rows)
s.mx.Unlock()
}

func (s *TablesProvider) setEncrypted(rows [][]string) {
s.mx.Lock()
s.encrypted = make([][]string, len(rows))
copy(s.encrypted, rows)
s.mx.Unlock()
}

func (s *TablesProvider) GetSecrets() (rows [][]string) {
s.mx.RLock()
rows = make([][]string, len(s.secrets))
copy(rows, s.secrets)
s.mx.RUnlock()
return rows
}

func (s *TablesProvider) GetEncrypted() (rows [][]string) {
s.mx.RLock()
rows = make([][]string, len(s.encrypted))
Expand Down

0 comments on commit 9924805

Please sign in to comment.